blob: 74dcf19cfe68ccde20ce786338b64dbeecf7683c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * raid5.c : Multiple Devices driver for Linux
3 * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
4 * Copyright (C) 1999, 2000 Ingo Molnar
NeilBrown16a53ec2006-06-26 00:27:38 -07005 * Copyright (C) 2002, 2003 H. Peter Anvin
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
NeilBrown16a53ec2006-06-26 00:27:38 -07007 * RAID-4/5/6 management functions.
8 * Thanks to Penguin Computing for making the RAID-6 development possible
9 * by donating a test server!
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
NeilBrownae3c20c2006-07-10 04:44:17 -070021/*
22 * BITMAP UNPLUGGING:
23 *
24 * The sequencing for updating the bitmap reliably is a little
25 * subtle (and I got it wrong the first time) so it deserves some
26 * explanation.
27 *
28 * We group bitmap updates into batches. Each batch has a number.
29 * We may write out several batches at once, but that isn't very important.
NeilBrown7c13edc2011-04-18 18:25:43 +100030 * conf->seq_write is the number of the last batch successfully written.
31 * conf->seq_flush is the number of the last batch that was closed to
NeilBrownae3c20c2006-07-10 04:44:17 -070032 * new additions.
33 * When we discover that we will need to write to any block in a stripe
34 * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq
NeilBrown7c13edc2011-04-18 18:25:43 +100035 * the number of the batch it will be in. This is seq_flush+1.
NeilBrownae3c20c2006-07-10 04:44:17 -070036 * When we are ready to do a write, if that batch hasn't been written yet,
37 * we plug the array and queue the stripe for later.
38 * When an unplug happens, we increment bm_flush, thus closing the current
39 * batch.
40 * When we notice that bm_flush > bm_write, we write out all pending updates
41 * to the bitmap, and advance bm_write to where bm_flush was.
42 * This may occasionally write a bit out twice, but is sure never to
43 * miss any bits.
44 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
NeilBrownbff61972009-03-31 14:33:13 +110046#include <linux/blkdev.h>
NeilBrownf6705572006-03-27 01:18:11 -080047#include <linux/kthread.h>
Dan Williamsf701d582009-03-31 15:09:39 +110048#include <linux/raid/pq.h>
Dan Williams91c00922007-01-02 13:52:30 -070049#include <linux/async_tx.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040050#include <linux/module.h>
Dan Williams07a3b412009-08-29 19:13:13 -070051#include <linux/async.h>
NeilBrownbff61972009-03-31 14:33:13 +110052#include <linux/seq_file.h>
Dan Williams36d1c642009-07-14 11:48:22 -070053#include <linux/cpu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090054#include <linux/slab.h>
Christian Dietrich8bda4702011-07-27 11:00:36 +100055#include <linux/ratelimit.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110056#include "md.h"
NeilBrownbff61972009-03-31 14:33:13 +110057#include "raid5.h"
Trela Maciej54071b32010-03-08 16:02:42 +110058#include "raid0.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110059#include "bitmap.h"
NeilBrown72626682005-09-09 16:23:54 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/*
62 * Stripe cache
63 */
64
65#define NR_STRIPES 256
66#define STRIPE_SIZE PAGE_SIZE
67#define STRIPE_SHIFT (PAGE_SHIFT - 9)
68#define STRIPE_SECTORS (STRIPE_SIZE>>9)
69#define IO_THRESHOLD 1
Dan Williams8b3e6cd2008-04-28 02:15:53 -070070#define BYPASS_THRESHOLD 1
NeilBrownfccddba2006-01-06 00:20:33 -080071#define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define HASH_MASK (NR_HASH - 1)
73
NeilBrownd1688a62011-10-11 16:49:52 +110074static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
NeilBrowndb298e12011-10-07 14:23:00 +110075{
76 int hash = (sect >> STRIPE_SHIFT) & HASH_MASK;
77 return &conf->stripe_hashtbl[hash];
78}
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80/* bio's attached to a stripe+device for I/O are linked together in bi_sector
81 * order without overlap. There may be several bio's per stripe+device, and
82 * a bio could span several devices.
83 * When walking this list for a particular stripe+device, we must never proceed
84 * beyond a bio that extends past this device, as the next bio might no longer
85 * be valid.
NeilBrowndb298e12011-10-07 14:23:00 +110086 * This function is used to determine the 'next' bio in the list, given the sector
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * of the current stripe+device
88 */
NeilBrowndb298e12011-10-07 14:23:00 +110089static inline struct bio *r5_next_bio(struct bio *bio, sector_t sector)
90{
91 int sectors = bio->bi_size >> 9;
92 if (bio->bi_sector + sectors < sector + STRIPE_SECTORS)
93 return bio->bi_next;
94 else
95 return NULL;
96}
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Jens Axboe960e7392008-08-15 10:41:18 +020098/*
Jens Axboe5b99c2f2008-08-15 10:56:11 +020099 * We maintain a biased count of active stripes in the bottom 16 bits of
100 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
Jens Axboe960e7392008-08-15 10:41:18 +0200101 */
Shaohua Lie7836bd62012-07-19 16:01:31 +1000102static inline int raid5_bi_processed_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200103{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000104 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
105 return (atomic_read(segments) >> 16) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200106}
107
Shaohua Lie7836bd62012-07-19 16:01:31 +1000108static inline int raid5_dec_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200109{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000110 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
111 return atomic_sub_return(1, segments) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200112}
113
Shaohua Lie7836bd62012-07-19 16:01:31 +1000114static inline void raid5_inc_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200115{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000116 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
117 atomic_inc(segments);
Jens Axboe960e7392008-08-15 10:41:18 +0200118}
119
Shaohua Lie7836bd62012-07-19 16:01:31 +1000120static inline void raid5_set_bi_processed_stripes(struct bio *bio,
121 unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200122{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000123 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
124 int old, new;
Jens Axboe960e7392008-08-15 10:41:18 +0200125
Shaohua Lie7836bd62012-07-19 16:01:31 +1000126 do {
127 old = atomic_read(segments);
128 new = (old & 0xffff) | (cnt << 16);
129 } while (atomic_cmpxchg(segments, old, new) != old);
Jens Axboe960e7392008-08-15 10:41:18 +0200130}
131
Shaohua Lie7836bd62012-07-19 16:01:31 +1000132static inline void raid5_set_bi_stripes(struct bio *bio, unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200133{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000134 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
135 atomic_set(segments, cnt);
Jens Axboe960e7392008-08-15 10:41:18 +0200136}
137
NeilBrownd0dabf72009-03-31 14:39:38 +1100138/* Find first data disk in a raid6 stripe */
139static inline int raid6_d0(struct stripe_head *sh)
140{
NeilBrown67cc2b82009-03-31 14:39:38 +1100141 if (sh->ddf_layout)
142 /* ddf always start from first device */
143 return 0;
144 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100145 if (sh->qd_idx == sh->disks - 1)
146 return 0;
147 else
148 return sh->qd_idx + 1;
149}
NeilBrown16a53ec2006-06-26 00:27:38 -0700150static inline int raid6_next_disk(int disk, int raid_disks)
151{
152 disk++;
153 return (disk < raid_disks) ? disk : 0;
154}
Dan Williamsa4456852007-07-09 11:56:43 -0700155
NeilBrownd0dabf72009-03-31 14:39:38 +1100156/* When walking through the disks in a raid5, starting at raid6_d0,
157 * We need to map each disk to a 'slot', where the data disks are slot
158 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
159 * is raid_disks-1. This help does that mapping.
160 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100161static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
162 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100163{
Dan Williams66295422009-10-19 18:09:32 -0700164 int slot = *count;
NeilBrown67cc2b82009-03-31 14:39:38 +1100165
NeilBrowne4424fe2009-10-16 16:27:34 +1100166 if (sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700167 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100168 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100169 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100170 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100171 return syndrome_disks + 1;
NeilBrowne4424fe2009-10-16 16:27:34 +1100172 if (!sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700173 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100174 return slot;
175}
176
Dan Williamsa4456852007-07-09 11:56:43 -0700177static void return_io(struct bio *return_bi)
178{
179 struct bio *bi = return_bi;
180 while (bi) {
Dan Williamsa4456852007-07-09 11:56:43 -0700181
182 return_bi = bi->bi_next;
183 bi->bi_next = NULL;
184 bi->bi_size = 0;
Neil Brown0e13fe232008-06-28 08:31:20 +1000185 bio_endio(bi, 0);
Dan Williamsa4456852007-07-09 11:56:43 -0700186 bi = return_bi;
187 }
188}
189
NeilBrownd1688a62011-10-11 16:49:52 +1100190static void print_raid5_conf (struct r5conf *conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Dan Williams600aa102008-06-28 08:32:05 +1000192static int stripe_operations_active(struct stripe_head *sh)
193{
194 return sh->check_state || sh->reconstruct_state ||
195 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
196 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
197}
198
Shaohua Li4eb788d2012-07-19 16:01:31 +1000199static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Shaohua Li4eb788d2012-07-19 16:01:31 +1000201 BUG_ON(!list_empty(&sh->lru));
202 BUG_ON(atomic_read(&conf->active_stripes)==0);
203 if (test_bit(STRIPE_HANDLE, &sh->state)) {
204 if (test_bit(STRIPE_DELAYED, &sh->state) &&
205 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
206 list_add_tail(&sh->lru, &conf->delayed_list);
207 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
208 sh->bm_seq - conf->seq_write > 0)
209 list_add_tail(&sh->lru, &conf->bitmap_list);
210 else {
211 clear_bit(STRIPE_DELAYED, &sh->state);
212 clear_bit(STRIPE_BIT_DELAY, &sh->state);
213 list_add_tail(&sh->lru, &conf->handle_list);
214 }
215 md_wakeup_thread(conf->mddev->thread);
216 } else {
217 BUG_ON(stripe_operations_active(sh));
218 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
219 if (atomic_dec_return(&conf->preread_active_stripes)
220 < IO_THRESHOLD)
221 md_wakeup_thread(conf->mddev->thread);
222 atomic_dec(&conf->active_stripes);
223 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
224 list_add_tail(&sh->lru, &conf->inactive_list);
225 wake_up(&conf->wait_for_stripe);
226 if (conf->retry_read_aligned)
227 md_wakeup_thread(conf->mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
229 }
230}
NeilBrownd0dabf72009-03-31 14:39:38 +1100231
Shaohua Li4eb788d2012-07-19 16:01:31 +1000232static void __release_stripe(struct r5conf *conf, struct stripe_head *sh)
233{
234 if (atomic_dec_and_test(&sh->count))
235 do_release_stripe(conf, sh);
236}
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238static void release_stripe(struct stripe_head *sh)
239{
NeilBrownd1688a62011-10-11 16:49:52 +1100240 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 unsigned long flags;
NeilBrown16a53ec2006-06-26 00:27:38 -0700242
Shaohua Li4eb788d2012-07-19 16:01:31 +1000243 local_irq_save(flags);
244 if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) {
245 do_release_stripe(conf, sh);
246 spin_unlock(&conf->device_lock);
247 }
248 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
NeilBrownfccddba2006-01-06 00:20:33 -0800251static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Dan Williams45b42332007-07-09 11:56:43 -0700253 pr_debug("remove_hash(), stripe %llu\n",
254 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
NeilBrownfccddba2006-01-06 00:20:33 -0800256 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
NeilBrownd1688a62011-10-11 16:49:52 +1100259static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
NeilBrownfccddba2006-01-06 00:20:33 -0800261 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Dan Williams45b42332007-07-09 11:56:43 -0700263 pr_debug("insert_hash(), stripe %llu\n",
264 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
NeilBrownfccddba2006-01-06 00:20:33 -0800266 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
269
270/* find an idle stripe, make sure it is unhashed, and return it. */
NeilBrownd1688a62011-10-11 16:49:52 +1100271static struct stripe_head *get_free_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 struct stripe_head *sh = NULL;
274 struct list_head *first;
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (list_empty(&conf->inactive_list))
277 goto out;
278 first = conf->inactive_list.next;
279 sh = list_entry(first, struct stripe_head, lru);
280 list_del_init(first);
281 remove_hash(sh);
282 atomic_inc(&conf->active_stripes);
283out:
284 return sh;
285}
286
NeilBrowne4e11e32010-06-16 16:45:16 +1000287static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 struct page *p;
290 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000291 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
NeilBrowne4e11e32010-06-16 16:45:16 +1000293 for (i = 0; i < num ; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 p = sh->dev[i].page;
295 if (!p)
296 continue;
297 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800298 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300}
301
NeilBrowne4e11e32010-06-16 16:45:16 +1000302static int grow_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000305 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
NeilBrowne4e11e32010-06-16 16:45:16 +1000307 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 struct page *page;
309
310 if (!(page = alloc_page(GFP_KERNEL))) {
311 return 1;
312 }
313 sh->dev[i].page = page;
314 }
315 return 0;
316}
317
NeilBrown784052e2009-03-31 15:19:07 +1100318static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrownd1688a62011-10-11 16:49:52 +1100319static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100320 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
NeilBrownb5663ba2009-03-31 14:39:38 +1100322static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
NeilBrownd1688a62011-10-11 16:49:52 +1100324 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800325 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200327 BUG_ON(atomic_read(&sh->count) != 0);
328 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000329 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700330
Dan Williams45b42332007-07-09 11:56:43 -0700331 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 (unsigned long long)sh->sector);
333
334 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700335
NeilBrown86b42c72009-03-31 15:19:03 +1100336 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100337 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100339 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 sh->state = 0;
341
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800342
343 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 struct r5dev *dev = &sh->dev[i];
345
Dan Williamsd84e0f12007-01-02 13:52:30 -0700346 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700348 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700350 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000352 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100355 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
357 insert_hash(conf, sh);
358}
359
NeilBrownd1688a62011-10-11 16:49:52 +1100360static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100361 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -0800364 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Dan Williams45b42332007-07-09 11:56:43 -0700366 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
NeilBrownfccddba2006-01-06 00:20:33 -0800367 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100368 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700370 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return NULL;
372}
373
NeilBrown674806d2010-06-16 17:17:53 +1000374/*
375 * Need to check if array has failed when deciding whether to:
376 * - start an array
377 * - remove non-faulty devices
378 * - add a spare
379 * - allow a reshape
380 * This determination is simple when no reshape is happening.
381 * However if there is a reshape, we need to carefully check
382 * both the before and after sections.
383 * This is because some failed devices may only affect one
384 * of the two sections, and some non-in_sync devices may
385 * be insync in the section most affected by failed devices.
386 */
NeilBrown908f4fb2011-12-23 10:17:50 +1100387static int calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000388{
NeilBrown908f4fb2011-12-23 10:17:50 +1100389 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000390 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000391
392 rcu_read_lock();
393 degraded = 0;
394 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100395 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown674806d2010-06-16 17:17:53 +1000396 if (!rdev || test_bit(Faulty, &rdev->flags))
397 degraded++;
398 else if (test_bit(In_sync, &rdev->flags))
399 ;
400 else
401 /* not in-sync or faulty.
402 * If the reshape increases the number of devices,
403 * this is being recovered by the reshape, so
404 * this 'previous' section is not in_sync.
405 * If the number of devices is being reduced however,
406 * the device can only be part of the array if
407 * we are reverting a reshape, so this section will
408 * be in-sync.
409 */
410 if (conf->raid_disks >= conf->previous_raid_disks)
411 degraded++;
412 }
413 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100414 if (conf->raid_disks == conf->previous_raid_disks)
415 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000416 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100417 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000418 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100419 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown674806d2010-06-16 17:17:53 +1000420 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100421 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000422 else if (test_bit(In_sync, &rdev->flags))
423 ;
424 else
425 /* not in-sync or faulty.
426 * If reshape increases the number of devices, this
427 * section has already been recovered, else it
428 * almost certainly hasn't.
429 */
430 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100431 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000432 }
433 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100434 if (degraded2 > degraded)
435 return degraded2;
436 return degraded;
437}
438
439static int has_failed(struct r5conf *conf)
440{
441 int degraded;
442
443 if (conf->mddev->reshape_position == MaxSector)
444 return conf->mddev->degraded > conf->max_degraded;
445
446 degraded = calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000447 if (degraded > conf->max_degraded)
448 return 1;
449 return 0;
450}
451
NeilBrownb5663ba2009-03-31 14:39:38 +1100452static struct stripe_head *
NeilBrownd1688a62011-10-11 16:49:52 +1100453get_active_stripe(struct r5conf *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000454 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 struct stripe_head *sh;
457
Dan Williams45b42332007-07-09 11:56:43 -0700458 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 spin_lock_irq(&conf->device_lock);
461
462 do {
NeilBrown72626682005-09-09 16:23:54 -0700463 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000464 conf->quiesce == 0 || noquiesce,
NeilBrown72626682005-09-09 16:23:54 -0700465 conf->device_lock, /* nothing */);
NeilBrown86b42c72009-03-31 15:19:03 +1100466 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (!sh) {
468 if (!conf->inactive_blocked)
469 sh = get_free_stripe(conf);
470 if (noblock && sh == NULL)
471 break;
472 if (!sh) {
473 conf->inactive_blocked = 1;
474 wait_event_lock_irq(conf->wait_for_stripe,
475 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800476 (atomic_read(&conf->active_stripes)
477 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 || !conf->inactive_blocked),
479 conf->device_lock,
NeilBrown7c13edc2011-04-18 18:25:43 +1000480 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 conf->inactive_blocked = 0;
482 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100483 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 } else {
485 if (atomic_read(&sh->count)) {
NeilBrownab69ae12009-03-31 15:26:47 +1100486 BUG_ON(!list_empty(&sh->lru)
Shaohua Li8811b592012-08-02 08:33:00 +1000487 && !test_bit(STRIPE_EXPANDING, &sh->state)
488 && !test_bit(STRIPE_ON_UNPLUG_LIST, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 } else {
490 if (!test_bit(STRIPE_HANDLE, &sh->state))
491 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700492 if (list_empty(&sh->lru) &&
493 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700494 BUG();
495 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
497 }
498 } while (sh == NULL);
499
500 if (sh)
501 atomic_inc(&sh->count);
502
503 spin_unlock_irq(&conf->device_lock);
504 return sh;
505}
506
NeilBrown05616be2012-05-21 09:27:00 +1000507/* Determine if 'data_offset' or 'new_data_offset' should be used
508 * in this stripe_head.
509 */
510static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
511{
512 sector_t progress = conf->reshape_progress;
513 /* Need a memory barrier to make sure we see the value
514 * of conf->generation, or ->data_offset that was set before
515 * reshape_progress was updated.
516 */
517 smp_rmb();
518 if (progress == MaxSector)
519 return 0;
520 if (sh->generation == conf->generation - 1)
521 return 0;
522 /* We are in a reshape, and this is a new-generation stripe,
523 * so use new_data_offset.
524 */
525 return 1;
526}
527
NeilBrown6712ecf2007-09-27 12:47:43 +0200528static void
529raid5_end_read_request(struct bio *bi, int error);
530static void
531raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700532
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000533static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700534{
NeilBrownd1688a62011-10-11 16:49:52 +1100535 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700536 int i, disks = sh->disks;
537
538 might_sleep();
539
540 for (i = disks; i--; ) {
541 int rw;
NeilBrown9a3e1102011-12-23 10:17:53 +1100542 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100543 struct bio *bi, *rbi;
544 struct md_rdev *rdev, *rrdev = NULL;
Tejun Heoe9c74692010-09-03 11:56:18 +0200545 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
546 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
547 rw = WRITE_FUA;
548 else
549 rw = WRITE;
Shaohua Li620125f2012-10-11 13:49:05 +1100550 if (test_and_clear_bit(R5_Discard, &sh->dev[i].flags))
551 rw |= REQ_DISCARD;
Tejun Heoe9c74692010-09-03 11:56:18 +0200552 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700553 rw = READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100554 else if (test_and_clear_bit(R5_WantReplace,
555 &sh->dev[i].flags)) {
556 rw = WRITE;
557 replace_only = 1;
558 } else
Dan Williams91c00922007-01-02 13:52:30 -0700559 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +1000560 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
561 rw |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -0700562
563 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100564 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700565
566 bi->bi_rw = rw;
NeilBrown977df362011-12-23 10:17:53 +1100567 rbi->bi_rw = rw;
568 if (rw & WRITE) {
Dan Williams91c00922007-01-02 13:52:30 -0700569 bi->bi_end_io = raid5_end_write_request;
NeilBrown977df362011-12-23 10:17:53 +1100570 rbi->bi_end_io = raid5_end_write_request;
571 } else
Dan Williams91c00922007-01-02 13:52:30 -0700572 bi->bi_end_io = raid5_end_read_request;
573
574 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100575 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100576 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
577 rdev = rcu_dereference(conf->disks[i].rdev);
578 if (!rdev) {
579 rdev = rrdev;
580 rrdev = NULL;
581 }
NeilBrown9a3e1102011-12-23 10:17:53 +1100582 if (rw & WRITE) {
583 if (replace_only)
584 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100585 if (rdev == rrdev)
586 /* We raced and saw duplicates */
587 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100588 } else {
NeilBrowndd054fc2011-12-23 10:17:53 +1100589 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100590 rdev = rrdev;
591 rrdev = NULL;
592 }
NeilBrown977df362011-12-23 10:17:53 +1100593
Dan Williams91c00922007-01-02 13:52:30 -0700594 if (rdev && test_bit(Faulty, &rdev->flags))
595 rdev = NULL;
596 if (rdev)
597 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100598 if (rrdev && test_bit(Faulty, &rrdev->flags))
599 rrdev = NULL;
600 if (rrdev)
601 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700602 rcu_read_unlock();
603
NeilBrown73e92e52011-07-28 11:39:22 +1000604 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100605 * need to check for writes. We never accept write errors
606 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000607 */
608 while ((rw & WRITE) && rdev &&
609 test_bit(WriteErrorSeen, &rdev->flags)) {
610 sector_t first_bad;
611 int bad_sectors;
612 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
613 &first_bad, &bad_sectors);
614 if (!bad)
615 break;
616
617 if (bad < 0) {
618 set_bit(BlockedBadBlocks, &rdev->flags);
619 if (!conf->mddev->external &&
620 conf->mddev->flags) {
621 /* It is very unlikely, but we might
622 * still need to write out the
623 * bad block log - better give it
624 * a chance*/
625 md_check_recovery(conf->mddev);
626 }
majianpeng18507532012-07-03 12:11:54 +1000627 /*
628 * Because md_wait_for_blocked_rdev
629 * will dec nr_pending, we must
630 * increment it first.
631 */
632 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +1000633 md_wait_for_blocked_rdev(rdev, conf->mddev);
634 } else {
635 /* Acknowledged bad block - skip the write */
636 rdev_dec_pending(rdev, conf->mddev);
637 rdev = NULL;
638 }
639 }
640
Dan Williams91c00922007-01-02 13:52:30 -0700641 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100642 if (s->syncing || s->expanding || s->expanded
643 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700644 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
645
Dan Williams2b7497f2008-06-28 08:31:52 +1000646 set_bit(STRIPE_IO_STARTED, &sh->state);
647
Dan Williams91c00922007-01-02 13:52:30 -0700648 bi->bi_bdev = rdev->bdev;
649 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700650 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700651 bi->bi_rw, i);
652 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000653 if (use_new_offset(conf, sh))
654 bi->bi_sector = (sh->sector
655 + rdev->new_data_offset);
656 else
657 bi->bi_sector = (sh->sector
658 + rdev->data_offset);
majianpeng3f9e7c12012-07-31 10:04:21 +1000659 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
660 bi->bi_rw |= REQ_FLUSH;
661
Dan Williams91c00922007-01-02 13:52:30 -0700662 bi->bi_flags = 1 << BIO_UPTODATE;
Dan Williams91c00922007-01-02 13:52:30 -0700663 bi->bi_idx = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700664 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
665 bi->bi_io_vec[0].bv_offset = 0;
666 bi->bi_size = STRIPE_SIZE;
667 bi->bi_next = NULL;
NeilBrown977df362011-12-23 10:17:53 +1100668 if (rrdev)
669 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Dan Williams91c00922007-01-02 13:52:30 -0700670 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100671 }
672 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100673 if (s->syncing || s->expanding || s->expanded
674 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +1100675 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
676
677 set_bit(STRIPE_IO_STARTED, &sh->state);
678
679 rbi->bi_bdev = rrdev->bdev;
680 pr_debug("%s: for %llu schedule op %ld on "
681 "replacement disc %d\n",
682 __func__, (unsigned long long)sh->sector,
683 rbi->bi_rw, i);
684 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000685 if (use_new_offset(conf, sh))
686 rbi->bi_sector = (sh->sector
687 + rrdev->new_data_offset);
688 else
689 rbi->bi_sector = (sh->sector
690 + rrdev->data_offset);
NeilBrown977df362011-12-23 10:17:53 +1100691 rbi->bi_flags = 1 << BIO_UPTODATE;
692 rbi->bi_idx = 0;
693 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
694 rbi->bi_io_vec[0].bv_offset = 0;
695 rbi->bi_size = STRIPE_SIZE;
696 rbi->bi_next = NULL;
697 generic_make_request(rbi);
698 }
699 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000700 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700701 set_bit(STRIPE_DEGRADED, &sh->state);
702 pr_debug("skip op %ld on disc %d for sector %llu\n",
703 bi->bi_rw, i, (unsigned long long)sh->sector);
704 clear_bit(R5_LOCKED, &sh->dev[i].flags);
705 set_bit(STRIPE_HANDLE, &sh->state);
706 }
707 }
708}
709
710static struct dma_async_tx_descriptor *
711async_copy_data(int frombio, struct bio *bio, struct page *page,
712 sector_t sector, struct dma_async_tx_descriptor *tx)
713{
714 struct bio_vec *bvl;
715 struct page *bio_page;
716 int i;
717 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700718 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700719 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700720
721 if (bio->bi_sector >= sector)
722 page_offset = (signed)(bio->bi_sector - sector) * 512;
723 else
724 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700725
Dan Williams0403e382009-09-08 17:42:50 -0700726 if (frombio)
727 flags |= ASYNC_TX_FENCE;
728 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
729
Dan Williams91c00922007-01-02 13:52:30 -0700730 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000731 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700732 int clen;
733 int b_offset = 0;
734
735 if (page_offset < 0) {
736 b_offset = -page_offset;
737 page_offset += b_offset;
738 len -= b_offset;
739 }
740
741 if (len > 0 && page_offset + len > STRIPE_SIZE)
742 clen = STRIPE_SIZE - page_offset;
743 else
744 clen = len;
745
746 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000747 b_offset += bvl->bv_offset;
748 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700749 if (frombio)
750 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700751 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700752 else
753 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700754 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700755 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700756 /* chain the operations */
757 submit.depend_tx = tx;
758
Dan Williams91c00922007-01-02 13:52:30 -0700759 if (clen < len) /* hit end of page */
760 break;
761 page_offset += len;
762 }
763
764 return tx;
765}
766
767static void ops_complete_biofill(void *stripe_head_ref)
768{
769 struct stripe_head *sh = stripe_head_ref;
770 struct bio *return_bi = NULL;
Dan Williamse4d84902007-09-24 10:06:13 -0700771 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700772
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700773 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700774 (unsigned long long)sh->sector);
775
776 /* clear completed biofills */
777 for (i = sh->disks; i--; ) {
778 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700779
780 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700781 /* and check if we need to reply to a read request,
782 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000783 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700784 */
785 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700786 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700787
Dan Williams91c00922007-01-02 13:52:30 -0700788 BUG_ON(!dev->read);
789 rbi = dev->read;
790 dev->read = NULL;
791 while (rbi && rbi->bi_sector <
792 dev->sector + STRIPE_SECTORS) {
793 rbi2 = r5_next_bio(rbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +1000794 if (!raid5_dec_bi_active_stripes(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700795 rbi->bi_next = return_bi;
796 return_bi = rbi;
797 }
Dan Williams91c00922007-01-02 13:52:30 -0700798 rbi = rbi2;
799 }
800 }
801 }
Dan Williams83de75c2008-06-28 08:31:58 +1000802 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700803
804 return_io(return_bi);
805
Dan Williamse4d84902007-09-24 10:06:13 -0700806 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700807 release_stripe(sh);
808}
809
810static void ops_run_biofill(struct stripe_head *sh)
811{
812 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa08abd82009-06-03 11:43:59 -0700813 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700814 int i;
815
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700816 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700817 (unsigned long long)sh->sector);
818
819 for (i = sh->disks; i--; ) {
820 struct r5dev *dev = &sh->dev[i];
821 if (test_bit(R5_Wantfill, &dev->flags)) {
822 struct bio *rbi;
Shaohua Lib17459c2012-07-19 16:01:31 +1000823 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700824 dev->read = rbi = dev->toread;
825 dev->toread = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +1000826 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700827 while (rbi && rbi->bi_sector <
828 dev->sector + STRIPE_SECTORS) {
829 tx = async_copy_data(0, rbi, dev->page,
830 dev->sector, tx);
831 rbi = r5_next_bio(rbi, dev->sector);
832 }
833 }
834 }
835
836 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700837 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
838 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700839}
840
Dan Williams4e7d2c02009-08-29 19:13:11 -0700841static void mark_target_uptodate(struct stripe_head *sh, int target)
842{
843 struct r5dev *tgt;
844
845 if (target < 0)
846 return;
847
848 tgt = &sh->dev[target];
849 set_bit(R5_UPTODATE, &tgt->flags);
850 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
851 clear_bit(R5_Wantcompute, &tgt->flags);
852}
853
Dan Williamsac6b53b2009-07-14 13:40:19 -0700854static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700855{
856 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700857
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700858 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700859 (unsigned long long)sh->sector);
860
Dan Williamsac6b53b2009-07-14 13:40:19 -0700861 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700862 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700863 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700864
Dan Williamsecc65c92008-06-28 08:31:57 +1000865 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
866 if (sh->check_state == check_state_compute_run)
867 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700868 set_bit(STRIPE_HANDLE, &sh->state);
869 release_stripe(sh);
870}
871
Dan Williamsd6f38f32009-07-14 11:50:52 -0700872/* return a pointer to the address conversion region of the scribble buffer */
873static addr_conv_t *to_addr_conv(struct stripe_head *sh,
874 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700875{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700876 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
877}
878
879static struct dma_async_tx_descriptor *
880ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
881{
Dan Williams91c00922007-01-02 13:52:30 -0700882 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700883 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700884 int target = sh->ops.target;
885 struct r5dev *tgt = &sh->dev[target];
886 struct page *xor_dest = tgt->page;
887 int count = 0;
888 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700889 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700890 int i;
891
892 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700893 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700894 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
895
896 for (i = disks; i--; )
897 if (i != target)
898 xor_srcs[count++] = sh->dev[i].page;
899
900 atomic_inc(&sh->count);
901
Dan Williams0403e382009-09-08 17:42:50 -0700902 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700903 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700904 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700905 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700906 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700907 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700908
Dan Williams91c00922007-01-02 13:52:30 -0700909 return tx;
910}
911
Dan Williamsac6b53b2009-07-14 13:40:19 -0700912/* set_syndrome_sources - populate source buffers for gen_syndrome
913 * @srcs - (struct page *) array of size sh->disks
914 * @sh - stripe_head to parse
915 *
916 * Populates srcs in proper layout order for the stripe and returns the
917 * 'count' of sources to be used in a call to async_gen_syndrome. The P
918 * destination buffer is recorded in srcs[count] and the Q destination
919 * is recorded in srcs[count+1]].
920 */
921static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
922{
923 int disks = sh->disks;
924 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
925 int d0_idx = raid6_d0(sh);
926 int count;
927 int i;
928
929 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100930 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700931
932 count = 0;
933 i = d0_idx;
934 do {
935 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
936
937 srcs[slot] = sh->dev[i].page;
938 i = raid6_next_disk(i, disks);
939 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700940
NeilBrowne4424fe2009-10-16 16:27:34 +1100941 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700942}
943
944static struct dma_async_tx_descriptor *
945ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
946{
947 int disks = sh->disks;
948 struct page **blocks = percpu->scribble;
949 int target;
950 int qd_idx = sh->qd_idx;
951 struct dma_async_tx_descriptor *tx;
952 struct async_submit_ctl submit;
953 struct r5dev *tgt;
954 struct page *dest;
955 int i;
956 int count;
957
958 if (sh->ops.target < 0)
959 target = sh->ops.target2;
960 else if (sh->ops.target2 < 0)
961 target = sh->ops.target;
962 else
963 /* we should only have one valid target */
964 BUG();
965 BUG_ON(target < 0);
966 pr_debug("%s: stripe %llu block: %d\n",
967 __func__, (unsigned long long)sh->sector, target);
968
969 tgt = &sh->dev[target];
970 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
971 dest = tgt->page;
972
973 atomic_inc(&sh->count);
974
975 if (target == qd_idx) {
976 count = set_syndrome_sources(blocks, sh);
977 blocks[count] = NULL; /* regenerating p is not necessary */
978 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700979 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
980 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700981 to_addr_conv(sh, percpu));
982 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
983 } else {
984 /* Compute any data- or p-drive using XOR */
985 count = 0;
986 for (i = disks; i-- ; ) {
987 if (i == target || i == qd_idx)
988 continue;
989 blocks[count++] = sh->dev[i].page;
990 }
991
Dan Williams0403e382009-09-08 17:42:50 -0700992 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
993 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700994 to_addr_conv(sh, percpu));
995 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
996 }
997
998 return tx;
999}
1000
1001static struct dma_async_tx_descriptor *
1002ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1003{
1004 int i, count, disks = sh->disks;
1005 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1006 int d0_idx = raid6_d0(sh);
1007 int faila = -1, failb = -1;
1008 int target = sh->ops.target;
1009 int target2 = sh->ops.target2;
1010 struct r5dev *tgt = &sh->dev[target];
1011 struct r5dev *tgt2 = &sh->dev[target2];
1012 struct dma_async_tx_descriptor *tx;
1013 struct page **blocks = percpu->scribble;
1014 struct async_submit_ctl submit;
1015
1016 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1017 __func__, (unsigned long long)sh->sector, target, target2);
1018 BUG_ON(target < 0 || target2 < 0);
1019 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1020 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1021
Dan Williams6c910a72009-09-16 12:24:54 -07001022 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001023 * slot number conversion for 'faila' and 'failb'
1024 */
1025 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001026 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001027 count = 0;
1028 i = d0_idx;
1029 do {
1030 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1031
1032 blocks[slot] = sh->dev[i].page;
1033
1034 if (i == target)
1035 faila = slot;
1036 if (i == target2)
1037 failb = slot;
1038 i = raid6_next_disk(i, disks);
1039 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001040
1041 BUG_ON(faila == failb);
1042 if (failb < faila)
1043 swap(faila, failb);
1044 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1045 __func__, (unsigned long long)sh->sector, faila, failb);
1046
1047 atomic_inc(&sh->count);
1048
1049 if (failb == syndrome_disks+1) {
1050 /* Q disk is one of the missing disks */
1051 if (faila == syndrome_disks) {
1052 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001053 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1054 ops_complete_compute, sh,
1055 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +11001056 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001057 STRIPE_SIZE, &submit);
1058 } else {
1059 struct page *dest;
1060 int data_target;
1061 int qd_idx = sh->qd_idx;
1062
1063 /* Missing D+Q: recompute D from P, then recompute Q */
1064 if (target == qd_idx)
1065 data_target = target2;
1066 else
1067 data_target = target;
1068
1069 count = 0;
1070 for (i = disks; i-- ; ) {
1071 if (i == data_target || i == qd_idx)
1072 continue;
1073 blocks[count++] = sh->dev[i].page;
1074 }
1075 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001076 init_async_submit(&submit,
1077 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1078 NULL, NULL, NULL,
1079 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001080 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1081 &submit);
1082
1083 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001084 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1085 ops_complete_compute, sh,
1086 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001087 return async_gen_syndrome(blocks, 0, count+2,
1088 STRIPE_SIZE, &submit);
1089 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001090 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001091 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1092 ops_complete_compute, sh,
1093 to_addr_conv(sh, percpu));
1094 if (failb == syndrome_disks) {
1095 /* We're missing D+P. */
1096 return async_raid6_datap_recov(syndrome_disks+2,
1097 STRIPE_SIZE, faila,
1098 blocks, &submit);
1099 } else {
1100 /* We're missing D+D. */
1101 return async_raid6_2data_recov(syndrome_disks+2,
1102 STRIPE_SIZE, faila, failb,
1103 blocks, &submit);
1104 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001105 }
1106}
1107
1108
Dan Williams91c00922007-01-02 13:52:30 -07001109static void ops_complete_prexor(void *stripe_head_ref)
1110{
1111 struct stripe_head *sh = stripe_head_ref;
1112
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001113 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001114 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001115}
1116
1117static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001118ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1119 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001120{
Dan Williams91c00922007-01-02 13:52:30 -07001121 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001122 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001123 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001124 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001125
1126 /* existing parity data subtracted */
1127 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1128
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001129 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001130 (unsigned long long)sh->sector);
1131
1132 for (i = disks; i--; ) {
1133 struct r5dev *dev = &sh->dev[i];
1134 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001135 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001136 xor_srcs[count++] = dev->page;
1137 }
1138
Dan Williams0403e382009-09-08 17:42:50 -07001139 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001140 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001141 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001142
1143 return tx;
1144}
1145
1146static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001147ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001148{
1149 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001150 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001151
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001152 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001153 (unsigned long long)sh->sector);
1154
1155 for (i = disks; i--; ) {
1156 struct r5dev *dev = &sh->dev[i];
1157 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001158
Dan Williamsd8ee0722008-06-28 08:32:06 +10001159 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001160 struct bio *wbi;
1161
Shaohua Lib17459c2012-07-19 16:01:31 +10001162 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001163 chosen = dev->towrite;
1164 dev->towrite = NULL;
1165 BUG_ON(dev->written);
1166 wbi = dev->written = chosen;
Shaohua Lib17459c2012-07-19 16:01:31 +10001167 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001168
1169 while (wbi && wbi->bi_sector <
1170 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001171 if (wbi->bi_rw & REQ_FUA)
1172 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001173 if (wbi->bi_rw & REQ_SYNC)
1174 set_bit(R5_SyncIO, &dev->flags);
Shaohua Li620125f2012-10-11 13:49:05 +11001175 if (wbi->bi_rw & REQ_DISCARD) {
1176 memset(page_address(dev->page), 0,
1177 STRIPE_SECTORS << 9);
1178 set_bit(R5_Discard, &dev->flags);
1179 } else
1180 tx = async_copy_data(1, wbi, dev->page,
1181 dev->sector, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001182 wbi = r5_next_bio(wbi, dev->sector);
1183 }
1184 }
1185 }
1186
1187 return tx;
1188}
1189
Dan Williamsac6b53b2009-07-14 13:40:19 -07001190static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001191{
1192 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001193 int disks = sh->disks;
1194 int pd_idx = sh->pd_idx;
1195 int qd_idx = sh->qd_idx;
1196 int i;
Shaohua Libc0934f2012-05-22 13:55:05 +10001197 bool fua = false, sync = false;
Dan Williams91c00922007-01-02 13:52:30 -07001198
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001199 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001200 (unsigned long long)sh->sector);
1201
Shaohua Libc0934f2012-05-22 13:55:05 +10001202 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001203 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001204 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
1205 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001206
Dan Williams91c00922007-01-02 13:52:30 -07001207 for (i = disks; i--; ) {
1208 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001209
Tejun Heoe9c74692010-09-03 11:56:18 +02001210 if (dev->written || i == pd_idx || i == qd_idx) {
Dan Williams91c00922007-01-02 13:52:30 -07001211 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001212 if (fua)
1213 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001214 if (sync)
1215 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001216 }
Dan Williams91c00922007-01-02 13:52:30 -07001217 }
1218
Dan Williamsd8ee0722008-06-28 08:32:06 +10001219 if (sh->reconstruct_state == reconstruct_state_drain_run)
1220 sh->reconstruct_state = reconstruct_state_drain_result;
1221 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1222 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1223 else {
1224 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1225 sh->reconstruct_state = reconstruct_state_result;
1226 }
Dan Williams91c00922007-01-02 13:52:30 -07001227
1228 set_bit(STRIPE_HANDLE, &sh->state);
1229 release_stripe(sh);
1230}
1231
1232static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001233ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1234 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001235{
Dan Williams91c00922007-01-02 13:52:30 -07001236 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001237 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001238 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001239 int count = 0, pd_idx = sh->pd_idx, i;
1240 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001241 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001242 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001243
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001244 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001245 (unsigned long long)sh->sector);
1246
Shaohua Li620125f2012-10-11 13:49:05 +11001247 for (i = 0; i < sh->disks; i++) {
1248 if (pd_idx == i)
1249 continue;
1250 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1251 break;
1252 }
1253 if (i >= sh->disks) {
1254 atomic_inc(&sh->count);
1255 memset(page_address(sh->dev[pd_idx].page), 0,
1256 STRIPE_SECTORS << 9);
1257 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1258 ops_complete_reconstruct(sh);
1259 return;
1260 }
Dan Williams91c00922007-01-02 13:52:30 -07001261 /* check if prexor is active which means only process blocks
1262 * that are part of a read-modify-write (written)
1263 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001264 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1265 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001266 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1267 for (i = disks; i--; ) {
1268 struct r5dev *dev = &sh->dev[i];
1269 if (dev->written)
1270 xor_srcs[count++] = dev->page;
1271 }
1272 } else {
1273 xor_dest = sh->dev[pd_idx].page;
1274 for (i = disks; i--; ) {
1275 struct r5dev *dev = &sh->dev[i];
1276 if (i != pd_idx)
1277 xor_srcs[count++] = dev->page;
1278 }
1279 }
1280
Dan Williams91c00922007-01-02 13:52:30 -07001281 /* 1/ if we prexor'd then the dest is reused as a source
1282 * 2/ if we did not prexor then we are redoing the parity
1283 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1284 * for the synchronous xor case
1285 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001286 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001287 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1288
1289 atomic_inc(&sh->count);
1290
Dan Williamsac6b53b2009-07-14 13:40:19 -07001291 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001292 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001293 if (unlikely(count == 1))
1294 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1295 else
1296 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001297}
1298
Dan Williamsac6b53b2009-07-14 13:40:19 -07001299static void
1300ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1301 struct dma_async_tx_descriptor *tx)
1302{
1303 struct async_submit_ctl submit;
1304 struct page **blocks = percpu->scribble;
Shaohua Li620125f2012-10-11 13:49:05 +11001305 int count, i;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001306
1307 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1308
Shaohua Li620125f2012-10-11 13:49:05 +11001309 for (i = 0; i < sh->disks; i++) {
1310 if (sh->pd_idx == i || sh->qd_idx == i)
1311 continue;
1312 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1313 break;
1314 }
1315 if (i >= sh->disks) {
1316 atomic_inc(&sh->count);
1317 memset(page_address(sh->dev[sh->pd_idx].page), 0,
1318 STRIPE_SECTORS << 9);
1319 memset(page_address(sh->dev[sh->qd_idx].page), 0,
1320 STRIPE_SECTORS << 9);
1321 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1322 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1323 ops_complete_reconstruct(sh);
1324 return;
1325 }
1326
Dan Williamsac6b53b2009-07-14 13:40:19 -07001327 count = set_syndrome_sources(blocks, sh);
1328
1329 atomic_inc(&sh->count);
1330
1331 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1332 sh, to_addr_conv(sh, percpu));
1333 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001334}
1335
1336static void ops_complete_check(void *stripe_head_ref)
1337{
1338 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001339
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001340 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001341 (unsigned long long)sh->sector);
1342
Dan Williamsecc65c92008-06-28 08:31:57 +10001343 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001344 set_bit(STRIPE_HANDLE, &sh->state);
1345 release_stripe(sh);
1346}
1347
Dan Williamsac6b53b2009-07-14 13:40:19 -07001348static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001349{
Dan Williams91c00922007-01-02 13:52:30 -07001350 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001351 int pd_idx = sh->pd_idx;
1352 int qd_idx = sh->qd_idx;
1353 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001354 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001355 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001356 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001357 int count;
1358 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001359
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001360 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001361 (unsigned long long)sh->sector);
1362
Dan Williamsac6b53b2009-07-14 13:40:19 -07001363 count = 0;
1364 xor_dest = sh->dev[pd_idx].page;
1365 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001366 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001367 if (i == pd_idx || i == qd_idx)
1368 continue;
1369 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001370 }
1371
Dan Williamsd6f38f32009-07-14 11:50:52 -07001372 init_async_submit(&submit, 0, NULL, NULL, NULL,
1373 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001374 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001375 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001376
Dan Williams91c00922007-01-02 13:52:30 -07001377 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001378 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1379 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001380}
1381
Dan Williamsac6b53b2009-07-14 13:40:19 -07001382static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1383{
1384 struct page **srcs = percpu->scribble;
1385 struct async_submit_ctl submit;
1386 int count;
1387
1388 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1389 (unsigned long long)sh->sector, checkp);
1390
1391 count = set_syndrome_sources(srcs, sh);
1392 if (!checkp)
1393 srcs[count] = NULL;
1394
1395 atomic_inc(&sh->count);
1396 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1397 sh, to_addr_conv(sh, percpu));
1398 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1399 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1400}
1401
Dan Williams417b8d42009-10-16 16:25:22 +11001402static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001403{
1404 int overlap_clear = 0, i, disks = sh->disks;
1405 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001406 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001407 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001408 struct raid5_percpu *percpu;
1409 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001410
Dan Williamsd6f38f32009-07-14 11:50:52 -07001411 cpu = get_cpu();
1412 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001413 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001414 ops_run_biofill(sh);
1415 overlap_clear++;
1416 }
1417
Dan Williams7b3a8712008-06-28 08:32:09 +10001418 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001419 if (level < 6)
1420 tx = ops_run_compute5(sh, percpu);
1421 else {
1422 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1423 tx = ops_run_compute6_1(sh, percpu);
1424 else
1425 tx = ops_run_compute6_2(sh, percpu);
1426 }
1427 /* terminate the chain if reconstruct is not set to be run */
1428 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001429 async_tx_ack(tx);
1430 }
Dan Williams91c00922007-01-02 13:52:30 -07001431
Dan Williams600aa102008-06-28 08:32:05 +10001432 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001433 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001434
Dan Williams600aa102008-06-28 08:32:05 +10001435 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001436 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001437 overlap_clear++;
1438 }
1439
Dan Williamsac6b53b2009-07-14 13:40:19 -07001440 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1441 if (level < 6)
1442 ops_run_reconstruct5(sh, percpu, tx);
1443 else
1444 ops_run_reconstruct6(sh, percpu, tx);
1445 }
Dan Williams91c00922007-01-02 13:52:30 -07001446
Dan Williamsac6b53b2009-07-14 13:40:19 -07001447 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1448 if (sh->check_state == check_state_run)
1449 ops_run_check_p(sh, percpu);
1450 else if (sh->check_state == check_state_run_q)
1451 ops_run_check_pq(sh, percpu, 0);
1452 else if (sh->check_state == check_state_run_pq)
1453 ops_run_check_pq(sh, percpu, 1);
1454 else
1455 BUG();
1456 }
Dan Williams91c00922007-01-02 13:52:30 -07001457
Dan Williams91c00922007-01-02 13:52:30 -07001458 if (overlap_clear)
1459 for (i = disks; i--; ) {
1460 struct r5dev *dev = &sh->dev[i];
1461 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1462 wake_up(&sh->raid_conf->wait_for_overlap);
1463 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001464 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001465}
1466
Dan Williams417b8d42009-10-16 16:25:22 +11001467#ifdef CONFIG_MULTICORE_RAID456
1468static void async_run_ops(void *param, async_cookie_t cookie)
1469{
1470 struct stripe_head *sh = param;
1471 unsigned long ops_request = sh->ops.request;
1472
1473 clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
1474 wake_up(&sh->ops.wait_for_ops);
1475
1476 __raid_run_ops(sh, ops_request);
1477 release_stripe(sh);
1478}
1479
1480static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1481{
1482 /* since handle_stripe can be called outside of raid5d context
1483 * we need to ensure sh->ops.request is de-staged before another
1484 * request arrives
1485 */
1486 wait_event(sh->ops.wait_for_ops,
1487 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
1488 sh->ops.request = ops_request;
1489
1490 atomic_inc(&sh->count);
1491 async_schedule(async_run_ops, sh);
1492}
1493#else
1494#define raid_run_ops __raid_run_ops
1495#endif
1496
NeilBrownd1688a62011-10-11 16:49:52 +11001497static int grow_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
1499 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001500 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001501 if (!sh)
1502 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001503
NeilBrown3f294f42005-11-08 21:39:25 -08001504 sh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001505 #ifdef CONFIG_MULTICORE_RAID456
1506 init_waitqueue_head(&sh->ops.wait_for_ops);
1507 #endif
NeilBrown3f294f42005-11-08 21:39:25 -08001508
Shaohua Lib17459c2012-07-19 16:01:31 +10001509 spin_lock_init(&sh->stripe_lock);
1510
NeilBrowne4e11e32010-06-16 16:45:16 +10001511 if (grow_buffers(sh)) {
1512 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001513 kmem_cache_free(conf->slab_cache, sh);
1514 return 0;
1515 }
1516 /* we just created an active stripe so... */
1517 atomic_set(&sh->count, 1);
1518 atomic_inc(&conf->active_stripes);
1519 INIT_LIST_HEAD(&sh->lru);
1520 release_stripe(sh);
1521 return 1;
1522}
1523
NeilBrownd1688a62011-10-11 16:49:52 +11001524static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001525{
Christoph Lametere18b8902006-12-06 20:33:20 -08001526 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001527 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
NeilBrownf4be6b42010-06-01 19:37:25 +10001529 if (conf->mddev->gendisk)
1530 sprintf(conf->cache_name[0],
1531 "raid%d-%s", conf->level, mdname(conf->mddev));
1532 else
1533 sprintf(conf->cache_name[0],
1534 "raid%d-%p", conf->level, conf->mddev);
1535 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1536
NeilBrownad01c9e2006-03-27 01:18:07 -08001537 conf->active_name = 0;
1538 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001540 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 if (!sc)
1542 return 1;
1543 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001544 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001545 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001546 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 return 0;
1549}
NeilBrown29269552006-03-27 01:18:10 -08001550
Dan Williamsd6f38f32009-07-14 11:50:52 -07001551/**
1552 * scribble_len - return the required size of the scribble region
1553 * @num - total number of disks in the array
1554 *
1555 * The size must be enough to contain:
1556 * 1/ a struct page pointer for each device in the array +2
1557 * 2/ room to convert each entry in (1) to its corresponding dma
1558 * (dma_map_page()) or page (page_address()) address.
1559 *
1560 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1561 * calculate over all devices (not just the data blocks), using zeros in place
1562 * of the P and Q blocks.
1563 */
1564static size_t scribble_len(int num)
1565{
1566 size_t len;
1567
1568 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1569
1570 return len;
1571}
1572
NeilBrownd1688a62011-10-11 16:49:52 +11001573static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001574{
1575 /* Make all the stripes able to hold 'newsize' devices.
1576 * New slots in each stripe get 'page' set to a new page.
1577 *
1578 * This happens in stages:
1579 * 1/ create a new kmem_cache and allocate the required number of
1580 * stripe_heads.
1581 * 2/ gather all the old stripe_heads and tranfer the pages across
1582 * to the new stripe_heads. This will have the side effect of
1583 * freezing the array as once all stripe_heads have been collected,
1584 * no IO will be possible. Old stripe heads are freed once their
1585 * pages have been transferred over, and the old kmem_cache is
1586 * freed when all stripes are done.
1587 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1588 * we simple return a failre status - no need to clean anything up.
1589 * 4/ allocate new pages for the new slots in the new stripe_heads.
1590 * If this fails, we don't bother trying the shrink the
1591 * stripe_heads down again, we just leave them as they are.
1592 * As each stripe_head is processed the new one is released into
1593 * active service.
1594 *
1595 * Once step2 is started, we cannot afford to wait for a write,
1596 * so we use GFP_NOIO allocations.
1597 */
1598 struct stripe_head *osh, *nsh;
1599 LIST_HEAD(newstripes);
1600 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001601 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001602 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001603 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001604 int i;
1605
1606 if (newsize <= conf->pool_size)
1607 return 0; /* never bother to shrink */
1608
Dan Williamsb5470dc2008-06-27 21:44:04 -07001609 err = md_allow_write(conf->mddev);
1610 if (err)
1611 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001612
NeilBrownad01c9e2006-03-27 01:18:07 -08001613 /* Step 1 */
1614 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1615 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001616 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001617 if (!sc)
1618 return -ENOMEM;
1619
1620 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001621 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001622 if (!nsh)
1623 break;
1624
NeilBrownad01c9e2006-03-27 01:18:07 -08001625 nsh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001626 #ifdef CONFIG_MULTICORE_RAID456
1627 init_waitqueue_head(&nsh->ops.wait_for_ops);
1628 #endif
NeilBrownad01c9e2006-03-27 01:18:07 -08001629
1630 list_add(&nsh->lru, &newstripes);
1631 }
1632 if (i) {
1633 /* didn't get enough, give up */
1634 while (!list_empty(&newstripes)) {
1635 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1636 list_del(&nsh->lru);
1637 kmem_cache_free(sc, nsh);
1638 }
1639 kmem_cache_destroy(sc);
1640 return -ENOMEM;
1641 }
1642 /* Step 2 - Must use GFP_NOIO now.
1643 * OK, we have enough stripes, start collecting inactive
1644 * stripes and copying them over
1645 */
1646 list_for_each_entry(nsh, &newstripes, lru) {
1647 spin_lock_irq(&conf->device_lock);
1648 wait_event_lock_irq(conf->wait_for_stripe,
1649 !list_empty(&conf->inactive_list),
1650 conf->device_lock,
NeilBrown482c0832011-04-18 18:25:42 +10001651 );
NeilBrownad01c9e2006-03-27 01:18:07 -08001652 osh = get_free_stripe(conf);
1653 spin_unlock_irq(&conf->device_lock);
1654 atomic_set(&nsh->count, 1);
1655 for(i=0; i<conf->pool_size; i++)
1656 nsh->dev[i].page = osh->dev[i].page;
1657 for( ; i<newsize; i++)
1658 nsh->dev[i].page = NULL;
1659 kmem_cache_free(conf->slab_cache, osh);
1660 }
1661 kmem_cache_destroy(conf->slab_cache);
1662
1663 /* Step 3.
1664 * At this point, we are holding all the stripes so the array
1665 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001666 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001667 */
1668 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1669 if (ndisks) {
1670 for (i=0; i<conf->raid_disks; i++)
1671 ndisks[i] = conf->disks[i];
1672 kfree(conf->disks);
1673 conf->disks = ndisks;
1674 } else
1675 err = -ENOMEM;
1676
Dan Williamsd6f38f32009-07-14 11:50:52 -07001677 get_online_cpus();
1678 conf->scribble_len = scribble_len(newsize);
1679 for_each_present_cpu(cpu) {
1680 struct raid5_percpu *percpu;
1681 void *scribble;
1682
1683 percpu = per_cpu_ptr(conf->percpu, cpu);
1684 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1685
1686 if (scribble) {
1687 kfree(percpu->scribble);
1688 percpu->scribble = scribble;
1689 } else {
1690 err = -ENOMEM;
1691 break;
1692 }
1693 }
1694 put_online_cpus();
1695
NeilBrownad01c9e2006-03-27 01:18:07 -08001696 /* Step 4, return new stripes to service */
1697 while(!list_empty(&newstripes)) {
1698 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1699 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001700
NeilBrownad01c9e2006-03-27 01:18:07 -08001701 for (i=conf->raid_disks; i < newsize; i++)
1702 if (nsh->dev[i].page == NULL) {
1703 struct page *p = alloc_page(GFP_NOIO);
1704 nsh->dev[i].page = p;
1705 if (!p)
1706 err = -ENOMEM;
1707 }
1708 release_stripe(nsh);
1709 }
1710 /* critical section pass, GFP_NOIO no longer needed */
1711
1712 conf->slab_cache = sc;
1713 conf->active_name = 1-conf->active_name;
1714 conf->pool_size = newsize;
1715 return err;
1716}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
NeilBrownd1688a62011-10-11 16:49:52 +11001718static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719{
1720 struct stripe_head *sh;
1721
NeilBrown3f294f42005-11-08 21:39:25 -08001722 spin_lock_irq(&conf->device_lock);
1723 sh = get_free_stripe(conf);
1724 spin_unlock_irq(&conf->device_lock);
1725 if (!sh)
1726 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001727 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001728 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001729 kmem_cache_free(conf->slab_cache, sh);
1730 atomic_dec(&conf->active_stripes);
1731 return 1;
1732}
1733
NeilBrownd1688a62011-10-11 16:49:52 +11001734static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001735{
1736 while (drop_one_stripe(conf))
1737 ;
1738
NeilBrown29fc7e32006-02-03 03:03:41 -08001739 if (conf->slab_cache)
1740 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 conf->slab_cache = NULL;
1742}
1743
NeilBrown6712ecf2007-09-27 12:47:43 +02001744static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745{
NeilBrown99c0fb52009-03-31 14:39:38 +11001746 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001747 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001748 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001750 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11001751 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10001752 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
1754 for (i=0 ; i<disks; i++)
1755 if (bi == &sh->dev[i].req)
1756 break;
1757
Dan Williams45b42332007-07-09 11:56:43 -07001758 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1759 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 uptodate);
1761 if (i == disks) {
1762 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001763 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 }
NeilBrown14a75d32011-12-23 10:17:52 +11001765 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11001766 /* If replacement finished while this request was outstanding,
1767 * 'replacement' might be NULL already.
1768 * In that case it moved down to 'rdev'.
1769 * rdev is not removed until all requests are finished.
1770 */
NeilBrown14a75d32011-12-23 10:17:52 +11001771 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001772 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11001773 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
NeilBrown05616be2012-05-21 09:27:00 +10001775 if (use_new_offset(conf, sh))
1776 s = sh->sector + rdev->new_data_offset;
1777 else
1778 s = sh->sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001781 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11001782 /* Note that this cannot happen on a
1783 * replacement device. We just fail those on
1784 * any error
1785 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001786 printk_ratelimited(
1787 KERN_INFO
1788 "md/raid:%s: read error corrected"
1789 " (%lu sectors at %llu on %s)\n",
1790 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10001791 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001792 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001793 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001794 clear_bit(R5_ReadError, &sh->dev[i].flags);
1795 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng3f9e7c12012-07-31 10:04:21 +10001796 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
1797 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1798
NeilBrown14a75d32011-12-23 10:17:52 +11001799 if (atomic_read(&rdev->read_errors))
1800 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11001802 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001803 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10001804 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001807 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11001808 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1809 printk_ratelimited(
1810 KERN_WARNING
1811 "md/raid:%s: read error on replacement device "
1812 "(sector %llu on %s).\n",
1813 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001814 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11001815 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001816 else if (conf->mddev->degraded >= conf->max_degraded) {
1817 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001818 printk_ratelimited(
1819 KERN_WARNING
1820 "md/raid:%s: read error not correctable "
1821 "(sector %llu on %s).\n",
1822 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001823 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001824 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001825 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08001826 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10001827 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001828 printk_ratelimited(
1829 KERN_WARNING
1830 "md/raid:%s: read error NOT corrected!! "
1831 "(sector %llu on %s).\n",
1832 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001833 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001834 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001835 } else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001836 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001837 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001838 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001839 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001840 else
1841 retry = 1;
1842 if (retry)
majianpeng3f9e7c12012-07-31 10:04:21 +10001843 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
1844 set_bit(R5_ReadError, &sh->dev[i].flags);
1845 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1846 } else
1847 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -08001848 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001849 clear_bit(R5_ReadError, &sh->dev[i].flags);
1850 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10001851 if (!(set_bad
1852 && test_bit(In_sync, &rdev->flags)
1853 && rdev_set_badblocks(
1854 rdev, sh->sector, STRIPE_SECTORS, 0)))
1855 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 }
NeilBrown14a75d32011-12-23 10:17:52 +11001858 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1860 set_bit(STRIPE_HANDLE, &sh->state);
1861 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862}
1863
NeilBrownd710e132008-10-13 11:55:12 +11001864static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
NeilBrown99c0fb52009-03-31 14:39:38 +11001866 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001867 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001868 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11001869 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001871 sector_t first_bad;
1872 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11001873 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
NeilBrown977df362011-12-23 10:17:53 +11001875 for (i = 0 ; i < disks; i++) {
1876 if (bi == &sh->dev[i].req) {
1877 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 break;
NeilBrown977df362011-12-23 10:17:53 +11001879 }
1880 if (bi == &sh->dev[i].rreq) {
1881 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001882 if (rdev)
1883 replacement = 1;
1884 else
1885 /* rdev was removed and 'replacement'
1886 * replaced it. rdev is not removed
1887 * until all requests are finished.
1888 */
1889 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11001890 break;
1891 }
1892 }
Dan Williams45b42332007-07-09 11:56:43 -07001893 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1895 uptodate);
1896 if (i == disks) {
1897 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001898 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 }
1900
NeilBrown977df362011-12-23 10:17:53 +11001901 if (replacement) {
1902 if (!uptodate)
1903 md_error(conf->mddev, rdev);
1904 else if (is_badblock(rdev, sh->sector,
1905 STRIPE_SECTORS,
1906 &first_bad, &bad_sectors))
1907 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1908 } else {
1909 if (!uptodate) {
1910 set_bit(WriteErrorSeen, &rdev->flags);
1911 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11001912 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1913 set_bit(MD_RECOVERY_NEEDED,
1914 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11001915 } else if (is_badblock(rdev, sh->sector,
1916 STRIPE_SECTORS,
1917 &first_bad, &bad_sectors))
1918 set_bit(R5_MadeGood, &sh->dev[i].flags);
1919 }
1920 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
NeilBrown977df362011-12-23 10:17:53 +11001922 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1923 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001925 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926}
1927
NeilBrown784052e2009-03-31 15:19:07 +11001928static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
NeilBrown784052e2009-03-31 15:19:07 +11001930static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931{
1932 struct r5dev *dev = &sh->dev[i];
1933
1934 bio_init(&dev->req);
1935 dev->req.bi_io_vec = &dev->vec;
1936 dev->req.bi_vcnt++;
1937 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11001939 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
NeilBrown977df362011-12-23 10:17:53 +11001941 bio_init(&dev->rreq);
1942 dev->rreq.bi_io_vec = &dev->rvec;
1943 dev->rreq.bi_vcnt++;
1944 dev->rreq.bi_max_vecs++;
1945 dev->rreq.bi_private = sh;
1946 dev->rvec.bv_page = dev->page;
1947
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001949 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950}
1951
NeilBrownfd01b882011-10-11 16:47:53 +11001952static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
1954 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11001955 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11001956 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10001957 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
NeilBrown908f4fb2011-12-23 10:17:50 +11001959 spin_lock_irqsave(&conf->device_lock, flags);
1960 clear_bit(In_sync, &rdev->flags);
1961 mddev->degraded = calc_degraded(conf);
1962 spin_unlock_irqrestore(&conf->device_lock, flags);
1963 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1964
NeilBrownde393cd2011-07-28 11:31:48 +10001965 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001966 set_bit(Faulty, &rdev->flags);
1967 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1968 printk(KERN_ALERT
1969 "md/raid:%s: Disk failure on %s, disabling device.\n"
1970 "md/raid:%s: Operation continuing on %d devices.\n",
1971 mdname(mddev),
1972 bdevname(rdev->bdev, b),
1973 mdname(mddev),
1974 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001975}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
1977/*
1978 * Input: a 'big' sector number,
1979 * Output: index of the data and parity disk, and the sector # in them.
1980 */
NeilBrownd1688a62011-10-11 16:49:52 +11001981static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001982 int previous, int *dd_idx,
1983 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001985 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001986 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001988 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001989 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001991 int algorithm = previous ? conf->prev_algo
1992 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001993 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1994 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001995 int raid_disks = previous ? conf->previous_raid_disks
1996 : conf->raid_disks;
1997 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
1999 /* First compute the information on this sector */
2000
2001 /*
2002 * Compute the chunk number and the sector offset inside the chunk
2003 */
2004 chunk_offset = sector_div(r_sector, sectors_per_chunk);
2005 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
2007 /*
2008 * Compute the stripe number
2009 */
NeilBrown35f2a592010-04-20 14:13:34 +10002010 stripe = chunk_number;
2011 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10002012 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 /*
2014 * Select the parity disk based on the user selected algorithm.
2015 */
NeilBrown84789552011-07-27 11:00:36 +10002016 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07002017 switch(conf->level) {
2018 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11002019 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002020 break;
2021 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002022 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002024 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002025 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 (*dd_idx)++;
2027 break;
2028 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002029 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002030 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 (*dd_idx)++;
2032 break;
2033 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002034 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002035 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 break;
2037 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002038 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002039 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002041 case ALGORITHM_PARITY_0:
2042 pd_idx = 0;
2043 (*dd_idx)++;
2044 break;
2045 case ALGORITHM_PARITY_N:
2046 pd_idx = data_disks;
2047 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002049 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002050 }
2051 break;
2052 case 6:
2053
NeilBrowne183eae2009-03-31 15:20:22 +11002054 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002055 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002056 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002057 qd_idx = pd_idx + 1;
2058 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002059 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002060 qd_idx = 0;
2061 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002062 (*dd_idx) += 2; /* D D P Q D */
2063 break;
2064 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002065 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002066 qd_idx = pd_idx + 1;
2067 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002068 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002069 qd_idx = 0;
2070 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002071 (*dd_idx) += 2; /* D D P Q D */
2072 break;
2073 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002074 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002075 qd_idx = (pd_idx + 1) % raid_disks;
2076 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002077 break;
2078 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002079 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002080 qd_idx = (pd_idx + 1) % raid_disks;
2081 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002082 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002083
2084 case ALGORITHM_PARITY_0:
2085 pd_idx = 0;
2086 qd_idx = 1;
2087 (*dd_idx) += 2;
2088 break;
2089 case ALGORITHM_PARITY_N:
2090 pd_idx = data_disks;
2091 qd_idx = data_disks + 1;
2092 break;
2093
2094 case ALGORITHM_ROTATING_ZERO_RESTART:
2095 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2096 * of blocks for computing Q is different.
2097 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002098 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002099 qd_idx = pd_idx + 1;
2100 if (pd_idx == raid_disks-1) {
2101 (*dd_idx)++; /* Q D D D P */
2102 qd_idx = 0;
2103 } else if (*dd_idx >= pd_idx)
2104 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002105 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002106 break;
2107
2108 case ALGORITHM_ROTATING_N_RESTART:
2109 /* Same a left_asymmetric, by first stripe is
2110 * D D D P Q rather than
2111 * Q D D D P
2112 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002113 stripe2 += 1;
2114 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002115 qd_idx = pd_idx + 1;
2116 if (pd_idx == raid_disks-1) {
2117 (*dd_idx)++; /* Q D D D P */
2118 qd_idx = 0;
2119 } else if (*dd_idx >= pd_idx)
2120 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002121 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002122 break;
2123
2124 case ALGORITHM_ROTATING_N_CONTINUE:
2125 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002126 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002127 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2128 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002129 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002130 break;
2131
2132 case ALGORITHM_LEFT_ASYMMETRIC_6:
2133 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002134 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002135 if (*dd_idx >= pd_idx)
2136 (*dd_idx)++;
2137 qd_idx = raid_disks - 1;
2138 break;
2139
2140 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002141 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002142 if (*dd_idx >= pd_idx)
2143 (*dd_idx)++;
2144 qd_idx = raid_disks - 1;
2145 break;
2146
2147 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002148 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002149 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2150 qd_idx = raid_disks - 1;
2151 break;
2152
2153 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002154 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002155 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2156 qd_idx = raid_disks - 1;
2157 break;
2158
2159 case ALGORITHM_PARITY_0_6:
2160 pd_idx = 0;
2161 (*dd_idx)++;
2162 qd_idx = raid_disks - 1;
2163 break;
2164
NeilBrown16a53ec2006-06-26 00:27:38 -07002165 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002166 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002167 }
2168 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 }
2170
NeilBrown911d4ee2009-03-31 14:39:38 +11002171 if (sh) {
2172 sh->pd_idx = pd_idx;
2173 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002174 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 /*
2177 * Finally, compute the new sector number
2178 */
2179 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2180 return new_sector;
2181}
2182
2183
NeilBrown784052e2009-03-31 15:19:07 +11002184static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
NeilBrownd1688a62011-10-11 16:49:52 +11002186 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002187 int raid_disks = sh->disks;
2188 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002190 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2191 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002192 int algorithm = previous ? conf->prev_algo
2193 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 sector_t stripe;
2195 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002196 sector_t chunk_number;
2197 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002199 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
NeilBrown16a53ec2006-06-26 00:27:38 -07002201
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2203 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
NeilBrown16a53ec2006-06-26 00:27:38 -07002205 if (i == sh->pd_idx)
2206 return 0;
2207 switch(conf->level) {
2208 case 4: break;
2209 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002210 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 case ALGORITHM_LEFT_ASYMMETRIC:
2212 case ALGORITHM_RIGHT_ASYMMETRIC:
2213 if (i > sh->pd_idx)
2214 i--;
2215 break;
2216 case ALGORITHM_LEFT_SYMMETRIC:
2217 case ALGORITHM_RIGHT_SYMMETRIC:
2218 if (i < sh->pd_idx)
2219 i += raid_disks;
2220 i -= (sh->pd_idx + 1);
2221 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002222 case ALGORITHM_PARITY_0:
2223 i -= 1;
2224 break;
2225 case ALGORITHM_PARITY_N:
2226 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002228 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002229 }
2230 break;
2231 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002232 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002233 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002234 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002235 case ALGORITHM_LEFT_ASYMMETRIC:
2236 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002237 case ALGORITHM_ROTATING_ZERO_RESTART:
2238 case ALGORITHM_ROTATING_N_RESTART:
2239 if (sh->pd_idx == raid_disks-1)
2240 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002241 else if (i > sh->pd_idx)
2242 i -= 2; /* D D P Q D */
2243 break;
2244 case ALGORITHM_LEFT_SYMMETRIC:
2245 case ALGORITHM_RIGHT_SYMMETRIC:
2246 if (sh->pd_idx == raid_disks-1)
2247 i--; /* Q D D D P */
2248 else {
2249 /* D D P Q D */
2250 if (i < sh->pd_idx)
2251 i += raid_disks;
2252 i -= (sh->pd_idx + 2);
2253 }
2254 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002255 case ALGORITHM_PARITY_0:
2256 i -= 2;
2257 break;
2258 case ALGORITHM_PARITY_N:
2259 break;
2260 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002261 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002262 if (sh->pd_idx == 0)
2263 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002264 else {
2265 /* D D Q P D */
2266 if (i < sh->pd_idx)
2267 i += raid_disks;
2268 i -= (sh->pd_idx + 1);
2269 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002270 break;
2271 case ALGORITHM_LEFT_ASYMMETRIC_6:
2272 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2273 if (i > sh->pd_idx)
2274 i--;
2275 break;
2276 case ALGORITHM_LEFT_SYMMETRIC_6:
2277 case ALGORITHM_RIGHT_SYMMETRIC_6:
2278 if (i < sh->pd_idx)
2279 i += data_disks + 1;
2280 i -= (sh->pd_idx + 1);
2281 break;
2282 case ALGORITHM_PARITY_0_6:
2283 i -= 1;
2284 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002285 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002286 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002287 }
2288 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 }
2290
2291 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002292 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
NeilBrown112bf892009-03-31 14:39:38 +11002294 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002295 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002296 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2297 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002298 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2299 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 return 0;
2301 }
2302 return r_sector;
2303}
2304
2305
Dan Williams600aa102008-06-28 08:32:05 +10002306static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002307schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002308 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002309{
2310 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002311 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002312 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002313
Dan Williamse33129d2007-01-02 13:52:30 -07002314 if (rcw) {
2315 /* if we are not expanding this is a proper write request, and
2316 * there will be bios with new data to be drained into the
2317 * stripe cache
2318 */
2319 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10002320 sh->reconstruct_state = reconstruct_state_drain_run;
2321 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2322 } else
2323 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07002324
Dan Williamsac6b53b2009-07-14 13:40:19 -07002325 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002326
2327 for (i = disks; i--; ) {
2328 struct r5dev *dev = &sh->dev[i];
2329
2330 if (dev->towrite) {
2331 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002332 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002333 if (!expand)
2334 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002335 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002336 }
2337 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002338 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002339 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002340 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002341 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002342 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002343 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2344 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2345
Dan Williamsd8ee0722008-06-28 08:32:06 +10002346 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002347 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2348 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002349 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002350
2351 for (i = disks; i--; ) {
2352 struct r5dev *dev = &sh->dev[i];
2353 if (i == pd_idx)
2354 continue;
2355
Dan Williamse33129d2007-01-02 13:52:30 -07002356 if (dev->towrite &&
2357 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002358 test_bit(R5_Wantcompute, &dev->flags))) {
2359 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002360 set_bit(R5_LOCKED, &dev->flags);
2361 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002362 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002363 }
2364 }
2365 }
2366
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002367 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002368 * are in flight
2369 */
2370 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2371 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002372 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002373
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002374 if (level == 6) {
2375 int qd_idx = sh->qd_idx;
2376 struct r5dev *dev = &sh->dev[qd_idx];
2377
2378 set_bit(R5_LOCKED, &dev->flags);
2379 clear_bit(R5_UPTODATE, &dev->flags);
2380 s->locked++;
2381 }
2382
Dan Williams600aa102008-06-28 08:32:05 +10002383 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07002384 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002385 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002386}
NeilBrown16a53ec2006-06-26 00:27:38 -07002387
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388/*
2389 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002390 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 * The bi_next chain must be in order.
2392 */
2393static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2394{
2395 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002396 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002397 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
NeilBrowncbe47ec2011-07-26 11:20:35 +10002399 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 (unsigned long long)bi->bi_sector,
2401 (unsigned long long)sh->sector);
2402
Shaohua Lib17459c2012-07-19 16:01:31 +10002403 /*
2404 * If several bio share a stripe. The bio bi_phys_segments acts as a
2405 * reference count to avoid race. The reference count should already be
2406 * increased before this function is called (for example, in
2407 * make_request()), so other bio sharing this stripe will not free the
2408 * stripe. If a stripe is owned by one stripe, the stripe lock will
2409 * protect it.
2410 */
2411 spin_lock_irq(&sh->stripe_lock);
NeilBrown72626682005-09-09 16:23:54 -07002412 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002414 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07002415 firstwrite = 1;
2416 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 bip = &sh->dev[dd_idx].toread;
2418 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2419 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2420 goto overlap;
2421 bip = & (*bip)->bi_next;
2422 }
2423 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2424 goto overlap;
2425
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002426 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 if (*bip)
2428 bi->bi_next = *bip;
2429 *bip = bi;
Shaohua Lie7836bd62012-07-19 16:01:31 +10002430 raid5_inc_bi_active_stripes(bi);
NeilBrown72626682005-09-09 16:23:54 -07002431
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 if (forwrite) {
2433 /* check if page is covered */
2434 sector_t sector = sh->dev[dd_idx].sector;
2435 for (bi=sh->dev[dd_idx].towrite;
2436 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2437 bi && bi->bi_sector <= sector;
2438 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2439 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2440 sector = bi->bi_sector + (bi->bi_size>>9);
2441 }
2442 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2443 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2444 }
Shaohua Lib17459c2012-07-19 16:01:31 +10002445 spin_unlock_irq(&sh->stripe_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002446
2447 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2448 (unsigned long long)(*bip)->bi_sector,
2449 (unsigned long long)sh->sector, dd_idx);
2450
2451 if (conf->mddev->bitmap && firstwrite) {
2452 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2453 STRIPE_SECTORS, 0);
2454 sh->bm_seq = conf->seq_flush+1;
2455 set_bit(STRIPE_BIT_DELAY, &sh->state);
2456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 return 1;
2458
2459 overlap:
2460 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10002461 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 return 0;
2463}
2464
NeilBrownd1688a62011-10-11 16:49:52 +11002465static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002466
NeilBrownd1688a62011-10-11 16:49:52 +11002467static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002468 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002469{
NeilBrown784052e2009-03-31 15:19:07 +11002470 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002471 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002472 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002473 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002474 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002475
NeilBrown112bf892009-03-31 14:39:38 +11002476 raid5_compute_sector(conf,
2477 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002478 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002479 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002480 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002481}
2482
Dan Williamsa4456852007-07-09 11:56:43 -07002483static void
NeilBrownd1688a62011-10-11 16:49:52 +11002484handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002485 struct stripe_head_state *s, int disks,
2486 struct bio **return_bi)
2487{
2488 int i;
2489 for (i = disks; i--; ) {
2490 struct bio *bi;
2491 int bitmap_end = 0;
2492
2493 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002494 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002495 rcu_read_lock();
2496 rdev = rcu_dereference(conf->disks[i].rdev);
2497 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002498 atomic_inc(&rdev->nr_pending);
2499 else
2500 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002501 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002502 if (rdev) {
2503 if (!rdev_set_badblocks(
2504 rdev,
2505 sh->sector,
2506 STRIPE_SECTORS, 0))
2507 md_error(conf->mddev, rdev);
2508 rdev_dec_pending(rdev, conf->mddev);
2509 }
Dan Williamsa4456852007-07-09 11:56:43 -07002510 }
Shaohua Lib17459c2012-07-19 16:01:31 +10002511 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002512 /* fail all writes first */
2513 bi = sh->dev[i].towrite;
2514 sh->dev[i].towrite = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10002515 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002516 if (bi) {
2517 s->to_write--;
2518 bitmap_end = 1;
2519 }
2520
2521 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2522 wake_up(&conf->wait_for_overlap);
2523
2524 while (bi && bi->bi_sector <
2525 sh->dev[i].sector + STRIPE_SECTORS) {
2526 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2527 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002528 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002529 md_write_end(conf->mddev);
2530 bi->bi_next = *return_bi;
2531 *return_bi = bi;
2532 }
2533 bi = nextbi;
2534 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002535 if (bitmap_end)
2536 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2537 STRIPE_SECTORS, 0, 0);
2538 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002539 /* and fail all 'written' */
2540 bi = sh->dev[i].written;
2541 sh->dev[i].written = NULL;
2542 if (bi) bitmap_end = 1;
2543 while (bi && bi->bi_sector <
2544 sh->dev[i].sector + STRIPE_SECTORS) {
2545 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2546 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002547 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002548 md_write_end(conf->mddev);
2549 bi->bi_next = *return_bi;
2550 *return_bi = bi;
2551 }
2552 bi = bi2;
2553 }
2554
Dan Williamsb5e98d62007-01-02 13:52:31 -07002555 /* fail any reads if this device is non-operational and
2556 * the data has not reached the cache yet.
2557 */
2558 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2559 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2560 test_bit(R5_ReadError, &sh->dev[i].flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002561 bi = sh->dev[i].toread;
2562 sh->dev[i].toread = NULL;
2563 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2564 wake_up(&conf->wait_for_overlap);
2565 if (bi) s->to_read--;
2566 while (bi && bi->bi_sector <
2567 sh->dev[i].sector + STRIPE_SECTORS) {
2568 struct bio *nextbi =
2569 r5_next_bio(bi, sh->dev[i].sector);
2570 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002571 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002572 bi->bi_next = *return_bi;
2573 *return_bi = bi;
2574 }
2575 bi = nextbi;
2576 }
2577 }
Dan Williamsa4456852007-07-09 11:56:43 -07002578 if (bitmap_end)
2579 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2580 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002581 /* If we were in the middle of a write the parity block might
2582 * still be locked - so just clear all R5_LOCKED flags
2583 */
2584 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002585 }
2586
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002587 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2588 if (atomic_dec_and_test(&conf->pending_full_writes))
2589 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002590}
2591
NeilBrown7f0da592011-07-28 11:39:22 +10002592static void
NeilBrownd1688a62011-10-11 16:49:52 +11002593handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002594 struct stripe_head_state *s)
2595{
2596 int abort = 0;
2597 int i;
2598
NeilBrown7f0da592011-07-28 11:39:22 +10002599 clear_bit(STRIPE_SYNCING, &sh->state);
2600 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002601 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002602 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10002603 * Don't even need to abort as that is handled elsewhere
2604 * if needed, and not always wanted e.g. if there is a known
2605 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11002606 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002607 * non-sync devices, or abort the recovery
2608 */
NeilBrown18b98372012-04-01 23:48:38 +10002609 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2610 /* During recovery devices cannot be removed, so
2611 * locking and refcounting of rdevs is not needed
2612 */
2613 for (i = 0; i < conf->raid_disks; i++) {
2614 struct md_rdev *rdev = conf->disks[i].rdev;
2615 if (rdev
2616 && !test_bit(Faulty, &rdev->flags)
2617 && !test_bit(In_sync, &rdev->flags)
2618 && !rdev_set_badblocks(rdev, sh->sector,
2619 STRIPE_SECTORS, 0))
2620 abort = 1;
2621 rdev = conf->disks[i].replacement;
2622 if (rdev
2623 && !test_bit(Faulty, &rdev->flags)
2624 && !test_bit(In_sync, &rdev->flags)
2625 && !rdev_set_badblocks(rdev, sh->sector,
2626 STRIPE_SECTORS, 0))
2627 abort = 1;
2628 }
2629 if (abort)
2630 conf->recovery_disabled =
2631 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10002632 }
NeilBrown18b98372012-04-01 23:48:38 +10002633 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10002634}
2635
NeilBrown9a3e1102011-12-23 10:17:53 +11002636static int want_replace(struct stripe_head *sh, int disk_idx)
2637{
2638 struct md_rdev *rdev;
2639 int rv = 0;
2640 /* Doing recovery so rcu locking not required */
2641 rdev = sh->raid_conf->disks[disk_idx].replacement;
2642 if (rdev
2643 && !test_bit(Faulty, &rdev->flags)
2644 && !test_bit(In_sync, &rdev->flags)
2645 && (rdev->recovery_offset <= sh->sector
2646 || rdev->mddev->recovery_cp <= sh->sector))
2647 rv = 1;
2648
2649 return rv;
2650}
2651
NeilBrown93b3dbc2011-07-27 11:00:36 +10002652/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002653 * to be read or computed to satisfy a request.
2654 *
2655 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002656 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002657 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002658static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2659 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002660{
2661 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002662 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2663 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002664
Dan Williamsf38e1212007-01-02 13:52:30 -07002665 /* is the data in this block needed, and can we get it? */
2666 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002667 !test_bit(R5_UPTODATE, &dev->flags) &&
2668 (dev->toread ||
2669 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2670 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002671 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002672 (s->failed >= 1 && fdev[0]->toread) ||
2673 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002674 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2675 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2676 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002677 /* we would like to get this block, possibly by computing it,
2678 * otherwise read it if the backing disk is insync
2679 */
2680 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2681 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2682 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002683 (s->failed && (disk_idx == s->failed_num[0] ||
2684 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002685 /* have disk failed, and we're requested to fetch it;
2686 * do compute it
2687 */
2688 pr_debug("Computing stripe %llu block %d\n",
2689 (unsigned long long)sh->sector, disk_idx);
2690 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2691 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2692 set_bit(R5_Wantcompute, &dev->flags);
2693 sh->ops.target = disk_idx;
2694 sh->ops.target2 = -1; /* no 2nd target */
2695 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002696 /* Careful: from this point on 'uptodate' is in the eye
2697 * of raid_run_ops which services 'compute' operations
2698 * before writes. R5_Wantcompute flags a block that will
2699 * be R5_UPTODATE by the time it is needed for a
2700 * subsequent operation.
2701 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002702 s->uptodate++;
2703 return 1;
2704 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2705 /* Computing 2-failure is *very* expensive; only
2706 * do it if failed >= 2
2707 */
2708 int other;
2709 for (other = disks; other--; ) {
2710 if (other == disk_idx)
2711 continue;
2712 if (!test_bit(R5_UPTODATE,
2713 &sh->dev[other].flags))
2714 break;
2715 }
2716 BUG_ON(other < 0);
2717 pr_debug("Computing stripe %llu blocks %d,%d\n",
2718 (unsigned long long)sh->sector,
2719 disk_idx, other);
2720 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2721 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2722 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2723 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2724 sh->ops.target = disk_idx;
2725 sh->ops.target2 = other;
2726 s->uptodate += 2;
2727 s->req_compute = 1;
2728 return 1;
2729 } else if (test_bit(R5_Insync, &dev->flags)) {
2730 set_bit(R5_LOCKED, &dev->flags);
2731 set_bit(R5_Wantread, &dev->flags);
2732 s->locked++;
2733 pr_debug("Reading block %d (sync=%d)\n",
2734 disk_idx, s->syncing);
2735 }
2736 }
2737
2738 return 0;
2739}
2740
2741/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002742 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002743 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002744static void handle_stripe_fill(struct stripe_head *sh,
2745 struct stripe_head_state *s,
2746 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002747{
2748 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002749
2750 /* look for blocks to read/compute, skip this if a compute
2751 * is already in flight, or if the stripe contents are in the
2752 * midst of changing due to a write
2753 */
2754 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2755 !sh->reconstruct_state)
2756 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002757 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002758 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002759 set_bit(STRIPE_HANDLE, &sh->state);
2760}
2761
2762
Dan Williams1fe797e2008-06-28 09:16:30 +10002763/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002764 * any written block on an uptodate or failed drive can be returned.
2765 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2766 * never LOCKED, so we don't need to test 'failed' directly.
2767 */
NeilBrownd1688a62011-10-11 16:49:52 +11002768static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002769 struct stripe_head *sh, int disks, struct bio **return_bi)
2770{
2771 int i;
2772 struct r5dev *dev;
2773
2774 for (i = disks; i--; )
2775 if (sh->dev[i].written) {
2776 dev = &sh->dev[i];
2777 if (!test_bit(R5_LOCKED, &dev->flags) &&
2778 test_bit(R5_UPTODATE, &dev->flags)) {
2779 /* We can return any write requests */
2780 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07002781 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002782 wbi = dev->written;
2783 dev->written = NULL;
2784 while (wbi && wbi->bi_sector <
2785 dev->sector + STRIPE_SECTORS) {
2786 wbi2 = r5_next_bio(wbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002787 if (!raid5_dec_bi_active_stripes(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002788 md_write_end(conf->mddev);
2789 wbi->bi_next = *return_bi;
2790 *return_bi = wbi;
2791 }
2792 wbi = wbi2;
2793 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002794 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2795 STRIPE_SECTORS,
Dan Williamsa4456852007-07-09 11:56:43 -07002796 !test_bit(STRIPE_DEGRADED, &sh->state),
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002797 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002798 }
2799 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002800
2801 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2802 if (atomic_dec_and_test(&conf->pending_full_writes))
2803 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002804}
2805
NeilBrownd1688a62011-10-11 16:49:52 +11002806static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002807 struct stripe_head *sh,
2808 struct stripe_head_state *s,
2809 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002810{
2811 int rmw = 0, rcw = 0, i;
NeilBrownc8ac1802011-07-27 11:00:36 +10002812 if (conf->max_degraded == 2) {
2813 /* RAID6 requires 'rcw' in current implementation
2814 * Calculate the real rcw later - for now fake it
2815 * look like rcw is cheaper
2816 */
2817 rcw = 1; rmw = 2;
2818 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002819 /* would I have to read this buffer for read_modify_write */
2820 struct r5dev *dev = &sh->dev[i];
2821 if ((dev->towrite || i == sh->pd_idx) &&
2822 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002823 !(test_bit(R5_UPTODATE, &dev->flags) ||
2824 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002825 if (test_bit(R5_Insync, &dev->flags))
2826 rmw++;
2827 else
2828 rmw += 2*disks; /* cannot read it */
2829 }
2830 /* Would I have to read this buffer for reconstruct_write */
2831 if (!test_bit(R5_OVERWRITE, &dev->flags) && 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))) {
2835 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002836 else
2837 rcw += 2*disks;
2838 }
2839 }
Dan Williams45b42332007-07-09 11:56:43 -07002840 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002841 (unsigned long long)sh->sector, rmw, rcw);
2842 set_bit(STRIPE_HANDLE, &sh->state);
2843 if (rmw < rcw && rmw > 0)
2844 /* prefer read-modify-write, but need to get some data */
2845 for (i = disks; i--; ) {
2846 struct r5dev *dev = &sh->dev[i];
2847 if ((dev->towrite || i == sh->pd_idx) &&
2848 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002849 !(test_bit(R5_UPTODATE, &dev->flags) ||
2850 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002851 test_bit(R5_Insync, &dev->flags)) {
2852 if (
2853 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002854 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002855 "%d for r-m-w\n", i);
2856 set_bit(R5_LOCKED, &dev->flags);
2857 set_bit(R5_Wantread, &dev->flags);
2858 s->locked++;
2859 } else {
2860 set_bit(STRIPE_DELAYED, &sh->state);
2861 set_bit(STRIPE_HANDLE, &sh->state);
2862 }
2863 }
2864 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002865 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002866 /* want reconstruct write, but need to get some data */
NeilBrownc8ac1802011-07-27 11:00:36 +10002867 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002868 for (i = disks; i--; ) {
2869 struct r5dev *dev = &sh->dev[i];
2870 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002871 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002872 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002873 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002874 test_bit(R5_Wantcompute, &dev->flags))) {
2875 rcw++;
2876 if (!test_bit(R5_Insync, &dev->flags))
2877 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002878 if (
2879 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002880 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002881 "%d for Reconstruct\n", i);
2882 set_bit(R5_LOCKED, &dev->flags);
2883 set_bit(R5_Wantread, &dev->flags);
2884 s->locked++;
2885 } else {
2886 set_bit(STRIPE_DELAYED, &sh->state);
2887 set_bit(STRIPE_HANDLE, &sh->state);
2888 }
2889 }
2890 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002891 }
Dan Williamsa4456852007-07-09 11:56:43 -07002892 /* now if nothing is locked, and if we have enough data,
2893 * we can start a write request
2894 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002895 /* since handle_stripe can be called at any time we need to handle the
2896 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002897 * subsequent call wants to start a write request. raid_run_ops only
2898 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002899 * simultaneously. If this is not the case then new writes need to be
2900 * held off until the compute completes.
2901 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002902 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2903 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2904 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002905 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002906}
2907
NeilBrownd1688a62011-10-11 16:49:52 +11002908static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002909 struct stripe_head_state *s, int disks)
2910{
Dan Williamsecc65c92008-06-28 08:31:57 +10002911 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002912
Dan Williamsbd2ab672008-04-10 21:29:27 -07002913 set_bit(STRIPE_HANDLE, &sh->state);
2914
Dan Williamsecc65c92008-06-28 08:31:57 +10002915 switch (sh->check_state) {
2916 case check_state_idle:
2917 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002918 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002919 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002920 sh->check_state = check_state_run;
2921 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002922 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002923 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002924 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002925 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002926 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002927 /* fall through */
2928 case check_state_compute_result:
2929 sh->check_state = check_state_idle;
2930 if (!dev)
2931 dev = &sh->dev[sh->pd_idx];
2932
2933 /* check that a write has not made the stripe insync */
2934 if (test_bit(STRIPE_INSYNC, &sh->state))
2935 break;
2936
2937 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002938 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2939 BUG_ON(s->uptodate != disks);
2940
2941 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002942 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002943 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002944
Dan Williamsa4456852007-07-09 11:56:43 -07002945 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002946 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002947 break;
2948 case check_state_run:
2949 break; /* we will be called again upon completion */
2950 case check_state_check_result:
2951 sh->check_state = check_state_idle;
2952
2953 /* if a failure occurred during the check operation, leave
2954 * STRIPE_INSYNC not set and let the stripe be handled again
2955 */
2956 if (s->failed)
2957 break;
2958
2959 /* handle a successful check operation, if parity is correct
2960 * we are done. Otherwise update the mismatch count and repair
2961 * parity if !MD_RECOVERY_CHECK
2962 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002963 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002964 /* parity is correct (on disc,
2965 * not in buffer any more)
2966 */
2967 set_bit(STRIPE_INSYNC, &sh->state);
2968 else {
2969 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2970 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2971 /* don't try to repair!! */
2972 set_bit(STRIPE_INSYNC, &sh->state);
2973 else {
2974 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002975 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002976 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2977 set_bit(R5_Wantcompute,
2978 &sh->dev[sh->pd_idx].flags);
2979 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002980 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10002981 s->uptodate++;
2982 }
2983 }
2984 break;
2985 case check_state_compute_run:
2986 break;
2987 default:
2988 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2989 __func__, sh->check_state,
2990 (unsigned long long) sh->sector);
2991 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002992 }
2993}
2994
2995
NeilBrownd1688a62011-10-11 16:49:52 +11002996static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07002997 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10002998 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002999{
Dan Williamsa4456852007-07-09 11:56:43 -07003000 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11003001 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07003002 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07003003
3004 set_bit(STRIPE_HANDLE, &sh->state);
3005
3006 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003007
Dan Williamsa4456852007-07-09 11:56:43 -07003008 /* Want to check and possibly repair P and Q.
3009 * However there could be one 'failed' device, in which
3010 * case we can only check one of them, possibly using the
3011 * other to generate missing data
3012 */
3013
Dan Williamsd82dfee2009-07-14 13:40:57 -07003014 switch (sh->check_state) {
3015 case check_state_idle:
3016 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10003017 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003018 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07003019 * makes sense to check P (If anything else were failed,
3020 * we would have used P to recreate it).
3021 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003022 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07003023 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003024 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003025 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07003026 * anything, so it makes sense to check it
3027 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003028 if (sh->check_state == check_state_run)
3029 sh->check_state = check_state_run_pq;
3030 else
3031 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07003032 }
Dan Williams36d1c642009-07-14 11:48:22 -07003033
Dan Williamsd82dfee2009-07-14 13:40:57 -07003034 /* discard potentially stale zero_sum_result */
3035 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07003036
Dan Williamsd82dfee2009-07-14 13:40:57 -07003037 if (sh->check_state == check_state_run) {
3038 /* async_xor_zero_sum destroys the contents of P */
3039 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
3040 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07003041 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003042 if (sh->check_state >= check_state_run &&
3043 sh->check_state <= check_state_run_pq) {
3044 /* async_syndrome_zero_sum preserves P and Q, so
3045 * no need to mark them !uptodate here
3046 */
3047 set_bit(STRIPE_OP_CHECK, &s->ops_request);
3048 break;
3049 }
Dan Williams36d1c642009-07-14 11:48:22 -07003050
Dan Williamsd82dfee2009-07-14 13:40:57 -07003051 /* we have 2-disk failure */
3052 BUG_ON(s->failed != 2);
3053 /* fall through */
3054 case check_state_compute_result:
3055 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07003056
Dan Williamsd82dfee2009-07-14 13:40:57 -07003057 /* check that a write has not made the stripe insync */
3058 if (test_bit(STRIPE_INSYNC, &sh->state))
3059 break;
Dan Williamsa4456852007-07-09 11:56:43 -07003060
3061 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07003062 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07003063 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003064 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07003065 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003066 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07003067 s->locked++;
3068 set_bit(R5_LOCKED, &dev->flags);
3069 set_bit(R5_Wantwrite, &dev->flags);
3070 }
3071 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003072 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07003073 s->locked++;
3074 set_bit(R5_LOCKED, &dev->flags);
3075 set_bit(R5_Wantwrite, &dev->flags);
3076 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003077 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003078 dev = &sh->dev[pd_idx];
3079 s->locked++;
3080 set_bit(R5_LOCKED, &dev->flags);
3081 set_bit(R5_Wantwrite, &dev->flags);
3082 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003083 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003084 dev = &sh->dev[qd_idx];
3085 s->locked++;
3086 set_bit(R5_LOCKED, &dev->flags);
3087 set_bit(R5_Wantwrite, &dev->flags);
3088 }
3089 clear_bit(STRIPE_DEGRADED, &sh->state);
3090
3091 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003092 break;
3093 case check_state_run:
3094 case check_state_run_q:
3095 case check_state_run_pq:
3096 break; /* we will be called again upon completion */
3097 case check_state_check_result:
3098 sh->check_state = check_state_idle;
3099
3100 /* handle a successful check operation, if parity is correct
3101 * we are done. Otherwise update the mismatch count and repair
3102 * parity if !MD_RECOVERY_CHECK
3103 */
3104 if (sh->ops.zero_sum_result == 0) {
3105 /* both parities are correct */
3106 if (!s->failed)
3107 set_bit(STRIPE_INSYNC, &sh->state);
3108 else {
3109 /* in contrast to the raid5 case we can validate
3110 * parity, but still have a failure to write
3111 * back
3112 */
3113 sh->check_state = check_state_compute_result;
3114 /* Returning at this point means that we may go
3115 * off and bring p and/or q uptodate again so
3116 * we make sure to check zero_sum_result again
3117 * to verify if p or q need writeback
3118 */
3119 }
3120 } else {
3121 conf->mddev->resync_mismatches += STRIPE_SECTORS;
3122 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3123 /* don't try to repair!! */
3124 set_bit(STRIPE_INSYNC, &sh->state);
3125 else {
3126 int *target = &sh->ops.target;
3127
3128 sh->ops.target = -1;
3129 sh->ops.target2 = -1;
3130 sh->check_state = check_state_compute_run;
3131 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3132 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3133 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3134 set_bit(R5_Wantcompute,
3135 &sh->dev[pd_idx].flags);
3136 *target = pd_idx;
3137 target = &sh->ops.target2;
3138 s->uptodate++;
3139 }
3140 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3141 set_bit(R5_Wantcompute,
3142 &sh->dev[qd_idx].flags);
3143 *target = qd_idx;
3144 s->uptodate++;
3145 }
3146 }
3147 }
3148 break;
3149 case check_state_compute_run:
3150 break;
3151 default:
3152 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3153 __func__, sh->check_state,
3154 (unsigned long long) sh->sector);
3155 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003156 }
3157}
3158
NeilBrownd1688a62011-10-11 16:49:52 +11003159static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003160{
3161 int i;
3162
3163 /* We have read all the blocks in this stripe and now we need to
3164 * copy some of them into a target stripe for expand.
3165 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003166 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003167 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3168 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003169 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003170 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003171 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003172 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003173
NeilBrown784052e2009-03-31 15:19:07 +11003174 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003175 sector_t s = raid5_compute_sector(conf, bn, 0,
3176 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003177 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003178 if (sh2 == NULL)
3179 /* so far only the early blocks of this stripe
3180 * have been requested. When later blocks
3181 * get requested, we will try again
3182 */
3183 continue;
3184 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3185 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3186 /* must have already done this block */
3187 release_stripe(sh2);
3188 continue;
3189 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003190
3191 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003192 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003193 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003194 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003195 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003196
Dan Williamsa4456852007-07-09 11:56:43 -07003197 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3198 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3199 for (j = 0; j < conf->raid_disks; j++)
3200 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003201 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003202 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3203 break;
3204 if (j == conf->raid_disks) {
3205 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3206 set_bit(STRIPE_HANDLE, &sh2->state);
3207 }
3208 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003209
Dan Williamsa4456852007-07-09 11:56:43 -07003210 }
NeilBrowna2e08552007-09-11 15:23:36 -07003211 /* done submitting copies, wait for them to complete */
3212 if (tx) {
3213 async_tx_ack(tx);
3214 dma_wait_for_async_tx(tx);
3215 }
Dan Williamsa4456852007-07-09 11:56:43 -07003216}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
3218/*
3219 * handle_stripe - do things to a stripe.
3220 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003221 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3222 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003224 * return some read requests which now have data
3225 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 * schedule a read on some buffers
3227 * schedule a write of some buffers
3228 * return confirmation of parity correctness
3229 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 */
Dan Williamsa4456852007-07-09 11:56:43 -07003231
NeilBrownacfe7262011-07-27 11:00:36 +10003232static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003233{
NeilBrownd1688a62011-10-11 16:49:52 +11003234 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003235 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003236 struct r5dev *dev;
3237 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003238 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003239
NeilBrownacfe7262011-07-27 11:00:36 +10003240 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003241
NeilBrownacfe7262011-07-27 11:00:36 +10003242 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3243 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3244 s->failed_num[0] = -1;
3245 s->failed_num[1] = -1;
3246
3247 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003248 rcu_read_lock();
3249 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003250 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003251 sector_t first_bad;
3252 int bad_sectors;
3253 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003254
NeilBrown16a53ec2006-06-26 00:27:38 -07003255 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003256
Dan Williams45b42332007-07-09 11:56:43 -07003257 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003258 i, dev->flags,
3259 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003260 /* maybe we can reply to a read
3261 *
3262 * new wantfill requests are only permitted while
3263 * ops_complete_biofill is guaranteed to be inactive
3264 */
3265 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3266 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3267 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003268
3269 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003270 if (test_bit(R5_LOCKED, &dev->flags))
3271 s->locked++;
3272 if (test_bit(R5_UPTODATE, &dev->flags))
3273 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003274 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003275 s->compute++;
3276 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003277 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003278
NeilBrownacfe7262011-07-27 11:00:36 +10003279 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003280 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003281 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003282 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003283 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003284 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003285 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003286 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003287 }
Dan Williamsa4456852007-07-09 11:56:43 -07003288 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003289 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003290 /* Prefer to use the replacement for reads, but only
3291 * if it is recovered enough and has no bad blocks.
3292 */
3293 rdev = rcu_dereference(conf->disks[i].replacement);
3294 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3295 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3296 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3297 &first_bad, &bad_sectors))
3298 set_bit(R5_ReadRepl, &dev->flags);
3299 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003300 if (rdev)
3301 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003302 rdev = rcu_dereference(conf->disks[i].rdev);
3303 clear_bit(R5_ReadRepl, &dev->flags);
3304 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003305 if (rdev && test_bit(Faulty, &rdev->flags))
3306 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003307 if (rdev) {
3308 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3309 &first_bad, &bad_sectors);
3310 if (s->blocked_rdev == NULL
3311 && (test_bit(Blocked, &rdev->flags)
3312 || is_bad < 0)) {
3313 if (is_bad < 0)
3314 set_bit(BlockedBadBlocks,
3315 &rdev->flags);
3316 s->blocked_rdev = rdev;
3317 atomic_inc(&rdev->nr_pending);
3318 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003319 }
NeilBrown415e72d2010-06-17 17:25:21 +10003320 clear_bit(R5_Insync, &dev->flags);
3321 if (!rdev)
3322 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003323 else if (is_bad) {
3324 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10003325 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3326 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003327 /* treat as in-sync, but with a read error
3328 * which we can now try to correct
3329 */
3330 set_bit(R5_Insync, &dev->flags);
3331 set_bit(R5_ReadError, &dev->flags);
3332 }
3333 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003334 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003335 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003336 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003337 set_bit(R5_Insync, &dev->flags);
3338 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3339 test_bit(R5_Expanded, &dev->flags))
3340 /* If we've reshaped into here, we assume it is Insync.
3341 * We will shortly update recovery_offset to make
3342 * it official.
3343 */
3344 set_bit(R5_Insync, &dev->flags);
3345
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003346 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003347 /* This flag does not apply to '.replacement'
3348 * only to .rdev, so make sure to check that*/
3349 struct md_rdev *rdev2 = rcu_dereference(
3350 conf->disks[i].rdev);
3351 if (rdev2 == rdev)
3352 clear_bit(R5_Insync, &dev->flags);
3353 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003354 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003355 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003356 } else
3357 clear_bit(R5_WriteError, &dev->flags);
3358 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003359 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003360 /* This flag does not apply to '.replacement'
3361 * only to .rdev, so make sure to check that*/
3362 struct md_rdev *rdev2 = rcu_dereference(
3363 conf->disks[i].rdev);
3364 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003365 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003366 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003367 } else
3368 clear_bit(R5_MadeGood, &dev->flags);
3369 }
NeilBrown977df362011-12-23 10:17:53 +11003370 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3371 struct md_rdev *rdev2 = rcu_dereference(
3372 conf->disks[i].replacement);
3373 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3374 s->handle_bad_blocks = 1;
3375 atomic_inc(&rdev2->nr_pending);
3376 } else
3377 clear_bit(R5_MadeGoodRepl, &dev->flags);
3378 }
NeilBrown415e72d2010-06-17 17:25:21 +10003379 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003380 /* The ReadError flag will just be confusing now */
3381 clear_bit(R5_ReadError, &dev->flags);
3382 clear_bit(R5_ReWrite, &dev->flags);
3383 }
NeilBrown415e72d2010-06-17 17:25:21 +10003384 if (test_bit(R5_ReadError, &dev->flags))
3385 clear_bit(R5_Insync, &dev->flags);
3386 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003387 if (s->failed < 2)
3388 s->failed_num[s->failed] = i;
3389 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003390 if (rdev && !test_bit(Faulty, &rdev->flags))
3391 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003392 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003393 }
NeilBrown9a3e1102011-12-23 10:17:53 +11003394 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3395 /* If there is a failed device being replaced,
3396 * we must be recovering.
3397 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10003398 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11003399 * else we can only be replacing
3400 * sync and recovery both need to read all devices, and so
3401 * use the same flag.
3402 */
3403 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10003404 sh->sector >= conf->mddev->recovery_cp ||
3405 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11003406 s->syncing = 1;
3407 else
3408 s->replacing = 1;
3409 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003410 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003411}
NeilBrownf4168852007-02-28 20:11:53 -08003412
NeilBrowncc940152011-07-26 11:35:35 +10003413static void handle_stripe(struct stripe_head *sh)
3414{
3415 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003416 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003417 int i;
NeilBrown84789552011-07-27 11:00:36 +10003418 int prexor;
3419 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003420 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003421
3422 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003423 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003424 /* already being handled, ensure it gets handled
3425 * again when current action finishes */
3426 set_bit(STRIPE_HANDLE, &sh->state);
3427 return;
3428 }
3429
3430 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3431 set_bit(STRIPE_SYNCING, &sh->state);
3432 clear_bit(STRIPE_INSYNC, &sh->state);
3433 }
3434 clear_bit(STRIPE_DELAYED, &sh->state);
3435
3436 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3437 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3438 (unsigned long long)sh->sector, sh->state,
3439 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3440 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003441
NeilBrownacfe7262011-07-27 11:00:36 +10003442 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003443
NeilBrownbc2607f2011-07-28 11:39:22 +10003444 if (s.handle_bad_blocks) {
3445 set_bit(STRIPE_HANDLE, &sh->state);
3446 goto finish;
3447 }
3448
NeilBrown474af965fe2011-07-27 11:00:36 +10003449 if (unlikely(s.blocked_rdev)) {
3450 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003451 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003452 set_bit(STRIPE_HANDLE, &sh->state);
3453 goto finish;
3454 }
3455 /* There is nothing for the blocked_rdev to block */
3456 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3457 s.blocked_rdev = NULL;
3458 }
3459
3460 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3461 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3462 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3463 }
3464
3465 pr_debug("locked=%d uptodate=%d to_read=%d"
3466 " to_write=%d failed=%d failed_num=%d,%d\n",
3467 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3468 s.failed_num[0], s.failed_num[1]);
3469 /* check if the array has lost more than max_degraded devices and,
3470 * if so, some requests might need to be failed.
3471 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003472 if (s.failed > conf->max_degraded) {
3473 sh->check_state = 0;
3474 sh->reconstruct_state = 0;
3475 if (s.to_read+s.to_write+s.written)
3476 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003477 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003478 handle_failed_sync(conf, sh, &s);
3479 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003480
3481 /*
3482 * might be able to return some write requests if the parity blocks
3483 * are safe, or on a failed drive
3484 */
3485 pdev = &sh->dev[sh->pd_idx];
3486 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3487 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3488 qdev = &sh->dev[sh->qd_idx];
3489 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3490 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3491 || conf->level < 6;
3492
3493 if (s.written &&
3494 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3495 && !test_bit(R5_LOCKED, &pdev->flags)
3496 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3497 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3498 && !test_bit(R5_LOCKED, &qdev->flags)
3499 && test_bit(R5_UPTODATE, &qdev->flags)))))
3500 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3501
3502 /* Now we might consider reading some blocks, either to check/generate
3503 * parity, or to satisfy requests
3504 * or to load a block that is being partially written.
3505 */
3506 if (s.to_read || s.non_overwrite
3507 || (conf->level == 6 && s.to_write && s.failed)
NeilBrown9a3e1102011-12-23 10:17:53 +11003508 || (s.syncing && (s.uptodate + s.compute < disks))
3509 || s.replacing
3510 || s.expanding)
NeilBrown474af965fe2011-07-27 11:00:36 +10003511 handle_stripe_fill(sh, &s, disks);
3512
NeilBrown84789552011-07-27 11:00:36 +10003513 /* Now we check to see if any write operations have recently
3514 * completed
3515 */
3516 prexor = 0;
3517 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3518 prexor = 1;
3519 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3520 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3521 sh->reconstruct_state = reconstruct_state_idle;
3522
3523 /* All the 'written' buffers and the parity block are ready to
3524 * be written back to disk
3525 */
3526 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3527 BUG_ON(sh->qd_idx >= 0 &&
3528 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags));
3529 for (i = disks; i--; ) {
3530 struct r5dev *dev = &sh->dev[i];
3531 if (test_bit(R5_LOCKED, &dev->flags) &&
3532 (i == sh->pd_idx || i == sh->qd_idx ||
3533 dev->written)) {
3534 pr_debug("Writing block %d\n", i);
3535 set_bit(R5_Wantwrite, &dev->flags);
3536 if (prexor)
3537 continue;
3538 if (!test_bit(R5_Insync, &dev->flags) ||
3539 ((i == sh->pd_idx || i == sh->qd_idx) &&
3540 s.failed == 0))
3541 set_bit(STRIPE_INSYNC, &sh->state);
3542 }
3543 }
3544 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3545 s.dec_preread_active = 1;
3546 }
3547
3548 /* Now to consider new write requests and what else, if anything
3549 * should be read. We do not handle new writes when:
3550 * 1/ A 'write' operation (copy+xor) is already in flight.
3551 * 2/ A 'check' operation is in flight, as it may clobber the parity
3552 * block.
3553 */
3554 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3555 handle_stripe_dirtying(conf, sh, &s, disks);
3556
3557 /* maybe we need to check and possibly fix the parity for this stripe
3558 * Any reads will already have been scheduled, so we just see if enough
3559 * data is available. The parity check is held off while parity
3560 * dependent operations are in flight.
3561 */
3562 if (sh->check_state ||
3563 (s.syncing && s.locked == 0 &&
3564 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3565 !test_bit(STRIPE_INSYNC, &sh->state))) {
3566 if (conf->level == 6)
3567 handle_parity_checks6(conf, sh, &s, disks);
3568 else
3569 handle_parity_checks5(conf, sh, &s, disks);
3570 }
NeilBrownc5a31002011-07-27 11:00:36 +10003571
NeilBrown9a3e1102011-12-23 10:17:53 +11003572 if (s.replacing && s.locked == 0
3573 && !test_bit(STRIPE_INSYNC, &sh->state)) {
3574 /* Write out to replacement devices where possible */
3575 for (i = 0; i < conf->raid_disks; i++)
3576 if (test_bit(R5_UPTODATE, &sh->dev[i].flags) &&
3577 test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3578 set_bit(R5_WantReplace, &sh->dev[i].flags);
3579 set_bit(R5_LOCKED, &sh->dev[i].flags);
3580 s.locked++;
3581 }
3582 set_bit(STRIPE_INSYNC, &sh->state);
3583 }
3584 if ((s.syncing || s.replacing) && s.locked == 0 &&
3585 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003586 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3587 clear_bit(STRIPE_SYNCING, &sh->state);
3588 }
3589
3590 /* If the failed drives are just a ReadError, then we might need
3591 * to progress the repair/check process
3592 */
3593 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3594 for (i = 0; i < s.failed; i++) {
3595 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3596 if (test_bit(R5_ReadError, &dev->flags)
3597 && !test_bit(R5_LOCKED, &dev->flags)
3598 && test_bit(R5_UPTODATE, &dev->flags)
3599 ) {
3600 if (!test_bit(R5_ReWrite, &dev->flags)) {
3601 set_bit(R5_Wantwrite, &dev->flags);
3602 set_bit(R5_ReWrite, &dev->flags);
3603 set_bit(R5_LOCKED, &dev->flags);
3604 s.locked++;
3605 } else {
3606 /* let's read it back */
3607 set_bit(R5_Wantread, &dev->flags);
3608 set_bit(R5_LOCKED, &dev->flags);
3609 s.locked++;
3610 }
3611 }
3612 }
3613
3614
NeilBrown3687c062011-07-27 11:00:36 +10003615 /* Finish reconstruct operations initiated by the expansion process */
3616 if (sh->reconstruct_state == reconstruct_state_result) {
3617 struct stripe_head *sh_src
3618 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3619 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3620 /* sh cannot be written until sh_src has been read.
3621 * so arrange for sh to be delayed a little
3622 */
3623 set_bit(STRIPE_DELAYED, &sh->state);
3624 set_bit(STRIPE_HANDLE, &sh->state);
3625 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3626 &sh_src->state))
3627 atomic_inc(&conf->preread_active_stripes);
3628 release_stripe(sh_src);
3629 goto finish;
3630 }
3631 if (sh_src)
3632 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003633
NeilBrown3687c062011-07-27 11:00:36 +10003634 sh->reconstruct_state = reconstruct_state_idle;
3635 clear_bit(STRIPE_EXPANDING, &sh->state);
3636 for (i = conf->raid_disks; i--; ) {
3637 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3638 set_bit(R5_LOCKED, &sh->dev[i].flags);
3639 s.locked++;
3640 }
3641 }
3642
3643 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3644 !sh->reconstruct_state) {
3645 /* Need to write out all blocks after computing parity */
3646 sh->disks = conf->raid_disks;
3647 stripe_set_idx(sh->sector, conf, 0, sh);
3648 schedule_reconstruction(sh, &s, 1, 1);
3649 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3650 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3651 atomic_dec(&conf->reshape_stripes);
3652 wake_up(&conf->wait_for_overlap);
3653 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3654 }
3655
3656 if (s.expanding && s.locked == 0 &&
3657 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3658 handle_stripe_expansion(conf, sh);
3659
3660finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003661 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10003662 if (unlikely(s.blocked_rdev)) {
3663 if (conf->mddev->external)
3664 md_wait_for_blocked_rdev(s.blocked_rdev,
3665 conf->mddev);
3666 else
3667 /* Internal metadata will immediately
3668 * be written by raid5d, so we don't
3669 * need to wait here.
3670 */
3671 rdev_dec_pending(s.blocked_rdev,
3672 conf->mddev);
3673 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003674
NeilBrownbc2607f2011-07-28 11:39:22 +10003675 if (s.handle_bad_blocks)
3676 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003677 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003678 struct r5dev *dev = &sh->dev[i];
3679 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3680 /* We own a safe reference to the rdev */
3681 rdev = conf->disks[i].rdev;
3682 if (!rdev_set_badblocks(rdev, sh->sector,
3683 STRIPE_SECTORS, 0))
3684 md_error(conf->mddev, rdev);
3685 rdev_dec_pending(rdev, conf->mddev);
3686 }
NeilBrownb84db562011-07-28 11:39:23 +10003687 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3688 rdev = conf->disks[i].rdev;
3689 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003690 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10003691 rdev_dec_pending(rdev, conf->mddev);
3692 }
NeilBrown977df362011-12-23 10:17:53 +11003693 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3694 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11003695 if (!rdev)
3696 /* rdev have been moved down */
3697 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11003698 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003699 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11003700 rdev_dec_pending(rdev, conf->mddev);
3701 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003702 }
3703
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003704 if (s.ops_request)
3705 raid_run_ops(sh, s.ops_request);
3706
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003707 ops_run_io(sh, &s);
3708
NeilBrownc5709ef2011-07-26 11:35:20 +10003709 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003710 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003711 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003712 * have actually been submitted.
3713 */
3714 atomic_dec(&conf->preread_active_stripes);
3715 if (atomic_read(&conf->preread_active_stripes) <
3716 IO_THRESHOLD)
3717 md_wakeup_thread(conf->mddev->thread);
3718 }
3719
NeilBrownc5709ef2011-07-26 11:35:20 +10003720 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003721
Dan Williams257a4b42011-11-08 16:22:06 +11003722 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003723}
3724
NeilBrownd1688a62011-10-11 16:49:52 +11003725static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726{
3727 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3728 while (!list_empty(&conf->delayed_list)) {
3729 struct list_head *l = conf->delayed_list.next;
3730 struct stripe_head *sh;
3731 sh = list_entry(l, struct stripe_head, lru);
3732 list_del_init(l);
3733 clear_bit(STRIPE_DELAYED, &sh->state);
3734 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3735 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003736 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 }
NeilBrown482c0832011-04-18 18:25:42 +10003738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739}
3740
NeilBrownd1688a62011-10-11 16:49:52 +11003741static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003742{
3743 /* device_lock is held */
3744 struct list_head head;
3745 list_add(&head, &conf->bitmap_list);
3746 list_del_init(&conf->bitmap_list);
3747 while (!list_empty(&head)) {
3748 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3749 list_del_init(&sh->lru);
3750 atomic_inc(&sh->count);
3751 __release_stripe(conf, sh);
3752 }
3753}
3754
NeilBrownfd01b882011-10-11 16:47:53 +11003755int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003756{
NeilBrownd1688a62011-10-11 16:49:52 +11003757 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003758
3759 /* No difference between reads and writes. Just check
3760 * how busy the stripe_cache is
3761 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003762
NeilBrownf022b2f2006-10-03 01:15:56 -07003763 if (conf->inactive_blocked)
3764 return 1;
3765 if (conf->quiesce)
3766 return 1;
3767 if (list_empty_careful(&conf->inactive_list))
3768 return 1;
3769
3770 return 0;
3771}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003772EXPORT_SYMBOL_GPL(md_raid5_congested);
3773
3774static int raid5_congested(void *data, int bits)
3775{
NeilBrownfd01b882011-10-11 16:47:53 +11003776 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003777
3778 return mddev_congested(mddev, bits) ||
3779 md_raid5_congested(mddev, bits);
3780}
NeilBrownf022b2f2006-10-03 01:15:56 -07003781
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003782/* We want read requests to align with chunks where possible,
3783 * but write requests don't need to.
3784 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003785static int raid5_mergeable_bvec(struct request_queue *q,
3786 struct bvec_merge_data *bvm,
3787 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003788{
NeilBrownfd01b882011-10-11 16:47:53 +11003789 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003790 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003791 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003792 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003793 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003794
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003795 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003796 return biovec->bv_len; /* always allow writes to be mergeable */
3797
Andre Noll664e7c42009-06-18 08:45:27 +10003798 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3799 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003800 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3801 if (max < 0) max = 0;
3802 if (max <= biovec->bv_len && bio_sectors == 0)
3803 return biovec->bv_len;
3804 else
3805 return max;
3806}
3807
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003808
NeilBrownfd01b882011-10-11 16:47:53 +11003809static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003810{
3811 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003812 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003813 unsigned int bio_sectors = bio->bi_size >> 9;
3814
Andre Noll664e7c42009-06-18 08:45:27 +10003815 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3816 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003817 return chunk_sectors >=
3818 ((sector & (chunk_sectors - 1)) + bio_sectors);
3819}
3820
3821/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003822 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3823 * later sampled by raid5d.
3824 */
NeilBrownd1688a62011-10-11 16:49:52 +11003825static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003826{
3827 unsigned long flags;
3828
3829 spin_lock_irqsave(&conf->device_lock, flags);
3830
3831 bi->bi_next = conf->retry_read_aligned_list;
3832 conf->retry_read_aligned_list = bi;
3833
3834 spin_unlock_irqrestore(&conf->device_lock, flags);
3835 md_wakeup_thread(conf->mddev->thread);
3836}
3837
3838
NeilBrownd1688a62011-10-11 16:49:52 +11003839static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003840{
3841 struct bio *bi;
3842
3843 bi = conf->retry_read_aligned;
3844 if (bi) {
3845 conf->retry_read_aligned = NULL;
3846 return bi;
3847 }
3848 bi = conf->retry_read_aligned_list;
3849 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003850 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003851 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003852 /*
3853 * this sets the active strip count to 1 and the processed
3854 * strip count to zero (upper 8 bits)
3855 */
Shaohua Lie7836bd62012-07-19 16:01:31 +10003856 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003857 }
3858
3859 return bi;
3860}
3861
3862
3863/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003864 * The "raid5_align_endio" should check if the read succeeded and if it
3865 * did, call bio_endio on the original bio (having bio_put the new bio
3866 * first).
3867 * If the read failed..
3868 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003869static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003870{
3871 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003872 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003873 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003874 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003875 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003876
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003877 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003878
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003879 rdev = (void*)raid_bi->bi_next;
3880 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003881 mddev = rdev->mddev;
3882 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003883
3884 rdev_dec_pending(rdev, conf->mddev);
3885
3886 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003887 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003888 if (atomic_dec_and_test(&conf->active_aligned_reads))
3889 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003890 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003891 }
3892
3893
Dan Williams45b42332007-07-09 11:56:43 -07003894 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003895
3896 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003897}
3898
Neil Brown387bb172007-02-08 14:20:29 -08003899static int bio_fits_rdev(struct bio *bi)
3900{
Jens Axboe165125e2007-07-24 09:28:11 +02003901 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003902
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003903 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003904 return 0;
3905 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003906 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003907 return 0;
3908
3909 if (q->merge_bvec_fn)
3910 /* it's too hard to apply the merge_bvec_fn at this stage,
3911 * just just give up
3912 */
3913 return 0;
3914
3915 return 1;
3916}
3917
3918
NeilBrownfd01b882011-10-11 16:47:53 +11003919static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003920{
NeilBrownd1688a62011-10-11 16:49:52 +11003921 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11003922 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003923 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11003924 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11003925 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003926
3927 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003928 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003929 return 0;
3930 }
3931 /*
NeilBrowna167f662010-10-26 18:31:13 +11003932 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003933 */
NeilBrowna167f662010-10-26 18:31:13 +11003934 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003935 if (!align_bi)
3936 return 0;
3937 /*
3938 * set bi_end_io to a new function, and set bi_private to the
3939 * original bio.
3940 */
3941 align_bi->bi_end_io = raid5_align_endio;
3942 align_bi->bi_private = raid_bio;
3943 /*
3944 * compute position
3945 */
NeilBrown112bf892009-03-31 14:39:38 +11003946 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3947 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003948 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003949
NeilBrown671488c2011-12-23 10:17:52 +11003950 end_sector = align_bi->bi_sector + (align_bi->bi_size >> 9);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003951 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11003952 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
3953 if (!rdev || test_bit(Faulty, &rdev->flags) ||
3954 rdev->recovery_offset < end_sector) {
3955 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3956 if (rdev &&
3957 (test_bit(Faulty, &rdev->flags) ||
3958 !(test_bit(In_sync, &rdev->flags) ||
3959 rdev->recovery_offset >= end_sector)))
3960 rdev = NULL;
3961 }
3962 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10003963 sector_t first_bad;
3964 int bad_sectors;
3965
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003966 atomic_inc(&rdev->nr_pending);
3967 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003968 raid_bio->bi_next = (void*)rdev;
3969 align_bi->bi_bdev = rdev->bdev;
3970 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003971
NeilBrown31c176e2011-07-28 11:39:22 +10003972 if (!bio_fits_rdev(align_bi) ||
3973 is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
3974 &first_bad, &bad_sectors)) {
3975 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08003976 bio_put(align_bi);
3977 rdev_dec_pending(rdev, mddev);
3978 return 0;
3979 }
3980
majianpeng6c0544e2012-06-12 08:31:10 +08003981 /* No reshape active, so we can trust rdev->data_offset */
3982 align_bi->bi_sector += rdev->data_offset;
3983
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003984 spin_lock_irq(&conf->device_lock);
3985 wait_event_lock_irq(conf->wait_for_stripe,
3986 conf->quiesce == 0,
3987 conf->device_lock, /* nothing */);
3988 atomic_inc(&conf->active_aligned_reads);
3989 spin_unlock_irq(&conf->device_lock);
3990
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003991 generic_make_request(align_bi);
3992 return 1;
3993 } else {
3994 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003995 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003996 return 0;
3997 }
3998}
3999
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004000/* __get_priority_stripe - get the next stripe to process
4001 *
4002 * Full stripe writes are allowed to pass preread active stripes up until
4003 * the bypass_threshold is exceeded. In general the bypass_count
4004 * increments when the handle_list is handled before the hold_list; however, it
4005 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
4006 * stripe with in flight i/o. The bypass_count will be reset when the
4007 * head of the hold_list has changed, i.e. the head was promoted to the
4008 * handle_list.
4009 */
NeilBrownd1688a62011-10-11 16:49:52 +11004010static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004011{
4012 struct stripe_head *sh;
4013
4014 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
4015 __func__,
4016 list_empty(&conf->handle_list) ? "empty" : "busy",
4017 list_empty(&conf->hold_list) ? "empty" : "busy",
4018 atomic_read(&conf->pending_full_writes), conf->bypass_count);
4019
4020 if (!list_empty(&conf->handle_list)) {
4021 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
4022
4023 if (list_empty(&conf->hold_list))
4024 conf->bypass_count = 0;
4025 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
4026 if (conf->hold_list.next == conf->last_hold)
4027 conf->bypass_count++;
4028 else {
4029 conf->last_hold = conf->hold_list.next;
4030 conf->bypass_count -= conf->bypass_threshold;
4031 if (conf->bypass_count < 0)
4032 conf->bypass_count = 0;
4033 }
4034 }
4035 } else if (!list_empty(&conf->hold_list) &&
4036 ((conf->bypass_threshold &&
4037 conf->bypass_count > conf->bypass_threshold) ||
4038 atomic_read(&conf->pending_full_writes) == 0)) {
4039 sh = list_entry(conf->hold_list.next,
4040 typeof(*sh), lru);
4041 conf->bypass_count -= conf->bypass_threshold;
4042 if (conf->bypass_count < 0)
4043 conf->bypass_count = 0;
4044 } else
4045 return NULL;
4046
4047 list_del_init(&sh->lru);
4048 atomic_inc(&sh->count);
4049 BUG_ON(atomic_read(&sh->count) != 1);
4050 return sh;
4051}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004052
Shaohua Li8811b592012-08-02 08:33:00 +10004053struct raid5_plug_cb {
4054 struct blk_plug_cb cb;
4055 struct list_head list;
4056};
4057
4058static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
4059{
4060 struct raid5_plug_cb *cb = container_of(
4061 blk_cb, struct raid5_plug_cb, cb);
4062 struct stripe_head *sh;
4063 struct mddev *mddev = cb->cb.data;
4064 struct r5conf *conf = mddev->private;
4065
4066 if (cb->list.next && !list_empty(&cb->list)) {
4067 spin_lock_irq(&conf->device_lock);
4068 while (!list_empty(&cb->list)) {
4069 sh = list_first_entry(&cb->list, struct stripe_head, lru);
4070 list_del_init(&sh->lru);
4071 /*
4072 * avoid race release_stripe_plug() sees
4073 * STRIPE_ON_UNPLUG_LIST clear but the stripe
4074 * is still in our list
4075 */
4076 smp_mb__before_clear_bit();
4077 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
4078 __release_stripe(conf, sh);
4079 }
4080 spin_unlock_irq(&conf->device_lock);
4081 }
4082 kfree(cb);
4083}
4084
4085static void release_stripe_plug(struct mddev *mddev,
4086 struct stripe_head *sh)
4087{
4088 struct blk_plug_cb *blk_cb = blk_check_plugged(
4089 raid5_unplug, mddev,
4090 sizeof(struct raid5_plug_cb));
4091 struct raid5_plug_cb *cb;
4092
4093 if (!blk_cb) {
4094 release_stripe(sh);
4095 return;
4096 }
4097
4098 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
4099
4100 if (cb->list.next == NULL)
4101 INIT_LIST_HEAD(&cb->list);
4102
4103 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
4104 list_add_tail(&sh->lru, &cb->list);
4105 else
4106 release_stripe(sh);
4107}
4108
Shaohua Li620125f2012-10-11 13:49:05 +11004109static void make_discard_request(struct mddev *mddev, struct bio *bi)
4110{
4111 struct r5conf *conf = mddev->private;
4112 sector_t logical_sector, last_sector;
4113 struct stripe_head *sh;
4114 int remaining;
4115 int stripe_sectors;
4116
4117 if (mddev->reshape_position != MaxSector)
4118 /* Skip discard while reshape is happening */
4119 return;
4120
4121 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4122 last_sector = bi->bi_sector + (bi->bi_size>>9);
4123
4124 bi->bi_next = NULL;
4125 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
4126
4127 stripe_sectors = conf->chunk_sectors *
4128 (conf->raid_disks - conf->max_degraded);
4129 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
4130 stripe_sectors);
4131 sector_div(last_sector, stripe_sectors);
4132
4133 logical_sector *= conf->chunk_sectors;
4134 last_sector *= conf->chunk_sectors;
4135
4136 for (; logical_sector < last_sector;
4137 logical_sector += STRIPE_SECTORS) {
4138 DEFINE_WAIT(w);
4139 int d;
4140 again:
4141 sh = get_active_stripe(conf, logical_sector, 0, 0, 0);
4142 prepare_to_wait(&conf->wait_for_overlap, &w,
4143 TASK_UNINTERRUPTIBLE);
4144 spin_lock_irq(&sh->stripe_lock);
4145 for (d = 0; d < conf->raid_disks; d++) {
4146 if (d == sh->pd_idx || d == sh->qd_idx)
4147 continue;
4148 if (sh->dev[d].towrite || sh->dev[d].toread) {
4149 set_bit(R5_Overlap, &sh->dev[d].flags);
4150 spin_unlock_irq(&sh->stripe_lock);
4151 release_stripe(sh);
4152 schedule();
4153 goto again;
4154 }
4155 }
4156 finish_wait(&conf->wait_for_overlap, &w);
4157 for (d = 0; d < conf->raid_disks; d++) {
4158 if (d == sh->pd_idx || d == sh->qd_idx)
4159 continue;
4160 sh->dev[d].towrite = bi;
4161 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
4162 raid5_inc_bi_active_stripes(bi);
4163 }
4164 spin_unlock_irq(&sh->stripe_lock);
4165 if (conf->mddev->bitmap) {
4166 for (d = 0;
4167 d < conf->raid_disks - conf->max_degraded;
4168 d++)
4169 bitmap_startwrite(mddev->bitmap,
4170 sh->sector,
4171 STRIPE_SECTORS,
4172 0);
4173 sh->bm_seq = conf->seq_flush + 1;
4174 set_bit(STRIPE_BIT_DELAY, &sh->state);
4175 }
4176
4177 set_bit(STRIPE_HANDLE, &sh->state);
4178 clear_bit(STRIPE_DELAYED, &sh->state);
4179 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4180 atomic_inc(&conf->preread_active_stripes);
4181 release_stripe_plug(mddev, sh);
4182 }
4183
4184 remaining = raid5_dec_bi_active_stripes(bi);
4185 if (remaining == 0) {
4186 md_write_end(mddev);
4187 bio_endio(bi, 0);
4188 }
4189}
4190
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07004191static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192{
NeilBrownd1688a62011-10-11 16:49:52 +11004193 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11004194 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 sector_t new_sector;
4196 sector_t logical_sector, last_sector;
4197 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01004198 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11004199 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200
Tejun Heoe9c74692010-09-03 11:56:18 +02004201 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
4202 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004203 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07004204 }
4205
NeilBrown3d310eb2005-06-21 17:17:26 -07004206 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07004207
NeilBrown802ba062006-12-13 00:34:13 -08004208 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004209 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11004210 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004211 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004212
Shaohua Li620125f2012-10-11 13:49:05 +11004213 if (unlikely(bi->bi_rw & REQ_DISCARD)) {
4214 make_discard_request(mddev, bi);
4215 return;
4216 }
4217
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4219 last_sector = bi->bi_sector + (bi->bi_size>>9);
4220 bi->bi_next = NULL;
4221 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07004222
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
4224 DEFINE_WAIT(w);
NeilBrownb5663ba2009-03-31 14:39:38 +11004225 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08004226
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004227 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11004228 previous = 0;
NeilBrownb578d552006-03-27 01:18:12 -08004229 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004230 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11004231 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f762006-03-27 01:18:15 -08004232 * 64bit on a 32bit platform, and so it might be
4233 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02004234 * Of course reshape_progress could change after
NeilBrowndf8e7f762006-03-27 01:18:15 -08004235 * the lock is dropped, so once we get a reference
4236 * to the stripe that we think it is, we will have
4237 * to check again.
4238 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004239 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004240 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004241 ? logical_sector < conf->reshape_progress
4242 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004243 previous = 1;
4244 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10004245 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004246 ? logical_sector < conf->reshape_safe
4247 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08004248 spin_unlock_irq(&conf->device_lock);
4249 schedule();
4250 goto retry;
4251 }
4252 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004253 spin_unlock_irq(&conf->device_lock);
4254 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004255
NeilBrown112bf892009-03-31 14:39:38 +11004256 new_sector = raid5_compute_sector(conf, logical_sector,
4257 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11004258 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10004259 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 (unsigned long long)new_sector,
4261 (unsigned long long)logical_sector);
4262
NeilBrownb5663ba2009-03-31 14:39:38 +11004263 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10004264 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004266 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004267 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f762006-03-27 01:18:15 -08004268 * stripe, so we must do the range check again.
4269 * Expansion could still move past after this
4270 * test, but as we are holding a reference to
4271 * 'sh', we know that if that happens,
4272 * STRIPE_EXPANDING will get set and the expansion
4273 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004274 */
4275 int must_retry = 0;
4276 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004277 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11004278 ? logical_sector >= conf->reshape_progress
4279 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004280 /* mismatch, need to try again */
4281 must_retry = 1;
4282 spin_unlock_irq(&conf->device_lock);
4283 if (must_retry) {
4284 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004285 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004286 goto retry;
4287 }
4288 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004289
Namhyung Kimffd96e32011-07-18 17:38:51 +10004290 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004291 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004292 logical_sector < mddev->suspend_hi) {
4293 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004294 /* As the suspend_* range is controlled by
4295 * userspace, we want an interruptible
4296 * wait.
4297 */
4298 flush_signals(current);
4299 prepare_to_wait(&conf->wait_for_overlap,
4300 &w, TASK_INTERRUPTIBLE);
4301 if (logical_sector >= mddev->suspend_lo &&
4302 logical_sector < mddev->suspend_hi)
4303 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004304 goto retry;
4305 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004306
4307 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004308 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004309 /* Stripe is busy expanding or
4310 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 * and wait a while
4312 */
NeilBrown482c0832011-04-18 18:25:42 +10004313 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 release_stripe(sh);
4315 schedule();
4316 goto retry;
4317 }
4318 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004319 set_bit(STRIPE_HANDLE, &sh->state);
4320 clear_bit(STRIPE_DELAYED, &sh->state);
majianpeng895e3c52012-07-31 10:05:44 +10004321 if ((bi->bi_rw & REQ_NOIDLE) &&
NeilBrown729a1862009-12-14 12:49:50 +11004322 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4323 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10004324 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 } else {
4326 /* cannot get stripe for read-ahead, just give-up */
4327 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4328 finish_wait(&conf->wait_for_overlap, &w);
4329 break;
4330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004332
Shaohua Lie7836bd62012-07-19 16:01:31 +10004333 remaining = raid5_dec_bi_active_stripes(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004334 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335
NeilBrown16a53ec2006-06-26 00:27:38 -07004336 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004338
Neil Brown0e13fe232008-06-28 08:31:20 +10004339 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341}
4342
NeilBrownfd01b882011-10-11 16:47:53 +11004343static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004344
NeilBrownfd01b882011-10-11 16:47:53 +11004345static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346{
NeilBrown52c03292006-06-26 00:27:43 -07004347 /* reshaping is quite different to recovery/resync so it is
4348 * handled quite separately ... here.
4349 *
4350 * On each call to sync_request, we gather one chunk worth of
4351 * destination stripes and flag them as expanding.
4352 * Then we find all the source stripes and request reads.
4353 * As the reads complete, handle_stripe will copy the data
4354 * into the destination stripe and release that stripe.
4355 */
NeilBrownd1688a62011-10-11 16:49:52 +11004356 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004358 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004359 int raid_disks = conf->previous_raid_disks;
4360 int data_disks = raid_disks - conf->max_degraded;
4361 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004362 int i;
4363 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004364 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004365 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004366 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004367 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004368
NeilBrownfef9c612009-03-31 15:16:46 +11004369 if (sector_nr == 0) {
4370 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10004371 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004372 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4373 sector_nr = raid5_size(mddev, 0, 0)
4374 - conf->reshape_progress;
NeilBrown2c810cd2012-05-21 09:27:00 +10004375 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004376 conf->reshape_progress > 0)
4377 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004378 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004379 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004380 mddev->curr_resync_completed = sector_nr;
4381 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004382 *skipped = 1;
4383 return sector_nr;
4384 }
NeilBrown52c03292006-06-26 00:27:43 -07004385 }
4386
NeilBrown7a661382009-03-31 15:21:40 +11004387 /* We need to process a full chunk at a time.
4388 * If old and new chunk sizes differ, we need to process the
4389 * largest of these
4390 */
Andre Noll664e7c42009-06-18 08:45:27 +10004391 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4392 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004393 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004394 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004395
NeilBrownb5254dd2012-05-21 09:27:01 +10004396 /* We update the metadata at least every 10 seconds, or when
4397 * the data about to be copied would over-write the source of
4398 * the data at the front of the range. i.e. one new_stripe
4399 * along from reshape_progress new_maps to after where
4400 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004401 */
NeilBrownfef9c612009-03-31 15:16:46 +11004402 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004403 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004404 readpos = conf->reshape_progress;
4405 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004406 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004407 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10004408 if (mddev->reshape_backwards) {
NeilBrowned37d832009-05-27 21:39:05 +10004409 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004410 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004411 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004412 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004413 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004414 readpos -= min_t(sector_t, reshape_sectors, readpos);
4415 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004416 }
NeilBrown52c03292006-06-26 00:27:43 -07004417
NeilBrownb5254dd2012-05-21 09:27:01 +10004418 /* Having calculated the 'writepos' possibly use it
4419 * to set 'stripe_addr' which is where we will write to.
4420 */
4421 if (mddev->reshape_backwards) {
4422 BUG_ON(conf->reshape_progress == 0);
4423 stripe_addr = writepos;
4424 BUG_ON((mddev->dev_sectors &
4425 ~((sector_t)reshape_sectors - 1))
4426 - reshape_sectors - stripe_addr
4427 != sector_nr);
4428 } else {
4429 BUG_ON(writepos != sector_nr + reshape_sectors);
4430 stripe_addr = sector_nr;
4431 }
4432
NeilBrownc8f517c2009-03-31 15:28:40 +11004433 /* 'writepos' is the most advanced device address we might write.
4434 * 'readpos' is the least advanced device address we might read.
4435 * 'safepos' is the least address recorded in the metadata as having
4436 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10004437 * If there is a min_offset_diff, these are adjusted either by
4438 * increasing the safepos/readpos if diff is negative, or
4439 * increasing writepos if diff is positive.
4440 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11004441 * ensure safety in the face of a crash - that must be done by userspace
4442 * making a backup of the data. So in that case there is no particular
4443 * rush to update metadata.
4444 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4445 * update the metadata to advance 'safepos' to match 'readpos' so that
4446 * we can be safe in the event of a crash.
4447 * So we insist on updating metadata if safepos is behind writepos and
4448 * readpos is beyond writepos.
4449 * In any case, update the metadata every 10 seconds.
4450 * Maybe that number should be configurable, but I'm not sure it is
4451 * worth it.... maybe it could be a multiple of safemode_delay???
4452 */
NeilBrownb5254dd2012-05-21 09:27:01 +10004453 if (conf->min_offset_diff < 0) {
4454 safepos += -conf->min_offset_diff;
4455 readpos += -conf->min_offset_diff;
4456 } else
4457 writepos += conf->min_offset_diff;
4458
NeilBrown2c810cd2012-05-21 09:27:00 +10004459 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11004460 ? (safepos > writepos && readpos < writepos)
4461 : (safepos < writepos && readpos > writepos)) ||
4462 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004463 /* Cannot proceed until we've updated the superblock... */
4464 wait_event(conf->wait_for_overlap,
4465 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004466 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004467 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004468 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b422006-10-03 01:15:46 -07004469 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004470 md_wakeup_thread(mddev->thread);
NeilBrown850b2b422006-10-03 01:15:46 -07004471 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004472 kthread_should_stop());
4473 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004474 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004475 spin_unlock_irq(&conf->device_lock);
4476 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004477 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004478 }
4479
NeilBrownab69ae12009-03-31 15:26:47 +11004480 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004481 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004482 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004483 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004484 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004485 set_bit(STRIPE_EXPANDING, &sh->state);
4486 atomic_inc(&conf->reshape_stripes);
4487 /* If any of this stripe is beyond the end of the old
4488 * array, then we need to zero those blocks
4489 */
4490 for (j=sh->disks; j--;) {
4491 sector_t s;
4492 if (j == sh->pd_idx)
4493 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004494 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004495 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004496 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004497 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004498 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004499 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004500 continue;
4501 }
4502 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4503 set_bit(R5_Expanded, &sh->dev[j].flags);
4504 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4505 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004506 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004507 set_bit(STRIPE_EXPAND_READY, &sh->state);
4508 set_bit(STRIPE_HANDLE, &sh->state);
4509 }
NeilBrownab69ae12009-03-31 15:26:47 +11004510 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004511 }
4512 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004513 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11004514 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004515 else
NeilBrown7a661382009-03-31 15:21:40 +11004516 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004517 spin_unlock_irq(&conf->device_lock);
4518 /* Ok, those stripe are ready. We can start scheduling
4519 * reads on the source stripes.
4520 * The source stripes are determined by mapping the first and last
4521 * block on the destination stripes.
4522 */
NeilBrown52c03292006-06-26 00:27:43 -07004523 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004524 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004525 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004526 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004527 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004528 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004529 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004530 if (last_sector >= mddev->dev_sectors)
4531 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004532 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004533 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004534 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4535 set_bit(STRIPE_HANDLE, &sh->state);
4536 release_stripe(sh);
4537 first_sector += STRIPE_SECTORS;
4538 }
NeilBrownab69ae12009-03-31 15:26:47 +11004539 /* Now that the sources are clearly marked, we can release
4540 * the destination stripes
4541 */
4542 while (!list_empty(&stripes)) {
4543 sh = list_entry(stripes.next, struct stripe_head, lru);
4544 list_del_init(&sh->lru);
4545 release_stripe(sh);
4546 }
NeilBrownc6207272008-02-06 01:39:52 -08004547 /* If this takes us to the resync_max point where we have to pause,
4548 * then we need to write out the superblock.
4549 */
NeilBrown7a661382009-03-31 15:21:40 +11004550 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004551 if ((sector_nr - mddev->curr_resync_completed) * 2
4552 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004553 /* Cannot proceed until we've updated the superblock... */
4554 wait_event(conf->wait_for_overlap,
4555 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004556 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004557 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004558 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004559 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4560 md_wakeup_thread(mddev->thread);
4561 wait_event(mddev->sb_wait,
4562 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4563 || kthread_should_stop());
4564 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004565 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004566 spin_unlock_irq(&conf->device_lock);
4567 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004568 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004569 }
NeilBrown7a661382009-03-31 15:21:40 +11004570 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004571}
4572
4573/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004574static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004575{
NeilBrownd1688a62011-10-11 16:49:52 +11004576 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004577 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004578 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004579 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004580 int still_degraded = 0;
4581 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582
NeilBrown72626682005-09-09 16:23:54 -07004583 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004585
NeilBrown29269552006-03-27 01:18:10 -08004586 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4587 end_reshape(conf);
4588 return 0;
4589 }
NeilBrown72626682005-09-09 16:23:54 -07004590
4591 if (mddev->curr_resync < max_sector) /* aborted */
4592 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4593 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004594 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004595 conf->fullsync = 0;
4596 bitmap_close_sync(mddev->bitmap);
4597
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 return 0;
4599 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004600
NeilBrown64bd6602009-08-03 10:59:58 +10004601 /* Allow raid5_quiesce to complete */
4602 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4603
NeilBrown52c03292006-06-26 00:27:43 -07004604 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4605 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004606
NeilBrownc6207272008-02-06 01:39:52 -08004607 /* No need to check resync_max as we never do more than one
4608 * stripe, and as resync_max will always be on a chunk boundary,
4609 * if the check in md_do_sync didn't fire, there is no chance
4610 * of overstepping resync_max here
4611 */
4612
NeilBrown16a53ec2006-06-26 00:27:38 -07004613 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 * to resync, then assert that we are finished, because there is
4615 * nothing we can do.
4616 */
NeilBrown3285edf2006-06-26 00:27:55 -07004617 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004618 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004619 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004620 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 return rv;
4622 }
NeilBrown72626682005-09-09 16:23:54 -07004623 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004624 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004625 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4626 /* we can skip this block, and probably more */
4627 sync_blocks /= STRIPE_SECTORS;
4628 *skipped = 1;
4629 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631
NeilBrownb47490c2008-02-06 01:39:50 -08004632 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4633
NeilBrowna8c906c2009-06-09 14:39:59 +10004634 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004636 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004638 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004640 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004642 /* Need to check if array will still be degraded after recovery/resync
4643 * We don't need to check the 'failed' flag as when that gets set,
4644 * recovery aborts.
4645 */
NeilBrownf001a702009-06-09 14:30:31 +10004646 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004647 if (conf->disks[i].rdev == NULL)
4648 still_degraded = 1;
4649
4650 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4651
NeilBrown83206d62011-07-26 11:19:49 +10004652 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653
NeilBrown14425772009-10-16 15:55:25 +11004654 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 release_stripe(sh);
4656
4657 return STRIPE_SECTORS;
4658}
4659
NeilBrownd1688a62011-10-11 16:49:52 +11004660static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004661{
4662 /* We may not be able to submit a whole bio at once as there
4663 * may not be enough stripe_heads available.
4664 * We cannot pre-allocate enough stripe_heads as we may need
4665 * more than exist in the cache (if we allow ever large chunks).
4666 * So we do one stripe head at a time and record in
4667 * ->bi_hw_segments how many have been done.
4668 *
4669 * We *know* that this entire raid_bio is in one chunk, so
4670 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4671 */
4672 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004673 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004674 sector_t sector, logical_sector, last_sector;
4675 int scnt = 0;
4676 int remaining;
4677 int handled = 0;
4678
4679 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004680 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004681 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004682 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4683
4684 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004685 logical_sector += STRIPE_SECTORS,
4686 sector += STRIPE_SECTORS,
4687 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004688
Shaohua Lie7836bd62012-07-19 16:01:31 +10004689 if (scnt < raid5_bi_processed_stripes(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004690 /* already done this stripe */
4691 continue;
4692
NeilBrowna8c906c2009-06-09 14:39:59 +10004693 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004694
4695 if (!sh) {
4696 /* failed to get a stripe - must wait */
Shaohua Lie7836bd62012-07-19 16:01:31 +10004697 raid5_set_bi_processed_stripes(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004698 conf->retry_read_aligned = raid_bio;
4699 return handled;
4700 }
4701
Neil Brown387bb172007-02-08 14:20:29 -08004702 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4703 release_stripe(sh);
Shaohua Lie7836bd62012-07-19 16:01:31 +10004704 raid5_set_bi_processed_stripes(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004705 conf->retry_read_aligned = raid_bio;
4706 return handled;
4707 }
4708
majianpeng3f9e7c12012-07-31 10:04:21 +10004709 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07004710 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004711 release_stripe(sh);
4712 handled++;
4713 }
Shaohua Lie7836bd62012-07-19 16:01:31 +10004714 remaining = raid5_dec_bi_active_stripes(raid_bio);
Neil Brown0e13fe232008-06-28 08:31:20 +10004715 if (remaining == 0)
4716 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004717 if (atomic_dec_and_test(&conf->active_aligned_reads))
4718 wake_up(&conf->wait_for_stripe);
4719 return handled;
4720}
4721
Shaohua Li46a06402012-08-02 08:33:15 +10004722#define MAX_STRIPE_BATCH 8
4723static int handle_active_stripes(struct r5conf *conf)
4724{
4725 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
4726 int i, batch_size = 0;
4727
4728 while (batch_size < MAX_STRIPE_BATCH &&
4729 (sh = __get_priority_stripe(conf)) != NULL)
4730 batch[batch_size++] = sh;
4731
4732 if (batch_size == 0)
4733 return batch_size;
4734 spin_unlock_irq(&conf->device_lock);
4735
4736 for (i = 0; i < batch_size; i++)
4737 handle_stripe(batch[i]);
4738
4739 cond_resched();
4740
4741 spin_lock_irq(&conf->device_lock);
4742 for (i = 0; i < batch_size; i++)
4743 __release_stripe(conf, batch[i]);
4744 return batch_size;
4745}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004746
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747/*
4748 * This is our raid5 kernel thread.
4749 *
4750 * We scan the hash table for stripes which can be handled now.
4751 * During the scan, completed stripes are saved for us by the interrupt
4752 * handler, so that they will not have to wait for our next wakeup.
4753 */
Shaohua Li4ed87312012-10-11 13:34:00 +11004754static void raid5d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755{
Shaohua Li4ed87312012-10-11 13:34:00 +11004756 struct mddev *mddev = thread->mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11004757 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004759 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760
Dan Williams45b42332007-07-09 11:56:43 -07004761 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762
4763 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004765 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 handled = 0;
4767 spin_lock_irq(&conf->device_lock);
4768 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004769 struct bio *bio;
Shaohua Li46a06402012-08-02 08:33:15 +10004770 int batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771
NeilBrown0021b7b2012-07-31 09:08:14 +02004772 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10004773 !list_empty(&conf->bitmap_list)) {
4774 /* Now is a good time to flush some bitmap updates */
4775 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004776 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004777 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004778 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004779 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004780 activate_bit_delay(conf);
4781 }
NeilBrown0021b7b2012-07-31 09:08:14 +02004782 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004783
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004784 while ((bio = remove_bio_from_retry(conf))) {
4785 int ok;
4786 spin_unlock_irq(&conf->device_lock);
4787 ok = retry_aligned_read(conf, bio);
4788 spin_lock_irq(&conf->device_lock);
4789 if (!ok)
4790 break;
4791 handled++;
4792 }
4793
Shaohua Li46a06402012-08-02 08:33:15 +10004794 batch_size = handle_active_stripes(conf);
4795 if (!batch_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 break;
Shaohua Li46a06402012-08-02 08:33:15 +10004797 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798
Shaohua Li46a06402012-08-02 08:33:15 +10004799 if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) {
4800 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10004801 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10004802 spin_lock_irq(&conf->device_lock);
4803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 }
Dan Williams45b42332007-07-09 11:56:43 -07004805 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806
4807 spin_unlock_irq(&conf->device_lock);
4808
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004809 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004810 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811
Dan Williams45b42332007-07-09 11:56:43 -07004812 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813}
4814
NeilBrown3f294f42005-11-08 21:39:25 -08004815static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004816raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004817{
NeilBrownd1688a62011-10-11 16:49:52 +11004818 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004819 if (conf)
4820 return sprintf(page, "%d\n", conf->max_nr_stripes);
4821 else
4822 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004823}
4824
NeilBrownc41d4ac2010-06-01 19:37:24 +10004825int
NeilBrownfd01b882011-10-11 16:47:53 +11004826raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004827{
NeilBrownd1688a62011-10-11 16:49:52 +11004828 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004829 int err;
4830
4831 if (size <= 16 || size > 32768)
4832 return -EINVAL;
4833 while (size < conf->max_nr_stripes) {
4834 if (drop_one_stripe(conf))
4835 conf->max_nr_stripes--;
4836 else
4837 break;
4838 }
4839 err = md_allow_write(mddev);
4840 if (err)
4841 return err;
4842 while (size > conf->max_nr_stripes) {
4843 if (grow_one_stripe(conf))
4844 conf->max_nr_stripes++;
4845 else break;
4846 }
4847 return 0;
4848}
4849EXPORT_SYMBOL(raid5_set_cache_size);
4850
NeilBrown3f294f42005-11-08 21:39:25 -08004851static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004852raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004853{
NeilBrownd1688a62011-10-11 16:49:52 +11004854 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004855 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004856 int err;
4857
NeilBrown3f294f42005-11-08 21:39:25 -08004858 if (len >= PAGE_SIZE)
4859 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004860 if (!conf)
4861 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004862
Dan Williams4ef197d82008-04-28 02:15:54 -07004863 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004864 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004865 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004866 if (err)
4867 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004868 return len;
4869}
NeilBrown007583c2005-11-08 21:39:30 -08004870
NeilBrown96de1e62005-11-08 21:39:39 -08004871static struct md_sysfs_entry
4872raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4873 raid5_show_stripe_cache_size,
4874 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004875
4876static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004877raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004878{
NeilBrownd1688a62011-10-11 16:49:52 +11004879 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004880 if (conf)
4881 return sprintf(page, "%d\n", conf->bypass_threshold);
4882 else
4883 return 0;
4884}
4885
4886static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004887raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004888{
NeilBrownd1688a62011-10-11 16:49:52 +11004889 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004890 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004891 if (len >= PAGE_SIZE)
4892 return -EINVAL;
4893 if (!conf)
4894 return -ENODEV;
4895
Dan Williams4ef197d82008-04-28 02:15:54 -07004896 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004897 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004898 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004899 return -EINVAL;
4900 conf->bypass_threshold = new;
4901 return len;
4902}
4903
4904static struct md_sysfs_entry
4905raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4906 S_IRUGO | S_IWUSR,
4907 raid5_show_preread_threshold,
4908 raid5_store_preread_threshold);
4909
4910static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004911stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004912{
NeilBrownd1688a62011-10-11 16:49:52 +11004913 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004914 if (conf)
4915 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4916 else
4917 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004918}
4919
NeilBrown96de1e62005-11-08 21:39:39 -08004920static struct md_sysfs_entry
4921raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004922
NeilBrown007583c2005-11-08 21:39:30 -08004923static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004924 &raid5_stripecache_size.attr,
4925 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004926 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004927 NULL,
4928};
NeilBrown007583c2005-11-08 21:39:30 -08004929static struct attribute_group raid5_attrs_group = {
4930 .name = NULL,
4931 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004932};
4933
Dan Williams80c3a6c2009-03-17 18:10:40 -07004934static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11004935raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07004936{
NeilBrownd1688a62011-10-11 16:49:52 +11004937 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004938
4939 if (!sectors)
4940 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004941 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11004942 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11004943 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004944
Andre Noll9d8f0362009-06-18 08:45:01 +10004945 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004946 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004947 return sectors * (raid_disks - conf->max_degraded);
4948}
4949
NeilBrownd1688a62011-10-11 16:49:52 +11004950static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004951{
4952 struct raid5_percpu *percpu;
4953 unsigned long cpu;
4954
4955 if (!conf->percpu)
4956 return;
4957
4958 get_online_cpus();
4959 for_each_possible_cpu(cpu) {
4960 percpu = per_cpu_ptr(conf->percpu, cpu);
4961 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004962 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004963 }
4964#ifdef CONFIG_HOTPLUG_CPU
4965 unregister_cpu_notifier(&conf->cpu_notify);
4966#endif
4967 put_online_cpus();
4968
4969 free_percpu(conf->percpu);
4970}
4971
NeilBrownd1688a62011-10-11 16:49:52 +11004972static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10004973{
4974 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004975 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10004976 kfree(conf->disks);
4977 kfree(conf->stripe_hashtbl);
4978 kfree(conf);
4979}
4980
Dan Williams36d1c642009-07-14 11:48:22 -07004981#ifdef CONFIG_HOTPLUG_CPU
4982static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4983 void *hcpu)
4984{
NeilBrownd1688a62011-10-11 16:49:52 +11004985 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07004986 long cpu = (long)hcpu;
4987 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4988
4989 switch (action) {
4990 case CPU_UP_PREPARE:
4991 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07004992 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07004993 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004994 if (!percpu->scribble)
4995 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4996
4997 if (!percpu->scribble ||
4998 (conf->level == 6 && !percpu->spare_page)) {
4999 safe_put_page(percpu->spare_page);
5000 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005001 pr_err("%s: failed memory allocation for cpu%ld\n",
5002 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07005003 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07005004 }
5005 break;
5006 case CPU_DEAD:
5007 case CPU_DEAD_FROZEN:
5008 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005009 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005010 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07005011 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07005012 break;
5013 default:
5014 break;
5015 }
5016 return NOTIFY_OK;
5017}
5018#endif
5019
NeilBrownd1688a62011-10-11 16:49:52 +11005020static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07005021{
5022 unsigned long cpu;
5023 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09005024 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07005025 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07005026 int err;
5027
Dan Williams36d1c642009-07-14 11:48:22 -07005028 allcpus = alloc_percpu(struct raid5_percpu);
5029 if (!allcpus)
5030 return -ENOMEM;
5031 conf->percpu = allcpus;
5032
5033 get_online_cpus();
5034 err = 0;
5035 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07005036 if (conf->level == 6) {
5037 spare_page = alloc_page(GFP_KERNEL);
5038 if (!spare_page) {
5039 err = -ENOMEM;
5040 break;
5041 }
5042 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
5043 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11005044 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005045 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07005046 err = -ENOMEM;
5047 break;
5048 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07005049 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07005050 }
5051#ifdef CONFIG_HOTPLUG_CPU
5052 conf->cpu_notify.notifier_call = raid456_cpu_notify;
5053 conf->cpu_notify.priority = 0;
5054 if (err == 0)
5055 err = register_cpu_notifier(&conf->cpu_notify);
5056#endif
5057 put_online_cpus();
5058
5059 return err;
5060}
5061
NeilBrownd1688a62011-10-11 16:49:52 +11005062static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063{
NeilBrownd1688a62011-10-11 16:49:52 +11005064 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005065 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11005066 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10005068 char pers_name[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069
NeilBrown91adb562009-03-31 14:39:39 +11005070 if (mddev->new_level != 5
5071 && mddev->new_level != 4
5072 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10005073 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005074 mdname(mddev), mddev->new_level);
5075 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 }
NeilBrown91adb562009-03-31 14:39:39 +11005077 if ((mddev->new_level == 5
5078 && !algorithm_valid_raid5(mddev->new_layout)) ||
5079 (mddev->new_level == 6
5080 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005081 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11005082 mdname(mddev), mddev->new_layout);
5083 return ERR_PTR(-EIO);
5084 }
5085 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10005086 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005087 mdname(mddev), mddev->raid_disks);
5088 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11005089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090
Andre Noll664e7c42009-06-18 08:45:27 +10005091 if (!mddev->new_chunk_sectors ||
5092 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
5093 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005094 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
5095 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11005096 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11005097 }
5098
NeilBrownd1688a62011-10-11 16:49:52 +11005099 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11005100 if (conf == NULL)
5101 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11005102 spin_lock_init(&conf->device_lock);
5103 init_waitqueue_head(&conf->wait_for_stripe);
5104 init_waitqueue_head(&conf->wait_for_overlap);
5105 INIT_LIST_HEAD(&conf->handle_list);
5106 INIT_LIST_HEAD(&conf->hold_list);
5107 INIT_LIST_HEAD(&conf->delayed_list);
5108 INIT_LIST_HEAD(&conf->bitmap_list);
5109 INIT_LIST_HEAD(&conf->inactive_list);
5110 atomic_set(&conf->active_stripes, 0);
5111 atomic_set(&conf->preread_active_stripes, 0);
5112 atomic_set(&conf->active_aligned_reads, 0);
5113 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11005114 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11005115
5116 conf->raid_disks = mddev->raid_disks;
5117 if (mddev->reshape_position == MaxSector)
5118 conf->previous_raid_disks = mddev->raid_disks;
5119 else
5120 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005121 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
5122 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11005123
NeilBrown5e5e3e72009-10-16 16:35:30 +11005124 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11005125 GFP_KERNEL);
5126 if (!conf->disks)
5127 goto abort;
5128
5129 conf->mddev = mddev;
5130
5131 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
5132 goto abort;
5133
Dan Williams36d1c642009-07-14 11:48:22 -07005134 conf->level = mddev->new_level;
5135 if (raid5_alloc_percpu(conf) != 0)
5136 goto abort;
5137
NeilBrown0c55e022010-05-03 14:09:02 +10005138 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11005139
NeilBrowndafb20f2012-03-19 12:46:39 +11005140 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11005141 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005142 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11005143 || raid_disk < 0)
5144 continue;
5145 disk = conf->disks + raid_disk;
5146
NeilBrown17045f52011-12-23 10:17:53 +11005147 if (test_bit(Replacement, &rdev->flags)) {
5148 if (disk->replacement)
5149 goto abort;
5150 disk->replacement = rdev;
5151 } else {
5152 if (disk->rdev)
5153 goto abort;
5154 disk->rdev = rdev;
5155 }
NeilBrown91adb562009-03-31 14:39:39 +11005156
5157 if (test_bit(In_sync, &rdev->flags)) {
5158 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10005159 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
5160 " disk %d\n",
5161 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05005162 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11005163 /* Cannot rely on bitmap to complete recovery */
5164 conf->fullsync = 1;
5165 }
5166
Andre Noll09c9e5f2009-06-18 08:45:55 +10005167 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11005168 conf->level = mddev->new_level;
5169 if (conf->level == 6)
5170 conf->max_degraded = 2;
5171 else
5172 conf->max_degraded = 1;
5173 conf->algorithm = mddev->new_layout;
5174 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11005175 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11005176 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10005177 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11005178 conf->prev_algo = mddev->layout;
5179 }
NeilBrown91adb562009-03-31 14:39:39 +11005180
5181 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11005182 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11005183 if (grow_stripes(conf, conf->max_nr_stripes)) {
5184 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005185 "md/raid:%s: couldn't allocate %dkB for buffers\n",
5186 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005187 goto abort;
5188 } else
NeilBrown0c55e022010-05-03 14:09:02 +10005189 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
5190 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005191
NeilBrown02326052012-07-03 15:56:52 +10005192 sprintf(pers_name, "raid%d", mddev->new_level);
5193 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11005194 if (!conf->thread) {
5195 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005196 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11005197 mdname(mddev));
5198 goto abort;
5199 }
5200
5201 return conf;
5202
5203 abort:
5204 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10005205 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11005206 return ERR_PTR(-EIO);
5207 } else
5208 return ERR_PTR(-ENOMEM);
5209}
5210
NeilBrownc148ffd2009-11-13 17:47:00 +11005211
5212static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
5213{
5214 switch (algo) {
5215 case ALGORITHM_PARITY_0:
5216 if (raid_disk < max_degraded)
5217 return 1;
5218 break;
5219 case ALGORITHM_PARITY_N:
5220 if (raid_disk >= raid_disks - max_degraded)
5221 return 1;
5222 break;
5223 case ALGORITHM_PARITY_0_6:
5224 if (raid_disk == 0 ||
5225 raid_disk == raid_disks - 1)
5226 return 1;
5227 break;
5228 case ALGORITHM_LEFT_ASYMMETRIC_6:
5229 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5230 case ALGORITHM_LEFT_SYMMETRIC_6:
5231 case ALGORITHM_RIGHT_SYMMETRIC_6:
5232 if (raid_disk == raid_disks - 1)
5233 return 1;
5234 }
5235 return 0;
5236}
5237
NeilBrownfd01b882011-10-11 16:47:53 +11005238static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11005239{
NeilBrownd1688a62011-10-11 16:49:52 +11005240 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10005241 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11005242 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11005243 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11005244 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11005245 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10005246 long long min_offset_diff = 0;
5247 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11005248
Andre Noll8c6ac8682009-06-18 08:48:06 +10005249 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10005250 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac8682009-06-18 08:48:06 +10005251 " -- starting background reconstruction\n",
5252 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10005253
5254 rdev_for_each(rdev, mddev) {
5255 long long diff;
5256 if (rdev->raid_disk < 0)
5257 continue;
5258 diff = (rdev->new_data_offset - rdev->data_offset);
5259 if (first) {
5260 min_offset_diff = diff;
5261 first = 0;
5262 } else if (mddev->reshape_backwards &&
5263 diff < min_offset_diff)
5264 min_offset_diff = diff;
5265 else if (!mddev->reshape_backwards &&
5266 diff > min_offset_diff)
5267 min_offset_diff = diff;
5268 }
5269
NeilBrownf6705572006-03-27 01:18:11 -08005270 if (mddev->reshape_position != MaxSector) {
5271 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10005272 * Difficulties arise if the stripe we would write to
5273 * next is at or after the stripe we would read from next.
5274 * For a reshape that changes the number of devices, this
5275 * is only possible for a very short time, and mdadm makes
5276 * sure that time appears to have past before assembling
5277 * the array. So we fail if that time hasn't passed.
5278 * For a reshape that keeps the number of devices the same
5279 * mdadm must be monitoring the reshape can keeping the
5280 * critical areas read-only and backed up. It will start
5281 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08005282 */
5283 sector_t here_new, here_old;
5284 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11005285 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08005286
NeilBrown88ce4932009-03-31 15:24:23 +11005287 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10005288 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08005289 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08005290 mdname(mddev));
5291 return -EINVAL;
5292 }
NeilBrownf6705572006-03-27 01:18:11 -08005293 old_disks = mddev->raid_disks - mddev->delta_disks;
5294 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08005295 * further up in new geometry must map after here in old
5296 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08005297 */
5298 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10005299 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005300 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005301 printk(KERN_ERR "md/raid:%s: reshape_position not "
5302 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005303 return -EINVAL;
5304 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005305 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005306 /* here_new is the stripe we will write to */
5307 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10005308 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005309 (old_disks-max_degraded));
5310 /* here_old is the first stripe that we might need to read
5311 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10005312 if (mddev->delta_disks == 0) {
NeilBrownb5254dd2012-05-21 09:27:01 +10005313 if ((here_new * mddev->new_chunk_sectors !=
5314 here_old * mddev->chunk_sectors)) {
5315 printk(KERN_ERR "md/raid:%s: reshape position is"
5316 " confused - aborting\n", mdname(mddev));
5317 return -EINVAL;
5318 }
NeilBrown67ac6012009-08-13 10:06:24 +10005319 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10005320 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10005321 * and taking constant backups.
5322 * mdadm always starts a situation like this in
5323 * readonly mode so it can take control before
5324 * allowing any writes. So just check for that.
5325 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005326 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
5327 abs(min_offset_diff) >= mddev->new_chunk_sectors)
5328 /* not really in-place - so OK */;
5329 else if (mddev->ro == 0) {
5330 printk(KERN_ERR "md/raid:%s: in-place reshape "
5331 "must be started in read-only mode "
5332 "- aborting\n",
NeilBrown0c55e022010-05-03 14:09:02 +10005333 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005334 return -EINVAL;
5335 }
NeilBrown2c810cd2012-05-21 09:27:00 +10005336 } else if (mddev->reshape_backwards
NeilBrownb5254dd2012-05-21 09:27:01 +10005337 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
NeilBrown67ac6012009-08-13 10:06:24 +10005338 here_old * mddev->chunk_sectors)
5339 : (here_new * mddev->new_chunk_sectors >=
NeilBrownb5254dd2012-05-21 09:27:01 +10005340 here_old * mddev->chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08005341 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005342 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5343 "auto-recovery - aborting.\n",
5344 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005345 return -EINVAL;
5346 }
NeilBrown0c55e022010-05-03 14:09:02 +10005347 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5348 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005349 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005350 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005351 BUG_ON(mddev->level != mddev->new_level);
5352 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005353 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005354 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005355 }
5356
NeilBrown245f46c2009-03-31 14:39:39 +11005357 if (mddev->private == NULL)
5358 conf = setup_conf(mddev);
5359 else
5360 conf = mddev->private;
5361
NeilBrown91adb562009-03-31 14:39:39 +11005362 if (IS_ERR(conf))
5363 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005364
NeilBrownb5254dd2012-05-21 09:27:01 +10005365 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11005366 mddev->thread = conf->thread;
5367 conf->thread = NULL;
5368 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369
NeilBrown17045f52011-12-23 10:17:53 +11005370 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5371 i++) {
5372 rdev = conf->disks[i].rdev;
5373 if (!rdev && conf->disks[i].replacement) {
5374 /* The replacement is all we have yet */
5375 rdev = conf->disks[i].replacement;
5376 conf->disks[i].replacement = NULL;
5377 clear_bit(Replacement, &rdev->flags);
5378 conf->disks[i].rdev = rdev;
5379 }
5380 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11005381 continue;
NeilBrown17045f52011-12-23 10:17:53 +11005382 if (conf->disks[i].replacement &&
5383 conf->reshape_progress != MaxSector) {
5384 /* replacements and reshape simply do not mix. */
5385 printk(KERN_ERR "md: cannot handle concurrent "
5386 "replacement and reshape.\n");
5387 goto abort;
5388 }
NeilBrown2f115882010-06-17 17:41:03 +10005389 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005390 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005391 continue;
5392 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005393 /* This disc is not fully in-sync. However if it
5394 * just stored parity (beyond the recovery_offset),
5395 * when we don't need to be concerned about the
5396 * array being dirty.
5397 * When reshape goes 'backwards', we never have
5398 * partially completed devices, so we only need
5399 * to worry about reshape going forwards.
5400 */
5401 /* Hack because v0.91 doesn't store recovery_offset properly. */
5402 if (mddev->major_version == 0 &&
5403 mddev->minor_version > 90)
5404 rdev->recovery_offset = reshape_offset;
5405
NeilBrownc148ffd2009-11-13 17:47:00 +11005406 if (rdev->recovery_offset < reshape_offset) {
5407 /* We need to check old and new layout */
5408 if (!only_parity(rdev->raid_disk,
5409 conf->algorithm,
5410 conf->raid_disks,
5411 conf->max_degraded))
5412 continue;
5413 }
5414 if (!only_parity(rdev->raid_disk,
5415 conf->prev_algo,
5416 conf->previous_raid_disks,
5417 conf->max_degraded))
5418 continue;
5419 dirty_parity_disks++;
5420 }
NeilBrown91adb562009-03-31 14:39:39 +11005421
NeilBrown17045f52011-12-23 10:17:53 +11005422 /*
5423 * 0 for a fully functional array, 1 or 2 for a degraded array.
5424 */
NeilBrown908f4fb2011-12-23 10:17:50 +11005425 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426
NeilBrown674806d2010-06-16 17:17:53 +10005427 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005428 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07005430 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 goto abort;
5432 }
5433
NeilBrown91adb562009-03-31 14:39:39 +11005434 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10005435 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11005436 mddev->resync_max_sectors = mddev->dev_sectors;
5437
NeilBrownc148ffd2009-11-13 17:47:00 +11005438 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005440 if (mddev->ok_start_degraded)
5441 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10005442 "md/raid:%s: starting dirty degraded array"
5443 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005444 mdname(mddev));
5445 else {
5446 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005447 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005448 mdname(mddev));
5449 goto abort;
5450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 }
5452
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10005454 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5455 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11005456 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5457 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 else
NeilBrown0c55e022010-05-03 14:09:02 +10005459 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5460 " out of %d devices, algorithm %d\n",
5461 mdname(mddev), conf->level,
5462 mddev->raid_disks - mddev->degraded,
5463 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464
5465 print_raid5_conf(conf);
5466
NeilBrownfef9c612009-03-31 15:16:46 +11005467 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11005468 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005469 atomic_set(&conf->reshape_stripes, 0);
5470 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5471 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5472 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5473 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5474 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005475 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005476 }
5477
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478
5479 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005480 if (mddev->to_remove == &raid5_attrs_group)
5481 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005482 else if (mddev->kobj.sd &&
5483 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005484 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10005485 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08005486 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10005487 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5488
5489 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005490 int chunk_size;
Shaohua Li620125f2012-10-11 13:49:05 +11005491 bool discard_supported = true;
NeilBrown4a5add42010-06-01 19:37:28 +10005492 /* read-ahead size must cover two whole stripes, which
5493 * is 2 * (datadisks) * chunksize where 'n' is the
5494 * number of raid devices
5495 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5497 int stripe = data_disks *
5498 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5499 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5500 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10005501
5502 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005503
5504 mddev->queue->backing_dev_info.congested_data = mddev;
5505 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10005506
5507 chunk_size = mddev->chunk_sectors << 9;
5508 blk_queue_io_min(mddev->queue, chunk_size);
5509 blk_queue_io_opt(mddev->queue, chunk_size *
5510 (conf->raid_disks - conf->max_degraded));
Shaohua Li620125f2012-10-11 13:49:05 +11005511 /*
5512 * We can only discard a whole stripe. It doesn't make sense to
5513 * discard data disk but write parity disk
5514 */
5515 stripe = stripe * PAGE_SIZE;
5516 mddev->queue->limits.discard_alignment = stripe;
5517 mddev->queue->limits.discard_granularity = stripe;
5518 /*
5519 * unaligned part of discard request will be ignored, so can't
5520 * guarantee discard_zerors_data
5521 */
5522 mddev->queue->limits.discard_zeroes_data = 0;
NeilBrown9f7c2222010-07-26 12:04:13 +10005523
NeilBrown05616be2012-05-21 09:27:00 +10005524 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005525 disk_stack_limits(mddev->gendisk, rdev->bdev,
5526 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10005527 disk_stack_limits(mddev->gendisk, rdev->bdev,
5528 rdev->new_data_offset << 9);
Shaohua Li620125f2012-10-11 13:49:05 +11005529 /*
5530 * discard_zeroes_data is required, otherwise data
5531 * could be lost. Consider a scenario: discard a stripe
5532 * (the stripe could be inconsistent if
5533 * discard_zeroes_data is 0); write one disk of the
5534 * stripe (the stripe could be inconsistent again
5535 * depending on which disks are used to calculate
5536 * parity); the disk is broken; The stripe data of this
5537 * disk is lost.
5538 */
5539 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
5540 !bdev_get_queue(rdev->bdev)->
5541 limits.discard_zeroes_data)
5542 discard_supported = false;
NeilBrown05616be2012-05-21 09:27:00 +10005543 }
Shaohua Li620125f2012-10-11 13:49:05 +11005544
5545 if (discard_supported &&
5546 mddev->queue->limits.max_discard_sectors >= stripe &&
5547 mddev->queue->limits.discard_granularity >= stripe)
5548 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
5549 mddev->queue);
5550 else
5551 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
5552 mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 }
5554
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555 return 0;
5556abort:
NeilBrown01f96c02011-09-21 15:30:20 +10005557 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11005558 print_raid5_conf(conf);
5559 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005561 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 return -EIO;
5563}
5564
NeilBrownfd01b882011-10-11 16:47:53 +11005565static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566{
NeilBrownd1688a62011-10-11 16:49:52 +11005567 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568
NeilBrown01f96c02011-09-21 15:30:20 +10005569 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005570 if (mddev->queue)
5571 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10005572 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005573 mddev->private = NULL;
5574 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575 return 0;
5576}
5577
NeilBrownfd01b882011-10-11 16:47:53 +11005578static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579{
NeilBrownd1688a62011-10-11 16:49:52 +11005580 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 int i;
5582
Andre Noll9d8f0362009-06-18 08:45:01 +10005583 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5584 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005585 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 for (i = 0; i < conf->raid_disks; i++)
5587 seq_printf (seq, "%s",
5588 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005589 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591}
5592
NeilBrownd1688a62011-10-11 16:49:52 +11005593static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594{
5595 int i;
5596 struct disk_info *tmp;
5597
NeilBrown0c55e022010-05-03 14:09:02 +10005598 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599 if (!conf) {
5600 printk("(conf==NULL)\n");
5601 return;
5602 }
NeilBrown0c55e022010-05-03 14:09:02 +10005603 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5604 conf->raid_disks,
5605 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606
5607 for (i = 0; i < conf->raid_disks; i++) {
5608 char b[BDEVNAME_SIZE];
5609 tmp = conf->disks + i;
5610 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005611 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5612 i, !test_bit(Faulty, &tmp->rdev->flags),
5613 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 }
5615}
5616
NeilBrownfd01b882011-10-11 16:47:53 +11005617static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618{
5619 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005620 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005622 int count = 0;
5623 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624
5625 for (i = 0; i < conf->raid_disks; i++) {
5626 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11005627 if (tmp->replacement
5628 && tmp->replacement->recovery_offset == MaxSector
5629 && !test_bit(Faulty, &tmp->replacement->flags)
5630 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
5631 /* Replacement has just become active. */
5632 if (!tmp->rdev
5633 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
5634 count++;
5635 if (tmp->rdev) {
5636 /* Replaced device not technically faulty,
5637 * but we need to be sure it gets removed
5638 * and never re-added.
5639 */
5640 set_bit(Faulty, &tmp->rdev->flags);
5641 sysfs_notify_dirent_safe(
5642 tmp->rdev->sysfs_state);
5643 }
5644 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
5645 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005646 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005647 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005648 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005649 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005650 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 }
5652 }
NeilBrown6b965622010-08-18 11:56:59 +10005653 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005654 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005655 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005657 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658}
5659
NeilBrownb8321b62011-12-23 10:17:51 +11005660static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661{
NeilBrownd1688a62011-10-11 16:49:52 +11005662 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11005664 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11005665 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 struct disk_info *p = conf->disks + number;
5667
5668 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11005669 if (rdev == p->rdev)
5670 rdevp = &p->rdev;
5671 else if (rdev == p->replacement)
5672 rdevp = &p->replacement;
5673 else
5674 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11005675
NeilBrown657e3e42011-12-23 10:17:52 +11005676 if (number >= conf->raid_disks &&
5677 conf->reshape_progress == MaxSector)
5678 clear_bit(In_sync, &rdev->flags);
5679
5680 if (test_bit(In_sync, &rdev->flags) ||
5681 atomic_read(&rdev->nr_pending)) {
5682 err = -EBUSY;
5683 goto abort;
5684 }
5685 /* Only remove non-faulty devices if recovery
5686 * isn't possible.
5687 */
5688 if (!test_bit(Faulty, &rdev->flags) &&
5689 mddev->recovery_disabled != conf->recovery_disabled &&
5690 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11005691 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11005692 number < conf->raid_disks) {
5693 err = -EBUSY;
5694 goto abort;
5695 }
5696 *rdevp = NULL;
5697 synchronize_rcu();
5698 if (atomic_read(&rdev->nr_pending)) {
5699 /* lost the race, try later */
5700 err = -EBUSY;
5701 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11005702 } else if (p->replacement) {
5703 /* We must have just cleared 'rdev' */
5704 p->rdev = p->replacement;
5705 clear_bit(Replacement, &p->replacement->flags);
5706 smp_mb(); /* Make sure other CPUs may see both as identical
5707 * but will never see neither - if they are careful
5708 */
5709 p->replacement = NULL;
5710 clear_bit(WantReplacement, &rdev->flags);
5711 } else
5712 /* We might have just removed the Replacement as faulty-
5713 * clear the bit just in case
5714 */
5715 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716abort:
5717
5718 print_raid5_conf(conf);
5719 return err;
5720}
5721
NeilBrownfd01b882011-10-11 16:47:53 +11005722static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723{
NeilBrownd1688a62011-10-11 16:49:52 +11005724 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005725 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726 int disk;
5727 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005728 int first = 0;
5729 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730
NeilBrown7f0da592011-07-28 11:39:22 +10005731 if (mddev->recovery_disabled == conf->recovery_disabled)
5732 return -EBUSY;
5733
NeilBrowndc10c642012-03-19 12:46:37 +11005734 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005736 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737
Neil Brown6c2fce22008-06-28 08:31:31 +10005738 if (rdev->raid_disk >= 0)
5739 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740
5741 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005742 * find the disk ... but prefer rdev->saved_raid_disk
5743 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005745 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005746 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005747 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10005748 first = rdev->saved_raid_disk;
5749
5750 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11005751 p = conf->disks + disk;
5752 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005753 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005755 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005756 if (rdev->saved_raid_disk != disk)
5757 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005758 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10005759 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005761 }
5762 for (disk = first; disk <= last; disk++) {
5763 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11005764 if (test_bit(WantReplacement, &p->rdev->flags) &&
5765 p->replacement == NULL) {
5766 clear_bit(In_sync, &rdev->flags);
5767 set_bit(Replacement, &rdev->flags);
5768 rdev->raid_disk = disk;
5769 err = 0;
5770 conf->fullsync = 1;
5771 rcu_assign_pointer(p->replacement, rdev);
5772 break;
5773 }
5774 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005775out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005777 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778}
5779
NeilBrownfd01b882011-10-11 16:47:53 +11005780static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781{
5782 /* no resync is happening, and there is enough space
5783 * on all devices, so we can resize.
5784 * We need to make sure resync covers any new space.
5785 * If the array is shrinking we should possibly wait until
5786 * any io in the removed space completes, but it hardly seems
5787 * worth it.
5788 */
NeilBrowna4a61252012-05-22 13:55:27 +10005789 sector_t newsize;
Andre Noll9d8f0362009-06-18 08:45:01 +10005790 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10005791 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
5792 if (mddev->external_size &&
5793 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11005794 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10005795 if (mddev->bitmap) {
5796 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
5797 if (ret)
5798 return ret;
5799 }
5800 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10005801 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005802 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005803 if (sectors > mddev->dev_sectors &&
5804 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005805 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5807 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005808 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005809 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810 return 0;
5811}
5812
NeilBrownfd01b882011-10-11 16:47:53 +11005813static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005814{
5815 /* Can only proceed if there are plenty of stripe_heads.
5816 * We need a minimum of one full stripe,, and for sensible progress
5817 * it is best to have about 4 times that.
5818 * If we require 4 times, then the default 256 4K stripe_heads will
5819 * allow for chunk sizes up to 256K, which is probably OK.
5820 * If the chunk size is greater, user-space should request more
5821 * stripe_heads first.
5822 */
NeilBrownd1688a62011-10-11 16:49:52 +11005823 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005824 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5825 > conf->max_nr_stripes ||
5826 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5827 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005828 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5829 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005830 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5831 / STRIPE_SIZE)*4);
5832 return 0;
5833 }
5834 return 1;
5835}
5836
NeilBrownfd01b882011-10-11 16:47:53 +11005837static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005838{
NeilBrownd1688a62011-10-11 16:49:52 +11005839 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005840
NeilBrown88ce4932009-03-31 15:24:23 +11005841 if (mddev->delta_disks == 0 &&
5842 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005843 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005844 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10005845 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005846 return -EINVAL;
5847 if (mddev->delta_disks < 0) {
5848 /* We might be able to shrink, but the devices must
5849 * be made bigger first.
5850 * For raid6, 4 is the minimum size.
5851 * Otherwise 2 is the minimum
5852 */
5853 int min = 2;
5854 if (mddev->level == 6)
5855 min = 4;
5856 if (mddev->raid_disks + mddev->delta_disks < min)
5857 return -EINVAL;
5858 }
NeilBrown29269552006-03-27 01:18:10 -08005859
NeilBrown01ee22b2009-06-18 08:47:20 +10005860 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005861 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005862
NeilBrownec32a2b2009-03-31 15:17:38 +11005863 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
NeilBrown63c70c42006-03-27 01:18:13 -08005864}
5865
NeilBrownfd01b882011-10-11 16:47:53 +11005866static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005867{
NeilBrownd1688a62011-10-11 16:49:52 +11005868 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005869 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005870 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005871 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005872
NeilBrownf4168852007-02-28 20:11:53 -08005873 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005874 return -EBUSY;
5875
NeilBrown01ee22b2009-06-18 08:47:20 +10005876 if (!check_stripe_cache(mddev))
5877 return -ENOSPC;
5878
NeilBrown30b67642012-05-22 13:55:28 +10005879 if (has_failed(conf))
5880 return -EINVAL;
5881
NeilBrownc6563a82012-05-21 09:27:00 +10005882 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11005883 if (!test_bit(In_sync, &rdev->flags)
5884 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005885 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10005886 }
NeilBrown63c70c42006-03-27 01:18:13 -08005887
NeilBrownf4168852007-02-28 20:11:53 -08005888 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005889 /* Not enough devices even to make a degraded array
5890 * of that size
5891 */
5892 return -EINVAL;
5893
NeilBrownec32a2b2009-03-31 15:17:38 +11005894 /* Refuse to reduce size of the array. Any reductions in
5895 * array size must be through explicit setting of array_size
5896 * attribute.
5897 */
5898 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5899 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10005900 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11005901 "before number of disks\n", mdname(mddev));
5902 return -EINVAL;
5903 }
5904
NeilBrownf6705572006-03-27 01:18:11 -08005905 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005906 spin_lock_irq(&conf->device_lock);
5907 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005908 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005909 conf->prev_chunk_sectors = conf->chunk_sectors;
5910 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005911 conf->prev_algo = conf->algorithm;
5912 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10005913 conf->generation++;
5914 /* Code that selects data_offset needs to see the generation update
5915 * if reshape_progress has been set - so a memory barrier needed.
5916 */
5917 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10005918 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11005919 conf->reshape_progress = raid5_size(mddev, 0, 0);
5920 else
5921 conf->reshape_progress = 0;
5922 conf->reshape_safe = conf->reshape_progress;
NeilBrown29269552006-03-27 01:18:10 -08005923 spin_unlock_irq(&conf->device_lock);
5924
5925 /* Add some new drives, as many as will fit.
5926 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10005927 * Don't add devices if we are reducing the number of
5928 * devices in the array. This is because it is not possible
5929 * to correctly record the "partially reconstructed" state of
5930 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08005931 */
NeilBrown87a8dec2011-01-31 11:57:43 +11005932 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11005933 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11005934 if (rdev->raid_disk < 0 &&
5935 !test_bit(Faulty, &rdev->flags)) {
5936 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11005937 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11005938 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11005939 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11005940 else
NeilBrown87a8dec2011-01-31 11:57:43 +11005941 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10005942
5943 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11005944 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11005945 }
NeilBrown87a8dec2011-01-31 11:57:43 +11005946 } else if (rdev->raid_disk >= conf->previous_raid_disks
5947 && !test_bit(Faulty, &rdev->flags)) {
5948 /* This is a spare that was manually added */
5949 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11005950 }
NeilBrown29269552006-03-27 01:18:10 -08005951
NeilBrown87a8dec2011-01-31 11:57:43 +11005952 /* When a reshape changes the number of devices,
5953 * ->degraded is measured against the larger of the
5954 * pre and post number of devices.
5955 */
NeilBrownec32a2b2009-03-31 15:17:38 +11005956 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005957 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11005958 spin_unlock_irqrestore(&conf->device_lock, flags);
5959 }
NeilBrown63c70c42006-03-27 01:18:13 -08005960 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10005961 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b422006-10-03 01:15:46 -07005962 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005963
NeilBrown29269552006-03-27 01:18:10 -08005964 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5965 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5966 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5967 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5968 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005969 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08005970 if (!mddev->sync_thread) {
5971 mddev->recovery = 0;
5972 spin_lock_irq(&conf->device_lock);
5973 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10005974 rdev_for_each(rdev, mddev)
5975 rdev->new_data_offset = rdev->data_offset;
5976 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11005977 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11005978 mddev->reshape_position = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005979 spin_unlock_irq(&conf->device_lock);
5980 return -EAGAIN;
5981 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005982 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005983 md_wakeup_thread(mddev->sync_thread);
5984 md_new_event(mddev);
5985 return 0;
5986}
NeilBrown29269552006-03-27 01:18:10 -08005987
NeilBrownec32a2b2009-03-31 15:17:38 +11005988/* This is called from the reshape thread and should make any
5989 * changes needed in 'conf'
5990 */
NeilBrownd1688a62011-10-11 16:49:52 +11005991static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08005992{
NeilBrown29269552006-03-27 01:18:10 -08005993
NeilBrownf6705572006-03-27 01:18:11 -08005994 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10005995 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07005996
NeilBrownf6705572006-03-27 01:18:11 -08005997 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11005998 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10005999 rdev_for_each(rdev, conf->mddev)
6000 rdev->data_offset = rdev->new_data_offset;
6001 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11006002 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08006003 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11006004 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07006005
6006 /* read-ahead size must cover two whole stripes, which is
6007 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
6008 */
NeilBrown4a5add42010-06-01 19:37:28 +10006009 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11006010 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006011 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11006012 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07006013 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
6014 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
6015 }
NeilBrown29269552006-03-27 01:18:10 -08006016 }
NeilBrown29269552006-03-27 01:18:10 -08006017}
6018
NeilBrownec32a2b2009-03-31 15:17:38 +11006019/* This is called from the raid5d thread with mddev_lock held.
6020 * It makes config changes to the device.
6021 */
NeilBrownfd01b882011-10-11 16:47:53 +11006022static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11006023{
NeilBrownd1688a62011-10-11 16:49:52 +11006024 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11006025
6026 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6027
NeilBrownec32a2b2009-03-31 15:17:38 +11006028 if (mddev->delta_disks > 0) {
6029 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
6030 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006031 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11006032 } else {
6033 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11006034 spin_lock_irq(&conf->device_lock);
6035 mddev->degraded = calc_degraded(conf);
6036 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11006037 for (d = conf->raid_disks ;
6038 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10006039 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11006040 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10006041 if (rdev)
6042 clear_bit(In_sync, &rdev->flags);
6043 rdev = conf->disks[d].replacement;
6044 if (rdev)
6045 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10006046 }
NeilBrowncea9c222009-03-31 15:15:05 +11006047 }
NeilBrown88ce4932009-03-31 15:24:23 +11006048 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006049 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11006050 mddev->reshape_position = MaxSector;
6051 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006052 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11006053 }
6054}
6055
NeilBrownfd01b882011-10-11 16:47:53 +11006056static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07006057{
NeilBrownd1688a62011-10-11 16:49:52 +11006058 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07006059
6060 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08006061 case 2: /* resume for a suspend */
6062 wake_up(&conf->wait_for_overlap);
6063 break;
6064
NeilBrown72626682005-09-09 16:23:54 -07006065 case 1: /* stop all writes */
6066 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006067 /* '2' tells resync/reshape to pause so that all
6068 * active stripes can drain
6069 */
6070 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07006071 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006072 atomic_read(&conf->active_stripes) == 0 &&
6073 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07006074 conf->device_lock, /* nothing */);
NeilBrown64bd6602009-08-03 10:59:58 +10006075 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07006076 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006077 /* allow reshape to continue */
6078 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006079 break;
6080
6081 case 0: /* re-enable writes */
6082 spin_lock_irq(&conf->device_lock);
6083 conf->quiesce = 0;
6084 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08006085 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006086 spin_unlock_irq(&conf->device_lock);
6087 break;
6088 }
NeilBrown72626682005-09-09 16:23:54 -07006089}
NeilBrownb15c2e52006-01-06 00:20:16 -08006090
NeilBrownd562b0c2009-03-31 14:39:39 +11006091
NeilBrownfd01b882011-10-11 16:47:53 +11006092static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11006093{
NeilBrowne373ab12011-10-11 16:48:59 +11006094 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07006095 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11006096
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006097 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11006098 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10006099 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
6100 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006101 return ERR_PTR(-EINVAL);
6102 }
6103
NeilBrowne373ab12011-10-11 16:48:59 +11006104 sectors = raid0_conf->strip_zone[0].zone_end;
6105 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10006106 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006107 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11006108 mddev->new_layout = ALGORITHM_PARITY_N;
6109 mddev->new_chunk_sectors = mddev->chunk_sectors;
6110 mddev->raid_disks += 1;
6111 mddev->delta_disks = 1;
6112 /* make sure it will be not marked as dirty */
6113 mddev->recovery_cp = MaxSector;
6114
6115 return setup_conf(mddev);
6116}
6117
6118
NeilBrownfd01b882011-10-11 16:47:53 +11006119static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006120{
6121 int chunksect;
6122
6123 if (mddev->raid_disks != 2 ||
6124 mddev->degraded > 1)
6125 return ERR_PTR(-EINVAL);
6126
6127 /* Should check if there are write-behind devices? */
6128
6129 chunksect = 64*2; /* 64K by default */
6130
6131 /* The array must be an exact multiple of chunksize */
6132 while (chunksect && (mddev->array_sectors & (chunksect-1)))
6133 chunksect >>= 1;
6134
6135 if ((chunksect<<9) < STRIPE_SIZE)
6136 /* array size does not allow a suitable chunk size */
6137 return ERR_PTR(-EINVAL);
6138
6139 mddev->new_level = 5;
6140 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10006141 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11006142
6143 return setup_conf(mddev);
6144}
6145
NeilBrownfd01b882011-10-11 16:47:53 +11006146static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11006147{
6148 int new_layout;
6149
6150 switch (mddev->layout) {
6151 case ALGORITHM_LEFT_ASYMMETRIC_6:
6152 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
6153 break;
6154 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6155 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
6156 break;
6157 case ALGORITHM_LEFT_SYMMETRIC_6:
6158 new_layout = ALGORITHM_LEFT_SYMMETRIC;
6159 break;
6160 case ALGORITHM_RIGHT_SYMMETRIC_6:
6161 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
6162 break;
6163 case ALGORITHM_PARITY_0_6:
6164 new_layout = ALGORITHM_PARITY_0;
6165 break;
6166 case ALGORITHM_PARITY_N:
6167 new_layout = ALGORITHM_PARITY_N;
6168 break;
6169 default:
6170 return ERR_PTR(-EINVAL);
6171 }
6172 mddev->new_level = 5;
6173 mddev->new_layout = new_layout;
6174 mddev->delta_disks = -1;
6175 mddev->raid_disks -= 1;
6176 return setup_conf(mddev);
6177}
6178
NeilBrownd562b0c2009-03-31 14:39:39 +11006179
NeilBrownfd01b882011-10-11 16:47:53 +11006180static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11006181{
NeilBrown88ce4932009-03-31 15:24:23 +11006182 /* For a 2-drive array, the layout and chunk size can be changed
6183 * immediately as not restriping is needed.
6184 * For larger arrays we record the new value - after validation
6185 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11006186 */
NeilBrownd1688a62011-10-11 16:49:52 +11006187 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10006188 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11006189
NeilBrown597a7112009-06-18 08:47:42 +10006190 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11006191 return -EINVAL;
6192 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006193 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11006194 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006195 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11006196 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006197 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11006198 /* not factor of array size */
6199 return -EINVAL;
6200 }
6201
6202 /* They look valid */
6203
NeilBrown88ce4932009-03-31 15:24:23 +11006204 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10006205 /* can make the change immediately */
6206 if (mddev->new_layout >= 0) {
6207 conf->algorithm = mddev->new_layout;
6208 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11006209 }
6210 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10006211 conf->chunk_sectors = new_chunk ;
6212 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11006213 }
6214 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6215 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11006216 }
NeilBrown50ac1682009-06-18 08:47:55 +10006217 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11006218}
6219
NeilBrownfd01b882011-10-11 16:47:53 +11006220static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11006221{
NeilBrown597a7112009-06-18 08:47:42 +10006222 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10006223
NeilBrown597a7112009-06-18 08:47:42 +10006224 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11006225 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006226 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006227 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11006228 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006229 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11006230 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006231 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11006232 /* not factor of array size */
6233 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006234 }
NeilBrown88ce4932009-03-31 15:24:23 +11006235
6236 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10006237 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11006238}
6239
NeilBrownfd01b882011-10-11 16:47:53 +11006240static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006241{
6242 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006243 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006244 * raid1 - if there are two drives. We need to know the chunk size
6245 * raid4 - trivial - just use a raid4 layout.
6246 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006247 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006248 if (mddev->level == 0)
6249 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11006250 if (mddev->level == 1)
6251 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11006252 if (mddev->level == 4) {
6253 mddev->new_layout = ALGORITHM_PARITY_N;
6254 mddev->new_level = 5;
6255 return setup_conf(mddev);
6256 }
NeilBrownfc9739c2009-03-31 14:57:20 +11006257 if (mddev->level == 6)
6258 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11006259
6260 return ERR_PTR(-EINVAL);
6261}
6262
NeilBrownfd01b882011-10-11 16:47:53 +11006263static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11006264{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006265 /* raid4 can take over:
6266 * raid0 - if there is only one strip zone
6267 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11006268 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006269 if (mddev->level == 0)
6270 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11006271 if (mddev->level == 5 &&
6272 mddev->layout == ALGORITHM_PARITY_N) {
6273 mddev->new_layout = 0;
6274 mddev->new_level = 4;
6275 return setup_conf(mddev);
6276 }
6277 return ERR_PTR(-EINVAL);
6278}
NeilBrownd562b0c2009-03-31 14:39:39 +11006279
NeilBrown84fc4b52011-10-11 16:49:58 +11006280static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11006281
NeilBrownfd01b882011-10-11 16:47:53 +11006282static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11006283{
6284 /* Currently can only take over a raid5. We map the
6285 * personality to an equivalent raid6 personality
6286 * with the Q block at the end.
6287 */
6288 int new_layout;
6289
6290 if (mddev->pers != &raid5_personality)
6291 return ERR_PTR(-EINVAL);
6292 if (mddev->degraded > 1)
6293 return ERR_PTR(-EINVAL);
6294 if (mddev->raid_disks > 253)
6295 return ERR_PTR(-EINVAL);
6296 if (mddev->raid_disks < 3)
6297 return ERR_PTR(-EINVAL);
6298
6299 switch (mddev->layout) {
6300 case ALGORITHM_LEFT_ASYMMETRIC:
6301 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
6302 break;
6303 case ALGORITHM_RIGHT_ASYMMETRIC:
6304 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
6305 break;
6306 case ALGORITHM_LEFT_SYMMETRIC:
6307 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
6308 break;
6309 case ALGORITHM_RIGHT_SYMMETRIC:
6310 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
6311 break;
6312 case ALGORITHM_PARITY_0:
6313 new_layout = ALGORITHM_PARITY_0_6;
6314 break;
6315 case ALGORITHM_PARITY_N:
6316 new_layout = ALGORITHM_PARITY_N;
6317 break;
6318 default:
6319 return ERR_PTR(-EINVAL);
6320 }
6321 mddev->new_level = 6;
6322 mddev->new_layout = new_layout;
6323 mddev->delta_disks = 1;
6324 mddev->raid_disks += 1;
6325 return setup_conf(mddev);
6326}
6327
6328
NeilBrown84fc4b52011-10-11 16:49:58 +11006329static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07006330{
6331 .name = "raid6",
6332 .level = 6,
6333 .owner = THIS_MODULE,
6334 .make_request = make_request,
6335 .run = run,
6336 .stop = stop,
6337 .status = status,
6338 .error_handler = error,
6339 .hot_add_disk = raid5_add_disk,
6340 .hot_remove_disk= raid5_remove_disk,
6341 .spare_active = raid5_spare_active,
6342 .sync_request = sync_request,
6343 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006344 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10006345 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08006346 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006347 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07006348 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11006349 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07006350};
NeilBrown84fc4b52011-10-11 16:49:58 +11006351static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006352{
6353 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08006354 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006355 .owner = THIS_MODULE,
6356 .make_request = make_request,
6357 .run = run,
6358 .stop = stop,
6359 .status = status,
6360 .error_handler = error,
6361 .hot_add_disk = raid5_add_disk,
6362 .hot_remove_disk= raid5_remove_disk,
6363 .spare_active = raid5_spare_active,
6364 .sync_request = sync_request,
6365 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006366 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08006367 .check_reshape = raid5_check_reshape,
6368 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006369 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07006370 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11006371 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006372};
6373
NeilBrown84fc4b52011-10-11 16:49:58 +11006374static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375{
NeilBrown2604b702006-01-06 00:20:36 -08006376 .name = "raid4",
6377 .level = 4,
6378 .owner = THIS_MODULE,
6379 .make_request = make_request,
6380 .run = run,
6381 .stop = stop,
6382 .status = status,
6383 .error_handler = error,
6384 .hot_add_disk = raid5_add_disk,
6385 .hot_remove_disk= raid5_remove_disk,
6386 .spare_active = raid5_spare_active,
6387 .sync_request = sync_request,
6388 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006389 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08006390 .check_reshape = raid5_check_reshape,
6391 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006392 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08006393 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11006394 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08006395};
6396
6397static int __init raid5_init(void)
6398{
NeilBrown16a53ec2006-06-26 00:27:38 -07006399 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006400 register_md_personality(&raid5_personality);
6401 register_md_personality(&raid4_personality);
6402 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006403}
6404
NeilBrown2604b702006-01-06 00:20:36 -08006405static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406{
NeilBrown16a53ec2006-06-26 00:27:38 -07006407 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006408 unregister_md_personality(&raid5_personality);
6409 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410}
6411
6412module_init(raid5_init);
6413module_exit(raid5_exit);
6414MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11006415MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08006417MODULE_ALIAS("md-raid5");
6418MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08006419MODULE_ALIAS("md-level-5");
6420MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07006421MODULE_ALIAS("md-personality-8"); /* RAID6 */
6422MODULE_ALIAS("md-raid6");
6423MODULE_ALIAS("md-level-6");
6424
6425/* This used to be two separate modules, they were: */
6426MODULE_ALIAS("raid5");
6427MODULE_ALIAS("raid6");