blob: 7031b865b3a030488614764528295d509c663ba5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * raid5.c : Multiple Devices driver for Linux
3 * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
4 * Copyright (C) 1999, 2000 Ingo Molnar
NeilBrown16a53ec2006-06-26 00:27:38 -07005 * Copyright (C) 2002, 2003 H. Peter Anvin
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
NeilBrown16a53ec2006-06-26 00:27:38 -07007 * RAID-4/5/6 management functions.
8 * Thanks to Penguin Computing for making the RAID-6 development possible
9 * by donating a test server!
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
NeilBrownae3c20c2006-07-10 04:44:17 -070021/*
22 * BITMAP UNPLUGGING:
23 *
24 * The sequencing for updating the bitmap reliably is a little
25 * subtle (and I got it wrong the first time) so it deserves some
26 * explanation.
27 *
28 * We group bitmap updates into batches. Each batch has a number.
29 * We may write out several batches at once, but that isn't very important.
NeilBrown7c13edc2011-04-18 18:25:43 +100030 * conf->seq_write is the number of the last batch successfully written.
31 * conf->seq_flush is the number of the last batch that was closed to
NeilBrownae3c20c2006-07-10 04:44:17 -070032 * new additions.
33 * When we discover that we will need to write to any block in a stripe
34 * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq
NeilBrown7c13edc2011-04-18 18:25:43 +100035 * the number of the batch it will be in. This is seq_flush+1.
NeilBrownae3c20c2006-07-10 04:44:17 -070036 * When we are ready to do a write, if that batch hasn't been written yet,
37 * we plug the array and queue the stripe for later.
38 * When an unplug happens, we increment bm_flush, thus closing the current
39 * batch.
40 * When we notice that bm_flush > bm_write, we write out all pending updates
41 * to the bitmap, and advance bm_write to where bm_flush was.
42 * This may occasionally write a bit out twice, but is sure never to
43 * miss any bits.
44 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
NeilBrownbff61972009-03-31 14:33:13 +110046#include <linux/blkdev.h>
NeilBrownf6705572006-03-27 01:18:11 -080047#include <linux/kthread.h>
Dan Williamsf701d582009-03-31 15:09:39 +110048#include <linux/raid/pq.h>
Dan Williams91c00922007-01-02 13:52:30 -070049#include <linux/async_tx.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040050#include <linux/module.h>
Dan Williams07a3b412009-08-29 19:13:13 -070051#include <linux/async.h>
NeilBrownbff61972009-03-31 14:33:13 +110052#include <linux/seq_file.h>
Dan Williams36d1c642009-07-14 11:48:22 -070053#include <linux/cpu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090054#include <linux/slab.h>
Christian Dietrich8bda4702011-07-27 11:00:36 +100055#include <linux/ratelimit.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110056#include "md.h"
NeilBrownbff61972009-03-31 14:33:13 +110057#include "raid5.h"
Trela Maciej54071b32010-03-08 16:02:42 +110058#include "raid0.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110059#include "bitmap.h"
NeilBrown72626682005-09-09 16:23:54 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/*
62 * Stripe cache
63 */
64
65#define NR_STRIPES 256
66#define STRIPE_SIZE PAGE_SIZE
67#define STRIPE_SHIFT (PAGE_SHIFT - 9)
68#define STRIPE_SECTORS (STRIPE_SIZE>>9)
69#define IO_THRESHOLD 1
Dan Williams8b3e6cd2008-04-28 02:15:53 -070070#define BYPASS_THRESHOLD 1
NeilBrownfccddba2006-01-06 00:20:33 -080071#define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define HASH_MASK (NR_HASH - 1)
73
NeilBrownd1688a62011-10-11 16:49:52 +110074static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
NeilBrowndb298e12011-10-07 14:23:00 +110075{
76 int hash = (sect >> STRIPE_SHIFT) & HASH_MASK;
77 return &conf->stripe_hashtbl[hash];
78}
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80/* bio's attached to a stripe+device for I/O are linked together in bi_sector
81 * order without overlap. There may be several bio's per stripe+device, and
82 * a bio could span several devices.
83 * When walking this list for a particular stripe+device, we must never proceed
84 * beyond a bio that extends past this device, as the next bio might no longer
85 * be valid.
NeilBrowndb298e12011-10-07 14:23:00 +110086 * This function is used to determine the 'next' bio in the list, given the sector
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * of the current stripe+device
88 */
NeilBrowndb298e12011-10-07 14:23:00 +110089static inline struct bio *r5_next_bio(struct bio *bio, sector_t sector)
90{
91 int sectors = bio->bi_size >> 9;
92 if (bio->bi_sector + sectors < sector + STRIPE_SECTORS)
93 return bio->bi_next;
94 else
95 return NULL;
96}
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Jens Axboe960e7392008-08-15 10:41:18 +020098/*
Jens Axboe5b99c2f2008-08-15 10:56:11 +020099 * We maintain a biased count of active stripes in the bottom 16 bits of
100 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
Jens Axboe960e7392008-08-15 10:41:18 +0200101 */
Shaohua Lie7836bd62012-07-19 16:01:31 +1000102static inline int raid5_bi_processed_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200103{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000104 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
105 return (atomic_read(segments) >> 16) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200106}
107
Shaohua Lie7836bd62012-07-19 16:01:31 +1000108static inline int raid5_dec_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200109{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000110 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
111 return atomic_sub_return(1, segments) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200112}
113
Shaohua Lie7836bd62012-07-19 16:01:31 +1000114static inline void raid5_inc_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200115{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000116 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
117 atomic_inc(segments);
Jens Axboe960e7392008-08-15 10:41:18 +0200118}
119
Shaohua Lie7836bd62012-07-19 16:01:31 +1000120static inline void raid5_set_bi_processed_stripes(struct bio *bio,
121 unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200122{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000123 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
124 int old, new;
Jens Axboe960e7392008-08-15 10:41:18 +0200125
Shaohua Lie7836bd62012-07-19 16:01:31 +1000126 do {
127 old = atomic_read(segments);
128 new = (old & 0xffff) | (cnt << 16);
129 } while (atomic_cmpxchg(segments, old, new) != old);
Jens Axboe960e7392008-08-15 10:41:18 +0200130}
131
Shaohua Lie7836bd62012-07-19 16:01:31 +1000132static inline void raid5_set_bi_stripes(struct bio *bio, unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200133{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000134 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
135 atomic_set(segments, cnt);
Jens Axboe960e7392008-08-15 10:41:18 +0200136}
137
NeilBrownd0dabf72009-03-31 14:39:38 +1100138/* Find first data disk in a raid6 stripe */
139static inline int raid6_d0(struct stripe_head *sh)
140{
NeilBrown67cc2b82009-03-31 14:39:38 +1100141 if (sh->ddf_layout)
142 /* ddf always start from first device */
143 return 0;
144 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100145 if (sh->qd_idx == sh->disks - 1)
146 return 0;
147 else
148 return sh->qd_idx + 1;
149}
NeilBrown16a53ec2006-06-26 00:27:38 -0700150static inline int raid6_next_disk(int disk, int raid_disks)
151{
152 disk++;
153 return (disk < raid_disks) ? disk : 0;
154}
Dan Williamsa4456852007-07-09 11:56:43 -0700155
NeilBrownd0dabf72009-03-31 14:39:38 +1100156/* When walking through the disks in a raid5, starting at raid6_d0,
157 * We need to map each disk to a 'slot', where the data disks are slot
158 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
159 * is raid_disks-1. This help does that mapping.
160 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100161static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
162 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100163{
Dan Williams66295422009-10-19 18:09:32 -0700164 int slot = *count;
NeilBrown67cc2b82009-03-31 14:39:38 +1100165
NeilBrowne4424fe2009-10-16 16:27:34 +1100166 if (sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700167 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100168 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100169 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100170 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100171 return syndrome_disks + 1;
NeilBrowne4424fe2009-10-16 16:27:34 +1100172 if (!sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700173 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100174 return slot;
175}
176
Dan Williamsa4456852007-07-09 11:56:43 -0700177static void return_io(struct bio *return_bi)
178{
179 struct bio *bi = return_bi;
180 while (bi) {
Dan Williamsa4456852007-07-09 11:56:43 -0700181
182 return_bi = bi->bi_next;
183 bi->bi_next = NULL;
184 bi->bi_size = 0;
Neil Brown0e13fe232008-06-28 08:31:20 +1000185 bio_endio(bi, 0);
Dan Williamsa4456852007-07-09 11:56:43 -0700186 bi = return_bi;
187 }
188}
189
NeilBrownd1688a62011-10-11 16:49:52 +1100190static void print_raid5_conf (struct r5conf *conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Dan Williams600aa102008-06-28 08:32:05 +1000192static int stripe_operations_active(struct stripe_head *sh)
193{
194 return sh->check_state || sh->reconstruct_state ||
195 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
196 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
197}
198
Shaohua Li4eb788d2012-07-19 16:01:31 +1000199static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Shaohua Li4eb788d2012-07-19 16:01:31 +1000201 BUG_ON(!list_empty(&sh->lru));
202 BUG_ON(atomic_read(&conf->active_stripes)==0);
203 if (test_bit(STRIPE_HANDLE, &sh->state)) {
204 if (test_bit(STRIPE_DELAYED, &sh->state) &&
205 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
206 list_add_tail(&sh->lru, &conf->delayed_list);
207 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
208 sh->bm_seq - conf->seq_write > 0)
209 list_add_tail(&sh->lru, &conf->bitmap_list);
210 else {
211 clear_bit(STRIPE_DELAYED, &sh->state);
212 clear_bit(STRIPE_BIT_DELAY, &sh->state);
213 list_add_tail(&sh->lru, &conf->handle_list);
214 }
215 md_wakeup_thread(conf->mddev->thread);
216 } else {
217 BUG_ON(stripe_operations_active(sh));
218 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
219 if (atomic_dec_return(&conf->preread_active_stripes)
220 < IO_THRESHOLD)
221 md_wakeup_thread(conf->mddev->thread);
222 atomic_dec(&conf->active_stripes);
223 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
224 list_add_tail(&sh->lru, &conf->inactive_list);
225 wake_up(&conf->wait_for_stripe);
226 if (conf->retry_read_aligned)
227 md_wakeup_thread(conf->mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
229 }
230}
NeilBrownd0dabf72009-03-31 14:39:38 +1100231
Shaohua Li4eb788d2012-07-19 16:01:31 +1000232static void __release_stripe(struct r5conf *conf, struct stripe_head *sh)
233{
234 if (atomic_dec_and_test(&sh->count))
235 do_release_stripe(conf, sh);
236}
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238static void release_stripe(struct stripe_head *sh)
239{
NeilBrownd1688a62011-10-11 16:49:52 +1100240 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 unsigned long flags;
NeilBrown16a53ec2006-06-26 00:27:38 -0700242
Shaohua Li4eb788d2012-07-19 16:01:31 +1000243 local_irq_save(flags);
244 if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) {
245 do_release_stripe(conf, sh);
246 spin_unlock(&conf->device_lock);
247 }
248 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
NeilBrownfccddba2006-01-06 00:20:33 -0800251static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Dan Williams45b42332007-07-09 11:56:43 -0700253 pr_debug("remove_hash(), stripe %llu\n",
254 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
NeilBrownfccddba2006-01-06 00:20:33 -0800256 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
NeilBrownd1688a62011-10-11 16:49:52 +1100259static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
NeilBrownfccddba2006-01-06 00:20:33 -0800261 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Dan Williams45b42332007-07-09 11:56:43 -0700263 pr_debug("insert_hash(), stripe %llu\n",
264 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
NeilBrownfccddba2006-01-06 00:20:33 -0800266 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
269
270/* find an idle stripe, make sure it is unhashed, and return it. */
NeilBrownd1688a62011-10-11 16:49:52 +1100271static struct stripe_head *get_free_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 struct stripe_head *sh = NULL;
274 struct list_head *first;
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (list_empty(&conf->inactive_list))
277 goto out;
278 first = conf->inactive_list.next;
279 sh = list_entry(first, struct stripe_head, lru);
280 list_del_init(first);
281 remove_hash(sh);
282 atomic_inc(&conf->active_stripes);
283out:
284 return sh;
285}
286
NeilBrowne4e11e32010-06-16 16:45:16 +1000287static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 struct page *p;
290 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000291 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
NeilBrowne4e11e32010-06-16 16:45:16 +1000293 for (i = 0; i < num ; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 p = sh->dev[i].page;
295 if (!p)
296 continue;
297 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800298 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300}
301
NeilBrowne4e11e32010-06-16 16:45:16 +1000302static int grow_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000305 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
NeilBrowne4e11e32010-06-16 16:45:16 +1000307 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 struct page *page;
309
310 if (!(page = alloc_page(GFP_KERNEL))) {
311 return 1;
312 }
313 sh->dev[i].page = page;
314 }
315 return 0;
316}
317
NeilBrown784052e2009-03-31 15:19:07 +1100318static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrownd1688a62011-10-11 16:49:52 +1100319static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100320 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
NeilBrownb5663ba2009-03-31 14:39:38 +1100322static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
NeilBrownd1688a62011-10-11 16:49:52 +1100324 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800325 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200327 BUG_ON(atomic_read(&sh->count) != 0);
328 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000329 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700330
Dan Williams45b42332007-07-09 11:56:43 -0700331 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 (unsigned long long)sh->sector);
333
334 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700335
NeilBrown86b42c72009-03-31 15:19:03 +1100336 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100337 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100339 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 sh->state = 0;
341
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800342
343 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 struct r5dev *dev = &sh->dev[i];
345
Dan Williamsd84e0f12007-01-02 13:52:30 -0700346 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700348 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700350 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000352 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100355 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
357 insert_hash(conf, sh);
358}
359
NeilBrownd1688a62011-10-11 16:49:52 +1100360static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100361 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -0800364 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Dan Williams45b42332007-07-09 11:56:43 -0700366 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
NeilBrownfccddba2006-01-06 00:20:33 -0800367 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100368 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700370 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return NULL;
372}
373
NeilBrown674806d2010-06-16 17:17:53 +1000374/*
375 * Need to check if array has failed when deciding whether to:
376 * - start an array
377 * - remove non-faulty devices
378 * - add a spare
379 * - allow a reshape
380 * This determination is simple when no reshape is happening.
381 * However if there is a reshape, we need to carefully check
382 * both the before and after sections.
383 * This is because some failed devices may only affect one
384 * of the two sections, and some non-in_sync devices may
385 * be insync in the section most affected by failed devices.
386 */
NeilBrown908f4fb2011-12-23 10:17:50 +1100387static int calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000388{
NeilBrown908f4fb2011-12-23 10:17:50 +1100389 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000390 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000391
392 rcu_read_lock();
393 degraded = 0;
394 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100395 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000396 if (rdev && test_bit(Faulty, &rdev->flags))
397 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000398 if (!rdev || test_bit(Faulty, &rdev->flags))
399 degraded++;
400 else if (test_bit(In_sync, &rdev->flags))
401 ;
402 else
403 /* not in-sync or faulty.
404 * If the reshape increases the number of devices,
405 * this is being recovered by the reshape, so
406 * this 'previous' section is not in_sync.
407 * If the number of devices is being reduced however,
408 * the device can only be part of the array if
409 * we are reverting a reshape, so this section will
410 * be in-sync.
411 */
412 if (conf->raid_disks >= conf->previous_raid_disks)
413 degraded++;
414 }
415 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100416 if (conf->raid_disks == conf->previous_raid_disks)
417 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000418 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100419 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000420 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100421 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000422 if (rdev && test_bit(Faulty, &rdev->flags))
423 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000424 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100425 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000426 else if (test_bit(In_sync, &rdev->flags))
427 ;
428 else
429 /* not in-sync or faulty.
430 * If reshape increases the number of devices, this
431 * section has already been recovered, else it
432 * almost certainly hasn't.
433 */
434 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100435 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000436 }
437 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100438 if (degraded2 > degraded)
439 return degraded2;
440 return degraded;
441}
442
443static int has_failed(struct r5conf *conf)
444{
445 int degraded;
446
447 if (conf->mddev->reshape_position == MaxSector)
448 return conf->mddev->degraded > conf->max_degraded;
449
450 degraded = calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000451 if (degraded > conf->max_degraded)
452 return 1;
453 return 0;
454}
455
NeilBrownb5663ba2009-03-31 14:39:38 +1100456static struct stripe_head *
NeilBrownd1688a62011-10-11 16:49:52 +1100457get_active_stripe(struct r5conf *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000458 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 struct stripe_head *sh;
461
Dan Williams45b42332007-07-09 11:56:43 -0700462 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 spin_lock_irq(&conf->device_lock);
465
466 do {
NeilBrown72626682005-09-09 16:23:54 -0700467 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000468 conf->quiesce == 0 || noquiesce,
NeilBrown72626682005-09-09 16:23:54 -0700469 conf->device_lock, /* nothing */);
NeilBrown86b42c72009-03-31 15:19:03 +1100470 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (!sh) {
472 if (!conf->inactive_blocked)
473 sh = get_free_stripe(conf);
474 if (noblock && sh == NULL)
475 break;
476 if (!sh) {
477 conf->inactive_blocked = 1;
478 wait_event_lock_irq(conf->wait_for_stripe,
479 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800480 (atomic_read(&conf->active_stripes)
481 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 || !conf->inactive_blocked),
483 conf->device_lock,
NeilBrown7c13edc2011-04-18 18:25:43 +1000484 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 conf->inactive_blocked = 0;
486 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100487 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 } else {
489 if (atomic_read(&sh->count)) {
NeilBrownab69ae12009-03-31 15:26:47 +1100490 BUG_ON(!list_empty(&sh->lru)
Shaohua Li8811b592012-08-02 08:33:00 +1000491 && !test_bit(STRIPE_EXPANDING, &sh->state)
492 && !test_bit(STRIPE_ON_UNPLUG_LIST, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 } else {
494 if (!test_bit(STRIPE_HANDLE, &sh->state))
495 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700496 if (list_empty(&sh->lru) &&
497 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700498 BUG();
499 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
501 }
502 } while (sh == NULL);
503
504 if (sh)
505 atomic_inc(&sh->count);
506
507 spin_unlock_irq(&conf->device_lock);
508 return sh;
509}
510
NeilBrown05616be2012-05-21 09:27:00 +1000511/* Determine if 'data_offset' or 'new_data_offset' should be used
512 * in this stripe_head.
513 */
514static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
515{
516 sector_t progress = conf->reshape_progress;
517 /* Need a memory barrier to make sure we see the value
518 * of conf->generation, or ->data_offset that was set before
519 * reshape_progress was updated.
520 */
521 smp_rmb();
522 if (progress == MaxSector)
523 return 0;
524 if (sh->generation == conf->generation - 1)
525 return 0;
526 /* We are in a reshape, and this is a new-generation stripe,
527 * so use new_data_offset.
528 */
529 return 1;
530}
531
NeilBrown6712ecf2007-09-27 12:47:43 +0200532static void
533raid5_end_read_request(struct bio *bi, int error);
534static void
535raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700536
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000537static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700538{
NeilBrownd1688a62011-10-11 16:49:52 +1100539 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700540 int i, disks = sh->disks;
541
542 might_sleep();
543
544 for (i = disks; i--; ) {
545 int rw;
NeilBrown9a3e1102011-12-23 10:17:53 +1100546 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100547 struct bio *bi, *rbi;
548 struct md_rdev *rdev, *rrdev = NULL;
Tejun Heoe9c74692010-09-03 11:56:18 +0200549 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
550 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
551 rw = WRITE_FUA;
552 else
553 rw = WRITE;
554 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700555 rw = READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100556 else if (test_and_clear_bit(R5_WantReplace,
557 &sh->dev[i].flags)) {
558 rw = WRITE;
559 replace_only = 1;
560 } else
Dan Williams91c00922007-01-02 13:52:30 -0700561 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +1000562 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
563 rw |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -0700564
565 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100566 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700567
568 bi->bi_rw = rw;
NeilBrown977df362011-12-23 10:17:53 +1100569 rbi->bi_rw = rw;
570 if (rw & WRITE) {
Dan Williams91c00922007-01-02 13:52:30 -0700571 bi->bi_end_io = raid5_end_write_request;
NeilBrown977df362011-12-23 10:17:53 +1100572 rbi->bi_end_io = raid5_end_write_request;
573 } else
Dan Williams91c00922007-01-02 13:52:30 -0700574 bi->bi_end_io = raid5_end_read_request;
575
576 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100577 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100578 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
579 rdev = rcu_dereference(conf->disks[i].rdev);
580 if (!rdev) {
581 rdev = rrdev;
582 rrdev = NULL;
583 }
NeilBrown9a3e1102011-12-23 10:17:53 +1100584 if (rw & WRITE) {
585 if (replace_only)
586 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100587 if (rdev == rrdev)
588 /* We raced and saw duplicates */
589 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100590 } else {
NeilBrowndd054fc2011-12-23 10:17:53 +1100591 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100592 rdev = rrdev;
593 rrdev = NULL;
594 }
NeilBrown977df362011-12-23 10:17:53 +1100595
Dan Williams91c00922007-01-02 13:52:30 -0700596 if (rdev && test_bit(Faulty, &rdev->flags))
597 rdev = NULL;
598 if (rdev)
599 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100600 if (rrdev && test_bit(Faulty, &rrdev->flags))
601 rrdev = NULL;
602 if (rrdev)
603 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700604 rcu_read_unlock();
605
NeilBrown73e92e52011-07-28 11:39:22 +1000606 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100607 * need to check for writes. We never accept write errors
608 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000609 */
610 while ((rw & WRITE) && rdev &&
611 test_bit(WriteErrorSeen, &rdev->flags)) {
612 sector_t first_bad;
613 int bad_sectors;
614 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
615 &first_bad, &bad_sectors);
616 if (!bad)
617 break;
618
619 if (bad < 0) {
620 set_bit(BlockedBadBlocks, &rdev->flags);
621 if (!conf->mddev->external &&
622 conf->mddev->flags) {
623 /* It is very unlikely, but we might
624 * still need to write out the
625 * bad block log - better give it
626 * a chance*/
627 md_check_recovery(conf->mddev);
628 }
majianpeng18507532012-07-03 12:11:54 +1000629 /*
630 * Because md_wait_for_blocked_rdev
631 * will dec nr_pending, we must
632 * increment it first.
633 */
634 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +1000635 md_wait_for_blocked_rdev(rdev, conf->mddev);
636 } else {
637 /* Acknowledged bad block - skip the write */
638 rdev_dec_pending(rdev, conf->mddev);
639 rdev = NULL;
640 }
641 }
642
Dan Williams91c00922007-01-02 13:52:30 -0700643 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100644 if (s->syncing || s->expanding || s->expanded
645 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700646 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
647
Dan Williams2b7497f2008-06-28 08:31:52 +1000648 set_bit(STRIPE_IO_STARTED, &sh->state);
649
Dan Williams91c00922007-01-02 13:52:30 -0700650 bi->bi_bdev = rdev->bdev;
651 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700652 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700653 bi->bi_rw, i);
654 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000655 if (use_new_offset(conf, sh))
656 bi->bi_sector = (sh->sector
657 + rdev->new_data_offset);
658 else
659 bi->bi_sector = (sh->sector
660 + rdev->data_offset);
majianpeng3f9e7c12012-07-31 10:04:21 +1000661 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
662 bi->bi_rw |= REQ_FLUSH;
663
Dan Williams91c00922007-01-02 13:52:30 -0700664 bi->bi_flags = 1 << BIO_UPTODATE;
Dan Williams91c00922007-01-02 13:52:30 -0700665 bi->bi_idx = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700666 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
667 bi->bi_io_vec[0].bv_offset = 0;
668 bi->bi_size = STRIPE_SIZE;
669 bi->bi_next = NULL;
NeilBrown977df362011-12-23 10:17:53 +1100670 if (rrdev)
671 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Dan Williams91c00922007-01-02 13:52:30 -0700672 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100673 }
674 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100675 if (s->syncing || s->expanding || s->expanded
676 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +1100677 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
678
679 set_bit(STRIPE_IO_STARTED, &sh->state);
680
681 rbi->bi_bdev = rrdev->bdev;
682 pr_debug("%s: for %llu schedule op %ld on "
683 "replacement disc %d\n",
684 __func__, (unsigned long long)sh->sector,
685 rbi->bi_rw, i);
686 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000687 if (use_new_offset(conf, sh))
688 rbi->bi_sector = (sh->sector
689 + rrdev->new_data_offset);
690 else
691 rbi->bi_sector = (sh->sector
692 + rrdev->data_offset);
NeilBrown977df362011-12-23 10:17:53 +1100693 rbi->bi_flags = 1 << BIO_UPTODATE;
694 rbi->bi_idx = 0;
695 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
696 rbi->bi_io_vec[0].bv_offset = 0;
697 rbi->bi_size = STRIPE_SIZE;
698 rbi->bi_next = NULL;
699 generic_make_request(rbi);
700 }
701 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000702 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700703 set_bit(STRIPE_DEGRADED, &sh->state);
704 pr_debug("skip op %ld on disc %d for sector %llu\n",
705 bi->bi_rw, i, (unsigned long long)sh->sector);
706 clear_bit(R5_LOCKED, &sh->dev[i].flags);
707 set_bit(STRIPE_HANDLE, &sh->state);
708 }
709 }
710}
711
712static struct dma_async_tx_descriptor *
713async_copy_data(int frombio, struct bio *bio, struct page *page,
714 sector_t sector, struct dma_async_tx_descriptor *tx)
715{
716 struct bio_vec *bvl;
717 struct page *bio_page;
718 int i;
719 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700720 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700721 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700722
723 if (bio->bi_sector >= sector)
724 page_offset = (signed)(bio->bi_sector - sector) * 512;
725 else
726 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700727
Dan Williams0403e382009-09-08 17:42:50 -0700728 if (frombio)
729 flags |= ASYNC_TX_FENCE;
730 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
731
Dan Williams91c00922007-01-02 13:52:30 -0700732 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000733 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700734 int clen;
735 int b_offset = 0;
736
737 if (page_offset < 0) {
738 b_offset = -page_offset;
739 page_offset += b_offset;
740 len -= b_offset;
741 }
742
743 if (len > 0 && page_offset + len > STRIPE_SIZE)
744 clen = STRIPE_SIZE - page_offset;
745 else
746 clen = len;
747
748 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000749 b_offset += bvl->bv_offset;
750 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700751 if (frombio)
752 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700753 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700754 else
755 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700756 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700757 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700758 /* chain the operations */
759 submit.depend_tx = tx;
760
Dan Williams91c00922007-01-02 13:52:30 -0700761 if (clen < len) /* hit end of page */
762 break;
763 page_offset += len;
764 }
765
766 return tx;
767}
768
769static void ops_complete_biofill(void *stripe_head_ref)
770{
771 struct stripe_head *sh = stripe_head_ref;
772 struct bio *return_bi = NULL;
Dan Williamse4d84902007-09-24 10:06:13 -0700773 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700774
Harvey Harrisone46b2722008-04-28 02:15:50 -0700775 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700776 (unsigned long long)sh->sector);
777
778 /* clear completed biofills */
779 for (i = sh->disks; i--; ) {
780 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700781
782 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700783 /* and check if we need to reply to a read request,
784 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000785 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700786 */
787 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700788 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700789
Dan Williams91c00922007-01-02 13:52:30 -0700790 BUG_ON(!dev->read);
791 rbi = dev->read;
792 dev->read = NULL;
793 while (rbi && rbi->bi_sector <
794 dev->sector + STRIPE_SECTORS) {
795 rbi2 = r5_next_bio(rbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +1000796 if (!raid5_dec_bi_active_stripes(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700797 rbi->bi_next = return_bi;
798 return_bi = rbi;
799 }
Dan Williams91c00922007-01-02 13:52:30 -0700800 rbi = rbi2;
801 }
802 }
803 }
Dan Williams83de75c2008-06-28 08:31:58 +1000804 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700805
806 return_io(return_bi);
807
Dan Williamse4d84902007-09-24 10:06:13 -0700808 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700809 release_stripe(sh);
810}
811
812static void ops_run_biofill(struct stripe_head *sh)
813{
814 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa08abd82009-06-03 11:43:59 -0700815 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700816 int i;
817
Harvey Harrisone46b2722008-04-28 02:15:50 -0700818 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700819 (unsigned long long)sh->sector);
820
821 for (i = sh->disks; i--; ) {
822 struct r5dev *dev = &sh->dev[i];
823 if (test_bit(R5_Wantfill, &dev->flags)) {
824 struct bio *rbi;
Shaohua Lib17459c2012-07-19 16:01:31 +1000825 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700826 dev->read = rbi = dev->toread;
827 dev->toread = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +1000828 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700829 while (rbi && rbi->bi_sector <
830 dev->sector + STRIPE_SECTORS) {
831 tx = async_copy_data(0, rbi, dev->page,
832 dev->sector, tx);
833 rbi = r5_next_bio(rbi, dev->sector);
834 }
835 }
836 }
837
838 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700839 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
840 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700841}
842
Dan Williams4e7d2c02009-08-29 19:13:11 -0700843static void mark_target_uptodate(struct stripe_head *sh, int target)
844{
845 struct r5dev *tgt;
846
847 if (target < 0)
848 return;
849
850 tgt = &sh->dev[target];
851 set_bit(R5_UPTODATE, &tgt->flags);
852 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
853 clear_bit(R5_Wantcompute, &tgt->flags);
854}
855
Dan Williamsac6b53b2009-07-14 13:40:19 -0700856static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700857{
858 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700859
Harvey Harrisone46b2722008-04-28 02:15:50 -0700860 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700861 (unsigned long long)sh->sector);
862
Dan Williamsac6b53b2009-07-14 13:40:19 -0700863 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700864 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700865 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700866
Dan Williamsecc65c92008-06-28 08:31:57 +1000867 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
868 if (sh->check_state == check_state_compute_run)
869 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700870 set_bit(STRIPE_HANDLE, &sh->state);
871 release_stripe(sh);
872}
873
Dan Williamsd6f38f32009-07-14 11:50:52 -0700874/* return a pointer to the address conversion region of the scribble buffer */
875static addr_conv_t *to_addr_conv(struct stripe_head *sh,
876 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700877{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700878 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
879}
880
881static struct dma_async_tx_descriptor *
882ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
883{
Dan Williams91c00922007-01-02 13:52:30 -0700884 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700885 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700886 int target = sh->ops.target;
887 struct r5dev *tgt = &sh->dev[target];
888 struct page *xor_dest = tgt->page;
889 int count = 0;
890 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700891 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700892 int i;
893
894 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700895 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700896 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
897
898 for (i = disks; i--; )
899 if (i != target)
900 xor_srcs[count++] = sh->dev[i].page;
901
902 atomic_inc(&sh->count);
903
Dan Williams0403e382009-09-08 17:42:50 -0700904 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700905 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700906 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700907 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700908 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700909 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700910
Dan Williams91c00922007-01-02 13:52:30 -0700911 return tx;
912}
913
Dan Williamsac6b53b2009-07-14 13:40:19 -0700914/* set_syndrome_sources - populate source buffers for gen_syndrome
915 * @srcs - (struct page *) array of size sh->disks
916 * @sh - stripe_head to parse
917 *
918 * Populates srcs in proper layout order for the stripe and returns the
919 * 'count' of sources to be used in a call to async_gen_syndrome. The P
920 * destination buffer is recorded in srcs[count] and the Q destination
921 * is recorded in srcs[count+1]].
922 */
923static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
924{
925 int disks = sh->disks;
926 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
927 int d0_idx = raid6_d0(sh);
928 int count;
929 int i;
930
931 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100932 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700933
934 count = 0;
935 i = d0_idx;
936 do {
937 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
938
939 srcs[slot] = sh->dev[i].page;
940 i = raid6_next_disk(i, disks);
941 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700942
NeilBrowne4424fe2009-10-16 16:27:34 +1100943 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700944}
945
946static struct dma_async_tx_descriptor *
947ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
948{
949 int disks = sh->disks;
950 struct page **blocks = percpu->scribble;
951 int target;
952 int qd_idx = sh->qd_idx;
953 struct dma_async_tx_descriptor *tx;
954 struct async_submit_ctl submit;
955 struct r5dev *tgt;
956 struct page *dest;
957 int i;
958 int count;
959
960 if (sh->ops.target < 0)
961 target = sh->ops.target2;
962 else if (sh->ops.target2 < 0)
963 target = sh->ops.target;
964 else
965 /* we should only have one valid target */
966 BUG();
967 BUG_ON(target < 0);
968 pr_debug("%s: stripe %llu block: %d\n",
969 __func__, (unsigned long long)sh->sector, target);
970
971 tgt = &sh->dev[target];
972 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
973 dest = tgt->page;
974
975 atomic_inc(&sh->count);
976
977 if (target == qd_idx) {
978 count = set_syndrome_sources(blocks, sh);
979 blocks[count] = NULL; /* regenerating p is not necessary */
980 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700981 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
982 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700983 to_addr_conv(sh, percpu));
984 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
985 } else {
986 /* Compute any data- or p-drive using XOR */
987 count = 0;
988 for (i = disks; i-- ; ) {
989 if (i == target || i == qd_idx)
990 continue;
991 blocks[count++] = sh->dev[i].page;
992 }
993
Dan Williams0403e382009-09-08 17:42:50 -0700994 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
995 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700996 to_addr_conv(sh, percpu));
997 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
998 }
999
1000 return tx;
1001}
1002
1003static struct dma_async_tx_descriptor *
1004ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1005{
1006 int i, count, disks = sh->disks;
1007 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1008 int d0_idx = raid6_d0(sh);
1009 int faila = -1, failb = -1;
1010 int target = sh->ops.target;
1011 int target2 = sh->ops.target2;
1012 struct r5dev *tgt = &sh->dev[target];
1013 struct r5dev *tgt2 = &sh->dev[target2];
1014 struct dma_async_tx_descriptor *tx;
1015 struct page **blocks = percpu->scribble;
1016 struct async_submit_ctl submit;
1017
1018 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1019 __func__, (unsigned long long)sh->sector, target, target2);
1020 BUG_ON(target < 0 || target2 < 0);
1021 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1022 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1023
Dan Williams6c910a72009-09-16 12:24:54 -07001024 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001025 * slot number conversion for 'faila' and 'failb'
1026 */
1027 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001028 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001029 count = 0;
1030 i = d0_idx;
1031 do {
1032 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1033
1034 blocks[slot] = sh->dev[i].page;
1035
1036 if (i == target)
1037 faila = slot;
1038 if (i == target2)
1039 failb = slot;
1040 i = raid6_next_disk(i, disks);
1041 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001042
1043 BUG_ON(faila == failb);
1044 if (failb < faila)
1045 swap(faila, failb);
1046 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1047 __func__, (unsigned long long)sh->sector, faila, failb);
1048
1049 atomic_inc(&sh->count);
1050
1051 if (failb == syndrome_disks+1) {
1052 /* Q disk is one of the missing disks */
1053 if (faila == syndrome_disks) {
1054 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001055 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1056 ops_complete_compute, sh,
1057 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +11001058 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001059 STRIPE_SIZE, &submit);
1060 } else {
1061 struct page *dest;
1062 int data_target;
1063 int qd_idx = sh->qd_idx;
1064
1065 /* Missing D+Q: recompute D from P, then recompute Q */
1066 if (target == qd_idx)
1067 data_target = target2;
1068 else
1069 data_target = target;
1070
1071 count = 0;
1072 for (i = disks; i-- ; ) {
1073 if (i == data_target || i == qd_idx)
1074 continue;
1075 blocks[count++] = sh->dev[i].page;
1076 }
1077 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001078 init_async_submit(&submit,
1079 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1080 NULL, NULL, NULL,
1081 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001082 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1083 &submit);
1084
1085 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001086 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1087 ops_complete_compute, sh,
1088 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001089 return async_gen_syndrome(blocks, 0, count+2,
1090 STRIPE_SIZE, &submit);
1091 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001092 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001093 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1094 ops_complete_compute, sh,
1095 to_addr_conv(sh, percpu));
1096 if (failb == syndrome_disks) {
1097 /* We're missing D+P. */
1098 return async_raid6_datap_recov(syndrome_disks+2,
1099 STRIPE_SIZE, faila,
1100 blocks, &submit);
1101 } else {
1102 /* We're missing D+D. */
1103 return async_raid6_2data_recov(syndrome_disks+2,
1104 STRIPE_SIZE, faila, failb,
1105 blocks, &submit);
1106 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001107 }
1108}
1109
1110
Dan Williams91c00922007-01-02 13:52:30 -07001111static void ops_complete_prexor(void *stripe_head_ref)
1112{
1113 struct stripe_head *sh = stripe_head_ref;
1114
Harvey Harrisone46b2722008-04-28 02:15:50 -07001115 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001116 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001117}
1118
1119static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001120ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1121 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001122{
Dan Williams91c00922007-01-02 13:52:30 -07001123 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001124 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001125 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001126 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001127
1128 /* existing parity data subtracted */
1129 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1130
Harvey Harrisone46b2722008-04-28 02:15:50 -07001131 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001132 (unsigned long long)sh->sector);
1133
1134 for (i = disks; i--; ) {
1135 struct r5dev *dev = &sh->dev[i];
1136 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001137 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001138 xor_srcs[count++] = dev->page;
1139 }
1140
Dan Williams0403e382009-09-08 17:42:50 -07001141 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001142 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001143 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001144
1145 return tx;
1146}
1147
1148static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001149ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001150{
1151 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001152 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001153
Harvey Harrisone46b2722008-04-28 02:15:50 -07001154 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001155 (unsigned long long)sh->sector);
1156
1157 for (i = disks; i--; ) {
1158 struct r5dev *dev = &sh->dev[i];
1159 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001160
Dan Williamsd8ee0722008-06-28 08:32:06 +10001161 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001162 struct bio *wbi;
1163
Shaohua Lib17459c2012-07-19 16:01:31 +10001164 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001165 chosen = dev->towrite;
1166 dev->towrite = NULL;
1167 BUG_ON(dev->written);
1168 wbi = dev->written = chosen;
Shaohua Lib17459c2012-07-19 16:01:31 +10001169 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001170
1171 while (wbi && wbi->bi_sector <
1172 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001173 if (wbi->bi_rw & REQ_FUA)
1174 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001175 if (wbi->bi_rw & REQ_SYNC)
1176 set_bit(R5_SyncIO, &dev->flags);
Dan Williams91c00922007-01-02 13:52:30 -07001177 tx = async_copy_data(1, wbi, dev->page,
1178 dev->sector, tx);
1179 wbi = r5_next_bio(wbi, dev->sector);
1180 }
1181 }
1182 }
1183
1184 return tx;
1185}
1186
Dan Williamsac6b53b2009-07-14 13:40:19 -07001187static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001188{
1189 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001190 int disks = sh->disks;
1191 int pd_idx = sh->pd_idx;
1192 int qd_idx = sh->qd_idx;
1193 int i;
Shaohua Libc0934f2012-05-22 13:55:05 +10001194 bool fua = false, sync = false;
Dan Williams91c00922007-01-02 13:52:30 -07001195
Harvey Harrisone46b2722008-04-28 02:15:50 -07001196 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001197 (unsigned long long)sh->sector);
1198
Shaohua Libc0934f2012-05-22 13:55:05 +10001199 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001200 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001201 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
1202 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001203
Dan Williams91c00922007-01-02 13:52:30 -07001204 for (i = disks; i--; ) {
1205 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001206
Tejun Heoe9c74692010-09-03 11:56:18 +02001207 if (dev->written || i == pd_idx || i == qd_idx) {
Dan Williams91c00922007-01-02 13:52:30 -07001208 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001209 if (fua)
1210 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001211 if (sync)
1212 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001213 }
Dan Williams91c00922007-01-02 13:52:30 -07001214 }
1215
Dan Williamsd8ee0722008-06-28 08:32:06 +10001216 if (sh->reconstruct_state == reconstruct_state_drain_run)
1217 sh->reconstruct_state = reconstruct_state_drain_result;
1218 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1219 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1220 else {
1221 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1222 sh->reconstruct_state = reconstruct_state_result;
1223 }
Dan Williams91c00922007-01-02 13:52:30 -07001224
1225 set_bit(STRIPE_HANDLE, &sh->state);
1226 release_stripe(sh);
1227}
1228
1229static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001230ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1231 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001232{
Dan Williams91c00922007-01-02 13:52:30 -07001233 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001234 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001235 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001236 int count = 0, pd_idx = sh->pd_idx, i;
1237 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001238 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001239 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001240
Harvey Harrisone46b2722008-04-28 02:15:50 -07001241 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001242 (unsigned long long)sh->sector);
1243
1244 /* check if prexor is active which means only process blocks
1245 * that are part of a read-modify-write (written)
1246 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001247 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1248 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001249 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1250 for (i = disks; i--; ) {
1251 struct r5dev *dev = &sh->dev[i];
1252 if (dev->written)
1253 xor_srcs[count++] = dev->page;
1254 }
1255 } else {
1256 xor_dest = sh->dev[pd_idx].page;
1257 for (i = disks; i--; ) {
1258 struct r5dev *dev = &sh->dev[i];
1259 if (i != pd_idx)
1260 xor_srcs[count++] = dev->page;
1261 }
1262 }
1263
Dan Williams91c00922007-01-02 13:52:30 -07001264 /* 1/ if we prexor'd then the dest is reused as a source
1265 * 2/ if we did not prexor then we are redoing the parity
1266 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1267 * for the synchronous xor case
1268 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001269 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001270 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1271
1272 atomic_inc(&sh->count);
1273
Dan Williamsac6b53b2009-07-14 13:40:19 -07001274 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001275 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001276 if (unlikely(count == 1))
1277 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1278 else
1279 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001280}
1281
Dan Williamsac6b53b2009-07-14 13:40:19 -07001282static void
1283ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1284 struct dma_async_tx_descriptor *tx)
1285{
1286 struct async_submit_ctl submit;
1287 struct page **blocks = percpu->scribble;
1288 int count;
1289
1290 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1291
1292 count = set_syndrome_sources(blocks, sh);
1293
1294 atomic_inc(&sh->count);
1295
1296 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1297 sh, to_addr_conv(sh, percpu));
1298 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001299}
1300
1301static void ops_complete_check(void *stripe_head_ref)
1302{
1303 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001304
Harvey Harrisone46b2722008-04-28 02:15:50 -07001305 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001306 (unsigned long long)sh->sector);
1307
Dan Williamsecc65c92008-06-28 08:31:57 +10001308 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001309 set_bit(STRIPE_HANDLE, &sh->state);
1310 release_stripe(sh);
1311}
1312
Dan Williamsac6b53b2009-07-14 13:40:19 -07001313static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001314{
Dan Williams91c00922007-01-02 13:52:30 -07001315 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001316 int pd_idx = sh->pd_idx;
1317 int qd_idx = sh->qd_idx;
1318 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001319 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001320 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001321 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001322 int count;
1323 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001324
Harvey Harrisone46b2722008-04-28 02:15:50 -07001325 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001326 (unsigned long long)sh->sector);
1327
Dan Williamsac6b53b2009-07-14 13:40:19 -07001328 count = 0;
1329 xor_dest = sh->dev[pd_idx].page;
1330 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001331 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001332 if (i == pd_idx || i == qd_idx)
1333 continue;
1334 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001335 }
1336
Dan Williamsd6f38f32009-07-14 11:50:52 -07001337 init_async_submit(&submit, 0, NULL, NULL, NULL,
1338 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001339 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001340 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001341
Dan Williams91c00922007-01-02 13:52:30 -07001342 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001343 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1344 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001345}
1346
Dan Williamsac6b53b2009-07-14 13:40:19 -07001347static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1348{
1349 struct page **srcs = percpu->scribble;
1350 struct async_submit_ctl submit;
1351 int count;
1352
1353 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1354 (unsigned long long)sh->sector, checkp);
1355
1356 count = set_syndrome_sources(srcs, sh);
1357 if (!checkp)
1358 srcs[count] = NULL;
1359
1360 atomic_inc(&sh->count);
1361 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1362 sh, to_addr_conv(sh, percpu));
1363 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1364 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1365}
1366
Dan Williams417b8d42009-10-16 16:25:22 +11001367static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001368{
1369 int overlap_clear = 0, i, disks = sh->disks;
1370 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001371 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001372 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001373 struct raid5_percpu *percpu;
1374 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001375
Dan Williamsd6f38f32009-07-14 11:50:52 -07001376 cpu = get_cpu();
1377 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001378 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001379 ops_run_biofill(sh);
1380 overlap_clear++;
1381 }
1382
Dan Williams7b3a8712008-06-28 08:32:09 +10001383 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001384 if (level < 6)
1385 tx = ops_run_compute5(sh, percpu);
1386 else {
1387 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1388 tx = ops_run_compute6_1(sh, percpu);
1389 else
1390 tx = ops_run_compute6_2(sh, percpu);
1391 }
1392 /* terminate the chain if reconstruct is not set to be run */
1393 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001394 async_tx_ack(tx);
1395 }
Dan Williams91c00922007-01-02 13:52:30 -07001396
Dan Williams600aa102008-06-28 08:32:05 +10001397 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001398 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001399
Dan Williams600aa102008-06-28 08:32:05 +10001400 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001401 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001402 overlap_clear++;
1403 }
1404
Dan Williamsac6b53b2009-07-14 13:40:19 -07001405 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1406 if (level < 6)
1407 ops_run_reconstruct5(sh, percpu, tx);
1408 else
1409 ops_run_reconstruct6(sh, percpu, tx);
1410 }
Dan Williams91c00922007-01-02 13:52:30 -07001411
Dan Williamsac6b53b2009-07-14 13:40:19 -07001412 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1413 if (sh->check_state == check_state_run)
1414 ops_run_check_p(sh, percpu);
1415 else if (sh->check_state == check_state_run_q)
1416 ops_run_check_pq(sh, percpu, 0);
1417 else if (sh->check_state == check_state_run_pq)
1418 ops_run_check_pq(sh, percpu, 1);
1419 else
1420 BUG();
1421 }
Dan Williams91c00922007-01-02 13:52:30 -07001422
Dan Williams91c00922007-01-02 13:52:30 -07001423 if (overlap_clear)
1424 for (i = disks; i--; ) {
1425 struct r5dev *dev = &sh->dev[i];
1426 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1427 wake_up(&sh->raid_conf->wait_for_overlap);
1428 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001429 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001430}
1431
Dan Williams417b8d42009-10-16 16:25:22 +11001432#ifdef CONFIG_MULTICORE_RAID456
1433static void async_run_ops(void *param, async_cookie_t cookie)
1434{
1435 struct stripe_head *sh = param;
1436 unsigned long ops_request = sh->ops.request;
1437
1438 clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
1439 wake_up(&sh->ops.wait_for_ops);
1440
1441 __raid_run_ops(sh, ops_request);
1442 release_stripe(sh);
1443}
1444
1445static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1446{
1447 /* since handle_stripe can be called outside of raid5d context
1448 * we need to ensure sh->ops.request is de-staged before another
1449 * request arrives
1450 */
1451 wait_event(sh->ops.wait_for_ops,
1452 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
1453 sh->ops.request = ops_request;
1454
1455 atomic_inc(&sh->count);
1456 async_schedule(async_run_ops, sh);
1457}
1458#else
1459#define raid_run_ops __raid_run_ops
1460#endif
1461
NeilBrownd1688a62011-10-11 16:49:52 +11001462static int grow_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
1464 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001465 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001466 if (!sh)
1467 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001468
NeilBrown3f294f42005-11-08 21:39:25 -08001469 sh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001470 #ifdef CONFIG_MULTICORE_RAID456
1471 init_waitqueue_head(&sh->ops.wait_for_ops);
1472 #endif
NeilBrown3f294f42005-11-08 21:39:25 -08001473
Shaohua Lib17459c2012-07-19 16:01:31 +10001474 spin_lock_init(&sh->stripe_lock);
1475
NeilBrowne4e11e32010-06-16 16:45:16 +10001476 if (grow_buffers(sh)) {
1477 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001478 kmem_cache_free(conf->slab_cache, sh);
1479 return 0;
1480 }
1481 /* we just created an active stripe so... */
1482 atomic_set(&sh->count, 1);
1483 atomic_inc(&conf->active_stripes);
1484 INIT_LIST_HEAD(&sh->lru);
1485 release_stripe(sh);
1486 return 1;
1487}
1488
NeilBrownd1688a62011-10-11 16:49:52 +11001489static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001490{
Christoph Lametere18b8902006-12-06 20:33:20 -08001491 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001492 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
NeilBrownf4be6b42010-06-01 19:37:25 +10001494 if (conf->mddev->gendisk)
1495 sprintf(conf->cache_name[0],
1496 "raid%d-%s", conf->level, mdname(conf->mddev));
1497 else
1498 sprintf(conf->cache_name[0],
1499 "raid%d-%p", conf->level, conf->mddev);
1500 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1501
NeilBrownad01c9e2006-03-27 01:18:07 -08001502 conf->active_name = 0;
1503 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001505 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 if (!sc)
1507 return 1;
1508 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001509 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001510 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001511 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 return 0;
1514}
NeilBrown29269552006-03-27 01:18:10 -08001515
Dan Williamsd6f38f32009-07-14 11:50:52 -07001516/**
1517 * scribble_len - return the required size of the scribble region
1518 * @num - total number of disks in the array
1519 *
1520 * The size must be enough to contain:
1521 * 1/ a struct page pointer for each device in the array +2
1522 * 2/ room to convert each entry in (1) to its corresponding dma
1523 * (dma_map_page()) or page (page_address()) address.
1524 *
1525 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1526 * calculate over all devices (not just the data blocks), using zeros in place
1527 * of the P and Q blocks.
1528 */
1529static size_t scribble_len(int num)
1530{
1531 size_t len;
1532
1533 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1534
1535 return len;
1536}
1537
NeilBrownd1688a62011-10-11 16:49:52 +11001538static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001539{
1540 /* Make all the stripes able to hold 'newsize' devices.
1541 * New slots in each stripe get 'page' set to a new page.
1542 *
1543 * This happens in stages:
1544 * 1/ create a new kmem_cache and allocate the required number of
1545 * stripe_heads.
1546 * 2/ gather all the old stripe_heads and tranfer the pages across
1547 * to the new stripe_heads. This will have the side effect of
1548 * freezing the array as once all stripe_heads have been collected,
1549 * no IO will be possible. Old stripe heads are freed once their
1550 * pages have been transferred over, and the old kmem_cache is
1551 * freed when all stripes are done.
1552 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1553 * we simple return a failre status - no need to clean anything up.
1554 * 4/ allocate new pages for the new slots in the new stripe_heads.
1555 * If this fails, we don't bother trying the shrink the
1556 * stripe_heads down again, we just leave them as they are.
1557 * As each stripe_head is processed the new one is released into
1558 * active service.
1559 *
1560 * Once step2 is started, we cannot afford to wait for a write,
1561 * so we use GFP_NOIO allocations.
1562 */
1563 struct stripe_head *osh, *nsh;
1564 LIST_HEAD(newstripes);
1565 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001566 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001567 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001568 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001569 int i;
1570
1571 if (newsize <= conf->pool_size)
1572 return 0; /* never bother to shrink */
1573
Dan Williamsb5470dc2008-06-27 21:44:04 -07001574 err = md_allow_write(conf->mddev);
1575 if (err)
1576 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001577
NeilBrownad01c9e2006-03-27 01:18:07 -08001578 /* Step 1 */
1579 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1580 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001581 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001582 if (!sc)
1583 return -ENOMEM;
1584
1585 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001586 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001587 if (!nsh)
1588 break;
1589
NeilBrownad01c9e2006-03-27 01:18:07 -08001590 nsh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001591 #ifdef CONFIG_MULTICORE_RAID456
1592 init_waitqueue_head(&nsh->ops.wait_for_ops);
1593 #endif
NeilBrownad01c9e2006-03-27 01:18:07 -08001594
1595 list_add(&nsh->lru, &newstripes);
1596 }
1597 if (i) {
1598 /* didn't get enough, give up */
1599 while (!list_empty(&newstripes)) {
1600 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1601 list_del(&nsh->lru);
1602 kmem_cache_free(sc, nsh);
1603 }
1604 kmem_cache_destroy(sc);
1605 return -ENOMEM;
1606 }
1607 /* Step 2 - Must use GFP_NOIO now.
1608 * OK, we have enough stripes, start collecting inactive
1609 * stripes and copying them over
1610 */
1611 list_for_each_entry(nsh, &newstripes, lru) {
1612 spin_lock_irq(&conf->device_lock);
1613 wait_event_lock_irq(conf->wait_for_stripe,
1614 !list_empty(&conf->inactive_list),
1615 conf->device_lock,
NeilBrown482c0832011-04-18 18:25:42 +10001616 );
NeilBrownad01c9e2006-03-27 01:18:07 -08001617 osh = get_free_stripe(conf);
1618 spin_unlock_irq(&conf->device_lock);
1619 atomic_set(&nsh->count, 1);
1620 for(i=0; i<conf->pool_size; i++)
1621 nsh->dev[i].page = osh->dev[i].page;
1622 for( ; i<newsize; i++)
1623 nsh->dev[i].page = NULL;
1624 kmem_cache_free(conf->slab_cache, osh);
1625 }
1626 kmem_cache_destroy(conf->slab_cache);
1627
1628 /* Step 3.
1629 * At this point, we are holding all the stripes so the array
1630 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001631 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001632 */
1633 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1634 if (ndisks) {
1635 for (i=0; i<conf->raid_disks; i++)
1636 ndisks[i] = conf->disks[i];
1637 kfree(conf->disks);
1638 conf->disks = ndisks;
1639 } else
1640 err = -ENOMEM;
1641
Dan Williamsd6f38f32009-07-14 11:50:52 -07001642 get_online_cpus();
1643 conf->scribble_len = scribble_len(newsize);
1644 for_each_present_cpu(cpu) {
1645 struct raid5_percpu *percpu;
1646 void *scribble;
1647
1648 percpu = per_cpu_ptr(conf->percpu, cpu);
1649 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1650
1651 if (scribble) {
1652 kfree(percpu->scribble);
1653 percpu->scribble = scribble;
1654 } else {
1655 err = -ENOMEM;
1656 break;
1657 }
1658 }
1659 put_online_cpus();
1660
NeilBrownad01c9e2006-03-27 01:18:07 -08001661 /* Step 4, return new stripes to service */
1662 while(!list_empty(&newstripes)) {
1663 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1664 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001665
NeilBrownad01c9e2006-03-27 01:18:07 -08001666 for (i=conf->raid_disks; i < newsize; i++)
1667 if (nsh->dev[i].page == NULL) {
1668 struct page *p = alloc_page(GFP_NOIO);
1669 nsh->dev[i].page = p;
1670 if (!p)
1671 err = -ENOMEM;
1672 }
1673 release_stripe(nsh);
1674 }
1675 /* critical section pass, GFP_NOIO no longer needed */
1676
1677 conf->slab_cache = sc;
1678 conf->active_name = 1-conf->active_name;
1679 conf->pool_size = newsize;
1680 return err;
1681}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
NeilBrownd1688a62011-10-11 16:49:52 +11001683static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
1685 struct stripe_head *sh;
1686
NeilBrown3f294f42005-11-08 21:39:25 -08001687 spin_lock_irq(&conf->device_lock);
1688 sh = get_free_stripe(conf);
1689 spin_unlock_irq(&conf->device_lock);
1690 if (!sh)
1691 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001692 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001693 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001694 kmem_cache_free(conf->slab_cache, sh);
1695 atomic_dec(&conf->active_stripes);
1696 return 1;
1697}
1698
NeilBrownd1688a62011-10-11 16:49:52 +11001699static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001700{
1701 while (drop_one_stripe(conf))
1702 ;
1703
NeilBrown29fc7e32006-02-03 03:03:41 -08001704 if (conf->slab_cache)
1705 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 conf->slab_cache = NULL;
1707}
1708
NeilBrown6712ecf2007-09-27 12:47:43 +02001709static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
NeilBrown99c0fb52009-03-31 14:39:38 +11001711 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001712 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001713 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001715 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11001716 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10001717 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
1719 for (i=0 ; i<disks; i++)
1720 if (bi == &sh->dev[i].req)
1721 break;
1722
Dan Williams45b42332007-07-09 11:56:43 -07001723 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1724 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 uptodate);
1726 if (i == disks) {
1727 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001728 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 }
NeilBrown14a75d32011-12-23 10:17:52 +11001730 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11001731 /* If replacement finished while this request was outstanding,
1732 * 'replacement' might be NULL already.
1733 * In that case it moved down to 'rdev'.
1734 * rdev is not removed until all requests are finished.
1735 */
NeilBrown14a75d32011-12-23 10:17:52 +11001736 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001737 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11001738 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
NeilBrown05616be2012-05-21 09:27:00 +10001740 if (use_new_offset(conf, sh))
1741 s = sh->sector + rdev->new_data_offset;
1742 else
1743 s = sh->sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001746 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11001747 /* Note that this cannot happen on a
1748 * replacement device. We just fail those on
1749 * any error
1750 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001751 printk_ratelimited(
1752 KERN_INFO
1753 "md/raid:%s: read error corrected"
1754 " (%lu sectors at %llu on %s)\n",
1755 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10001756 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001757 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001758 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001759 clear_bit(R5_ReadError, &sh->dev[i].flags);
1760 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng3f9e7c12012-07-31 10:04:21 +10001761 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
1762 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1763
NeilBrown14a75d32011-12-23 10:17:52 +11001764 if (atomic_read(&rdev->read_errors))
1765 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11001767 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001768 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10001769 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001770
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001772 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11001773 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1774 printk_ratelimited(
1775 KERN_WARNING
1776 "md/raid:%s: read error on replacement device "
1777 "(sector %llu on %s).\n",
1778 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001779 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11001780 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001781 else if (conf->mddev->degraded >= conf->max_degraded) {
1782 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001783 printk_ratelimited(
1784 KERN_WARNING
1785 "md/raid:%s: read error not correctable "
1786 "(sector %llu on %s).\n",
1787 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001788 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001789 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001790 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08001791 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10001792 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001793 printk_ratelimited(
1794 KERN_WARNING
1795 "md/raid:%s: read error NOT corrected!! "
1796 "(sector %llu on %s).\n",
1797 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001798 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001799 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001800 } else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001801 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001802 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001803 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001804 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001805 else
1806 retry = 1;
1807 if (retry)
majianpeng3f9e7c12012-07-31 10:04:21 +10001808 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
1809 set_bit(R5_ReadError, &sh->dev[i].flags);
1810 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1811 } else
1812 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -08001813 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001814 clear_bit(R5_ReadError, &sh->dev[i].flags);
1815 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10001816 if (!(set_bad
1817 && test_bit(In_sync, &rdev->flags)
1818 && rdev_set_badblocks(
1819 rdev, sh->sector, STRIPE_SECTORS, 0)))
1820 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 }
NeilBrown14a75d32011-12-23 10:17:52 +11001823 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1825 set_bit(STRIPE_HANDLE, &sh->state);
1826 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827}
1828
NeilBrownd710e132008-10-13 11:55:12 +11001829static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830{
NeilBrown99c0fb52009-03-31 14:39:38 +11001831 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001832 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001833 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11001834 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001836 sector_t first_bad;
1837 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11001838 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
NeilBrown977df362011-12-23 10:17:53 +11001840 for (i = 0 ; i < disks; i++) {
1841 if (bi == &sh->dev[i].req) {
1842 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 break;
NeilBrown977df362011-12-23 10:17:53 +11001844 }
1845 if (bi == &sh->dev[i].rreq) {
1846 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001847 if (rdev)
1848 replacement = 1;
1849 else
1850 /* rdev was removed and 'replacement'
1851 * replaced it. rdev is not removed
1852 * until all requests are finished.
1853 */
1854 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11001855 break;
1856 }
1857 }
Dan Williams45b42332007-07-09 11:56:43 -07001858 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1860 uptodate);
1861 if (i == disks) {
1862 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001863 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 }
1865
NeilBrown977df362011-12-23 10:17:53 +11001866 if (replacement) {
1867 if (!uptodate)
1868 md_error(conf->mddev, rdev);
1869 else if (is_badblock(rdev, sh->sector,
1870 STRIPE_SECTORS,
1871 &first_bad, &bad_sectors))
1872 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1873 } else {
1874 if (!uptodate) {
1875 set_bit(WriteErrorSeen, &rdev->flags);
1876 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11001877 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1878 set_bit(MD_RECOVERY_NEEDED,
1879 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11001880 } else if (is_badblock(rdev, sh->sector,
1881 STRIPE_SECTORS,
1882 &first_bad, &bad_sectors))
1883 set_bit(R5_MadeGood, &sh->dev[i].flags);
1884 }
1885 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
NeilBrown977df362011-12-23 10:17:53 +11001887 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1888 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001890 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891}
1892
NeilBrown784052e2009-03-31 15:19:07 +11001893static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
NeilBrown784052e2009-03-31 15:19:07 +11001895static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896{
1897 struct r5dev *dev = &sh->dev[i];
1898
1899 bio_init(&dev->req);
1900 dev->req.bi_io_vec = &dev->vec;
1901 dev->req.bi_vcnt++;
1902 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11001904 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
NeilBrown977df362011-12-23 10:17:53 +11001906 bio_init(&dev->rreq);
1907 dev->rreq.bi_io_vec = &dev->rvec;
1908 dev->rreq.bi_vcnt++;
1909 dev->rreq.bi_max_vecs++;
1910 dev->rreq.bi_private = sh;
1911 dev->rvec.bv_page = dev->page;
1912
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001914 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915}
1916
NeilBrownfd01b882011-10-11 16:47:53 +11001917static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
1919 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11001920 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11001921 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10001922 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
NeilBrown908f4fb2011-12-23 10:17:50 +11001924 spin_lock_irqsave(&conf->device_lock, flags);
1925 clear_bit(In_sync, &rdev->flags);
1926 mddev->degraded = calc_degraded(conf);
1927 spin_unlock_irqrestore(&conf->device_lock, flags);
1928 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1929
NeilBrownde393cd2011-07-28 11:31:48 +10001930 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001931 set_bit(Faulty, &rdev->flags);
1932 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1933 printk(KERN_ALERT
1934 "md/raid:%s: Disk failure on %s, disabling device.\n"
1935 "md/raid:%s: Operation continuing on %d devices.\n",
1936 mdname(mddev),
1937 bdevname(rdev->bdev, b),
1938 mdname(mddev),
1939 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001940}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
1942/*
1943 * Input: a 'big' sector number,
1944 * Output: index of the data and parity disk, and the sector # in them.
1945 */
NeilBrownd1688a62011-10-11 16:49:52 +11001946static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001947 int previous, int *dd_idx,
1948 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001950 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001951 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001953 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001954 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001956 int algorithm = previous ? conf->prev_algo
1957 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001958 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1959 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001960 int raid_disks = previous ? conf->previous_raid_disks
1961 : conf->raid_disks;
1962 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
1964 /* First compute the information on this sector */
1965
1966 /*
1967 * Compute the chunk number and the sector offset inside the chunk
1968 */
1969 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1970 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
1972 /*
1973 * Compute the stripe number
1974 */
NeilBrown35f2a592010-04-20 14:13:34 +10001975 stripe = chunk_number;
1976 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10001977 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 /*
1979 * Select the parity disk based on the user selected algorithm.
1980 */
NeilBrown84789552011-07-27 11:00:36 +10001981 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07001982 switch(conf->level) {
1983 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001984 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001985 break;
1986 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001987 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001989 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001990 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 (*dd_idx)++;
1992 break;
1993 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001994 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001995 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 (*dd_idx)++;
1997 break;
1998 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001999 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002000 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 break;
2002 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002003 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002004 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002006 case ALGORITHM_PARITY_0:
2007 pd_idx = 0;
2008 (*dd_idx)++;
2009 break;
2010 case ALGORITHM_PARITY_N:
2011 pd_idx = data_disks;
2012 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002014 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002015 }
2016 break;
2017 case 6:
2018
NeilBrowne183eae2009-03-31 15:20:22 +11002019 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002020 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002021 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002022 qd_idx = pd_idx + 1;
2023 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002024 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002025 qd_idx = 0;
2026 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002027 (*dd_idx) += 2; /* D D P Q D */
2028 break;
2029 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002030 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002031 qd_idx = pd_idx + 1;
2032 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002033 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002034 qd_idx = 0;
2035 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002036 (*dd_idx) += 2; /* D D P Q D */
2037 break;
2038 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002039 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002040 qd_idx = (pd_idx + 1) % raid_disks;
2041 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002042 break;
2043 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002044 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002045 qd_idx = (pd_idx + 1) % raid_disks;
2046 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002047 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002048
2049 case ALGORITHM_PARITY_0:
2050 pd_idx = 0;
2051 qd_idx = 1;
2052 (*dd_idx) += 2;
2053 break;
2054 case ALGORITHM_PARITY_N:
2055 pd_idx = data_disks;
2056 qd_idx = data_disks + 1;
2057 break;
2058
2059 case ALGORITHM_ROTATING_ZERO_RESTART:
2060 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2061 * of blocks for computing Q is different.
2062 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002063 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002064 qd_idx = pd_idx + 1;
2065 if (pd_idx == raid_disks-1) {
2066 (*dd_idx)++; /* Q D D D P */
2067 qd_idx = 0;
2068 } else if (*dd_idx >= pd_idx)
2069 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002070 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002071 break;
2072
2073 case ALGORITHM_ROTATING_N_RESTART:
2074 /* Same a left_asymmetric, by first stripe is
2075 * D D D P Q rather than
2076 * Q D D D P
2077 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002078 stripe2 += 1;
2079 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002080 qd_idx = pd_idx + 1;
2081 if (pd_idx == raid_disks-1) {
2082 (*dd_idx)++; /* Q D D D P */
2083 qd_idx = 0;
2084 } else if (*dd_idx >= pd_idx)
2085 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002086 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002087 break;
2088
2089 case ALGORITHM_ROTATING_N_CONTINUE:
2090 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002091 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002092 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2093 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002094 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002095 break;
2096
2097 case ALGORITHM_LEFT_ASYMMETRIC_6:
2098 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002099 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002100 if (*dd_idx >= pd_idx)
2101 (*dd_idx)++;
2102 qd_idx = raid_disks - 1;
2103 break;
2104
2105 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002106 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002107 if (*dd_idx >= pd_idx)
2108 (*dd_idx)++;
2109 qd_idx = raid_disks - 1;
2110 break;
2111
2112 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002113 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002114 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2115 qd_idx = raid_disks - 1;
2116 break;
2117
2118 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002119 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002120 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2121 qd_idx = raid_disks - 1;
2122 break;
2123
2124 case ALGORITHM_PARITY_0_6:
2125 pd_idx = 0;
2126 (*dd_idx)++;
2127 qd_idx = raid_disks - 1;
2128 break;
2129
NeilBrown16a53ec2006-06-26 00:27:38 -07002130 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002131 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002132 }
2133 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 }
2135
NeilBrown911d4ee2009-03-31 14:39:38 +11002136 if (sh) {
2137 sh->pd_idx = pd_idx;
2138 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002139 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 /*
2142 * Finally, compute the new sector number
2143 */
2144 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2145 return new_sector;
2146}
2147
2148
NeilBrown784052e2009-03-31 15:19:07 +11002149static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150{
NeilBrownd1688a62011-10-11 16:49:52 +11002151 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002152 int raid_disks = sh->disks;
2153 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002155 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2156 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002157 int algorithm = previous ? conf->prev_algo
2158 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 sector_t stripe;
2160 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002161 sector_t chunk_number;
2162 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002164 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
NeilBrown16a53ec2006-06-26 00:27:38 -07002166
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2168 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
NeilBrown16a53ec2006-06-26 00:27:38 -07002170 if (i == sh->pd_idx)
2171 return 0;
2172 switch(conf->level) {
2173 case 4: break;
2174 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002175 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 case ALGORITHM_LEFT_ASYMMETRIC:
2177 case ALGORITHM_RIGHT_ASYMMETRIC:
2178 if (i > sh->pd_idx)
2179 i--;
2180 break;
2181 case ALGORITHM_LEFT_SYMMETRIC:
2182 case ALGORITHM_RIGHT_SYMMETRIC:
2183 if (i < sh->pd_idx)
2184 i += raid_disks;
2185 i -= (sh->pd_idx + 1);
2186 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002187 case ALGORITHM_PARITY_0:
2188 i -= 1;
2189 break;
2190 case ALGORITHM_PARITY_N:
2191 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002193 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002194 }
2195 break;
2196 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002197 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002198 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002199 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002200 case ALGORITHM_LEFT_ASYMMETRIC:
2201 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002202 case ALGORITHM_ROTATING_ZERO_RESTART:
2203 case ALGORITHM_ROTATING_N_RESTART:
2204 if (sh->pd_idx == raid_disks-1)
2205 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002206 else if (i > sh->pd_idx)
2207 i -= 2; /* D D P Q D */
2208 break;
2209 case ALGORITHM_LEFT_SYMMETRIC:
2210 case ALGORITHM_RIGHT_SYMMETRIC:
2211 if (sh->pd_idx == raid_disks-1)
2212 i--; /* Q D D D P */
2213 else {
2214 /* D D P Q D */
2215 if (i < sh->pd_idx)
2216 i += raid_disks;
2217 i -= (sh->pd_idx + 2);
2218 }
2219 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002220 case ALGORITHM_PARITY_0:
2221 i -= 2;
2222 break;
2223 case ALGORITHM_PARITY_N:
2224 break;
2225 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002226 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002227 if (sh->pd_idx == 0)
2228 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002229 else {
2230 /* D D Q P D */
2231 if (i < sh->pd_idx)
2232 i += raid_disks;
2233 i -= (sh->pd_idx + 1);
2234 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002235 break;
2236 case ALGORITHM_LEFT_ASYMMETRIC_6:
2237 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2238 if (i > sh->pd_idx)
2239 i--;
2240 break;
2241 case ALGORITHM_LEFT_SYMMETRIC_6:
2242 case ALGORITHM_RIGHT_SYMMETRIC_6:
2243 if (i < sh->pd_idx)
2244 i += data_disks + 1;
2245 i -= (sh->pd_idx + 1);
2246 break;
2247 case ALGORITHM_PARITY_0_6:
2248 i -= 1;
2249 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002250 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002251 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002252 }
2253 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 }
2255
2256 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002257 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
NeilBrown112bf892009-03-31 14:39:38 +11002259 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002260 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002261 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2262 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002263 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2264 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 return 0;
2266 }
2267 return r_sector;
2268}
2269
2270
Dan Williams600aa102008-06-28 08:32:05 +10002271static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002272schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002273 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002274{
2275 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002276 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002277 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002278
Dan Williamse33129d2007-01-02 13:52:30 -07002279 if (rcw) {
2280 /* if we are not expanding this is a proper write request, and
2281 * there will be bios with new data to be drained into the
2282 * stripe cache
2283 */
2284 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10002285 sh->reconstruct_state = reconstruct_state_drain_run;
2286 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2287 } else
2288 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07002289
Dan Williamsac6b53b2009-07-14 13:40:19 -07002290 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002291
2292 for (i = disks; i--; ) {
2293 struct r5dev *dev = &sh->dev[i];
2294
2295 if (dev->towrite) {
2296 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002297 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002298 if (!expand)
2299 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002300 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002301 }
2302 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002303 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002304 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002305 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002306 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002307 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002308 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2309 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2310
Dan Williamsd8ee0722008-06-28 08:32:06 +10002311 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002312 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2313 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002314 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002315
2316 for (i = disks; i--; ) {
2317 struct r5dev *dev = &sh->dev[i];
2318 if (i == pd_idx)
2319 continue;
2320
Dan Williamse33129d2007-01-02 13:52:30 -07002321 if (dev->towrite &&
2322 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002323 test_bit(R5_Wantcompute, &dev->flags))) {
2324 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002325 set_bit(R5_LOCKED, &dev->flags);
2326 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002327 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002328 }
2329 }
2330 }
2331
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002332 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002333 * are in flight
2334 */
2335 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2336 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002337 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002338
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002339 if (level == 6) {
2340 int qd_idx = sh->qd_idx;
2341 struct r5dev *dev = &sh->dev[qd_idx];
2342
2343 set_bit(R5_LOCKED, &dev->flags);
2344 clear_bit(R5_UPTODATE, &dev->flags);
2345 s->locked++;
2346 }
2347
Dan Williams600aa102008-06-28 08:32:05 +10002348 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002349 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002350 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002351}
NeilBrown16a53ec2006-06-26 00:27:38 -07002352
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353/*
2354 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002355 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 * The bi_next chain must be in order.
2357 */
2358static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2359{
2360 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002361 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002362 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
NeilBrowncbe47ec2011-07-26 11:20:35 +10002364 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 (unsigned long long)bi->bi_sector,
2366 (unsigned long long)sh->sector);
2367
Shaohua Lib17459c2012-07-19 16:01:31 +10002368 /*
2369 * If several bio share a stripe. The bio bi_phys_segments acts as a
2370 * reference count to avoid race. The reference count should already be
2371 * increased before this function is called (for example, in
2372 * make_request()), so other bio sharing this stripe will not free the
2373 * stripe. If a stripe is owned by one stripe, the stripe lock will
2374 * protect it.
2375 */
2376 spin_lock_irq(&sh->stripe_lock);
NeilBrown72626682005-09-09 16:23:54 -07002377 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002379 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07002380 firstwrite = 1;
2381 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 bip = &sh->dev[dd_idx].toread;
2383 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2384 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2385 goto overlap;
2386 bip = & (*bip)->bi_next;
2387 }
2388 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2389 goto overlap;
2390
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002391 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 if (*bip)
2393 bi->bi_next = *bip;
2394 *bip = bi;
Shaohua Lie7836bd62012-07-19 16:01:31 +10002395 raid5_inc_bi_active_stripes(bi);
NeilBrown72626682005-09-09 16:23:54 -07002396
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 if (forwrite) {
2398 /* check if page is covered */
2399 sector_t sector = sh->dev[dd_idx].sector;
2400 for (bi=sh->dev[dd_idx].towrite;
2401 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2402 bi && bi->bi_sector <= sector;
2403 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2404 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2405 sector = bi->bi_sector + (bi->bi_size>>9);
2406 }
2407 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2408 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2409 }
Shaohua Lib17459c2012-07-19 16:01:31 +10002410 spin_unlock_irq(&sh->stripe_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002411
2412 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2413 (unsigned long long)(*bip)->bi_sector,
2414 (unsigned long long)sh->sector, dd_idx);
2415
2416 if (conf->mddev->bitmap && firstwrite) {
2417 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2418 STRIPE_SECTORS, 0);
2419 sh->bm_seq = conf->seq_flush+1;
2420 set_bit(STRIPE_BIT_DELAY, &sh->state);
2421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 return 1;
2423
2424 overlap:
2425 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10002426 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 return 0;
2428}
2429
NeilBrownd1688a62011-10-11 16:49:52 +11002430static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002431
NeilBrownd1688a62011-10-11 16:49:52 +11002432static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002433 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002434{
NeilBrown784052e2009-03-31 15:19:07 +11002435 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002436 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002437 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002438 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002439 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002440
NeilBrown112bf892009-03-31 14:39:38 +11002441 raid5_compute_sector(conf,
2442 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002443 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002444 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002445 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002446}
2447
Dan Williamsa4456852007-07-09 11:56:43 -07002448static void
NeilBrownd1688a62011-10-11 16:49:52 +11002449handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002450 struct stripe_head_state *s, int disks,
2451 struct bio **return_bi)
2452{
2453 int i;
2454 for (i = disks; i--; ) {
2455 struct bio *bi;
2456 int bitmap_end = 0;
2457
2458 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002459 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002460 rcu_read_lock();
2461 rdev = rcu_dereference(conf->disks[i].rdev);
2462 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002463 atomic_inc(&rdev->nr_pending);
2464 else
2465 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002466 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002467 if (rdev) {
2468 if (!rdev_set_badblocks(
2469 rdev,
2470 sh->sector,
2471 STRIPE_SECTORS, 0))
2472 md_error(conf->mddev, rdev);
2473 rdev_dec_pending(rdev, conf->mddev);
2474 }
Dan Williamsa4456852007-07-09 11:56:43 -07002475 }
Shaohua Lib17459c2012-07-19 16:01:31 +10002476 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002477 /* fail all writes first */
2478 bi = sh->dev[i].towrite;
2479 sh->dev[i].towrite = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10002480 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002481 if (bi) {
2482 s->to_write--;
2483 bitmap_end = 1;
2484 }
2485
2486 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2487 wake_up(&conf->wait_for_overlap);
2488
2489 while (bi && bi->bi_sector <
2490 sh->dev[i].sector + STRIPE_SECTORS) {
2491 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2492 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002493 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002494 md_write_end(conf->mddev);
2495 bi->bi_next = *return_bi;
2496 *return_bi = bi;
2497 }
2498 bi = nextbi;
2499 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002500 if (bitmap_end)
2501 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2502 STRIPE_SECTORS, 0, 0);
2503 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002504 /* and fail all 'written' */
2505 bi = sh->dev[i].written;
2506 sh->dev[i].written = NULL;
2507 if (bi) bitmap_end = 1;
2508 while (bi && bi->bi_sector <
2509 sh->dev[i].sector + STRIPE_SECTORS) {
2510 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2511 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002512 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002513 md_write_end(conf->mddev);
2514 bi->bi_next = *return_bi;
2515 *return_bi = bi;
2516 }
2517 bi = bi2;
2518 }
2519
Dan Williamsb5e98d62007-01-02 13:52:31 -07002520 /* fail any reads if this device is non-operational and
2521 * the data has not reached the cache yet.
2522 */
2523 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2524 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2525 test_bit(R5_ReadError, &sh->dev[i].flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002526 bi = sh->dev[i].toread;
2527 sh->dev[i].toread = NULL;
2528 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2529 wake_up(&conf->wait_for_overlap);
2530 if (bi) s->to_read--;
2531 while (bi && bi->bi_sector <
2532 sh->dev[i].sector + STRIPE_SECTORS) {
2533 struct bio *nextbi =
2534 r5_next_bio(bi, sh->dev[i].sector);
2535 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002536 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002537 bi->bi_next = *return_bi;
2538 *return_bi = bi;
2539 }
2540 bi = nextbi;
2541 }
2542 }
Dan Williamsa4456852007-07-09 11:56:43 -07002543 if (bitmap_end)
2544 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2545 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002546 /* If we were in the middle of a write the parity block might
2547 * still be locked - so just clear all R5_LOCKED flags
2548 */
2549 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002550 }
2551
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002552 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2553 if (atomic_dec_and_test(&conf->pending_full_writes))
2554 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002555}
2556
NeilBrown7f0da592011-07-28 11:39:22 +10002557static void
NeilBrownd1688a62011-10-11 16:49:52 +11002558handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002559 struct stripe_head_state *s)
2560{
2561 int abort = 0;
2562 int i;
2563
NeilBrown7f0da592011-07-28 11:39:22 +10002564 clear_bit(STRIPE_SYNCING, &sh->state);
2565 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002566 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002567 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10002568 * Don't even need to abort as that is handled elsewhere
2569 * if needed, and not always wanted e.g. if there is a known
2570 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11002571 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002572 * non-sync devices, or abort the recovery
2573 */
NeilBrown18b98372012-04-01 23:48:38 +10002574 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2575 /* During recovery devices cannot be removed, so
2576 * locking and refcounting of rdevs is not needed
2577 */
2578 for (i = 0; i < conf->raid_disks; i++) {
2579 struct md_rdev *rdev = conf->disks[i].rdev;
2580 if (rdev
2581 && !test_bit(Faulty, &rdev->flags)
2582 && !test_bit(In_sync, &rdev->flags)
2583 && !rdev_set_badblocks(rdev, sh->sector,
2584 STRIPE_SECTORS, 0))
2585 abort = 1;
2586 rdev = conf->disks[i].replacement;
2587 if (rdev
2588 && !test_bit(Faulty, &rdev->flags)
2589 && !test_bit(In_sync, &rdev->flags)
2590 && !rdev_set_badblocks(rdev, sh->sector,
2591 STRIPE_SECTORS, 0))
2592 abort = 1;
2593 }
2594 if (abort)
2595 conf->recovery_disabled =
2596 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10002597 }
NeilBrown18b98372012-04-01 23:48:38 +10002598 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10002599}
2600
NeilBrown9a3e1102011-12-23 10:17:53 +11002601static int want_replace(struct stripe_head *sh, int disk_idx)
2602{
2603 struct md_rdev *rdev;
2604 int rv = 0;
2605 /* Doing recovery so rcu locking not required */
2606 rdev = sh->raid_conf->disks[disk_idx].replacement;
2607 if (rdev
2608 && !test_bit(Faulty, &rdev->flags)
2609 && !test_bit(In_sync, &rdev->flags)
2610 && (rdev->recovery_offset <= sh->sector
2611 || rdev->mddev->recovery_cp <= sh->sector))
2612 rv = 1;
2613
2614 return rv;
2615}
2616
NeilBrown93b3dbc2011-07-27 11:00:36 +10002617/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002618 * to be read or computed to satisfy a request.
2619 *
2620 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002621 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002622 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002623static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2624 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002625{
2626 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002627 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2628 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002629
Dan Williamsf38e1212007-01-02 13:52:30 -07002630 /* is the data in this block needed, and can we get it? */
2631 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002632 !test_bit(R5_UPTODATE, &dev->flags) &&
2633 (dev->toread ||
2634 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2635 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002636 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002637 (s->failed >= 1 && fdev[0]->toread) ||
2638 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002639 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2640 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2641 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002642 /* we would like to get this block, possibly by computing it,
2643 * otherwise read it if the backing disk is insync
2644 */
2645 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2646 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2647 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002648 (s->failed && (disk_idx == s->failed_num[0] ||
2649 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002650 /* have disk failed, and we're requested to fetch it;
2651 * do compute it
2652 */
2653 pr_debug("Computing stripe %llu block %d\n",
2654 (unsigned long long)sh->sector, disk_idx);
2655 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2656 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2657 set_bit(R5_Wantcompute, &dev->flags);
2658 sh->ops.target = disk_idx;
2659 sh->ops.target2 = -1; /* no 2nd target */
2660 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002661 /* Careful: from this point on 'uptodate' is in the eye
2662 * of raid_run_ops which services 'compute' operations
2663 * before writes. R5_Wantcompute flags a block that will
2664 * be R5_UPTODATE by the time it is needed for a
2665 * subsequent operation.
2666 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002667 s->uptodate++;
2668 return 1;
2669 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2670 /* Computing 2-failure is *very* expensive; only
2671 * do it if failed >= 2
2672 */
2673 int other;
2674 for (other = disks; other--; ) {
2675 if (other == disk_idx)
2676 continue;
2677 if (!test_bit(R5_UPTODATE,
2678 &sh->dev[other].flags))
2679 break;
2680 }
2681 BUG_ON(other < 0);
2682 pr_debug("Computing stripe %llu blocks %d,%d\n",
2683 (unsigned long long)sh->sector,
2684 disk_idx, other);
2685 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2686 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2687 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2688 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2689 sh->ops.target = disk_idx;
2690 sh->ops.target2 = other;
2691 s->uptodate += 2;
2692 s->req_compute = 1;
2693 return 1;
2694 } else if (test_bit(R5_Insync, &dev->flags)) {
2695 set_bit(R5_LOCKED, &dev->flags);
2696 set_bit(R5_Wantread, &dev->flags);
2697 s->locked++;
2698 pr_debug("Reading block %d (sync=%d)\n",
2699 disk_idx, s->syncing);
2700 }
2701 }
2702
2703 return 0;
2704}
2705
2706/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002707 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002708 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002709static void handle_stripe_fill(struct stripe_head *sh,
2710 struct stripe_head_state *s,
2711 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002712{
2713 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002714
2715 /* look for blocks to read/compute, skip this if a compute
2716 * is already in flight, or if the stripe contents are in the
2717 * midst of changing due to a write
2718 */
2719 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2720 !sh->reconstruct_state)
2721 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002722 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002723 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002724 set_bit(STRIPE_HANDLE, &sh->state);
2725}
2726
2727
Dan Williams1fe797e2008-06-28 09:16:30 +10002728/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002729 * any written block on an uptodate or failed drive can be returned.
2730 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2731 * never LOCKED, so we don't need to test 'failed' directly.
2732 */
NeilBrownd1688a62011-10-11 16:49:52 +11002733static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002734 struct stripe_head *sh, int disks, struct bio **return_bi)
2735{
2736 int i;
2737 struct r5dev *dev;
2738
2739 for (i = disks; i--; )
2740 if (sh->dev[i].written) {
2741 dev = &sh->dev[i];
2742 if (!test_bit(R5_LOCKED, &dev->flags) &&
2743 test_bit(R5_UPTODATE, &dev->flags)) {
2744 /* We can return any write requests */
2745 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07002746 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002747 wbi = dev->written;
2748 dev->written = NULL;
2749 while (wbi && wbi->bi_sector <
2750 dev->sector + STRIPE_SECTORS) {
2751 wbi2 = r5_next_bio(wbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002752 if (!raid5_dec_bi_active_stripes(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002753 md_write_end(conf->mddev);
2754 wbi->bi_next = *return_bi;
2755 *return_bi = wbi;
2756 }
2757 wbi = wbi2;
2758 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002759 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2760 STRIPE_SECTORS,
Dan Williamsa4456852007-07-09 11:56:43 -07002761 !test_bit(STRIPE_DEGRADED, &sh->state),
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002762 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002763 }
2764 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002765
2766 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2767 if (atomic_dec_and_test(&conf->pending_full_writes))
2768 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002769}
2770
NeilBrownd1688a62011-10-11 16:49:52 +11002771static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002772 struct stripe_head *sh,
2773 struct stripe_head_state *s,
2774 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002775{
2776 int rmw = 0, rcw = 0, i;
NeilBrownc8ac1802011-07-27 11:00:36 +10002777 if (conf->max_degraded == 2) {
2778 /* RAID6 requires 'rcw' in current implementation
2779 * Calculate the real rcw later - for now fake it
2780 * look like rcw is cheaper
2781 */
2782 rcw = 1; rmw = 2;
2783 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002784 /* would I have to read this buffer for read_modify_write */
2785 struct r5dev *dev = &sh->dev[i];
2786 if ((dev->towrite || i == sh->pd_idx) &&
2787 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002788 !(test_bit(R5_UPTODATE, &dev->flags) ||
2789 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002790 if (test_bit(R5_Insync, &dev->flags))
2791 rmw++;
2792 else
2793 rmw += 2*disks; /* cannot read it */
2794 }
2795 /* Would I have to read this buffer for reconstruct_write */
2796 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2797 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002798 !(test_bit(R5_UPTODATE, &dev->flags) ||
2799 test_bit(R5_Wantcompute, &dev->flags))) {
2800 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002801 else
2802 rcw += 2*disks;
2803 }
2804 }
Dan Williams45b42332007-07-09 11:56:43 -07002805 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002806 (unsigned long long)sh->sector, rmw, rcw);
2807 set_bit(STRIPE_HANDLE, &sh->state);
2808 if (rmw < rcw && rmw > 0)
2809 /* prefer read-modify-write, but need to get some data */
2810 for (i = disks; i--; ) {
2811 struct r5dev *dev = &sh->dev[i];
2812 if ((dev->towrite || i == sh->pd_idx) &&
2813 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002814 !(test_bit(R5_UPTODATE, &dev->flags) ||
2815 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002816 test_bit(R5_Insync, &dev->flags)) {
2817 if (
2818 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002819 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002820 "%d for r-m-w\n", i);
2821 set_bit(R5_LOCKED, &dev->flags);
2822 set_bit(R5_Wantread, &dev->flags);
2823 s->locked++;
2824 } else {
2825 set_bit(STRIPE_DELAYED, &sh->state);
2826 set_bit(STRIPE_HANDLE, &sh->state);
2827 }
2828 }
2829 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002830 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002831 /* want reconstruct write, but need to get some data */
NeilBrownc8ac1802011-07-27 11:00:36 +10002832 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002833 for (i = disks; i--; ) {
2834 struct r5dev *dev = &sh->dev[i];
2835 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002836 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002837 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002838 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002839 test_bit(R5_Wantcompute, &dev->flags))) {
2840 rcw++;
2841 if (!test_bit(R5_Insync, &dev->flags))
2842 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002843 if (
2844 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002845 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002846 "%d for Reconstruct\n", i);
2847 set_bit(R5_LOCKED, &dev->flags);
2848 set_bit(R5_Wantread, &dev->flags);
2849 s->locked++;
2850 } else {
2851 set_bit(STRIPE_DELAYED, &sh->state);
2852 set_bit(STRIPE_HANDLE, &sh->state);
2853 }
2854 }
2855 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002856 }
Dan Williamsa4456852007-07-09 11:56:43 -07002857 /* now if nothing is locked, and if we have enough data,
2858 * we can start a write request
2859 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002860 /* since handle_stripe can be called at any time we need to handle the
2861 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002862 * subsequent call wants to start a write request. raid_run_ops only
2863 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002864 * simultaneously. If this is not the case then new writes need to be
2865 * held off until the compute completes.
2866 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002867 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2868 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2869 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002870 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002871}
2872
NeilBrownd1688a62011-10-11 16:49:52 +11002873static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002874 struct stripe_head_state *s, int disks)
2875{
Dan Williamsecc65c92008-06-28 08:31:57 +10002876 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002877
Dan Williamsbd2ab672008-04-10 21:29:27 -07002878 set_bit(STRIPE_HANDLE, &sh->state);
2879
Dan Williamsecc65c92008-06-28 08:31:57 +10002880 switch (sh->check_state) {
2881 case check_state_idle:
2882 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002883 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002884 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002885 sh->check_state = check_state_run;
2886 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002887 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002888 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002889 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002890 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002891 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002892 /* fall through */
2893 case check_state_compute_result:
2894 sh->check_state = check_state_idle;
2895 if (!dev)
2896 dev = &sh->dev[sh->pd_idx];
2897
2898 /* check that a write has not made the stripe insync */
2899 if (test_bit(STRIPE_INSYNC, &sh->state))
2900 break;
2901
2902 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002903 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2904 BUG_ON(s->uptodate != disks);
2905
2906 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002907 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002908 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002909
Dan Williamsa4456852007-07-09 11:56:43 -07002910 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002911 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002912 break;
2913 case check_state_run:
2914 break; /* we will be called again upon completion */
2915 case check_state_check_result:
2916 sh->check_state = check_state_idle;
2917
2918 /* if a failure occurred during the check operation, leave
2919 * STRIPE_INSYNC not set and let the stripe be handled again
2920 */
2921 if (s->failed)
2922 break;
2923
2924 /* handle a successful check operation, if parity is correct
2925 * we are done. Otherwise update the mismatch count and repair
2926 * parity if !MD_RECOVERY_CHECK
2927 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002928 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002929 /* parity is correct (on disc,
2930 * not in buffer any more)
2931 */
2932 set_bit(STRIPE_INSYNC, &sh->state);
2933 else {
2934 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2935 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2936 /* don't try to repair!! */
2937 set_bit(STRIPE_INSYNC, &sh->state);
2938 else {
2939 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002940 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002941 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2942 set_bit(R5_Wantcompute,
2943 &sh->dev[sh->pd_idx].flags);
2944 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002945 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10002946 s->uptodate++;
2947 }
2948 }
2949 break;
2950 case check_state_compute_run:
2951 break;
2952 default:
2953 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2954 __func__, sh->check_state,
2955 (unsigned long long) sh->sector);
2956 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002957 }
2958}
2959
2960
NeilBrownd1688a62011-10-11 16:49:52 +11002961static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07002962 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10002963 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002964{
Dan Williamsa4456852007-07-09 11:56:43 -07002965 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11002966 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07002967 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07002968
2969 set_bit(STRIPE_HANDLE, &sh->state);
2970
2971 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002972
Dan Williamsa4456852007-07-09 11:56:43 -07002973 /* Want to check and possibly repair P and Q.
2974 * However there could be one 'failed' device, in which
2975 * case we can only check one of them, possibly using the
2976 * other to generate missing data
2977 */
2978
Dan Williamsd82dfee2009-07-14 13:40:57 -07002979 switch (sh->check_state) {
2980 case check_state_idle:
2981 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10002982 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002983 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07002984 * makes sense to check P (If anything else were failed,
2985 * we would have used P to recreate it).
2986 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002987 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07002988 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002989 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002990 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07002991 * anything, so it makes sense to check it
2992 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002993 if (sh->check_state == check_state_run)
2994 sh->check_state = check_state_run_pq;
2995 else
2996 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07002997 }
Dan Williams36d1c642009-07-14 11:48:22 -07002998
Dan Williamsd82dfee2009-07-14 13:40:57 -07002999 /* discard potentially stale zero_sum_result */
3000 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07003001
Dan Williamsd82dfee2009-07-14 13:40:57 -07003002 if (sh->check_state == check_state_run) {
3003 /* async_xor_zero_sum destroys the contents of P */
3004 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
3005 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07003006 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003007 if (sh->check_state >= check_state_run &&
3008 sh->check_state <= check_state_run_pq) {
3009 /* async_syndrome_zero_sum preserves P and Q, so
3010 * no need to mark them !uptodate here
3011 */
3012 set_bit(STRIPE_OP_CHECK, &s->ops_request);
3013 break;
3014 }
Dan Williams36d1c642009-07-14 11:48:22 -07003015
Dan Williamsd82dfee2009-07-14 13:40:57 -07003016 /* we have 2-disk failure */
3017 BUG_ON(s->failed != 2);
3018 /* fall through */
3019 case check_state_compute_result:
3020 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07003021
Dan Williamsd82dfee2009-07-14 13:40:57 -07003022 /* check that a write has not made the stripe insync */
3023 if (test_bit(STRIPE_INSYNC, &sh->state))
3024 break;
Dan Williamsa4456852007-07-09 11:56:43 -07003025
3026 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07003027 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07003028 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003029 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07003030 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003031 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07003032 s->locked++;
3033 set_bit(R5_LOCKED, &dev->flags);
3034 set_bit(R5_Wantwrite, &dev->flags);
3035 }
3036 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003037 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07003038 s->locked++;
3039 set_bit(R5_LOCKED, &dev->flags);
3040 set_bit(R5_Wantwrite, &dev->flags);
3041 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003042 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003043 dev = &sh->dev[pd_idx];
3044 s->locked++;
3045 set_bit(R5_LOCKED, &dev->flags);
3046 set_bit(R5_Wantwrite, &dev->flags);
3047 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003048 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003049 dev = &sh->dev[qd_idx];
3050 s->locked++;
3051 set_bit(R5_LOCKED, &dev->flags);
3052 set_bit(R5_Wantwrite, &dev->flags);
3053 }
3054 clear_bit(STRIPE_DEGRADED, &sh->state);
3055
3056 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003057 break;
3058 case check_state_run:
3059 case check_state_run_q:
3060 case check_state_run_pq:
3061 break; /* we will be called again upon completion */
3062 case check_state_check_result:
3063 sh->check_state = check_state_idle;
3064
3065 /* handle a successful check operation, if parity is correct
3066 * we are done. Otherwise update the mismatch count and repair
3067 * parity if !MD_RECOVERY_CHECK
3068 */
3069 if (sh->ops.zero_sum_result == 0) {
3070 /* both parities are correct */
3071 if (!s->failed)
3072 set_bit(STRIPE_INSYNC, &sh->state);
3073 else {
3074 /* in contrast to the raid5 case we can validate
3075 * parity, but still have a failure to write
3076 * back
3077 */
3078 sh->check_state = check_state_compute_result;
3079 /* Returning at this point means that we may go
3080 * off and bring p and/or q uptodate again so
3081 * we make sure to check zero_sum_result again
3082 * to verify if p or q need writeback
3083 */
3084 }
3085 } else {
3086 conf->mddev->resync_mismatches += STRIPE_SECTORS;
3087 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3088 /* don't try to repair!! */
3089 set_bit(STRIPE_INSYNC, &sh->state);
3090 else {
3091 int *target = &sh->ops.target;
3092
3093 sh->ops.target = -1;
3094 sh->ops.target2 = -1;
3095 sh->check_state = check_state_compute_run;
3096 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3097 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3098 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3099 set_bit(R5_Wantcompute,
3100 &sh->dev[pd_idx].flags);
3101 *target = pd_idx;
3102 target = &sh->ops.target2;
3103 s->uptodate++;
3104 }
3105 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3106 set_bit(R5_Wantcompute,
3107 &sh->dev[qd_idx].flags);
3108 *target = qd_idx;
3109 s->uptodate++;
3110 }
3111 }
3112 }
3113 break;
3114 case check_state_compute_run:
3115 break;
3116 default:
3117 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3118 __func__, sh->check_state,
3119 (unsigned long long) sh->sector);
3120 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003121 }
3122}
3123
NeilBrownd1688a62011-10-11 16:49:52 +11003124static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003125{
3126 int i;
3127
3128 /* We have read all the blocks in this stripe and now we need to
3129 * copy some of them into a target stripe for expand.
3130 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003131 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003132 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3133 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003134 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003135 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003136 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003137 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003138
NeilBrown784052e2009-03-31 15:19:07 +11003139 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003140 sector_t s = raid5_compute_sector(conf, bn, 0,
3141 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003142 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003143 if (sh2 == NULL)
3144 /* so far only the early blocks of this stripe
3145 * have been requested. When later blocks
3146 * get requested, we will try again
3147 */
3148 continue;
3149 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3150 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3151 /* must have already done this block */
3152 release_stripe(sh2);
3153 continue;
3154 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003155
3156 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003157 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003158 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003159 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003160 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003161
Dan Williamsa4456852007-07-09 11:56:43 -07003162 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3163 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3164 for (j = 0; j < conf->raid_disks; j++)
3165 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003166 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003167 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3168 break;
3169 if (j == conf->raid_disks) {
3170 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3171 set_bit(STRIPE_HANDLE, &sh2->state);
3172 }
3173 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003174
Dan Williamsa4456852007-07-09 11:56:43 -07003175 }
NeilBrowna2e08552007-09-11 15:23:36 -07003176 /* done submitting copies, wait for them to complete */
3177 if (tx) {
3178 async_tx_ack(tx);
3179 dma_wait_for_async_tx(tx);
3180 }
Dan Williamsa4456852007-07-09 11:56:43 -07003181}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
3183/*
3184 * handle_stripe - do things to a stripe.
3185 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003186 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3187 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003189 * return some read requests which now have data
3190 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 * schedule a read on some buffers
3192 * schedule a write of some buffers
3193 * return confirmation of parity correctness
3194 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 */
Dan Williamsa4456852007-07-09 11:56:43 -07003196
NeilBrownacfe7262011-07-27 11:00:36 +10003197static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003198{
NeilBrownd1688a62011-10-11 16:49:52 +11003199 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003200 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003201 struct r5dev *dev;
3202 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003203 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003204
NeilBrownacfe7262011-07-27 11:00:36 +10003205 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003206
NeilBrownacfe7262011-07-27 11:00:36 +10003207 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3208 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3209 s->failed_num[0] = -1;
3210 s->failed_num[1] = -1;
3211
3212 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003213 rcu_read_lock();
3214 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003215 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003216 sector_t first_bad;
3217 int bad_sectors;
3218 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003219
NeilBrown16a53ec2006-06-26 00:27:38 -07003220 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003221
Dan Williams45b42332007-07-09 11:56:43 -07003222 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003223 i, dev->flags,
3224 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003225 /* maybe we can reply to a read
3226 *
3227 * new wantfill requests are only permitted while
3228 * ops_complete_biofill is guaranteed to be inactive
3229 */
3230 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3231 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3232 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003233
3234 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003235 if (test_bit(R5_LOCKED, &dev->flags))
3236 s->locked++;
3237 if (test_bit(R5_UPTODATE, &dev->flags))
3238 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003239 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003240 s->compute++;
3241 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003242 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003243
NeilBrownacfe7262011-07-27 11:00:36 +10003244 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003245 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003246 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003247 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003248 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003249 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003250 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003251 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003252 }
Dan Williamsa4456852007-07-09 11:56:43 -07003253 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003254 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003255 /* Prefer to use the replacement for reads, but only
3256 * if it is recovered enough and has no bad blocks.
3257 */
3258 rdev = rcu_dereference(conf->disks[i].replacement);
3259 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3260 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3261 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3262 &first_bad, &bad_sectors))
3263 set_bit(R5_ReadRepl, &dev->flags);
3264 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003265 if (rdev)
3266 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003267 rdev = rcu_dereference(conf->disks[i].rdev);
3268 clear_bit(R5_ReadRepl, &dev->flags);
3269 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003270 if (rdev && test_bit(Faulty, &rdev->flags))
3271 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003272 if (rdev) {
3273 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3274 &first_bad, &bad_sectors);
3275 if (s->blocked_rdev == NULL
3276 && (test_bit(Blocked, &rdev->flags)
3277 || is_bad < 0)) {
3278 if (is_bad < 0)
3279 set_bit(BlockedBadBlocks,
3280 &rdev->flags);
3281 s->blocked_rdev = rdev;
3282 atomic_inc(&rdev->nr_pending);
3283 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003284 }
NeilBrown415e72d2010-06-17 17:25:21 +10003285 clear_bit(R5_Insync, &dev->flags);
3286 if (!rdev)
3287 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003288 else if (is_bad) {
3289 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10003290 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3291 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003292 /* treat as in-sync, but with a read error
3293 * which we can now try to correct
3294 */
3295 set_bit(R5_Insync, &dev->flags);
3296 set_bit(R5_ReadError, &dev->flags);
3297 }
3298 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003299 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003300 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003301 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003302 set_bit(R5_Insync, &dev->flags);
3303 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3304 test_bit(R5_Expanded, &dev->flags))
3305 /* If we've reshaped into here, we assume it is Insync.
3306 * We will shortly update recovery_offset to make
3307 * it official.
3308 */
3309 set_bit(R5_Insync, &dev->flags);
3310
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003311 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003312 /* This flag does not apply to '.replacement'
3313 * only to .rdev, so make sure to check that*/
3314 struct md_rdev *rdev2 = rcu_dereference(
3315 conf->disks[i].rdev);
3316 if (rdev2 == rdev)
3317 clear_bit(R5_Insync, &dev->flags);
3318 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003319 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003320 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003321 } else
3322 clear_bit(R5_WriteError, &dev->flags);
3323 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003324 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003325 /* This flag does not apply to '.replacement'
3326 * only to .rdev, so make sure to check that*/
3327 struct md_rdev *rdev2 = rcu_dereference(
3328 conf->disks[i].rdev);
3329 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003330 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003331 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003332 } else
3333 clear_bit(R5_MadeGood, &dev->flags);
3334 }
NeilBrown977df362011-12-23 10:17:53 +11003335 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3336 struct md_rdev *rdev2 = rcu_dereference(
3337 conf->disks[i].replacement);
3338 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3339 s->handle_bad_blocks = 1;
3340 atomic_inc(&rdev2->nr_pending);
3341 } else
3342 clear_bit(R5_MadeGoodRepl, &dev->flags);
3343 }
NeilBrown415e72d2010-06-17 17:25:21 +10003344 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003345 /* The ReadError flag will just be confusing now */
3346 clear_bit(R5_ReadError, &dev->flags);
3347 clear_bit(R5_ReWrite, &dev->flags);
3348 }
NeilBrown415e72d2010-06-17 17:25:21 +10003349 if (test_bit(R5_ReadError, &dev->flags))
3350 clear_bit(R5_Insync, &dev->flags);
3351 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003352 if (s->failed < 2)
3353 s->failed_num[s->failed] = i;
3354 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003355 if (rdev && !test_bit(Faulty, &rdev->flags))
3356 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003357 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003358 }
NeilBrown9a3e1102011-12-23 10:17:53 +11003359 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3360 /* If there is a failed device being replaced,
3361 * we must be recovering.
3362 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10003363 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11003364 * else we can only be replacing
3365 * sync and recovery both need to read all devices, and so
3366 * use the same flag.
3367 */
3368 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10003369 sh->sector >= conf->mddev->recovery_cp ||
3370 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11003371 s->syncing = 1;
3372 else
3373 s->replacing = 1;
3374 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003375 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003376}
NeilBrownf4168852007-02-28 20:11:53 -08003377
NeilBrowncc940152011-07-26 11:35:35 +10003378static void handle_stripe(struct stripe_head *sh)
3379{
3380 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003381 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003382 int i;
NeilBrown84789552011-07-27 11:00:36 +10003383 int prexor;
3384 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003385 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003386
3387 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003388 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003389 /* already being handled, ensure it gets handled
3390 * again when current action finishes */
3391 set_bit(STRIPE_HANDLE, &sh->state);
3392 return;
3393 }
3394
3395 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3396 set_bit(STRIPE_SYNCING, &sh->state);
3397 clear_bit(STRIPE_INSYNC, &sh->state);
3398 }
3399 clear_bit(STRIPE_DELAYED, &sh->state);
3400
3401 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3402 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3403 (unsigned long long)sh->sector, sh->state,
3404 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3405 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003406
NeilBrownacfe7262011-07-27 11:00:36 +10003407 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003408
NeilBrownbc2607f2011-07-28 11:39:22 +10003409 if (s.handle_bad_blocks) {
3410 set_bit(STRIPE_HANDLE, &sh->state);
3411 goto finish;
3412 }
3413
NeilBrown474af965fe2011-07-27 11:00:36 +10003414 if (unlikely(s.blocked_rdev)) {
3415 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003416 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003417 set_bit(STRIPE_HANDLE, &sh->state);
3418 goto finish;
3419 }
3420 /* There is nothing for the blocked_rdev to block */
3421 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3422 s.blocked_rdev = NULL;
3423 }
3424
3425 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3426 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3427 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3428 }
3429
3430 pr_debug("locked=%d uptodate=%d to_read=%d"
3431 " to_write=%d failed=%d failed_num=%d,%d\n",
3432 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3433 s.failed_num[0], s.failed_num[1]);
3434 /* check if the array has lost more than max_degraded devices and,
3435 * if so, some requests might need to be failed.
3436 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003437 if (s.failed > conf->max_degraded) {
3438 sh->check_state = 0;
3439 sh->reconstruct_state = 0;
3440 if (s.to_read+s.to_write+s.written)
3441 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003442 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003443 handle_failed_sync(conf, sh, &s);
3444 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003445
3446 /*
3447 * might be able to return some write requests if the parity blocks
3448 * are safe, or on a failed drive
3449 */
3450 pdev = &sh->dev[sh->pd_idx];
3451 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3452 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3453 qdev = &sh->dev[sh->qd_idx];
3454 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3455 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3456 || conf->level < 6;
3457
3458 if (s.written &&
3459 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3460 && !test_bit(R5_LOCKED, &pdev->flags)
3461 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3462 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3463 && !test_bit(R5_LOCKED, &qdev->flags)
3464 && test_bit(R5_UPTODATE, &qdev->flags)))))
3465 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3466
3467 /* Now we might consider reading some blocks, either to check/generate
3468 * parity, or to satisfy requests
3469 * or to load a block that is being partially written.
3470 */
3471 if (s.to_read || s.non_overwrite
3472 || (conf->level == 6 && s.to_write && s.failed)
NeilBrown9a3e1102011-12-23 10:17:53 +11003473 || (s.syncing && (s.uptodate + s.compute < disks))
3474 || s.replacing
3475 || s.expanding)
NeilBrown474af965fe2011-07-27 11:00:36 +10003476 handle_stripe_fill(sh, &s, disks);
3477
NeilBrown84789552011-07-27 11:00:36 +10003478 /* Now we check to see if any write operations have recently
3479 * completed
3480 */
3481 prexor = 0;
3482 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3483 prexor = 1;
3484 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3485 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3486 sh->reconstruct_state = reconstruct_state_idle;
3487
3488 /* All the 'written' buffers and the parity block are ready to
3489 * be written back to disk
3490 */
3491 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3492 BUG_ON(sh->qd_idx >= 0 &&
3493 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags));
3494 for (i = disks; i--; ) {
3495 struct r5dev *dev = &sh->dev[i];
3496 if (test_bit(R5_LOCKED, &dev->flags) &&
3497 (i == sh->pd_idx || i == sh->qd_idx ||
3498 dev->written)) {
3499 pr_debug("Writing block %d\n", i);
3500 set_bit(R5_Wantwrite, &dev->flags);
3501 if (prexor)
3502 continue;
3503 if (!test_bit(R5_Insync, &dev->flags) ||
3504 ((i == sh->pd_idx || i == sh->qd_idx) &&
3505 s.failed == 0))
3506 set_bit(STRIPE_INSYNC, &sh->state);
3507 }
3508 }
3509 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3510 s.dec_preread_active = 1;
3511 }
3512
3513 /* Now to consider new write requests and what else, if anything
3514 * should be read. We do not handle new writes when:
3515 * 1/ A 'write' operation (copy+xor) is already in flight.
3516 * 2/ A 'check' operation is in flight, as it may clobber the parity
3517 * block.
3518 */
3519 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3520 handle_stripe_dirtying(conf, sh, &s, disks);
3521
3522 /* maybe we need to check and possibly fix the parity for this stripe
3523 * Any reads will already have been scheduled, so we just see if enough
3524 * data is available. The parity check is held off while parity
3525 * dependent operations are in flight.
3526 */
3527 if (sh->check_state ||
3528 (s.syncing && s.locked == 0 &&
3529 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3530 !test_bit(STRIPE_INSYNC, &sh->state))) {
3531 if (conf->level == 6)
3532 handle_parity_checks6(conf, sh, &s, disks);
3533 else
3534 handle_parity_checks5(conf, sh, &s, disks);
3535 }
NeilBrownc5a31002011-07-27 11:00:36 +10003536
NeilBrown9a3e1102011-12-23 10:17:53 +11003537 if (s.replacing && s.locked == 0
3538 && !test_bit(STRIPE_INSYNC, &sh->state)) {
3539 /* Write out to replacement devices where possible */
3540 for (i = 0; i < conf->raid_disks; i++)
3541 if (test_bit(R5_UPTODATE, &sh->dev[i].flags) &&
3542 test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3543 set_bit(R5_WantReplace, &sh->dev[i].flags);
3544 set_bit(R5_LOCKED, &sh->dev[i].flags);
3545 s.locked++;
3546 }
3547 set_bit(STRIPE_INSYNC, &sh->state);
3548 }
3549 if ((s.syncing || s.replacing) && s.locked == 0 &&
3550 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003551 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3552 clear_bit(STRIPE_SYNCING, &sh->state);
3553 }
3554
3555 /* If the failed drives are just a ReadError, then we might need
3556 * to progress the repair/check process
3557 */
3558 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3559 for (i = 0; i < s.failed; i++) {
3560 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3561 if (test_bit(R5_ReadError, &dev->flags)
3562 && !test_bit(R5_LOCKED, &dev->flags)
3563 && test_bit(R5_UPTODATE, &dev->flags)
3564 ) {
3565 if (!test_bit(R5_ReWrite, &dev->flags)) {
3566 set_bit(R5_Wantwrite, &dev->flags);
3567 set_bit(R5_ReWrite, &dev->flags);
3568 set_bit(R5_LOCKED, &dev->flags);
3569 s.locked++;
3570 } else {
3571 /* let's read it back */
3572 set_bit(R5_Wantread, &dev->flags);
3573 set_bit(R5_LOCKED, &dev->flags);
3574 s.locked++;
3575 }
3576 }
3577 }
3578
3579
NeilBrown3687c062011-07-27 11:00:36 +10003580 /* Finish reconstruct operations initiated by the expansion process */
3581 if (sh->reconstruct_state == reconstruct_state_result) {
3582 struct stripe_head *sh_src
3583 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3584 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3585 /* sh cannot be written until sh_src has been read.
3586 * so arrange for sh to be delayed a little
3587 */
3588 set_bit(STRIPE_DELAYED, &sh->state);
3589 set_bit(STRIPE_HANDLE, &sh->state);
3590 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3591 &sh_src->state))
3592 atomic_inc(&conf->preread_active_stripes);
3593 release_stripe(sh_src);
3594 goto finish;
3595 }
3596 if (sh_src)
3597 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003598
NeilBrown3687c062011-07-27 11:00:36 +10003599 sh->reconstruct_state = reconstruct_state_idle;
3600 clear_bit(STRIPE_EXPANDING, &sh->state);
3601 for (i = conf->raid_disks; i--; ) {
3602 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3603 set_bit(R5_LOCKED, &sh->dev[i].flags);
3604 s.locked++;
3605 }
3606 }
3607
3608 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3609 !sh->reconstruct_state) {
3610 /* Need to write out all blocks after computing parity */
3611 sh->disks = conf->raid_disks;
3612 stripe_set_idx(sh->sector, conf, 0, sh);
3613 schedule_reconstruction(sh, &s, 1, 1);
3614 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3615 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3616 atomic_dec(&conf->reshape_stripes);
3617 wake_up(&conf->wait_for_overlap);
3618 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3619 }
3620
3621 if (s.expanding && s.locked == 0 &&
3622 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3623 handle_stripe_expansion(conf, sh);
3624
3625finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003626 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10003627 if (unlikely(s.blocked_rdev)) {
3628 if (conf->mddev->external)
3629 md_wait_for_blocked_rdev(s.blocked_rdev,
3630 conf->mddev);
3631 else
3632 /* Internal metadata will immediately
3633 * be written by raid5d, so we don't
3634 * need to wait here.
3635 */
3636 rdev_dec_pending(s.blocked_rdev,
3637 conf->mddev);
3638 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003639
NeilBrownbc2607f2011-07-28 11:39:22 +10003640 if (s.handle_bad_blocks)
3641 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003642 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003643 struct r5dev *dev = &sh->dev[i];
3644 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3645 /* We own a safe reference to the rdev */
3646 rdev = conf->disks[i].rdev;
3647 if (!rdev_set_badblocks(rdev, sh->sector,
3648 STRIPE_SECTORS, 0))
3649 md_error(conf->mddev, rdev);
3650 rdev_dec_pending(rdev, conf->mddev);
3651 }
NeilBrownb84db562011-07-28 11:39:23 +10003652 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3653 rdev = conf->disks[i].rdev;
3654 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003655 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10003656 rdev_dec_pending(rdev, conf->mddev);
3657 }
NeilBrown977df362011-12-23 10:17:53 +11003658 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3659 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11003660 if (!rdev)
3661 /* rdev have been moved down */
3662 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11003663 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003664 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11003665 rdev_dec_pending(rdev, conf->mddev);
3666 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003667 }
3668
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003669 if (s.ops_request)
3670 raid_run_ops(sh, s.ops_request);
3671
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003672 ops_run_io(sh, &s);
3673
NeilBrownc5709ef2011-07-26 11:35:20 +10003674 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003675 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003676 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003677 * have actually been submitted.
3678 */
3679 atomic_dec(&conf->preread_active_stripes);
3680 if (atomic_read(&conf->preread_active_stripes) <
3681 IO_THRESHOLD)
3682 md_wakeup_thread(conf->mddev->thread);
3683 }
3684
NeilBrownc5709ef2011-07-26 11:35:20 +10003685 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003686
Dan Williams257a4b42011-11-08 16:22:06 +11003687 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003688}
3689
NeilBrownd1688a62011-10-11 16:49:52 +11003690static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691{
3692 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3693 while (!list_empty(&conf->delayed_list)) {
3694 struct list_head *l = conf->delayed_list.next;
3695 struct stripe_head *sh;
3696 sh = list_entry(l, struct stripe_head, lru);
3697 list_del_init(l);
3698 clear_bit(STRIPE_DELAYED, &sh->state);
3699 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3700 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003701 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 }
NeilBrown482c0832011-04-18 18:25:42 +10003703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704}
3705
NeilBrownd1688a62011-10-11 16:49:52 +11003706static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003707{
3708 /* device_lock is held */
3709 struct list_head head;
3710 list_add(&head, &conf->bitmap_list);
3711 list_del_init(&conf->bitmap_list);
3712 while (!list_empty(&head)) {
3713 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3714 list_del_init(&sh->lru);
3715 atomic_inc(&sh->count);
3716 __release_stripe(conf, sh);
3717 }
3718}
3719
NeilBrownfd01b882011-10-11 16:47:53 +11003720int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003721{
NeilBrownd1688a62011-10-11 16:49:52 +11003722 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003723
3724 /* No difference between reads and writes. Just check
3725 * how busy the stripe_cache is
3726 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003727
NeilBrownf022b2f2006-10-03 01:15:56 -07003728 if (conf->inactive_blocked)
3729 return 1;
3730 if (conf->quiesce)
3731 return 1;
3732 if (list_empty_careful(&conf->inactive_list))
3733 return 1;
3734
3735 return 0;
3736}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003737EXPORT_SYMBOL_GPL(md_raid5_congested);
3738
3739static int raid5_congested(void *data, int bits)
3740{
NeilBrownfd01b882011-10-11 16:47:53 +11003741 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003742
3743 return mddev_congested(mddev, bits) ||
3744 md_raid5_congested(mddev, bits);
3745}
NeilBrownf022b2f2006-10-03 01:15:56 -07003746
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003747/* We want read requests to align with chunks where possible,
3748 * but write requests don't need to.
3749 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003750static int raid5_mergeable_bvec(struct request_queue *q,
3751 struct bvec_merge_data *bvm,
3752 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003753{
NeilBrownfd01b882011-10-11 16:47:53 +11003754 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003755 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003756 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003757 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003758 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003759
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003760 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003761 return biovec->bv_len; /* always allow writes to be mergeable */
3762
Andre Noll664e7c42009-06-18 08:45:27 +10003763 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3764 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003765 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3766 if (max < 0) max = 0;
3767 if (max <= biovec->bv_len && bio_sectors == 0)
3768 return biovec->bv_len;
3769 else
3770 return max;
3771}
3772
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003773
NeilBrownfd01b882011-10-11 16:47:53 +11003774static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003775{
3776 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003777 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003778 unsigned int bio_sectors = bio->bi_size >> 9;
3779
Andre Noll664e7c42009-06-18 08:45:27 +10003780 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3781 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003782 return chunk_sectors >=
3783 ((sector & (chunk_sectors - 1)) + bio_sectors);
3784}
3785
3786/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003787 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3788 * later sampled by raid5d.
3789 */
NeilBrownd1688a62011-10-11 16:49:52 +11003790static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003791{
3792 unsigned long flags;
3793
3794 spin_lock_irqsave(&conf->device_lock, flags);
3795
3796 bi->bi_next = conf->retry_read_aligned_list;
3797 conf->retry_read_aligned_list = bi;
3798
3799 spin_unlock_irqrestore(&conf->device_lock, flags);
3800 md_wakeup_thread(conf->mddev->thread);
3801}
3802
3803
NeilBrownd1688a62011-10-11 16:49:52 +11003804static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003805{
3806 struct bio *bi;
3807
3808 bi = conf->retry_read_aligned;
3809 if (bi) {
3810 conf->retry_read_aligned = NULL;
3811 return bi;
3812 }
3813 bi = conf->retry_read_aligned_list;
3814 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003815 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003816 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003817 /*
3818 * this sets the active strip count to 1 and the processed
3819 * strip count to zero (upper 8 bits)
3820 */
Shaohua Lie7836bd62012-07-19 16:01:31 +10003821 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003822 }
3823
3824 return bi;
3825}
3826
3827
3828/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003829 * The "raid5_align_endio" should check if the read succeeded and if it
3830 * did, call bio_endio on the original bio (having bio_put the new bio
3831 * first).
3832 * If the read failed..
3833 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003834static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003835{
3836 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003837 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003838 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003839 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003840 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003841
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003842 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003843
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003844 rdev = (void*)raid_bi->bi_next;
3845 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003846 mddev = rdev->mddev;
3847 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003848
3849 rdev_dec_pending(rdev, conf->mddev);
3850
3851 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003852 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003853 if (atomic_dec_and_test(&conf->active_aligned_reads))
3854 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003855 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003856 }
3857
3858
Dan Williams45b42332007-07-09 11:56:43 -07003859 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003860
3861 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003862}
3863
Neil Brown387bb172007-02-08 14:20:29 -08003864static int bio_fits_rdev(struct bio *bi)
3865{
Jens Axboe165125e2007-07-24 09:28:11 +02003866 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003867
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003868 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003869 return 0;
3870 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003871 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003872 return 0;
3873
3874 if (q->merge_bvec_fn)
3875 /* it's too hard to apply the merge_bvec_fn at this stage,
3876 * just just give up
3877 */
3878 return 0;
3879
3880 return 1;
3881}
3882
3883
NeilBrownfd01b882011-10-11 16:47:53 +11003884static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003885{
NeilBrownd1688a62011-10-11 16:49:52 +11003886 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11003887 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003888 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11003889 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11003890 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003891
3892 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003893 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003894 return 0;
3895 }
3896 /*
NeilBrowna167f662010-10-26 18:31:13 +11003897 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003898 */
NeilBrowna167f662010-10-26 18:31:13 +11003899 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003900 if (!align_bi)
3901 return 0;
3902 /*
3903 * set bi_end_io to a new function, and set bi_private to the
3904 * original bio.
3905 */
3906 align_bi->bi_end_io = raid5_align_endio;
3907 align_bi->bi_private = raid_bio;
3908 /*
3909 * compute position
3910 */
NeilBrown112bf892009-03-31 14:39:38 +11003911 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3912 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003913 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003914
NeilBrown671488c2011-12-23 10:17:52 +11003915 end_sector = align_bi->bi_sector + (align_bi->bi_size >> 9);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003916 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11003917 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
3918 if (!rdev || test_bit(Faulty, &rdev->flags) ||
3919 rdev->recovery_offset < end_sector) {
3920 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3921 if (rdev &&
3922 (test_bit(Faulty, &rdev->flags) ||
3923 !(test_bit(In_sync, &rdev->flags) ||
3924 rdev->recovery_offset >= end_sector)))
3925 rdev = NULL;
3926 }
3927 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10003928 sector_t first_bad;
3929 int bad_sectors;
3930
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003931 atomic_inc(&rdev->nr_pending);
3932 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003933 raid_bio->bi_next = (void*)rdev;
3934 align_bi->bi_bdev = rdev->bdev;
3935 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003936
NeilBrown31c176e2011-07-28 11:39:22 +10003937 if (!bio_fits_rdev(align_bi) ||
3938 is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
3939 &first_bad, &bad_sectors)) {
3940 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08003941 bio_put(align_bi);
3942 rdev_dec_pending(rdev, mddev);
3943 return 0;
3944 }
3945
majianpeng6c0544e2012-06-12 08:31:10 +08003946 /* No reshape active, so we can trust rdev->data_offset */
3947 align_bi->bi_sector += rdev->data_offset;
3948
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003949 spin_lock_irq(&conf->device_lock);
3950 wait_event_lock_irq(conf->wait_for_stripe,
3951 conf->quiesce == 0,
3952 conf->device_lock, /* nothing */);
3953 atomic_inc(&conf->active_aligned_reads);
3954 spin_unlock_irq(&conf->device_lock);
3955
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003956 generic_make_request(align_bi);
3957 return 1;
3958 } else {
3959 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003960 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003961 return 0;
3962 }
3963}
3964
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003965/* __get_priority_stripe - get the next stripe to process
3966 *
3967 * Full stripe writes are allowed to pass preread active stripes up until
3968 * the bypass_threshold is exceeded. In general the bypass_count
3969 * increments when the handle_list is handled before the hold_list; however, it
3970 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3971 * stripe with in flight i/o. The bypass_count will be reset when the
3972 * head of the hold_list has changed, i.e. the head was promoted to the
3973 * handle_list.
3974 */
NeilBrownd1688a62011-10-11 16:49:52 +11003975static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003976{
3977 struct stripe_head *sh;
3978
3979 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3980 __func__,
3981 list_empty(&conf->handle_list) ? "empty" : "busy",
3982 list_empty(&conf->hold_list) ? "empty" : "busy",
3983 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3984
3985 if (!list_empty(&conf->handle_list)) {
3986 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3987
3988 if (list_empty(&conf->hold_list))
3989 conf->bypass_count = 0;
3990 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3991 if (conf->hold_list.next == conf->last_hold)
3992 conf->bypass_count++;
3993 else {
3994 conf->last_hold = conf->hold_list.next;
3995 conf->bypass_count -= conf->bypass_threshold;
3996 if (conf->bypass_count < 0)
3997 conf->bypass_count = 0;
3998 }
3999 }
4000 } else if (!list_empty(&conf->hold_list) &&
4001 ((conf->bypass_threshold &&
4002 conf->bypass_count > conf->bypass_threshold) ||
4003 atomic_read(&conf->pending_full_writes) == 0)) {
4004 sh = list_entry(conf->hold_list.next,
4005 typeof(*sh), lru);
4006 conf->bypass_count -= conf->bypass_threshold;
4007 if (conf->bypass_count < 0)
4008 conf->bypass_count = 0;
4009 } else
4010 return NULL;
4011
4012 list_del_init(&sh->lru);
4013 atomic_inc(&sh->count);
4014 BUG_ON(atomic_read(&sh->count) != 1);
4015 return sh;
4016}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004017
Shaohua Li8811b592012-08-02 08:33:00 +10004018struct raid5_plug_cb {
4019 struct blk_plug_cb cb;
4020 struct list_head list;
4021};
4022
4023static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
4024{
4025 struct raid5_plug_cb *cb = container_of(
4026 blk_cb, struct raid5_plug_cb, cb);
4027 struct stripe_head *sh;
4028 struct mddev *mddev = cb->cb.data;
4029 struct r5conf *conf = mddev->private;
4030
4031 if (cb->list.next && !list_empty(&cb->list)) {
4032 spin_lock_irq(&conf->device_lock);
4033 while (!list_empty(&cb->list)) {
4034 sh = list_first_entry(&cb->list, struct stripe_head, lru);
4035 list_del_init(&sh->lru);
4036 /*
4037 * avoid race release_stripe_plug() sees
4038 * STRIPE_ON_UNPLUG_LIST clear but the stripe
4039 * is still in our list
4040 */
4041 smp_mb__before_clear_bit();
4042 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
4043 __release_stripe(conf, sh);
4044 }
4045 spin_unlock_irq(&conf->device_lock);
4046 }
4047 kfree(cb);
4048}
4049
4050static void release_stripe_plug(struct mddev *mddev,
4051 struct stripe_head *sh)
4052{
4053 struct blk_plug_cb *blk_cb = blk_check_plugged(
4054 raid5_unplug, mddev,
4055 sizeof(struct raid5_plug_cb));
4056 struct raid5_plug_cb *cb;
4057
4058 if (!blk_cb) {
4059 release_stripe(sh);
4060 return;
4061 }
4062
4063 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
4064
4065 if (cb->list.next == NULL)
4066 INIT_LIST_HEAD(&cb->list);
4067
4068 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
4069 list_add_tail(&sh->lru, &cb->list);
4070 else
4071 release_stripe(sh);
4072}
4073
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07004074static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075{
NeilBrownd1688a62011-10-11 16:49:52 +11004076 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11004077 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 sector_t new_sector;
4079 sector_t logical_sector, last_sector;
4080 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01004081 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11004082 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083
Tejun Heoe9c74692010-09-03 11:56:18 +02004084 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
4085 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004086 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07004087 }
4088
NeilBrown3d310eb2005-06-21 17:17:26 -07004089 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07004090
NeilBrown802ba062006-12-13 00:34:13 -08004091 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004092 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11004093 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004094 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004095
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4097 last_sector = bi->bi_sector + (bi->bi_size>>9);
4098 bi->bi_next = NULL;
4099 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07004100
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
4102 DEFINE_WAIT(w);
NeilBrownb5663ba2009-03-31 14:39:38 +11004103 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08004104
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004105 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11004106 previous = 0;
NeilBrownb578d552006-03-27 01:18:12 -08004107 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004108 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11004109 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08004110 * 64bit on a 32bit platform, and so it might be
4111 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02004112 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08004113 * the lock is dropped, so once we get a reference
4114 * to the stripe that we think it is, we will have
4115 * to check again.
4116 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004117 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004118 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004119 ? logical_sector < conf->reshape_progress
4120 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004121 previous = 1;
4122 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10004123 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004124 ? logical_sector < conf->reshape_safe
4125 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08004126 spin_unlock_irq(&conf->device_lock);
4127 schedule();
4128 goto retry;
4129 }
4130 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004131 spin_unlock_irq(&conf->device_lock);
4132 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004133
NeilBrown112bf892009-03-31 14:39:38 +11004134 new_sector = raid5_compute_sector(conf, logical_sector,
4135 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11004136 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10004137 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 (unsigned long long)new_sector,
4139 (unsigned long long)logical_sector);
4140
NeilBrownb5663ba2009-03-31 14:39:38 +11004141 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10004142 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004144 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004145 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08004146 * stripe, so we must do the range check again.
4147 * Expansion could still move past after this
4148 * test, but as we are holding a reference to
4149 * 'sh', we know that if that happens,
4150 * STRIPE_EXPANDING will get set and the expansion
4151 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004152 */
4153 int must_retry = 0;
4154 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004155 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11004156 ? logical_sector >= conf->reshape_progress
4157 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004158 /* mismatch, need to try again */
4159 must_retry = 1;
4160 spin_unlock_irq(&conf->device_lock);
4161 if (must_retry) {
4162 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004163 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004164 goto retry;
4165 }
4166 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004167
Namhyung Kimffd96e32011-07-18 17:38:51 +10004168 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004169 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004170 logical_sector < mddev->suspend_hi) {
4171 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004172 /* As the suspend_* range is controlled by
4173 * userspace, we want an interruptible
4174 * wait.
4175 */
4176 flush_signals(current);
4177 prepare_to_wait(&conf->wait_for_overlap,
4178 &w, TASK_INTERRUPTIBLE);
4179 if (logical_sector >= mddev->suspend_lo &&
4180 logical_sector < mddev->suspend_hi)
4181 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004182 goto retry;
4183 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004184
4185 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004186 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004187 /* Stripe is busy expanding or
4188 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 * and wait a while
4190 */
NeilBrown482c0832011-04-18 18:25:42 +10004191 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 release_stripe(sh);
4193 schedule();
4194 goto retry;
4195 }
4196 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004197 set_bit(STRIPE_HANDLE, &sh->state);
4198 clear_bit(STRIPE_DELAYED, &sh->state);
NeilBrowna852d7b2012-09-19 12:48:30 +10004199 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004200 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4201 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10004202 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 } else {
4204 /* cannot get stripe for read-ahead, just give-up */
4205 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4206 finish_wait(&conf->wait_for_overlap, &w);
4207 break;
4208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004210
Shaohua Lie7836bd62012-07-19 16:01:31 +10004211 remaining = raid5_dec_bi_active_stripes(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004212 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213
NeilBrown16a53ec2006-06-26 00:27:38 -07004214 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004216
Neil Brown0e13fe232008-06-28 08:31:20 +10004217 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219}
4220
NeilBrownfd01b882011-10-11 16:47:53 +11004221static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004222
NeilBrownfd01b882011-10-11 16:47:53 +11004223static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224{
NeilBrown52c03292006-06-26 00:27:43 -07004225 /* reshaping is quite different to recovery/resync so it is
4226 * handled quite separately ... here.
4227 *
4228 * On each call to sync_request, we gather one chunk worth of
4229 * destination stripes and flag them as expanding.
4230 * Then we find all the source stripes and request reads.
4231 * As the reads complete, handle_stripe will copy the data
4232 * into the destination stripe and release that stripe.
4233 */
NeilBrownd1688a62011-10-11 16:49:52 +11004234 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004236 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004237 int raid_disks = conf->previous_raid_disks;
4238 int data_disks = raid_disks - conf->max_degraded;
4239 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004240 int i;
4241 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004242 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004243 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004244 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004245 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004246
NeilBrownfef9c612009-03-31 15:16:46 +11004247 if (sector_nr == 0) {
4248 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10004249 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004250 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4251 sector_nr = raid5_size(mddev, 0, 0)
4252 - conf->reshape_progress;
NeilBrown2c810cd2012-05-21 09:27:00 +10004253 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004254 conf->reshape_progress > 0)
4255 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004256 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004257 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004258 mddev->curr_resync_completed = sector_nr;
4259 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004260 *skipped = 1;
4261 return sector_nr;
4262 }
NeilBrown52c03292006-06-26 00:27:43 -07004263 }
4264
NeilBrown7a661382009-03-31 15:21:40 +11004265 /* We need to process a full chunk at a time.
4266 * If old and new chunk sizes differ, we need to process the
4267 * largest of these
4268 */
Andre Noll664e7c42009-06-18 08:45:27 +10004269 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4270 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004271 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004272 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004273
NeilBrownb5254dd2012-05-21 09:27:01 +10004274 /* We update the metadata at least every 10 seconds, or when
4275 * the data about to be copied would over-write the source of
4276 * the data at the front of the range. i.e. one new_stripe
4277 * along from reshape_progress new_maps to after where
4278 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004279 */
NeilBrownfef9c612009-03-31 15:16:46 +11004280 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004281 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004282 readpos = conf->reshape_progress;
4283 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004284 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004285 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10004286 if (mddev->reshape_backwards) {
NeilBrowned37d832009-05-27 21:39:05 +10004287 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004288 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004289 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004290 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004291 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004292 readpos -= min_t(sector_t, reshape_sectors, readpos);
4293 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004294 }
NeilBrown52c03292006-06-26 00:27:43 -07004295
NeilBrownb5254dd2012-05-21 09:27:01 +10004296 /* Having calculated the 'writepos' possibly use it
4297 * to set 'stripe_addr' which is where we will write to.
4298 */
4299 if (mddev->reshape_backwards) {
4300 BUG_ON(conf->reshape_progress == 0);
4301 stripe_addr = writepos;
4302 BUG_ON((mddev->dev_sectors &
4303 ~((sector_t)reshape_sectors - 1))
4304 - reshape_sectors - stripe_addr
4305 != sector_nr);
4306 } else {
4307 BUG_ON(writepos != sector_nr + reshape_sectors);
4308 stripe_addr = sector_nr;
4309 }
4310
NeilBrownc8f517c2009-03-31 15:28:40 +11004311 /* 'writepos' is the most advanced device address we might write.
4312 * 'readpos' is the least advanced device address we might read.
4313 * 'safepos' is the least address recorded in the metadata as having
4314 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10004315 * If there is a min_offset_diff, these are adjusted either by
4316 * increasing the safepos/readpos if diff is negative, or
4317 * increasing writepos if diff is positive.
4318 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11004319 * ensure safety in the face of a crash - that must be done by userspace
4320 * making a backup of the data. So in that case there is no particular
4321 * rush to update metadata.
4322 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4323 * update the metadata to advance 'safepos' to match 'readpos' so that
4324 * we can be safe in the event of a crash.
4325 * So we insist on updating metadata if safepos is behind writepos and
4326 * readpos is beyond writepos.
4327 * In any case, update the metadata every 10 seconds.
4328 * Maybe that number should be configurable, but I'm not sure it is
4329 * worth it.... maybe it could be a multiple of safemode_delay???
4330 */
NeilBrownb5254dd2012-05-21 09:27:01 +10004331 if (conf->min_offset_diff < 0) {
4332 safepos += -conf->min_offset_diff;
4333 readpos += -conf->min_offset_diff;
4334 } else
4335 writepos += conf->min_offset_diff;
4336
NeilBrown2c810cd2012-05-21 09:27:00 +10004337 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11004338 ? (safepos > writepos && readpos < writepos)
4339 : (safepos < writepos && readpos > writepos)) ||
4340 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004341 /* Cannot proceed until we've updated the superblock... */
4342 wait_event(conf->wait_for_overlap,
4343 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004344 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004345 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004346 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07004347 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004348 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07004349 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004350 kthread_should_stop());
4351 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004352 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004353 spin_unlock_irq(&conf->device_lock);
4354 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004355 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004356 }
4357
NeilBrownab69ae12009-03-31 15:26:47 +11004358 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004359 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004360 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004361 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004362 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004363 set_bit(STRIPE_EXPANDING, &sh->state);
4364 atomic_inc(&conf->reshape_stripes);
4365 /* If any of this stripe is beyond the end of the old
4366 * array, then we need to zero those blocks
4367 */
4368 for (j=sh->disks; j--;) {
4369 sector_t s;
4370 if (j == sh->pd_idx)
4371 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004372 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004373 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004374 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004375 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004376 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004377 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004378 continue;
4379 }
4380 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4381 set_bit(R5_Expanded, &sh->dev[j].flags);
4382 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4383 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004384 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004385 set_bit(STRIPE_EXPAND_READY, &sh->state);
4386 set_bit(STRIPE_HANDLE, &sh->state);
4387 }
NeilBrownab69ae12009-03-31 15:26:47 +11004388 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004389 }
4390 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004391 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11004392 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004393 else
NeilBrown7a661382009-03-31 15:21:40 +11004394 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004395 spin_unlock_irq(&conf->device_lock);
4396 /* Ok, those stripe are ready. We can start scheduling
4397 * reads on the source stripes.
4398 * The source stripes are determined by mapping the first and last
4399 * block on the destination stripes.
4400 */
NeilBrown52c03292006-06-26 00:27:43 -07004401 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004402 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004403 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004404 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004405 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004406 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004407 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004408 if (last_sector >= mddev->dev_sectors)
4409 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004410 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004411 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004412 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4413 set_bit(STRIPE_HANDLE, &sh->state);
4414 release_stripe(sh);
4415 first_sector += STRIPE_SECTORS;
4416 }
NeilBrownab69ae12009-03-31 15:26:47 +11004417 /* Now that the sources are clearly marked, we can release
4418 * the destination stripes
4419 */
4420 while (!list_empty(&stripes)) {
4421 sh = list_entry(stripes.next, struct stripe_head, lru);
4422 list_del_init(&sh->lru);
4423 release_stripe(sh);
4424 }
NeilBrownc6207272008-02-06 01:39:52 -08004425 /* If this takes us to the resync_max point where we have to pause,
4426 * then we need to write out the superblock.
4427 */
NeilBrown7a661382009-03-31 15:21:40 +11004428 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004429 if ((sector_nr - mddev->curr_resync_completed) * 2
4430 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004431 /* Cannot proceed until we've updated the superblock... */
4432 wait_event(conf->wait_for_overlap,
4433 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004434 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004435 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004436 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004437 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4438 md_wakeup_thread(mddev->thread);
4439 wait_event(mddev->sb_wait,
4440 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4441 || kthread_should_stop());
4442 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004443 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004444 spin_unlock_irq(&conf->device_lock);
4445 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004446 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004447 }
NeilBrown7a661382009-03-31 15:21:40 +11004448 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004449}
4450
4451/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004452static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004453{
NeilBrownd1688a62011-10-11 16:49:52 +11004454 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004455 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004456 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004457 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004458 int still_degraded = 0;
4459 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460
NeilBrown72626682005-09-09 16:23:54 -07004461 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004463
NeilBrown29269552006-03-27 01:18:10 -08004464 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4465 end_reshape(conf);
4466 return 0;
4467 }
NeilBrown72626682005-09-09 16:23:54 -07004468
4469 if (mddev->curr_resync < max_sector) /* aborted */
4470 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4471 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004472 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004473 conf->fullsync = 0;
4474 bitmap_close_sync(mddev->bitmap);
4475
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 return 0;
4477 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004478
NeilBrown64bd6602009-08-03 10:59:58 +10004479 /* Allow raid5_quiesce to complete */
4480 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4481
NeilBrown52c03292006-06-26 00:27:43 -07004482 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4483 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004484
NeilBrownc6207272008-02-06 01:39:52 -08004485 /* No need to check resync_max as we never do more than one
4486 * stripe, and as resync_max will always be on a chunk boundary,
4487 * if the check in md_do_sync didn't fire, there is no chance
4488 * of overstepping resync_max here
4489 */
4490
NeilBrown16a53ec2006-06-26 00:27:38 -07004491 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 * to resync, then assert that we are finished, because there is
4493 * nothing we can do.
4494 */
NeilBrown3285edf2006-06-26 00:27:55 -07004495 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004496 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004497 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004498 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 return rv;
4500 }
NeilBrown72626682005-09-09 16:23:54 -07004501 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004502 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004503 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4504 /* we can skip this block, and probably more */
4505 sync_blocks /= STRIPE_SECTORS;
4506 *skipped = 1;
4507 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509
NeilBrownb47490c2008-02-06 01:39:50 -08004510 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4511
NeilBrowna8c906c2009-06-09 14:39:59 +10004512 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004514 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004516 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004518 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004520 /* Need to check if array will still be degraded after recovery/resync
4521 * We don't need to check the 'failed' flag as when that gets set,
4522 * recovery aborts.
4523 */
NeilBrownf001a702009-06-09 14:30:31 +10004524 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004525 if (conf->disks[i].rdev == NULL)
4526 still_degraded = 1;
4527
4528 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4529
NeilBrown83206d62011-07-26 11:19:49 +10004530 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531
NeilBrown14425772009-10-16 15:55:25 +11004532 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 release_stripe(sh);
4534
4535 return STRIPE_SECTORS;
4536}
4537
NeilBrownd1688a62011-10-11 16:49:52 +11004538static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004539{
4540 /* We may not be able to submit a whole bio at once as there
4541 * may not be enough stripe_heads available.
4542 * We cannot pre-allocate enough stripe_heads as we may need
4543 * more than exist in the cache (if we allow ever large chunks).
4544 * So we do one stripe head at a time and record in
4545 * ->bi_hw_segments how many have been done.
4546 *
4547 * We *know* that this entire raid_bio is in one chunk, so
4548 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4549 */
4550 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004551 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004552 sector_t sector, logical_sector, last_sector;
4553 int scnt = 0;
4554 int remaining;
4555 int handled = 0;
4556
4557 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004558 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004559 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004560 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4561
4562 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004563 logical_sector += STRIPE_SECTORS,
4564 sector += STRIPE_SECTORS,
4565 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004566
Shaohua Lie7836bd62012-07-19 16:01:31 +10004567 if (scnt < raid5_bi_processed_stripes(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004568 /* already done this stripe */
4569 continue;
4570
NeilBrowna8c906c2009-06-09 14:39:59 +10004571 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004572
4573 if (!sh) {
4574 /* failed to get a stripe - must wait */
Shaohua Lie7836bd62012-07-19 16:01:31 +10004575 raid5_set_bi_processed_stripes(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004576 conf->retry_read_aligned = raid_bio;
4577 return handled;
4578 }
4579
Neil Brown387bb172007-02-08 14:20:29 -08004580 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4581 release_stripe(sh);
Shaohua Lie7836bd62012-07-19 16:01:31 +10004582 raid5_set_bi_processed_stripes(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004583 conf->retry_read_aligned = raid_bio;
4584 return handled;
4585 }
4586
majianpeng3f9e7c12012-07-31 10:04:21 +10004587 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07004588 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004589 release_stripe(sh);
4590 handled++;
4591 }
Shaohua Lie7836bd62012-07-19 16:01:31 +10004592 remaining = raid5_dec_bi_active_stripes(raid_bio);
Neil Brown0e13fe232008-06-28 08:31:20 +10004593 if (remaining == 0)
4594 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004595 if (atomic_dec_and_test(&conf->active_aligned_reads))
4596 wake_up(&conf->wait_for_stripe);
4597 return handled;
4598}
4599
Shaohua Li46a06402012-08-02 08:33:15 +10004600#define MAX_STRIPE_BATCH 8
4601static int handle_active_stripes(struct r5conf *conf)
4602{
4603 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
4604 int i, batch_size = 0;
4605
4606 while (batch_size < MAX_STRIPE_BATCH &&
4607 (sh = __get_priority_stripe(conf)) != NULL)
4608 batch[batch_size++] = sh;
4609
4610 if (batch_size == 0)
4611 return batch_size;
4612 spin_unlock_irq(&conf->device_lock);
4613
4614 for (i = 0; i < batch_size; i++)
4615 handle_stripe(batch[i]);
4616
4617 cond_resched();
4618
4619 spin_lock_irq(&conf->device_lock);
4620 for (i = 0; i < batch_size; i++)
4621 __release_stripe(conf, batch[i]);
4622 return batch_size;
4623}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004624
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625/*
4626 * This is our raid5 kernel thread.
4627 *
4628 * We scan the hash table for stripes which can be handled now.
4629 * During the scan, completed stripes are saved for us by the interrupt
4630 * handler, so that they will not have to wait for our next wakeup.
4631 */
NeilBrownfd01b882011-10-11 16:47:53 +11004632static void raid5d(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633{
NeilBrownd1688a62011-10-11 16:49:52 +11004634 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004636 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637
Dan Williams45b42332007-07-09 11:56:43 -07004638 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639
4640 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004642 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 handled = 0;
4644 spin_lock_irq(&conf->device_lock);
4645 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004646 struct bio *bio;
Shaohua Li46a06402012-08-02 08:33:15 +10004647 int batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648
NeilBrown0021b7b2012-07-31 09:08:14 +02004649 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10004650 !list_empty(&conf->bitmap_list)) {
4651 /* Now is a good time to flush some bitmap updates */
4652 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004653 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004654 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004655 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004656 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004657 activate_bit_delay(conf);
4658 }
NeilBrown0021b7b2012-07-31 09:08:14 +02004659 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004660
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004661 while ((bio = remove_bio_from_retry(conf))) {
4662 int ok;
4663 spin_unlock_irq(&conf->device_lock);
4664 ok = retry_aligned_read(conf, bio);
4665 spin_lock_irq(&conf->device_lock);
4666 if (!ok)
4667 break;
4668 handled++;
4669 }
4670
Shaohua Li46a06402012-08-02 08:33:15 +10004671 batch_size = handle_active_stripes(conf);
4672 if (!batch_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 break;
Shaohua Li46a06402012-08-02 08:33:15 +10004674 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675
Shaohua Li46a06402012-08-02 08:33:15 +10004676 if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) {
4677 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10004678 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10004679 spin_lock_irq(&conf->device_lock);
4680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 }
Dan Williams45b42332007-07-09 11:56:43 -07004682 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683
4684 spin_unlock_irq(&conf->device_lock);
4685
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004686 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004687 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688
Dan Williams45b42332007-07-09 11:56:43 -07004689 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690}
4691
NeilBrown3f294f42005-11-08 21:39:25 -08004692static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004693raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004694{
NeilBrownd1688a62011-10-11 16:49:52 +11004695 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004696 if (conf)
4697 return sprintf(page, "%d\n", conf->max_nr_stripes);
4698 else
4699 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004700}
4701
NeilBrownc41d4ac2010-06-01 19:37:24 +10004702int
NeilBrownfd01b882011-10-11 16:47:53 +11004703raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004704{
NeilBrownd1688a62011-10-11 16:49:52 +11004705 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004706 int err;
4707
4708 if (size <= 16 || size > 32768)
4709 return -EINVAL;
4710 while (size < conf->max_nr_stripes) {
4711 if (drop_one_stripe(conf))
4712 conf->max_nr_stripes--;
4713 else
4714 break;
4715 }
4716 err = md_allow_write(mddev);
4717 if (err)
4718 return err;
4719 while (size > conf->max_nr_stripes) {
4720 if (grow_one_stripe(conf))
4721 conf->max_nr_stripes++;
4722 else break;
4723 }
4724 return 0;
4725}
4726EXPORT_SYMBOL(raid5_set_cache_size);
4727
NeilBrown3f294f42005-11-08 21:39:25 -08004728static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004729raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004730{
NeilBrownd1688a62011-10-11 16:49:52 +11004731 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004732 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004733 int err;
4734
NeilBrown3f294f42005-11-08 21:39:25 -08004735 if (len >= PAGE_SIZE)
4736 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004737 if (!conf)
4738 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004739
Dan Williams4ef197d82008-04-28 02:15:54 -07004740 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004741 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004742 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004743 if (err)
4744 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004745 return len;
4746}
NeilBrown007583c2005-11-08 21:39:30 -08004747
NeilBrown96de1e62005-11-08 21:39:39 -08004748static struct md_sysfs_entry
4749raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4750 raid5_show_stripe_cache_size,
4751 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004752
4753static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004754raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004755{
NeilBrownd1688a62011-10-11 16:49:52 +11004756 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004757 if (conf)
4758 return sprintf(page, "%d\n", conf->bypass_threshold);
4759 else
4760 return 0;
4761}
4762
4763static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004764raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004765{
NeilBrownd1688a62011-10-11 16:49:52 +11004766 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004767 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004768 if (len >= PAGE_SIZE)
4769 return -EINVAL;
4770 if (!conf)
4771 return -ENODEV;
4772
Dan Williams4ef197d82008-04-28 02:15:54 -07004773 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004774 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004775 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004776 return -EINVAL;
4777 conf->bypass_threshold = new;
4778 return len;
4779}
4780
4781static struct md_sysfs_entry
4782raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4783 S_IRUGO | S_IWUSR,
4784 raid5_show_preread_threshold,
4785 raid5_store_preread_threshold);
4786
4787static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004788stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004789{
NeilBrownd1688a62011-10-11 16:49:52 +11004790 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004791 if (conf)
4792 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4793 else
4794 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004795}
4796
NeilBrown96de1e62005-11-08 21:39:39 -08004797static struct md_sysfs_entry
4798raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004799
NeilBrown007583c2005-11-08 21:39:30 -08004800static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004801 &raid5_stripecache_size.attr,
4802 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004803 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004804 NULL,
4805};
NeilBrown007583c2005-11-08 21:39:30 -08004806static struct attribute_group raid5_attrs_group = {
4807 .name = NULL,
4808 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004809};
4810
Dan Williams80c3a6c2009-03-17 18:10:40 -07004811static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11004812raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07004813{
NeilBrownd1688a62011-10-11 16:49:52 +11004814 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004815
4816 if (!sectors)
4817 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004818 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11004819 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11004820 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004821
Andre Noll9d8f0362009-06-18 08:45:01 +10004822 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004823 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004824 return sectors * (raid_disks - conf->max_degraded);
4825}
4826
NeilBrownd1688a62011-10-11 16:49:52 +11004827static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004828{
4829 struct raid5_percpu *percpu;
4830 unsigned long cpu;
4831
4832 if (!conf->percpu)
4833 return;
4834
4835 get_online_cpus();
4836 for_each_possible_cpu(cpu) {
4837 percpu = per_cpu_ptr(conf->percpu, cpu);
4838 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004839 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004840 }
4841#ifdef CONFIG_HOTPLUG_CPU
4842 unregister_cpu_notifier(&conf->cpu_notify);
4843#endif
4844 put_online_cpus();
4845
4846 free_percpu(conf->percpu);
4847}
4848
NeilBrownd1688a62011-10-11 16:49:52 +11004849static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10004850{
4851 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004852 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10004853 kfree(conf->disks);
4854 kfree(conf->stripe_hashtbl);
4855 kfree(conf);
4856}
4857
Dan Williams36d1c642009-07-14 11:48:22 -07004858#ifdef CONFIG_HOTPLUG_CPU
4859static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4860 void *hcpu)
4861{
NeilBrownd1688a62011-10-11 16:49:52 +11004862 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07004863 long cpu = (long)hcpu;
4864 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4865
4866 switch (action) {
4867 case CPU_UP_PREPARE:
4868 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07004869 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07004870 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004871 if (!percpu->scribble)
4872 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4873
4874 if (!percpu->scribble ||
4875 (conf->level == 6 && !percpu->spare_page)) {
4876 safe_put_page(percpu->spare_page);
4877 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004878 pr_err("%s: failed memory allocation for cpu%ld\n",
4879 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07004880 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07004881 }
4882 break;
4883 case CPU_DEAD:
4884 case CPU_DEAD_FROZEN:
4885 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004886 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004887 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004888 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07004889 break;
4890 default:
4891 break;
4892 }
4893 return NOTIFY_OK;
4894}
4895#endif
4896
NeilBrownd1688a62011-10-11 16:49:52 +11004897static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004898{
4899 unsigned long cpu;
4900 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09004901 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004902 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004903 int err;
4904
Dan Williams36d1c642009-07-14 11:48:22 -07004905 allcpus = alloc_percpu(struct raid5_percpu);
4906 if (!allcpus)
4907 return -ENOMEM;
4908 conf->percpu = allcpus;
4909
4910 get_online_cpus();
4911 err = 0;
4912 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07004913 if (conf->level == 6) {
4914 spare_page = alloc_page(GFP_KERNEL);
4915 if (!spare_page) {
4916 err = -ENOMEM;
4917 break;
4918 }
4919 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
4920 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11004921 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004922 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07004923 err = -ENOMEM;
4924 break;
4925 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07004926 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004927 }
4928#ifdef CONFIG_HOTPLUG_CPU
4929 conf->cpu_notify.notifier_call = raid456_cpu_notify;
4930 conf->cpu_notify.priority = 0;
4931 if (err == 0)
4932 err = register_cpu_notifier(&conf->cpu_notify);
4933#endif
4934 put_online_cpus();
4935
4936 return err;
4937}
4938
NeilBrownd1688a62011-10-11 16:49:52 +11004939static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940{
NeilBrownd1688a62011-10-11 16:49:52 +11004941 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004942 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11004943 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10004945 char pers_name[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946
NeilBrown91adb562009-03-31 14:39:39 +11004947 if (mddev->new_level != 5
4948 && mddev->new_level != 4
4949 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10004950 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004951 mdname(mddev), mddev->new_level);
4952 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 }
NeilBrown91adb562009-03-31 14:39:39 +11004954 if ((mddev->new_level == 5
4955 && !algorithm_valid_raid5(mddev->new_layout)) ||
4956 (mddev->new_level == 6
4957 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004958 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11004959 mdname(mddev), mddev->new_layout);
4960 return ERR_PTR(-EIO);
4961 }
4962 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10004963 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004964 mdname(mddev), mddev->raid_disks);
4965 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11004966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967
Andre Noll664e7c42009-06-18 08:45:27 +10004968 if (!mddev->new_chunk_sectors ||
4969 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
4970 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10004971 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
4972 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11004973 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11004974 }
4975
NeilBrownd1688a62011-10-11 16:49:52 +11004976 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11004977 if (conf == NULL)
4978 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11004979 spin_lock_init(&conf->device_lock);
4980 init_waitqueue_head(&conf->wait_for_stripe);
4981 init_waitqueue_head(&conf->wait_for_overlap);
4982 INIT_LIST_HEAD(&conf->handle_list);
4983 INIT_LIST_HEAD(&conf->hold_list);
4984 INIT_LIST_HEAD(&conf->delayed_list);
4985 INIT_LIST_HEAD(&conf->bitmap_list);
4986 INIT_LIST_HEAD(&conf->inactive_list);
4987 atomic_set(&conf->active_stripes, 0);
4988 atomic_set(&conf->preread_active_stripes, 0);
4989 atomic_set(&conf->active_aligned_reads, 0);
4990 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11004991 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11004992
4993 conf->raid_disks = mddev->raid_disks;
4994 if (mddev->reshape_position == MaxSector)
4995 conf->previous_raid_disks = mddev->raid_disks;
4996 else
4997 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004998 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
4999 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11005000
NeilBrown5e5e3e72009-10-16 16:35:30 +11005001 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11005002 GFP_KERNEL);
5003 if (!conf->disks)
5004 goto abort;
5005
5006 conf->mddev = mddev;
5007
5008 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
5009 goto abort;
5010
Dan Williams36d1c642009-07-14 11:48:22 -07005011 conf->level = mddev->new_level;
5012 if (raid5_alloc_percpu(conf) != 0)
5013 goto abort;
5014
NeilBrown0c55e022010-05-03 14:09:02 +10005015 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11005016
NeilBrowndafb20f2012-03-19 12:46:39 +11005017 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11005018 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005019 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11005020 || raid_disk < 0)
5021 continue;
5022 disk = conf->disks + raid_disk;
5023
NeilBrown17045f52011-12-23 10:17:53 +11005024 if (test_bit(Replacement, &rdev->flags)) {
5025 if (disk->replacement)
5026 goto abort;
5027 disk->replacement = rdev;
5028 } else {
5029 if (disk->rdev)
5030 goto abort;
5031 disk->rdev = rdev;
5032 }
NeilBrown91adb562009-03-31 14:39:39 +11005033
5034 if (test_bit(In_sync, &rdev->flags)) {
5035 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10005036 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
5037 " disk %d\n",
5038 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05005039 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11005040 /* Cannot rely on bitmap to complete recovery */
5041 conf->fullsync = 1;
5042 }
5043
Andre Noll09c9e5f2009-06-18 08:45:55 +10005044 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11005045 conf->level = mddev->new_level;
5046 if (conf->level == 6)
5047 conf->max_degraded = 2;
5048 else
5049 conf->max_degraded = 1;
5050 conf->algorithm = mddev->new_layout;
5051 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11005052 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11005053 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10005054 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11005055 conf->prev_algo = mddev->layout;
5056 }
NeilBrown91adb562009-03-31 14:39:39 +11005057
5058 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11005059 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11005060 if (grow_stripes(conf, conf->max_nr_stripes)) {
5061 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005062 "md/raid:%s: couldn't allocate %dkB for buffers\n",
5063 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005064 goto abort;
5065 } else
NeilBrown0c55e022010-05-03 14:09:02 +10005066 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
5067 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005068
NeilBrown02326052012-07-03 15:56:52 +10005069 sprintf(pers_name, "raid%d", mddev->new_level);
5070 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11005071 if (!conf->thread) {
5072 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005073 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11005074 mdname(mddev));
5075 goto abort;
5076 }
5077
5078 return conf;
5079
5080 abort:
5081 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10005082 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11005083 return ERR_PTR(-EIO);
5084 } else
5085 return ERR_PTR(-ENOMEM);
5086}
5087
NeilBrownc148ffd2009-11-13 17:47:00 +11005088
5089static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
5090{
5091 switch (algo) {
5092 case ALGORITHM_PARITY_0:
5093 if (raid_disk < max_degraded)
5094 return 1;
5095 break;
5096 case ALGORITHM_PARITY_N:
5097 if (raid_disk >= raid_disks - max_degraded)
5098 return 1;
5099 break;
5100 case ALGORITHM_PARITY_0_6:
5101 if (raid_disk == 0 ||
5102 raid_disk == raid_disks - 1)
5103 return 1;
5104 break;
5105 case ALGORITHM_LEFT_ASYMMETRIC_6:
5106 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5107 case ALGORITHM_LEFT_SYMMETRIC_6:
5108 case ALGORITHM_RIGHT_SYMMETRIC_6:
5109 if (raid_disk == raid_disks - 1)
5110 return 1;
5111 }
5112 return 0;
5113}
5114
NeilBrownfd01b882011-10-11 16:47:53 +11005115static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11005116{
NeilBrownd1688a62011-10-11 16:49:52 +11005117 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10005118 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11005119 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11005120 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11005121 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11005122 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10005123 long long min_offset_diff = 0;
5124 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11005125
Andre Noll8c6ac862009-06-18 08:48:06 +10005126 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10005127 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10005128 " -- starting background reconstruction\n",
5129 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10005130
5131 rdev_for_each(rdev, mddev) {
5132 long long diff;
5133 if (rdev->raid_disk < 0)
5134 continue;
5135 diff = (rdev->new_data_offset - rdev->data_offset);
5136 if (first) {
5137 min_offset_diff = diff;
5138 first = 0;
5139 } else if (mddev->reshape_backwards &&
5140 diff < min_offset_diff)
5141 min_offset_diff = diff;
5142 else if (!mddev->reshape_backwards &&
5143 diff > min_offset_diff)
5144 min_offset_diff = diff;
5145 }
5146
NeilBrownf6705572006-03-27 01:18:11 -08005147 if (mddev->reshape_position != MaxSector) {
5148 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10005149 * Difficulties arise if the stripe we would write to
5150 * next is at or after the stripe we would read from next.
5151 * For a reshape that changes the number of devices, this
5152 * is only possible for a very short time, and mdadm makes
5153 * sure that time appears to have past before assembling
5154 * the array. So we fail if that time hasn't passed.
5155 * For a reshape that keeps the number of devices the same
5156 * mdadm must be monitoring the reshape can keeping the
5157 * critical areas read-only and backed up. It will start
5158 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08005159 */
5160 sector_t here_new, here_old;
5161 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11005162 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08005163
NeilBrown88ce4932009-03-31 15:24:23 +11005164 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10005165 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08005166 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08005167 mdname(mddev));
5168 return -EINVAL;
5169 }
NeilBrownf6705572006-03-27 01:18:11 -08005170 old_disks = mddev->raid_disks - mddev->delta_disks;
5171 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08005172 * further up in new geometry must map after here in old
5173 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08005174 */
5175 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10005176 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005177 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005178 printk(KERN_ERR "md/raid:%s: reshape_position not "
5179 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005180 return -EINVAL;
5181 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005182 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005183 /* here_new is the stripe we will write to */
5184 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10005185 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005186 (old_disks-max_degraded));
5187 /* here_old is the first stripe that we might need to read
5188 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10005189 if (mddev->delta_disks == 0) {
NeilBrownb5254dd2012-05-21 09:27:01 +10005190 if ((here_new * mddev->new_chunk_sectors !=
5191 here_old * mddev->chunk_sectors)) {
5192 printk(KERN_ERR "md/raid:%s: reshape position is"
5193 " confused - aborting\n", mdname(mddev));
5194 return -EINVAL;
5195 }
NeilBrown67ac6012009-08-13 10:06:24 +10005196 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10005197 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10005198 * and taking constant backups.
5199 * mdadm always starts a situation like this in
5200 * readonly mode so it can take control before
5201 * allowing any writes. So just check for that.
5202 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005203 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
5204 abs(min_offset_diff) >= mddev->new_chunk_sectors)
5205 /* not really in-place - so OK */;
5206 else if (mddev->ro == 0) {
5207 printk(KERN_ERR "md/raid:%s: in-place reshape "
5208 "must be started in read-only mode "
5209 "- aborting\n",
NeilBrown0c55e022010-05-03 14:09:02 +10005210 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005211 return -EINVAL;
5212 }
NeilBrown2c810cd2012-05-21 09:27:00 +10005213 } else if (mddev->reshape_backwards
NeilBrownb5254dd2012-05-21 09:27:01 +10005214 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
NeilBrown67ac6012009-08-13 10:06:24 +10005215 here_old * mddev->chunk_sectors)
5216 : (here_new * mddev->new_chunk_sectors >=
NeilBrownb5254dd2012-05-21 09:27:01 +10005217 here_old * mddev->chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08005218 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005219 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5220 "auto-recovery - aborting.\n",
5221 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005222 return -EINVAL;
5223 }
NeilBrown0c55e022010-05-03 14:09:02 +10005224 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5225 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005226 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005227 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005228 BUG_ON(mddev->level != mddev->new_level);
5229 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005230 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005231 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005232 }
5233
NeilBrown245f46c2009-03-31 14:39:39 +11005234 if (mddev->private == NULL)
5235 conf = setup_conf(mddev);
5236 else
5237 conf = mddev->private;
5238
NeilBrown91adb562009-03-31 14:39:39 +11005239 if (IS_ERR(conf))
5240 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005241
NeilBrownb5254dd2012-05-21 09:27:01 +10005242 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11005243 mddev->thread = conf->thread;
5244 conf->thread = NULL;
5245 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246
NeilBrown17045f52011-12-23 10:17:53 +11005247 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5248 i++) {
5249 rdev = conf->disks[i].rdev;
5250 if (!rdev && conf->disks[i].replacement) {
5251 /* The replacement is all we have yet */
5252 rdev = conf->disks[i].replacement;
5253 conf->disks[i].replacement = NULL;
5254 clear_bit(Replacement, &rdev->flags);
5255 conf->disks[i].rdev = rdev;
5256 }
5257 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11005258 continue;
NeilBrown17045f52011-12-23 10:17:53 +11005259 if (conf->disks[i].replacement &&
5260 conf->reshape_progress != MaxSector) {
5261 /* replacements and reshape simply do not mix. */
5262 printk(KERN_ERR "md: cannot handle concurrent "
5263 "replacement and reshape.\n");
5264 goto abort;
5265 }
NeilBrown2f115882010-06-17 17:41:03 +10005266 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005267 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005268 continue;
5269 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005270 /* This disc is not fully in-sync. However if it
5271 * just stored parity (beyond the recovery_offset),
5272 * when we don't need to be concerned about the
5273 * array being dirty.
5274 * When reshape goes 'backwards', we never have
5275 * partially completed devices, so we only need
5276 * to worry about reshape going forwards.
5277 */
5278 /* Hack because v0.91 doesn't store recovery_offset properly. */
5279 if (mddev->major_version == 0 &&
5280 mddev->minor_version > 90)
5281 rdev->recovery_offset = reshape_offset;
5282
NeilBrownc148ffd2009-11-13 17:47:00 +11005283 if (rdev->recovery_offset < reshape_offset) {
5284 /* We need to check old and new layout */
5285 if (!only_parity(rdev->raid_disk,
5286 conf->algorithm,
5287 conf->raid_disks,
5288 conf->max_degraded))
5289 continue;
5290 }
5291 if (!only_parity(rdev->raid_disk,
5292 conf->prev_algo,
5293 conf->previous_raid_disks,
5294 conf->max_degraded))
5295 continue;
5296 dirty_parity_disks++;
5297 }
NeilBrown91adb562009-03-31 14:39:39 +11005298
NeilBrown17045f52011-12-23 10:17:53 +11005299 /*
5300 * 0 for a fully functional array, 1 or 2 for a degraded array.
5301 */
NeilBrown908f4fb2011-12-23 10:17:50 +11005302 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303
NeilBrown674806d2010-06-16 17:17:53 +10005304 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005305 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07005307 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308 goto abort;
5309 }
5310
NeilBrown91adb562009-03-31 14:39:39 +11005311 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10005312 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11005313 mddev->resync_max_sectors = mddev->dev_sectors;
5314
NeilBrownc148ffd2009-11-13 17:47:00 +11005315 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005317 if (mddev->ok_start_degraded)
5318 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10005319 "md/raid:%s: starting dirty degraded array"
5320 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005321 mdname(mddev));
5322 else {
5323 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005324 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005325 mdname(mddev));
5326 goto abort;
5327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 }
5329
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10005331 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5332 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11005333 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5334 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 else
NeilBrown0c55e022010-05-03 14:09:02 +10005336 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5337 " out of %d devices, algorithm %d\n",
5338 mdname(mddev), conf->level,
5339 mddev->raid_disks - mddev->degraded,
5340 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341
5342 print_raid5_conf(conf);
5343
NeilBrownfef9c612009-03-31 15:16:46 +11005344 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11005345 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005346 atomic_set(&conf->reshape_stripes, 0);
5347 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5348 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5349 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5350 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5351 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005352 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005353 }
5354
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355
5356 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005357 if (mddev->to_remove == &raid5_attrs_group)
5358 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005359 else if (mddev->kobj.sd &&
5360 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005361 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10005362 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08005363 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10005364 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5365
5366 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005367 int chunk_size;
NeilBrown4a5add42010-06-01 19:37:28 +10005368 /* read-ahead size must cover two whole stripes, which
5369 * is 2 * (datadisks) * chunksize where 'n' is the
5370 * number of raid devices
5371 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5373 int stripe = data_disks *
5374 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5375 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5376 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10005377
5378 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005379
5380 mddev->queue->backing_dev_info.congested_data = mddev;
5381 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10005382
5383 chunk_size = mddev->chunk_sectors << 9;
5384 blk_queue_io_min(mddev->queue, chunk_size);
5385 blk_queue_io_opt(mddev->queue, chunk_size *
5386 (conf->raid_disks - conf->max_degraded));
5387
NeilBrown05616be2012-05-21 09:27:00 +10005388 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005389 disk_stack_limits(mddev->gendisk, rdev->bdev,
5390 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10005391 disk_stack_limits(mddev->gendisk, rdev->bdev,
5392 rdev->new_data_offset << 9);
5393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 }
5395
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 return 0;
5397abort:
NeilBrown01f96c02011-09-21 15:30:20 +10005398 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11005399 print_raid5_conf(conf);
5400 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005402 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 return -EIO;
5404}
5405
NeilBrownfd01b882011-10-11 16:47:53 +11005406static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407{
NeilBrownd1688a62011-10-11 16:49:52 +11005408 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409
NeilBrown01f96c02011-09-21 15:30:20 +10005410 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005411 if (mddev->queue)
5412 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10005413 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005414 mddev->private = NULL;
5415 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 return 0;
5417}
5418
NeilBrownfd01b882011-10-11 16:47:53 +11005419static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420{
NeilBrownd1688a62011-10-11 16:49:52 +11005421 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 int i;
5423
Andre Noll9d8f0362009-06-18 08:45:01 +10005424 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5425 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005426 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 for (i = 0; i < conf->raid_disks; i++)
5428 seq_printf (seq, "%s",
5429 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005430 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432}
5433
NeilBrownd1688a62011-10-11 16:49:52 +11005434static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435{
5436 int i;
5437 struct disk_info *tmp;
5438
NeilBrown0c55e022010-05-03 14:09:02 +10005439 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 if (!conf) {
5441 printk("(conf==NULL)\n");
5442 return;
5443 }
NeilBrown0c55e022010-05-03 14:09:02 +10005444 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5445 conf->raid_disks,
5446 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447
5448 for (i = 0; i < conf->raid_disks; i++) {
5449 char b[BDEVNAME_SIZE];
5450 tmp = conf->disks + i;
5451 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005452 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5453 i, !test_bit(Faulty, &tmp->rdev->flags),
5454 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 }
5456}
5457
NeilBrownfd01b882011-10-11 16:47:53 +11005458static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459{
5460 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005461 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005463 int count = 0;
5464 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465
5466 for (i = 0; i < conf->raid_disks; i++) {
5467 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11005468 if (tmp->replacement
5469 && tmp->replacement->recovery_offset == MaxSector
5470 && !test_bit(Faulty, &tmp->replacement->flags)
5471 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
5472 /* Replacement has just become active. */
5473 if (!tmp->rdev
5474 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
5475 count++;
5476 if (tmp->rdev) {
5477 /* Replaced device not technically faulty,
5478 * but we need to be sure it gets removed
5479 * and never re-added.
5480 */
5481 set_bit(Faulty, &tmp->rdev->flags);
5482 sysfs_notify_dirent_safe(
5483 tmp->rdev->sysfs_state);
5484 }
5485 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
5486 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005487 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005488 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005489 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005490 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005491 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 }
5493 }
NeilBrown6b965622010-08-18 11:56:59 +10005494 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005495 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005496 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005498 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499}
5500
NeilBrownb8321b62011-12-23 10:17:51 +11005501static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502{
NeilBrownd1688a62011-10-11 16:49:52 +11005503 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11005505 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11005506 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 struct disk_info *p = conf->disks + number;
5508
5509 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11005510 if (rdev == p->rdev)
5511 rdevp = &p->rdev;
5512 else if (rdev == p->replacement)
5513 rdevp = &p->replacement;
5514 else
5515 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11005516
NeilBrown657e3e42011-12-23 10:17:52 +11005517 if (number >= conf->raid_disks &&
5518 conf->reshape_progress == MaxSector)
5519 clear_bit(In_sync, &rdev->flags);
5520
5521 if (test_bit(In_sync, &rdev->flags) ||
5522 atomic_read(&rdev->nr_pending)) {
5523 err = -EBUSY;
5524 goto abort;
5525 }
5526 /* Only remove non-faulty devices if recovery
5527 * isn't possible.
5528 */
5529 if (!test_bit(Faulty, &rdev->flags) &&
5530 mddev->recovery_disabled != conf->recovery_disabled &&
5531 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11005532 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11005533 number < conf->raid_disks) {
5534 err = -EBUSY;
5535 goto abort;
5536 }
5537 *rdevp = NULL;
5538 synchronize_rcu();
5539 if (atomic_read(&rdev->nr_pending)) {
5540 /* lost the race, try later */
5541 err = -EBUSY;
5542 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11005543 } else if (p->replacement) {
5544 /* We must have just cleared 'rdev' */
5545 p->rdev = p->replacement;
5546 clear_bit(Replacement, &p->replacement->flags);
5547 smp_mb(); /* Make sure other CPUs may see both as identical
5548 * but will never see neither - if they are careful
5549 */
5550 p->replacement = NULL;
5551 clear_bit(WantReplacement, &rdev->flags);
5552 } else
5553 /* We might have just removed the Replacement as faulty-
5554 * clear the bit just in case
5555 */
5556 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557abort:
5558
5559 print_raid5_conf(conf);
5560 return err;
5561}
5562
NeilBrownfd01b882011-10-11 16:47:53 +11005563static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564{
NeilBrownd1688a62011-10-11 16:49:52 +11005565 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005566 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 int disk;
5568 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005569 int first = 0;
5570 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571
NeilBrown7f0da592011-07-28 11:39:22 +10005572 if (mddev->recovery_disabled == conf->recovery_disabled)
5573 return -EBUSY;
5574
NeilBrowndc10c642012-03-19 12:46:37 +11005575 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005577 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578
Neil Brown6c2fce22008-06-28 08:31:31 +10005579 if (rdev->raid_disk >= 0)
5580 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581
5582 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005583 * find the disk ... but prefer rdev->saved_raid_disk
5584 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005586 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005587 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005588 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10005589 first = rdev->saved_raid_disk;
5590
5591 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11005592 p = conf->disks + disk;
5593 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005594 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005596 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005597 if (rdev->saved_raid_disk != disk)
5598 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005599 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10005600 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005602 }
5603 for (disk = first; disk <= last; disk++) {
5604 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11005605 if (test_bit(WantReplacement, &p->rdev->flags) &&
5606 p->replacement == NULL) {
5607 clear_bit(In_sync, &rdev->flags);
5608 set_bit(Replacement, &rdev->flags);
5609 rdev->raid_disk = disk;
5610 err = 0;
5611 conf->fullsync = 1;
5612 rcu_assign_pointer(p->replacement, rdev);
5613 break;
5614 }
5615 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005616out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005618 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619}
5620
NeilBrownfd01b882011-10-11 16:47:53 +11005621static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622{
5623 /* no resync is happening, and there is enough space
5624 * on all devices, so we can resize.
5625 * We need to make sure resync covers any new space.
5626 * If the array is shrinking we should possibly wait until
5627 * any io in the removed space completes, but it hardly seems
5628 * worth it.
5629 */
NeilBrowna4a61252012-05-22 13:55:27 +10005630 sector_t newsize;
Andre Noll9d8f0362009-06-18 08:45:01 +10005631 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10005632 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
5633 if (mddev->external_size &&
5634 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11005635 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10005636 if (mddev->bitmap) {
5637 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
5638 if (ret)
5639 return ret;
5640 }
5641 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10005642 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005643 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005644 if (sectors > mddev->dev_sectors &&
5645 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005646 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5648 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005649 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005650 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 return 0;
5652}
5653
NeilBrownfd01b882011-10-11 16:47:53 +11005654static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005655{
5656 /* Can only proceed if there are plenty of stripe_heads.
5657 * We need a minimum of one full stripe,, and for sensible progress
5658 * it is best to have about 4 times that.
5659 * If we require 4 times, then the default 256 4K stripe_heads will
5660 * allow for chunk sizes up to 256K, which is probably OK.
5661 * If the chunk size is greater, user-space should request more
5662 * stripe_heads first.
5663 */
NeilBrownd1688a62011-10-11 16:49:52 +11005664 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005665 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5666 > conf->max_nr_stripes ||
5667 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5668 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005669 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5670 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005671 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5672 / STRIPE_SIZE)*4);
5673 return 0;
5674 }
5675 return 1;
5676}
5677
NeilBrownfd01b882011-10-11 16:47:53 +11005678static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005679{
NeilBrownd1688a62011-10-11 16:49:52 +11005680 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005681
NeilBrown88ce4932009-03-31 15:24:23 +11005682 if (mddev->delta_disks == 0 &&
5683 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005684 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005685 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10005686 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005687 return -EINVAL;
5688 if (mddev->delta_disks < 0) {
5689 /* We might be able to shrink, but the devices must
5690 * be made bigger first.
5691 * For raid6, 4 is the minimum size.
5692 * Otherwise 2 is the minimum
5693 */
5694 int min = 2;
5695 if (mddev->level == 6)
5696 min = 4;
5697 if (mddev->raid_disks + mddev->delta_disks < min)
5698 return -EINVAL;
5699 }
NeilBrown29269552006-03-27 01:18:10 -08005700
NeilBrown01ee22b2009-06-18 08:47:20 +10005701 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005702 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005703
NeilBrownec32a2b2009-03-31 15:17:38 +11005704 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
NeilBrown63c70c42006-03-27 01:18:13 -08005705}
5706
NeilBrownfd01b882011-10-11 16:47:53 +11005707static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005708{
NeilBrownd1688a62011-10-11 16:49:52 +11005709 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005710 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005711 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005712 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005713
NeilBrownf4168852007-02-28 20:11:53 -08005714 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005715 return -EBUSY;
5716
NeilBrown01ee22b2009-06-18 08:47:20 +10005717 if (!check_stripe_cache(mddev))
5718 return -ENOSPC;
5719
NeilBrown30b67642012-05-22 13:55:28 +10005720 if (has_failed(conf))
5721 return -EINVAL;
5722
NeilBrownc6563a82012-05-21 09:27:00 +10005723 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11005724 if (!test_bit(In_sync, &rdev->flags)
5725 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005726 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10005727 }
NeilBrown63c70c42006-03-27 01:18:13 -08005728
NeilBrownf4168852007-02-28 20:11:53 -08005729 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005730 /* Not enough devices even to make a degraded array
5731 * of that size
5732 */
5733 return -EINVAL;
5734
NeilBrownec32a2b2009-03-31 15:17:38 +11005735 /* Refuse to reduce size of the array. Any reductions in
5736 * array size must be through explicit setting of array_size
5737 * attribute.
5738 */
5739 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5740 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10005741 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11005742 "before number of disks\n", mdname(mddev));
5743 return -EINVAL;
5744 }
5745
NeilBrownf6705572006-03-27 01:18:11 -08005746 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005747 spin_lock_irq(&conf->device_lock);
5748 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005749 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005750 conf->prev_chunk_sectors = conf->chunk_sectors;
5751 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005752 conf->prev_algo = conf->algorithm;
5753 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10005754 conf->generation++;
5755 /* Code that selects data_offset needs to see the generation update
5756 * if reshape_progress has been set - so a memory barrier needed.
5757 */
5758 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10005759 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11005760 conf->reshape_progress = raid5_size(mddev, 0, 0);
5761 else
5762 conf->reshape_progress = 0;
5763 conf->reshape_safe = conf->reshape_progress;
NeilBrown29269552006-03-27 01:18:10 -08005764 spin_unlock_irq(&conf->device_lock);
5765
5766 /* Add some new drives, as many as will fit.
5767 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10005768 * Don't add devices if we are reducing the number of
5769 * devices in the array. This is because it is not possible
5770 * to correctly record the "partially reconstructed" state of
5771 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08005772 */
NeilBrown87a8dec2011-01-31 11:57:43 +11005773 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11005774 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11005775 if (rdev->raid_disk < 0 &&
5776 !test_bit(Faulty, &rdev->flags)) {
5777 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11005778 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11005779 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11005780 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11005781 else
NeilBrown87a8dec2011-01-31 11:57:43 +11005782 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10005783
5784 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11005785 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11005786 }
NeilBrown87a8dec2011-01-31 11:57:43 +11005787 } else if (rdev->raid_disk >= conf->previous_raid_disks
5788 && !test_bit(Faulty, &rdev->flags)) {
5789 /* This is a spare that was manually added */
5790 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11005791 }
NeilBrown29269552006-03-27 01:18:10 -08005792
NeilBrown87a8dec2011-01-31 11:57:43 +11005793 /* When a reshape changes the number of devices,
5794 * ->degraded is measured against the larger of the
5795 * pre and post number of devices.
5796 */
NeilBrownec32a2b2009-03-31 15:17:38 +11005797 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005798 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11005799 spin_unlock_irqrestore(&conf->device_lock, flags);
5800 }
NeilBrown63c70c42006-03-27 01:18:13 -08005801 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10005802 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07005803 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005804
NeilBrown29269552006-03-27 01:18:10 -08005805 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5806 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5807 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5808 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5809 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005810 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08005811 if (!mddev->sync_thread) {
5812 mddev->recovery = 0;
5813 spin_lock_irq(&conf->device_lock);
5814 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10005815 rdev_for_each(rdev, mddev)
5816 rdev->new_data_offset = rdev->data_offset;
5817 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11005818 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11005819 mddev->reshape_position = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005820 spin_unlock_irq(&conf->device_lock);
5821 return -EAGAIN;
5822 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005823 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005824 md_wakeup_thread(mddev->sync_thread);
5825 md_new_event(mddev);
5826 return 0;
5827}
NeilBrown29269552006-03-27 01:18:10 -08005828
NeilBrownec32a2b2009-03-31 15:17:38 +11005829/* This is called from the reshape thread and should make any
5830 * changes needed in 'conf'
5831 */
NeilBrownd1688a62011-10-11 16:49:52 +11005832static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08005833{
NeilBrown29269552006-03-27 01:18:10 -08005834
NeilBrownf6705572006-03-27 01:18:11 -08005835 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10005836 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07005837
NeilBrownf6705572006-03-27 01:18:11 -08005838 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11005839 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10005840 rdev_for_each(rdev, conf->mddev)
5841 rdev->data_offset = rdev->new_data_offset;
5842 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11005843 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08005844 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005845 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07005846
5847 /* read-ahead size must cover two whole stripes, which is
5848 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5849 */
NeilBrown4a5add42010-06-01 19:37:28 +10005850 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11005851 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005852 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11005853 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07005854 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5855 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5856 }
NeilBrown29269552006-03-27 01:18:10 -08005857 }
NeilBrown29269552006-03-27 01:18:10 -08005858}
5859
NeilBrownec32a2b2009-03-31 15:17:38 +11005860/* This is called from the raid5d thread with mddev_lock held.
5861 * It makes config changes to the device.
5862 */
NeilBrownfd01b882011-10-11 16:47:53 +11005863static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11005864{
NeilBrownd1688a62011-10-11 16:49:52 +11005865 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11005866
5867 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5868
NeilBrownec32a2b2009-03-31 15:17:38 +11005869 if (mddev->delta_disks > 0) {
5870 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5871 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005872 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11005873 } else {
5874 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11005875 spin_lock_irq(&conf->device_lock);
5876 mddev->degraded = calc_degraded(conf);
5877 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11005878 for (d = conf->raid_disks ;
5879 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10005880 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11005881 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10005882 if (rdev)
5883 clear_bit(In_sync, &rdev->flags);
5884 rdev = conf->disks[d].replacement;
5885 if (rdev)
5886 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10005887 }
NeilBrowncea9c222009-03-31 15:15:05 +11005888 }
NeilBrown88ce4932009-03-31 15:24:23 +11005889 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005890 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11005891 mddev->reshape_position = MaxSector;
5892 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005893 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11005894 }
5895}
5896
NeilBrownfd01b882011-10-11 16:47:53 +11005897static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07005898{
NeilBrownd1688a62011-10-11 16:49:52 +11005899 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07005900
5901 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08005902 case 2: /* resume for a suspend */
5903 wake_up(&conf->wait_for_overlap);
5904 break;
5905
NeilBrown72626682005-09-09 16:23:54 -07005906 case 1: /* stop all writes */
5907 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005908 /* '2' tells resync/reshape to pause so that all
5909 * active stripes can drain
5910 */
5911 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07005912 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005913 atomic_read(&conf->active_stripes) == 0 &&
5914 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07005915 conf->device_lock, /* nothing */);
NeilBrown64bd6602009-08-03 10:59:58 +10005916 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07005917 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005918 /* allow reshape to continue */
5919 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005920 break;
5921
5922 case 0: /* re-enable writes */
5923 spin_lock_irq(&conf->device_lock);
5924 conf->quiesce = 0;
5925 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08005926 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005927 spin_unlock_irq(&conf->device_lock);
5928 break;
5929 }
NeilBrown72626682005-09-09 16:23:54 -07005930}
NeilBrownb15c2e52006-01-06 00:20:16 -08005931
NeilBrownd562b0c2009-03-31 14:39:39 +11005932
NeilBrownfd01b882011-10-11 16:47:53 +11005933static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11005934{
NeilBrowne373ab12011-10-11 16:48:59 +11005935 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07005936 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11005937
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005938 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11005939 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10005940 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
5941 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005942 return ERR_PTR(-EINVAL);
5943 }
5944
NeilBrowne373ab12011-10-11 16:48:59 +11005945 sectors = raid0_conf->strip_zone[0].zone_end;
5946 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10005947 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005948 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11005949 mddev->new_layout = ALGORITHM_PARITY_N;
5950 mddev->new_chunk_sectors = mddev->chunk_sectors;
5951 mddev->raid_disks += 1;
5952 mddev->delta_disks = 1;
5953 /* make sure it will be not marked as dirty */
5954 mddev->recovery_cp = MaxSector;
5955
5956 return setup_conf(mddev);
5957}
5958
5959
NeilBrownfd01b882011-10-11 16:47:53 +11005960static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005961{
5962 int chunksect;
5963
5964 if (mddev->raid_disks != 2 ||
5965 mddev->degraded > 1)
5966 return ERR_PTR(-EINVAL);
5967
5968 /* Should check if there are write-behind devices? */
5969
5970 chunksect = 64*2; /* 64K by default */
5971
5972 /* The array must be an exact multiple of chunksize */
5973 while (chunksect && (mddev->array_sectors & (chunksect-1)))
5974 chunksect >>= 1;
5975
5976 if ((chunksect<<9) < STRIPE_SIZE)
5977 /* array size does not allow a suitable chunk size */
5978 return ERR_PTR(-EINVAL);
5979
5980 mddev->new_level = 5;
5981 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10005982 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11005983
5984 return setup_conf(mddev);
5985}
5986
NeilBrownfd01b882011-10-11 16:47:53 +11005987static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11005988{
5989 int new_layout;
5990
5991 switch (mddev->layout) {
5992 case ALGORITHM_LEFT_ASYMMETRIC_6:
5993 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
5994 break;
5995 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5996 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
5997 break;
5998 case ALGORITHM_LEFT_SYMMETRIC_6:
5999 new_layout = ALGORITHM_LEFT_SYMMETRIC;
6000 break;
6001 case ALGORITHM_RIGHT_SYMMETRIC_6:
6002 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
6003 break;
6004 case ALGORITHM_PARITY_0_6:
6005 new_layout = ALGORITHM_PARITY_0;
6006 break;
6007 case ALGORITHM_PARITY_N:
6008 new_layout = ALGORITHM_PARITY_N;
6009 break;
6010 default:
6011 return ERR_PTR(-EINVAL);
6012 }
6013 mddev->new_level = 5;
6014 mddev->new_layout = new_layout;
6015 mddev->delta_disks = -1;
6016 mddev->raid_disks -= 1;
6017 return setup_conf(mddev);
6018}
6019
NeilBrownd562b0c2009-03-31 14:39:39 +11006020
NeilBrownfd01b882011-10-11 16:47:53 +11006021static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11006022{
NeilBrown88ce4932009-03-31 15:24:23 +11006023 /* For a 2-drive array, the layout and chunk size can be changed
6024 * immediately as not restriping is needed.
6025 * For larger arrays we record the new value - after validation
6026 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11006027 */
NeilBrownd1688a62011-10-11 16:49:52 +11006028 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10006029 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11006030
NeilBrown597a7112009-06-18 08:47:42 +10006031 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11006032 return -EINVAL;
6033 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006034 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11006035 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006036 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11006037 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006038 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11006039 /* not factor of array size */
6040 return -EINVAL;
6041 }
6042
6043 /* They look valid */
6044
NeilBrown88ce4932009-03-31 15:24:23 +11006045 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10006046 /* can make the change immediately */
6047 if (mddev->new_layout >= 0) {
6048 conf->algorithm = mddev->new_layout;
6049 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11006050 }
6051 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10006052 conf->chunk_sectors = new_chunk ;
6053 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11006054 }
6055 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6056 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11006057 }
NeilBrown50ac1682009-06-18 08:47:55 +10006058 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11006059}
6060
NeilBrownfd01b882011-10-11 16:47:53 +11006061static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11006062{
NeilBrown597a7112009-06-18 08:47:42 +10006063 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10006064
NeilBrown597a7112009-06-18 08:47:42 +10006065 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11006066 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006067 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006068 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11006069 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006070 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11006071 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006072 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11006073 /* not factor of array size */
6074 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006075 }
NeilBrown88ce4932009-03-31 15:24:23 +11006076
6077 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10006078 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11006079}
6080
NeilBrownfd01b882011-10-11 16:47:53 +11006081static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006082{
6083 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006084 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006085 * raid1 - if there are two drives. We need to know the chunk size
6086 * raid4 - trivial - just use a raid4 layout.
6087 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006088 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006089 if (mddev->level == 0)
6090 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11006091 if (mddev->level == 1)
6092 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11006093 if (mddev->level == 4) {
6094 mddev->new_layout = ALGORITHM_PARITY_N;
6095 mddev->new_level = 5;
6096 return setup_conf(mddev);
6097 }
NeilBrownfc9739c2009-03-31 14:57:20 +11006098 if (mddev->level == 6)
6099 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11006100
6101 return ERR_PTR(-EINVAL);
6102}
6103
NeilBrownfd01b882011-10-11 16:47:53 +11006104static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11006105{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006106 /* raid4 can take over:
6107 * raid0 - if there is only one strip zone
6108 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11006109 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006110 if (mddev->level == 0)
6111 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11006112 if (mddev->level == 5 &&
6113 mddev->layout == ALGORITHM_PARITY_N) {
6114 mddev->new_layout = 0;
6115 mddev->new_level = 4;
6116 return setup_conf(mddev);
6117 }
6118 return ERR_PTR(-EINVAL);
6119}
NeilBrownd562b0c2009-03-31 14:39:39 +11006120
NeilBrown84fc4b52011-10-11 16:49:58 +11006121static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11006122
NeilBrownfd01b882011-10-11 16:47:53 +11006123static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11006124{
6125 /* Currently can only take over a raid5. We map the
6126 * personality to an equivalent raid6 personality
6127 * with the Q block at the end.
6128 */
6129 int new_layout;
6130
6131 if (mddev->pers != &raid5_personality)
6132 return ERR_PTR(-EINVAL);
6133 if (mddev->degraded > 1)
6134 return ERR_PTR(-EINVAL);
6135 if (mddev->raid_disks > 253)
6136 return ERR_PTR(-EINVAL);
6137 if (mddev->raid_disks < 3)
6138 return ERR_PTR(-EINVAL);
6139
6140 switch (mddev->layout) {
6141 case ALGORITHM_LEFT_ASYMMETRIC:
6142 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
6143 break;
6144 case ALGORITHM_RIGHT_ASYMMETRIC:
6145 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
6146 break;
6147 case ALGORITHM_LEFT_SYMMETRIC:
6148 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
6149 break;
6150 case ALGORITHM_RIGHT_SYMMETRIC:
6151 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
6152 break;
6153 case ALGORITHM_PARITY_0:
6154 new_layout = ALGORITHM_PARITY_0_6;
6155 break;
6156 case ALGORITHM_PARITY_N:
6157 new_layout = ALGORITHM_PARITY_N;
6158 break;
6159 default:
6160 return ERR_PTR(-EINVAL);
6161 }
6162 mddev->new_level = 6;
6163 mddev->new_layout = new_layout;
6164 mddev->delta_disks = 1;
6165 mddev->raid_disks += 1;
6166 return setup_conf(mddev);
6167}
6168
6169
NeilBrown84fc4b52011-10-11 16:49:58 +11006170static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07006171{
6172 .name = "raid6",
6173 .level = 6,
6174 .owner = THIS_MODULE,
6175 .make_request = make_request,
6176 .run = run,
6177 .stop = stop,
6178 .status = status,
6179 .error_handler = error,
6180 .hot_add_disk = raid5_add_disk,
6181 .hot_remove_disk= raid5_remove_disk,
6182 .spare_active = raid5_spare_active,
6183 .sync_request = sync_request,
6184 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006185 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10006186 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08006187 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006188 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07006189 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11006190 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07006191};
NeilBrown84fc4b52011-10-11 16:49:58 +11006192static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193{
6194 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08006195 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196 .owner = THIS_MODULE,
6197 .make_request = make_request,
6198 .run = run,
6199 .stop = stop,
6200 .status = status,
6201 .error_handler = error,
6202 .hot_add_disk = raid5_add_disk,
6203 .hot_remove_disk= raid5_remove_disk,
6204 .spare_active = raid5_spare_active,
6205 .sync_request = sync_request,
6206 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006207 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08006208 .check_reshape = raid5_check_reshape,
6209 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006210 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07006211 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11006212 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213};
6214
NeilBrown84fc4b52011-10-11 16:49:58 +11006215static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216{
NeilBrown2604b702006-01-06 00:20:36 -08006217 .name = "raid4",
6218 .level = 4,
6219 .owner = THIS_MODULE,
6220 .make_request = make_request,
6221 .run = run,
6222 .stop = stop,
6223 .status = status,
6224 .error_handler = error,
6225 .hot_add_disk = raid5_add_disk,
6226 .hot_remove_disk= raid5_remove_disk,
6227 .spare_active = raid5_spare_active,
6228 .sync_request = sync_request,
6229 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006230 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08006231 .check_reshape = raid5_check_reshape,
6232 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006233 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08006234 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11006235 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08006236};
6237
6238static int __init raid5_init(void)
6239{
NeilBrown16a53ec2006-06-26 00:27:38 -07006240 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006241 register_md_personality(&raid5_personality);
6242 register_md_personality(&raid4_personality);
6243 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006244}
6245
NeilBrown2604b702006-01-06 00:20:36 -08006246static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006247{
NeilBrown16a53ec2006-06-26 00:27:38 -07006248 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006249 unregister_md_personality(&raid5_personality);
6250 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251}
6252
6253module_init(raid5_init);
6254module_exit(raid5_exit);
6255MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11006256MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08006258MODULE_ALIAS("md-raid5");
6259MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08006260MODULE_ALIAS("md-level-5");
6261MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07006262MODULE_ALIAS("md-personality-8"); /* RAID6 */
6263MODULE_ALIAS("md-raid6");
6264MODULE_ALIAS("md-level-6");
6265
6266/* This used to be two separate modules, they were: */
6267MODULE_ALIAS("raid5");
6268MODULE_ALIAS("raid6");