blob: 68706970d2175bc0f394cf5eab8da05f39505752 [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>
NeilBrowna9add5d2012-10-31 11:59:09 +110056#include <trace/events/block.h>
57
NeilBrown43b2e5d2009-03-31 14:33:13 +110058#include "md.h"
NeilBrownbff61972009-03-31 14:33:13 +110059#include "raid5.h"
Trela Maciej54071b32010-03-08 16:02:42 +110060#include "raid0.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110061#include "bitmap.h"
NeilBrown72626682005-09-09 16:23:54 -070062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/*
64 * Stripe cache
65 */
66
67#define NR_STRIPES 256
68#define STRIPE_SIZE PAGE_SIZE
69#define STRIPE_SHIFT (PAGE_SHIFT - 9)
70#define STRIPE_SECTORS (STRIPE_SIZE>>9)
71#define IO_THRESHOLD 1
Dan Williams8b3e6cd2008-04-28 02:15:53 -070072#define BYPASS_THRESHOLD 1
NeilBrownfccddba2006-01-06 00:20:33 -080073#define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define HASH_MASK (NR_HASH - 1)
75
NeilBrownd1688a62011-10-11 16:49:52 +110076static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
NeilBrowndb298e12011-10-07 14:23:00 +110077{
78 int hash = (sect >> STRIPE_SHIFT) & HASH_MASK;
79 return &conf->stripe_hashtbl[hash];
80}
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82/* bio's attached to a stripe+device for I/O are linked together in bi_sector
83 * order without overlap. There may be several bio's per stripe+device, and
84 * a bio could span several devices.
85 * When walking this list for a particular stripe+device, we must never proceed
86 * beyond a bio that extends past this device, as the next bio might no longer
87 * be valid.
NeilBrowndb298e12011-10-07 14:23:00 +110088 * This function is used to determine the 'next' bio in the list, given the sector
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 * of the current stripe+device
90 */
NeilBrowndb298e12011-10-07 14:23:00 +110091static inline struct bio *r5_next_bio(struct bio *bio, sector_t sector)
92{
93 int sectors = bio->bi_size >> 9;
94 if (bio->bi_sector + sectors < sector + STRIPE_SECTORS)
95 return bio->bi_next;
96 else
97 return NULL;
98}
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Jens Axboe960e7392008-08-15 10:41:18 +0200100/*
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200101 * We maintain a biased count of active stripes in the bottom 16 bits of
102 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
Jens Axboe960e7392008-08-15 10:41:18 +0200103 */
Shaohua Lie7836bd62012-07-19 16:01:31 +1000104static inline int raid5_bi_processed_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200105{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000106 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
107 return (atomic_read(segments) >> 16) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200108}
109
Shaohua Lie7836bd62012-07-19 16:01:31 +1000110static inline int raid5_dec_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200111{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000112 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
113 return atomic_sub_return(1, segments) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200114}
115
Shaohua Lie7836bd62012-07-19 16:01:31 +1000116static inline void raid5_inc_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200117{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000118 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
119 atomic_inc(segments);
Jens Axboe960e7392008-08-15 10:41:18 +0200120}
121
Shaohua Lie7836bd62012-07-19 16:01:31 +1000122static inline void raid5_set_bi_processed_stripes(struct bio *bio,
123 unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200124{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000125 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
126 int old, new;
Jens Axboe960e7392008-08-15 10:41:18 +0200127
Shaohua Lie7836bd62012-07-19 16:01:31 +1000128 do {
129 old = atomic_read(segments);
130 new = (old & 0xffff) | (cnt << 16);
131 } while (atomic_cmpxchg(segments, old, new) != old);
Jens Axboe960e7392008-08-15 10:41:18 +0200132}
133
Shaohua Lie7836bd62012-07-19 16:01:31 +1000134static inline void raid5_set_bi_stripes(struct bio *bio, unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200135{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000136 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
137 atomic_set(segments, cnt);
Jens Axboe960e7392008-08-15 10:41:18 +0200138}
139
NeilBrownd0dabf72009-03-31 14:39:38 +1100140/* Find first data disk in a raid6 stripe */
141static inline int raid6_d0(struct stripe_head *sh)
142{
NeilBrown67cc2b82009-03-31 14:39:38 +1100143 if (sh->ddf_layout)
144 /* ddf always start from first device */
145 return 0;
146 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100147 if (sh->qd_idx == sh->disks - 1)
148 return 0;
149 else
150 return sh->qd_idx + 1;
151}
NeilBrown16a53ec2006-06-26 00:27:38 -0700152static inline int raid6_next_disk(int disk, int raid_disks)
153{
154 disk++;
155 return (disk < raid_disks) ? disk : 0;
156}
Dan Williamsa4456852007-07-09 11:56:43 -0700157
NeilBrownd0dabf72009-03-31 14:39:38 +1100158/* When walking through the disks in a raid5, starting at raid6_d0,
159 * We need to map each disk to a 'slot', where the data disks are slot
160 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
161 * is raid_disks-1. This help does that mapping.
162 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100163static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
164 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100165{
Dan Williams66295422009-10-19 18:09:32 -0700166 int slot = *count;
NeilBrown67cc2b82009-03-31 14:39:38 +1100167
NeilBrowne4424fe2009-10-16 16:27:34 +1100168 if (sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700169 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100170 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100171 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100172 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100173 return syndrome_disks + 1;
NeilBrowne4424fe2009-10-16 16:27:34 +1100174 if (!sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700175 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100176 return slot;
177}
178
Dan Williamsa4456852007-07-09 11:56:43 -0700179static void return_io(struct bio *return_bi)
180{
181 struct bio *bi = return_bi;
182 while (bi) {
Dan Williamsa4456852007-07-09 11:56:43 -0700183
184 return_bi = bi->bi_next;
185 bi->bi_next = NULL;
186 bi->bi_size = 0;
Neil Brown0e13fe232008-06-28 08:31:20 +1000187 bio_endio(bi, 0);
Dan Williamsa4456852007-07-09 11:56:43 -0700188 bi = return_bi;
189 }
190}
191
NeilBrownd1688a62011-10-11 16:49:52 +1100192static void print_raid5_conf (struct r5conf *conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Dan Williams600aa102008-06-28 08:32:05 +1000194static int stripe_operations_active(struct stripe_head *sh)
195{
196 return sh->check_state || sh->reconstruct_state ||
197 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
198 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
199}
200
Shaohua Li4eb788d2012-07-19 16:01:31 +1000201static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Shaohua Li4eb788d2012-07-19 16:01:31 +1000203 BUG_ON(!list_empty(&sh->lru));
204 BUG_ON(atomic_read(&conf->active_stripes)==0);
205 if (test_bit(STRIPE_HANDLE, &sh->state)) {
206 if (test_bit(STRIPE_DELAYED, &sh->state) &&
207 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
208 list_add_tail(&sh->lru, &conf->delayed_list);
209 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
210 sh->bm_seq - conf->seq_write > 0)
211 list_add_tail(&sh->lru, &conf->bitmap_list);
212 else {
213 clear_bit(STRIPE_DELAYED, &sh->state);
214 clear_bit(STRIPE_BIT_DELAY, &sh->state);
215 list_add_tail(&sh->lru, &conf->handle_list);
216 }
217 md_wakeup_thread(conf->mddev->thread);
218 } else {
219 BUG_ON(stripe_operations_active(sh));
220 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
221 if (atomic_dec_return(&conf->preread_active_stripes)
222 < IO_THRESHOLD)
223 md_wakeup_thread(conf->mddev->thread);
224 atomic_dec(&conf->active_stripes);
225 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
226 list_add_tail(&sh->lru, &conf->inactive_list);
227 wake_up(&conf->wait_for_stripe);
228 if (conf->retry_read_aligned)
229 md_wakeup_thread(conf->mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
231 }
232}
NeilBrownd0dabf72009-03-31 14:39:38 +1100233
Shaohua Li4eb788d2012-07-19 16:01:31 +1000234static void __release_stripe(struct r5conf *conf, struct stripe_head *sh)
235{
236 if (atomic_dec_and_test(&sh->count))
237 do_release_stripe(conf, sh);
238}
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240static void release_stripe(struct stripe_head *sh)
241{
NeilBrownd1688a62011-10-11 16:49:52 +1100242 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 unsigned long flags;
NeilBrown16a53ec2006-06-26 00:27:38 -0700244
Shaohua Li4eb788d2012-07-19 16:01:31 +1000245 local_irq_save(flags);
246 if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) {
247 do_release_stripe(conf, sh);
248 spin_unlock(&conf->device_lock);
249 }
250 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
NeilBrownfccddba2006-01-06 00:20:33 -0800253static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Dan Williams45b42332007-07-09 11:56:43 -0700255 pr_debug("remove_hash(), stripe %llu\n",
256 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
NeilBrownfccddba2006-01-06 00:20:33 -0800258 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
NeilBrownd1688a62011-10-11 16:49:52 +1100261static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
NeilBrownfccddba2006-01-06 00:20:33 -0800263 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Dan Williams45b42332007-07-09 11:56:43 -0700265 pr_debug("insert_hash(), stripe %llu\n",
266 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
NeilBrownfccddba2006-01-06 00:20:33 -0800268 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
271
272/* find an idle stripe, make sure it is unhashed, and return it. */
NeilBrownd1688a62011-10-11 16:49:52 +1100273static struct stripe_head *get_free_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 struct stripe_head *sh = NULL;
276 struct list_head *first;
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (list_empty(&conf->inactive_list))
279 goto out;
280 first = conf->inactive_list.next;
281 sh = list_entry(first, struct stripe_head, lru);
282 list_del_init(first);
283 remove_hash(sh);
284 atomic_inc(&conf->active_stripes);
285out:
286 return sh;
287}
288
NeilBrowne4e11e32010-06-16 16:45:16 +1000289static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 struct page *p;
292 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000293 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
NeilBrowne4e11e32010-06-16 16:45:16 +1000295 for (i = 0; i < num ; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 p = sh->dev[i].page;
297 if (!p)
298 continue;
299 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800300 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302}
303
NeilBrowne4e11e32010-06-16 16:45:16 +1000304static int grow_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
306 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000307 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
NeilBrowne4e11e32010-06-16 16:45:16 +1000309 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 struct page *page;
311
312 if (!(page = alloc_page(GFP_KERNEL))) {
313 return 1;
314 }
315 sh->dev[i].page = page;
316 }
317 return 0;
318}
319
NeilBrown784052e2009-03-31 15:19:07 +1100320static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrownd1688a62011-10-11 16:49:52 +1100321static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100322 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
NeilBrownb5663ba2009-03-31 14:39:38 +1100324static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
NeilBrownd1688a62011-10-11 16:49:52 +1100326 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800327 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200329 BUG_ON(atomic_read(&sh->count) != 0);
330 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000331 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700332
Dan Williams45b42332007-07-09 11:56:43 -0700333 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 (unsigned long long)sh->sector);
335
336 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700337
NeilBrown86b42c72009-03-31 15:19:03 +1100338 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100339 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100341 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 sh->state = 0;
343
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800344
345 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 struct r5dev *dev = &sh->dev[i];
347
Dan Williamsd84e0f12007-01-02 13:52:30 -0700348 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700350 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700352 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000354 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
356 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100357 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
359 insert_hash(conf, sh);
360}
361
NeilBrownd1688a62011-10-11 16:49:52 +1100362static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100363 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
365 struct stripe_head *sh;
366
Dan Williams45b42332007-07-09 11:56:43 -0700367 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800368 hlist_for_each_entry(sh, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100369 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700371 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return NULL;
373}
374
NeilBrown674806d2010-06-16 17:17:53 +1000375/*
376 * Need to check if array has failed when deciding whether to:
377 * - start an array
378 * - remove non-faulty devices
379 * - add a spare
380 * - allow a reshape
381 * This determination is simple when no reshape is happening.
382 * However if there is a reshape, we need to carefully check
383 * both the before and after sections.
384 * This is because some failed devices may only affect one
385 * of the two sections, and some non-in_sync devices may
386 * be insync in the section most affected by failed devices.
387 */
NeilBrown908f4fb2011-12-23 10:17:50 +1100388static int calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000389{
NeilBrown908f4fb2011-12-23 10:17:50 +1100390 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000391 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000392
393 rcu_read_lock();
394 degraded = 0;
395 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100396 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000397 if (rdev && test_bit(Faulty, &rdev->flags))
398 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000399 if (!rdev || test_bit(Faulty, &rdev->flags))
400 degraded++;
401 else if (test_bit(In_sync, &rdev->flags))
402 ;
403 else
404 /* not in-sync or faulty.
405 * If the reshape increases the number of devices,
406 * this is being recovered by the reshape, so
407 * this 'previous' section is not in_sync.
408 * If the number of devices is being reduced however,
409 * the device can only be part of the array if
410 * we are reverting a reshape, so this section will
411 * be in-sync.
412 */
413 if (conf->raid_disks >= conf->previous_raid_disks)
414 degraded++;
415 }
416 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100417 if (conf->raid_disks == conf->previous_raid_disks)
418 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000419 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100420 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000421 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100422 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000423 if (rdev && test_bit(Faulty, &rdev->flags))
424 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000425 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100426 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000427 else if (test_bit(In_sync, &rdev->flags))
428 ;
429 else
430 /* not in-sync or faulty.
431 * If reshape increases the number of devices, this
432 * section has already been recovered, else it
433 * almost certainly hasn't.
434 */
435 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100436 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000437 }
438 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100439 if (degraded2 > degraded)
440 return degraded2;
441 return degraded;
442}
443
444static int has_failed(struct r5conf *conf)
445{
446 int degraded;
447
448 if (conf->mddev->reshape_position == MaxSector)
449 return conf->mddev->degraded > conf->max_degraded;
450
451 degraded = calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000452 if (degraded > conf->max_degraded)
453 return 1;
454 return 0;
455}
456
NeilBrownb5663ba2009-03-31 14:39:38 +1100457static struct stripe_head *
NeilBrownd1688a62011-10-11 16:49:52 +1100458get_active_stripe(struct r5conf *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000459 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
461 struct stripe_head *sh;
462
Dan Williams45b42332007-07-09 11:56:43 -0700463 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 spin_lock_irq(&conf->device_lock);
466
467 do {
NeilBrown72626682005-09-09 16:23:54 -0700468 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000469 conf->quiesce == 0 || noquiesce,
Lukas Czernereed8c022012-11-30 11:42:40 +0100470 conf->device_lock);
NeilBrown86b42c72009-03-31 15:19:03 +1100471 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (!sh) {
473 if (!conf->inactive_blocked)
474 sh = get_free_stripe(conf);
475 if (noblock && sh == NULL)
476 break;
477 if (!sh) {
478 conf->inactive_blocked = 1;
479 wait_event_lock_irq(conf->wait_for_stripe,
480 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800481 (atomic_read(&conf->active_stripes)
482 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 || !conf->inactive_blocked),
Lukas Czernereed8c022012-11-30 11:42:40 +0100484 conf->device_lock);
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;
Shaohua Li9e4447682012-10-11 13:49:49 +1100554 if (test_bit(R5_Discard, &sh->dev[i].flags))
Shaohua Li620125f2012-10-11 13:49:05 +1100555 rw |= REQ_DISCARD;
Tejun Heoe9c74692010-09-03 11:56:18 +0200556 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700557 rw = READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100558 else if (test_and_clear_bit(R5_WantReplace,
559 &sh->dev[i].flags)) {
560 rw = WRITE;
561 replace_only = 1;
562 } else
Dan Williams91c00922007-01-02 13:52:30 -0700563 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +1000564 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
565 rw |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -0700566
567 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100568 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700569
570 bi->bi_rw = rw;
NeilBrown977df362011-12-23 10:17:53 +1100571 rbi->bi_rw = rw;
572 if (rw & WRITE) {
Dan Williams91c00922007-01-02 13:52:30 -0700573 bi->bi_end_io = raid5_end_write_request;
NeilBrown977df362011-12-23 10:17:53 +1100574 rbi->bi_end_io = raid5_end_write_request;
575 } else
Dan Williams91c00922007-01-02 13:52:30 -0700576 bi->bi_end_io = raid5_end_read_request;
577
578 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100579 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100580 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
581 rdev = rcu_dereference(conf->disks[i].rdev);
582 if (!rdev) {
583 rdev = rrdev;
584 rrdev = NULL;
585 }
NeilBrown9a3e1102011-12-23 10:17:53 +1100586 if (rw & WRITE) {
587 if (replace_only)
588 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100589 if (rdev == rrdev)
590 /* We raced and saw duplicates */
591 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100592 } else {
NeilBrowndd054fc2011-12-23 10:17:53 +1100593 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100594 rdev = rrdev;
595 rrdev = NULL;
596 }
NeilBrown977df362011-12-23 10:17:53 +1100597
Dan Williams91c00922007-01-02 13:52:30 -0700598 if (rdev && test_bit(Faulty, &rdev->flags))
599 rdev = NULL;
600 if (rdev)
601 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100602 if (rrdev && test_bit(Faulty, &rrdev->flags))
603 rrdev = NULL;
604 if (rrdev)
605 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700606 rcu_read_unlock();
607
NeilBrown73e92e52011-07-28 11:39:22 +1000608 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100609 * need to check for writes. We never accept write errors
610 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000611 */
612 while ((rw & WRITE) && rdev &&
613 test_bit(WriteErrorSeen, &rdev->flags)) {
614 sector_t first_bad;
615 int bad_sectors;
616 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
617 &first_bad, &bad_sectors);
618 if (!bad)
619 break;
620
621 if (bad < 0) {
622 set_bit(BlockedBadBlocks, &rdev->flags);
623 if (!conf->mddev->external &&
624 conf->mddev->flags) {
625 /* It is very unlikely, but we might
626 * still need to write out the
627 * bad block log - better give it
628 * a chance*/
629 md_check_recovery(conf->mddev);
630 }
majianpeng18507532012-07-03 12:11:54 +1000631 /*
632 * Because md_wait_for_blocked_rdev
633 * will dec nr_pending, we must
634 * increment it first.
635 */
636 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +1000637 md_wait_for_blocked_rdev(rdev, conf->mddev);
638 } else {
639 /* Acknowledged bad block - skip the write */
640 rdev_dec_pending(rdev, conf->mddev);
641 rdev = NULL;
642 }
643 }
644
Dan Williams91c00922007-01-02 13:52:30 -0700645 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100646 if (s->syncing || s->expanding || s->expanded
647 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700648 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
649
Dan Williams2b7497f2008-06-28 08:31:52 +1000650 set_bit(STRIPE_IO_STARTED, &sh->state);
651
Dan Williams91c00922007-01-02 13:52:30 -0700652 bi->bi_bdev = rdev->bdev;
653 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700654 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700655 bi->bi_rw, i);
656 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000657 if (use_new_offset(conf, sh))
658 bi->bi_sector = (sh->sector
659 + rdev->new_data_offset);
660 else
661 bi->bi_sector = (sh->sector
662 + rdev->data_offset);
majianpeng3f9e7c12012-07-31 10:04:21 +1000663 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
664 bi->bi_rw |= REQ_FLUSH;
665
Dan Williams91c00922007-01-02 13:52:30 -0700666 bi->bi_flags = 1 << BIO_UPTODATE;
Dan Williams91c00922007-01-02 13:52:30 -0700667 bi->bi_idx = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700668 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
669 bi->bi_io_vec[0].bv_offset = 0;
670 bi->bi_size = STRIPE_SIZE;
671 bi->bi_next = NULL;
NeilBrown977df362011-12-23 10:17:53 +1100672 if (rrdev)
673 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
NeilBrowna9add5d2012-10-31 11:59:09 +1100674 trace_block_bio_remap(bdev_get_queue(bi->bi_bdev),
675 bi, disk_devt(conf->mddev->gendisk),
676 sh->dev[i].sector);
Dan Williams91c00922007-01-02 13:52:30 -0700677 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100678 }
679 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100680 if (s->syncing || s->expanding || s->expanded
681 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +1100682 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
683
684 set_bit(STRIPE_IO_STARTED, &sh->state);
685
686 rbi->bi_bdev = rrdev->bdev;
687 pr_debug("%s: for %llu schedule op %ld on "
688 "replacement disc %d\n",
689 __func__, (unsigned long long)sh->sector,
690 rbi->bi_rw, i);
691 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000692 if (use_new_offset(conf, sh))
693 rbi->bi_sector = (sh->sector
694 + rrdev->new_data_offset);
695 else
696 rbi->bi_sector = (sh->sector
697 + rrdev->data_offset);
NeilBrown977df362011-12-23 10:17:53 +1100698 rbi->bi_flags = 1 << BIO_UPTODATE;
699 rbi->bi_idx = 0;
700 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
701 rbi->bi_io_vec[0].bv_offset = 0;
702 rbi->bi_size = STRIPE_SIZE;
703 rbi->bi_next = NULL;
NeilBrowna9add5d2012-10-31 11:59:09 +1100704 trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev),
705 rbi, disk_devt(conf->mddev->gendisk),
706 sh->dev[i].sector);
NeilBrown977df362011-12-23 10:17:53 +1100707 generic_make_request(rbi);
708 }
709 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000710 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700711 set_bit(STRIPE_DEGRADED, &sh->state);
712 pr_debug("skip op %ld on disc %d for sector %llu\n",
713 bi->bi_rw, i, (unsigned long long)sh->sector);
714 clear_bit(R5_LOCKED, &sh->dev[i].flags);
715 set_bit(STRIPE_HANDLE, &sh->state);
716 }
717 }
718}
719
720static struct dma_async_tx_descriptor *
721async_copy_data(int frombio, struct bio *bio, struct page *page,
722 sector_t sector, struct dma_async_tx_descriptor *tx)
723{
724 struct bio_vec *bvl;
725 struct page *bio_page;
726 int i;
727 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700728 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700729 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700730
731 if (bio->bi_sector >= sector)
732 page_offset = (signed)(bio->bi_sector - sector) * 512;
733 else
734 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700735
Dan Williams0403e382009-09-08 17:42:50 -0700736 if (frombio)
737 flags |= ASYNC_TX_FENCE;
738 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
739
Dan Williams91c00922007-01-02 13:52:30 -0700740 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000741 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700742 int clen;
743 int b_offset = 0;
744
745 if (page_offset < 0) {
746 b_offset = -page_offset;
747 page_offset += b_offset;
748 len -= b_offset;
749 }
750
751 if (len > 0 && page_offset + len > STRIPE_SIZE)
752 clen = STRIPE_SIZE - page_offset;
753 else
754 clen = len;
755
756 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000757 b_offset += bvl->bv_offset;
758 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700759 if (frombio)
760 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700761 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700762 else
763 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700764 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700765 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700766 /* chain the operations */
767 submit.depend_tx = tx;
768
Dan Williams91c00922007-01-02 13:52:30 -0700769 if (clen < len) /* hit end of page */
770 break;
771 page_offset += len;
772 }
773
774 return tx;
775}
776
777static void ops_complete_biofill(void *stripe_head_ref)
778{
779 struct stripe_head *sh = stripe_head_ref;
780 struct bio *return_bi = NULL;
Dan Williamse4d84902007-09-24 10:06:13 -0700781 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700782
Harvey Harrisone46b2722008-04-28 02:15:50 -0700783 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700784 (unsigned long long)sh->sector);
785
786 /* clear completed biofills */
787 for (i = sh->disks; i--; ) {
788 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700789
790 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700791 /* and check if we need to reply to a read request,
792 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000793 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700794 */
795 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700796 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700797
Dan Williams91c00922007-01-02 13:52:30 -0700798 BUG_ON(!dev->read);
799 rbi = dev->read;
800 dev->read = NULL;
801 while (rbi && rbi->bi_sector <
802 dev->sector + STRIPE_SECTORS) {
803 rbi2 = r5_next_bio(rbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +1000804 if (!raid5_dec_bi_active_stripes(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700805 rbi->bi_next = return_bi;
806 return_bi = rbi;
807 }
Dan Williams91c00922007-01-02 13:52:30 -0700808 rbi = rbi2;
809 }
810 }
811 }
Dan Williams83de75c2008-06-28 08:31:58 +1000812 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700813
814 return_io(return_bi);
815
Dan Williamse4d84902007-09-24 10:06:13 -0700816 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700817 release_stripe(sh);
818}
819
820static void ops_run_biofill(struct stripe_head *sh)
821{
822 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa08abd82009-06-03 11:43:59 -0700823 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700824 int i;
825
Harvey Harrisone46b2722008-04-28 02:15:50 -0700826 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700827 (unsigned long long)sh->sector);
828
829 for (i = sh->disks; i--; ) {
830 struct r5dev *dev = &sh->dev[i];
831 if (test_bit(R5_Wantfill, &dev->flags)) {
832 struct bio *rbi;
Shaohua Lib17459c2012-07-19 16:01:31 +1000833 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700834 dev->read = rbi = dev->toread;
835 dev->toread = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +1000836 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700837 while (rbi && rbi->bi_sector <
838 dev->sector + STRIPE_SECTORS) {
839 tx = async_copy_data(0, rbi, dev->page,
840 dev->sector, tx);
841 rbi = r5_next_bio(rbi, dev->sector);
842 }
843 }
844 }
845
846 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700847 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
848 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700849}
850
Dan Williams4e7d2c02009-08-29 19:13:11 -0700851static void mark_target_uptodate(struct stripe_head *sh, int target)
852{
853 struct r5dev *tgt;
854
855 if (target < 0)
856 return;
857
858 tgt = &sh->dev[target];
859 set_bit(R5_UPTODATE, &tgt->flags);
860 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
861 clear_bit(R5_Wantcompute, &tgt->flags);
862}
863
Dan Williamsac6b53b2009-07-14 13:40:19 -0700864static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700865{
866 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700867
Harvey Harrisone46b2722008-04-28 02:15:50 -0700868 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700869 (unsigned long long)sh->sector);
870
Dan Williamsac6b53b2009-07-14 13:40:19 -0700871 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700872 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700873 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700874
Dan Williamsecc65c92008-06-28 08:31:57 +1000875 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
876 if (sh->check_state == check_state_compute_run)
877 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700878 set_bit(STRIPE_HANDLE, &sh->state);
879 release_stripe(sh);
880}
881
Dan Williamsd6f38f32009-07-14 11:50:52 -0700882/* return a pointer to the address conversion region of the scribble buffer */
883static addr_conv_t *to_addr_conv(struct stripe_head *sh,
884 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700885{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700886 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
887}
888
889static struct dma_async_tx_descriptor *
890ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
891{
Dan Williams91c00922007-01-02 13:52:30 -0700892 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700893 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700894 int target = sh->ops.target;
895 struct r5dev *tgt = &sh->dev[target];
896 struct page *xor_dest = tgt->page;
897 int count = 0;
898 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700899 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700900 int i;
901
902 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700903 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700904 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
905
906 for (i = disks; i--; )
907 if (i != target)
908 xor_srcs[count++] = sh->dev[i].page;
909
910 atomic_inc(&sh->count);
911
Dan Williams0403e382009-09-08 17:42:50 -0700912 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700913 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700914 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700915 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700916 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700917 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700918
Dan Williams91c00922007-01-02 13:52:30 -0700919 return tx;
920}
921
Dan Williamsac6b53b2009-07-14 13:40:19 -0700922/* set_syndrome_sources - populate source buffers for gen_syndrome
923 * @srcs - (struct page *) array of size sh->disks
924 * @sh - stripe_head to parse
925 *
926 * Populates srcs in proper layout order for the stripe and returns the
927 * 'count' of sources to be used in a call to async_gen_syndrome. The P
928 * destination buffer is recorded in srcs[count] and the Q destination
929 * is recorded in srcs[count+1]].
930 */
931static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
932{
933 int disks = sh->disks;
934 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
935 int d0_idx = raid6_d0(sh);
936 int count;
937 int i;
938
939 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100940 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700941
942 count = 0;
943 i = d0_idx;
944 do {
945 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
946
947 srcs[slot] = sh->dev[i].page;
948 i = raid6_next_disk(i, disks);
949 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700950
NeilBrowne4424fe2009-10-16 16:27:34 +1100951 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700952}
953
954static struct dma_async_tx_descriptor *
955ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
956{
957 int disks = sh->disks;
958 struct page **blocks = percpu->scribble;
959 int target;
960 int qd_idx = sh->qd_idx;
961 struct dma_async_tx_descriptor *tx;
962 struct async_submit_ctl submit;
963 struct r5dev *tgt;
964 struct page *dest;
965 int i;
966 int count;
967
968 if (sh->ops.target < 0)
969 target = sh->ops.target2;
970 else if (sh->ops.target2 < 0)
971 target = sh->ops.target;
972 else
973 /* we should only have one valid target */
974 BUG();
975 BUG_ON(target < 0);
976 pr_debug("%s: stripe %llu block: %d\n",
977 __func__, (unsigned long long)sh->sector, target);
978
979 tgt = &sh->dev[target];
980 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
981 dest = tgt->page;
982
983 atomic_inc(&sh->count);
984
985 if (target == qd_idx) {
986 count = set_syndrome_sources(blocks, sh);
987 blocks[count] = NULL; /* regenerating p is not necessary */
988 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700989 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
990 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700991 to_addr_conv(sh, percpu));
992 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
993 } else {
994 /* Compute any data- or p-drive using XOR */
995 count = 0;
996 for (i = disks; i-- ; ) {
997 if (i == target || i == qd_idx)
998 continue;
999 blocks[count++] = sh->dev[i].page;
1000 }
1001
Dan Williams0403e382009-09-08 17:42:50 -07001002 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1003 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001004 to_addr_conv(sh, percpu));
1005 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
1006 }
1007
1008 return tx;
1009}
1010
1011static struct dma_async_tx_descriptor *
1012ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1013{
1014 int i, count, disks = sh->disks;
1015 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1016 int d0_idx = raid6_d0(sh);
1017 int faila = -1, failb = -1;
1018 int target = sh->ops.target;
1019 int target2 = sh->ops.target2;
1020 struct r5dev *tgt = &sh->dev[target];
1021 struct r5dev *tgt2 = &sh->dev[target2];
1022 struct dma_async_tx_descriptor *tx;
1023 struct page **blocks = percpu->scribble;
1024 struct async_submit_ctl submit;
1025
1026 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1027 __func__, (unsigned long long)sh->sector, target, target2);
1028 BUG_ON(target < 0 || target2 < 0);
1029 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1030 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1031
Dan Williams6c910a72009-09-16 12:24:54 -07001032 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001033 * slot number conversion for 'faila' and 'failb'
1034 */
1035 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001036 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001037 count = 0;
1038 i = d0_idx;
1039 do {
1040 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1041
1042 blocks[slot] = sh->dev[i].page;
1043
1044 if (i == target)
1045 faila = slot;
1046 if (i == target2)
1047 failb = slot;
1048 i = raid6_next_disk(i, disks);
1049 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001050
1051 BUG_ON(faila == failb);
1052 if (failb < faila)
1053 swap(faila, failb);
1054 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1055 __func__, (unsigned long long)sh->sector, faila, failb);
1056
1057 atomic_inc(&sh->count);
1058
1059 if (failb == syndrome_disks+1) {
1060 /* Q disk is one of the missing disks */
1061 if (faila == syndrome_disks) {
1062 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001063 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1064 ops_complete_compute, sh,
1065 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +11001066 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001067 STRIPE_SIZE, &submit);
1068 } else {
1069 struct page *dest;
1070 int data_target;
1071 int qd_idx = sh->qd_idx;
1072
1073 /* Missing D+Q: recompute D from P, then recompute Q */
1074 if (target == qd_idx)
1075 data_target = target2;
1076 else
1077 data_target = target;
1078
1079 count = 0;
1080 for (i = disks; i-- ; ) {
1081 if (i == data_target || i == qd_idx)
1082 continue;
1083 blocks[count++] = sh->dev[i].page;
1084 }
1085 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001086 init_async_submit(&submit,
1087 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1088 NULL, NULL, NULL,
1089 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001090 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1091 &submit);
1092
1093 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001094 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1095 ops_complete_compute, sh,
1096 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001097 return async_gen_syndrome(blocks, 0, count+2,
1098 STRIPE_SIZE, &submit);
1099 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001100 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001101 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1102 ops_complete_compute, sh,
1103 to_addr_conv(sh, percpu));
1104 if (failb == syndrome_disks) {
1105 /* We're missing D+P. */
1106 return async_raid6_datap_recov(syndrome_disks+2,
1107 STRIPE_SIZE, faila,
1108 blocks, &submit);
1109 } else {
1110 /* We're missing D+D. */
1111 return async_raid6_2data_recov(syndrome_disks+2,
1112 STRIPE_SIZE, faila, failb,
1113 blocks, &submit);
1114 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001115 }
1116}
1117
1118
Dan Williams91c00922007-01-02 13:52:30 -07001119static void ops_complete_prexor(void *stripe_head_ref)
1120{
1121 struct stripe_head *sh = stripe_head_ref;
1122
Harvey Harrisone46b2722008-04-28 02:15:50 -07001123 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001124 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001125}
1126
1127static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001128ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1129 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001130{
Dan Williams91c00922007-01-02 13:52:30 -07001131 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001132 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001133 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001134 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001135
1136 /* existing parity data subtracted */
1137 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1138
Harvey Harrisone46b2722008-04-28 02:15:50 -07001139 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001140 (unsigned long long)sh->sector);
1141
1142 for (i = disks; i--; ) {
1143 struct r5dev *dev = &sh->dev[i];
1144 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001145 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001146 xor_srcs[count++] = dev->page;
1147 }
1148
Dan Williams0403e382009-09-08 17:42:50 -07001149 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001150 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001151 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001152
1153 return tx;
1154}
1155
1156static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001157ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001158{
1159 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001160 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001161
Harvey Harrisone46b2722008-04-28 02:15:50 -07001162 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001163 (unsigned long long)sh->sector);
1164
1165 for (i = disks; i--; ) {
1166 struct r5dev *dev = &sh->dev[i];
1167 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001168
Dan Williamsd8ee0722008-06-28 08:32:06 +10001169 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001170 struct bio *wbi;
1171
Shaohua Lib17459c2012-07-19 16:01:31 +10001172 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001173 chosen = dev->towrite;
1174 dev->towrite = NULL;
1175 BUG_ON(dev->written);
1176 wbi = dev->written = chosen;
Shaohua Lib17459c2012-07-19 16:01:31 +10001177 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001178
1179 while (wbi && wbi->bi_sector <
1180 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001181 if (wbi->bi_rw & REQ_FUA)
1182 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001183 if (wbi->bi_rw & REQ_SYNC)
1184 set_bit(R5_SyncIO, &dev->flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001185 if (wbi->bi_rw & REQ_DISCARD)
Shaohua Li620125f2012-10-11 13:49:05 +11001186 set_bit(R5_Discard, &dev->flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001187 else
Shaohua Li620125f2012-10-11 13:49:05 +11001188 tx = async_copy_data(1, wbi, dev->page,
1189 dev->sector, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001190 wbi = r5_next_bio(wbi, dev->sector);
1191 }
1192 }
1193 }
1194
1195 return tx;
1196}
1197
Dan Williamsac6b53b2009-07-14 13:40:19 -07001198static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001199{
1200 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001201 int disks = sh->disks;
1202 int pd_idx = sh->pd_idx;
1203 int qd_idx = sh->qd_idx;
1204 int i;
Shaohua Li9e4447682012-10-11 13:49:49 +11001205 bool fua = false, sync = false, discard = false;
Dan Williams91c00922007-01-02 13:52:30 -07001206
Harvey Harrisone46b2722008-04-28 02:15:50 -07001207 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001208 (unsigned long long)sh->sector);
1209
Shaohua Libc0934f2012-05-22 13:55:05 +10001210 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001211 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001212 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001213 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001214 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001215
Dan Williams91c00922007-01-02 13:52:30 -07001216 for (i = disks; i--; ) {
1217 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001218
Tejun Heoe9c74692010-09-03 11:56:18 +02001219 if (dev->written || i == pd_idx || i == qd_idx) {
Shaohua Li9e4447682012-10-11 13:49:49 +11001220 if (!discard)
1221 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001222 if (fua)
1223 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001224 if (sync)
1225 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001226 }
Dan Williams91c00922007-01-02 13:52:30 -07001227 }
1228
Dan Williamsd8ee0722008-06-28 08:32:06 +10001229 if (sh->reconstruct_state == reconstruct_state_drain_run)
1230 sh->reconstruct_state = reconstruct_state_drain_result;
1231 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1232 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1233 else {
1234 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1235 sh->reconstruct_state = reconstruct_state_result;
1236 }
Dan Williams91c00922007-01-02 13:52:30 -07001237
1238 set_bit(STRIPE_HANDLE, &sh->state);
1239 release_stripe(sh);
1240}
1241
1242static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001243ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1244 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001245{
Dan Williams91c00922007-01-02 13:52:30 -07001246 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001247 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001248 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001249 int count = 0, pd_idx = sh->pd_idx, i;
1250 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001251 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001252 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001253
Harvey Harrisone46b2722008-04-28 02:15:50 -07001254 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001255 (unsigned long long)sh->sector);
1256
Shaohua Li620125f2012-10-11 13:49:05 +11001257 for (i = 0; i < sh->disks; i++) {
1258 if (pd_idx == i)
1259 continue;
1260 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1261 break;
1262 }
1263 if (i >= sh->disks) {
1264 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001265 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1266 ops_complete_reconstruct(sh);
1267 return;
1268 }
Dan Williams91c00922007-01-02 13:52:30 -07001269 /* check if prexor is active which means only process blocks
1270 * that are part of a read-modify-write (written)
1271 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001272 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1273 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001274 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1275 for (i = disks; i--; ) {
1276 struct r5dev *dev = &sh->dev[i];
1277 if (dev->written)
1278 xor_srcs[count++] = dev->page;
1279 }
1280 } else {
1281 xor_dest = sh->dev[pd_idx].page;
1282 for (i = disks; i--; ) {
1283 struct r5dev *dev = &sh->dev[i];
1284 if (i != pd_idx)
1285 xor_srcs[count++] = dev->page;
1286 }
1287 }
1288
Dan Williams91c00922007-01-02 13:52:30 -07001289 /* 1/ if we prexor'd then the dest is reused as a source
1290 * 2/ if we did not prexor then we are redoing the parity
1291 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1292 * for the synchronous xor case
1293 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001294 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001295 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1296
1297 atomic_inc(&sh->count);
1298
Dan Williamsac6b53b2009-07-14 13:40:19 -07001299 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001300 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001301 if (unlikely(count == 1))
1302 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1303 else
1304 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001305}
1306
Dan Williamsac6b53b2009-07-14 13:40:19 -07001307static void
1308ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1309 struct dma_async_tx_descriptor *tx)
1310{
1311 struct async_submit_ctl submit;
1312 struct page **blocks = percpu->scribble;
Shaohua Li620125f2012-10-11 13:49:05 +11001313 int count, i;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001314
1315 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1316
Shaohua Li620125f2012-10-11 13:49:05 +11001317 for (i = 0; i < sh->disks; i++) {
1318 if (sh->pd_idx == i || sh->qd_idx == i)
1319 continue;
1320 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1321 break;
1322 }
1323 if (i >= sh->disks) {
1324 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001325 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1326 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1327 ops_complete_reconstruct(sh);
1328 return;
1329 }
1330
Dan Williamsac6b53b2009-07-14 13:40:19 -07001331 count = set_syndrome_sources(blocks, sh);
1332
1333 atomic_inc(&sh->count);
1334
1335 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1336 sh, to_addr_conv(sh, percpu));
1337 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001338}
1339
1340static void ops_complete_check(void *stripe_head_ref)
1341{
1342 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001343
Harvey Harrisone46b2722008-04-28 02:15:50 -07001344 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001345 (unsigned long long)sh->sector);
1346
Dan Williamsecc65c92008-06-28 08:31:57 +10001347 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001348 set_bit(STRIPE_HANDLE, &sh->state);
1349 release_stripe(sh);
1350}
1351
Dan Williamsac6b53b2009-07-14 13:40:19 -07001352static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001353{
Dan Williams91c00922007-01-02 13:52:30 -07001354 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001355 int pd_idx = sh->pd_idx;
1356 int qd_idx = sh->qd_idx;
1357 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001358 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001359 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001360 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001361 int count;
1362 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001363
Harvey Harrisone46b2722008-04-28 02:15:50 -07001364 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001365 (unsigned long long)sh->sector);
1366
Dan Williamsac6b53b2009-07-14 13:40:19 -07001367 count = 0;
1368 xor_dest = sh->dev[pd_idx].page;
1369 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001370 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001371 if (i == pd_idx || i == qd_idx)
1372 continue;
1373 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001374 }
1375
Dan Williamsd6f38f32009-07-14 11:50:52 -07001376 init_async_submit(&submit, 0, NULL, NULL, NULL,
1377 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001378 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001379 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001380
Dan Williams91c00922007-01-02 13:52:30 -07001381 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001382 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1383 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001384}
1385
Dan Williamsac6b53b2009-07-14 13:40:19 -07001386static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1387{
1388 struct page **srcs = percpu->scribble;
1389 struct async_submit_ctl submit;
1390 int count;
1391
1392 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1393 (unsigned long long)sh->sector, checkp);
1394
1395 count = set_syndrome_sources(srcs, sh);
1396 if (!checkp)
1397 srcs[count] = NULL;
1398
1399 atomic_inc(&sh->count);
1400 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1401 sh, to_addr_conv(sh, percpu));
1402 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1403 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1404}
1405
NeilBrown51acbce2013-02-28 09:08:34 +11001406static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001407{
1408 int overlap_clear = 0, i, disks = sh->disks;
1409 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001410 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001411 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001412 struct raid5_percpu *percpu;
1413 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001414
Dan Williamsd6f38f32009-07-14 11:50:52 -07001415 cpu = get_cpu();
1416 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001417 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001418 ops_run_biofill(sh);
1419 overlap_clear++;
1420 }
1421
Dan Williams7b3a8712008-06-28 08:32:09 +10001422 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001423 if (level < 6)
1424 tx = ops_run_compute5(sh, percpu);
1425 else {
1426 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1427 tx = ops_run_compute6_1(sh, percpu);
1428 else
1429 tx = ops_run_compute6_2(sh, percpu);
1430 }
1431 /* terminate the chain if reconstruct is not set to be run */
1432 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001433 async_tx_ack(tx);
1434 }
Dan Williams91c00922007-01-02 13:52:30 -07001435
Dan Williams600aa102008-06-28 08:32:05 +10001436 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001437 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001438
Dan Williams600aa102008-06-28 08:32:05 +10001439 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001440 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001441 overlap_clear++;
1442 }
1443
Dan Williamsac6b53b2009-07-14 13:40:19 -07001444 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1445 if (level < 6)
1446 ops_run_reconstruct5(sh, percpu, tx);
1447 else
1448 ops_run_reconstruct6(sh, percpu, tx);
1449 }
Dan Williams91c00922007-01-02 13:52:30 -07001450
Dan Williamsac6b53b2009-07-14 13:40:19 -07001451 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1452 if (sh->check_state == check_state_run)
1453 ops_run_check_p(sh, percpu);
1454 else if (sh->check_state == check_state_run_q)
1455 ops_run_check_pq(sh, percpu, 0);
1456 else if (sh->check_state == check_state_run_pq)
1457 ops_run_check_pq(sh, percpu, 1);
1458 else
1459 BUG();
1460 }
Dan Williams91c00922007-01-02 13:52:30 -07001461
Dan Williams91c00922007-01-02 13:52:30 -07001462 if (overlap_clear)
1463 for (i = disks; i--; ) {
1464 struct r5dev *dev = &sh->dev[i];
1465 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1466 wake_up(&sh->raid_conf->wait_for_overlap);
1467 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001468 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001469}
1470
NeilBrownd1688a62011-10-11 16:49:52 +11001471static int grow_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
1473 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001474 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001475 if (!sh)
1476 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001477
NeilBrown3f294f42005-11-08 21:39:25 -08001478 sh->raid_conf = conf;
NeilBrown3f294f42005-11-08 21:39:25 -08001479
Shaohua Lib17459c2012-07-19 16:01:31 +10001480 spin_lock_init(&sh->stripe_lock);
1481
NeilBrowne4e11e32010-06-16 16:45:16 +10001482 if (grow_buffers(sh)) {
1483 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001484 kmem_cache_free(conf->slab_cache, sh);
1485 return 0;
1486 }
1487 /* we just created an active stripe so... */
1488 atomic_set(&sh->count, 1);
1489 atomic_inc(&conf->active_stripes);
1490 INIT_LIST_HEAD(&sh->lru);
1491 release_stripe(sh);
1492 return 1;
1493}
1494
NeilBrownd1688a62011-10-11 16:49:52 +11001495static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001496{
Christoph Lametere18b8902006-12-06 20:33:20 -08001497 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001498 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
NeilBrownf4be6b42010-06-01 19:37:25 +10001500 if (conf->mddev->gendisk)
1501 sprintf(conf->cache_name[0],
1502 "raid%d-%s", conf->level, mdname(conf->mddev));
1503 else
1504 sprintf(conf->cache_name[0],
1505 "raid%d-%p", conf->level, conf->mddev);
1506 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1507
NeilBrownad01c9e2006-03-27 01:18:07 -08001508 conf->active_name = 0;
1509 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001511 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 if (!sc)
1513 return 1;
1514 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001515 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001516 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001517 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 return 0;
1520}
NeilBrown29269552006-03-27 01:18:10 -08001521
Dan Williamsd6f38f32009-07-14 11:50:52 -07001522/**
1523 * scribble_len - return the required size of the scribble region
1524 * @num - total number of disks in the array
1525 *
1526 * The size must be enough to contain:
1527 * 1/ a struct page pointer for each device in the array +2
1528 * 2/ room to convert each entry in (1) to its corresponding dma
1529 * (dma_map_page()) or page (page_address()) address.
1530 *
1531 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1532 * calculate over all devices (not just the data blocks), using zeros in place
1533 * of the P and Q blocks.
1534 */
1535static size_t scribble_len(int num)
1536{
1537 size_t len;
1538
1539 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1540
1541 return len;
1542}
1543
NeilBrownd1688a62011-10-11 16:49:52 +11001544static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001545{
1546 /* Make all the stripes able to hold 'newsize' devices.
1547 * New slots in each stripe get 'page' set to a new page.
1548 *
1549 * This happens in stages:
1550 * 1/ create a new kmem_cache and allocate the required number of
1551 * stripe_heads.
Masanari Iida83f0d772012-10-30 00:18:08 +09001552 * 2/ gather all the old stripe_heads and transfer the pages across
NeilBrownad01c9e2006-03-27 01:18:07 -08001553 * to the new stripe_heads. This will have the side effect of
1554 * freezing the array as once all stripe_heads have been collected,
1555 * no IO will be possible. Old stripe heads are freed once their
1556 * pages have been transferred over, and the old kmem_cache is
1557 * freed when all stripes are done.
1558 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1559 * we simple return a failre status - no need to clean anything up.
1560 * 4/ allocate new pages for the new slots in the new stripe_heads.
1561 * If this fails, we don't bother trying the shrink the
1562 * stripe_heads down again, we just leave them as they are.
1563 * As each stripe_head is processed the new one is released into
1564 * active service.
1565 *
1566 * Once step2 is started, we cannot afford to wait for a write,
1567 * so we use GFP_NOIO allocations.
1568 */
1569 struct stripe_head *osh, *nsh;
1570 LIST_HEAD(newstripes);
1571 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001572 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001573 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001574 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001575 int i;
1576
1577 if (newsize <= conf->pool_size)
1578 return 0; /* never bother to shrink */
1579
Dan Williamsb5470dc2008-06-27 21:44:04 -07001580 err = md_allow_write(conf->mddev);
1581 if (err)
1582 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001583
NeilBrownad01c9e2006-03-27 01:18:07 -08001584 /* Step 1 */
1585 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1586 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001587 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001588 if (!sc)
1589 return -ENOMEM;
1590
1591 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001592 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001593 if (!nsh)
1594 break;
1595
NeilBrownad01c9e2006-03-27 01:18:07 -08001596 nsh->raid_conf = conf;
NeilBrowncb13ff62012-09-24 16:27:20 +10001597 spin_lock_init(&nsh->stripe_lock);
NeilBrownad01c9e2006-03-27 01:18:07 -08001598
1599 list_add(&nsh->lru, &newstripes);
1600 }
1601 if (i) {
1602 /* didn't get enough, give up */
1603 while (!list_empty(&newstripes)) {
1604 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1605 list_del(&nsh->lru);
1606 kmem_cache_free(sc, nsh);
1607 }
1608 kmem_cache_destroy(sc);
1609 return -ENOMEM;
1610 }
1611 /* Step 2 - Must use GFP_NOIO now.
1612 * OK, we have enough stripes, start collecting inactive
1613 * stripes and copying them over
1614 */
1615 list_for_each_entry(nsh, &newstripes, lru) {
1616 spin_lock_irq(&conf->device_lock);
1617 wait_event_lock_irq(conf->wait_for_stripe,
1618 !list_empty(&conf->inactive_list),
Lukas Czernereed8c022012-11-30 11:42:40 +01001619 conf->device_lock);
NeilBrownad01c9e2006-03-27 01:18:07 -08001620 osh = get_free_stripe(conf);
1621 spin_unlock_irq(&conf->device_lock);
1622 atomic_set(&nsh->count, 1);
1623 for(i=0; i<conf->pool_size; i++)
1624 nsh->dev[i].page = osh->dev[i].page;
1625 for( ; i<newsize; i++)
1626 nsh->dev[i].page = NULL;
1627 kmem_cache_free(conf->slab_cache, osh);
1628 }
1629 kmem_cache_destroy(conf->slab_cache);
1630
1631 /* Step 3.
1632 * At this point, we are holding all the stripes so the array
1633 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001634 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001635 */
1636 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1637 if (ndisks) {
1638 for (i=0; i<conf->raid_disks; i++)
1639 ndisks[i] = conf->disks[i];
1640 kfree(conf->disks);
1641 conf->disks = ndisks;
1642 } else
1643 err = -ENOMEM;
1644
Dan Williamsd6f38f32009-07-14 11:50:52 -07001645 get_online_cpus();
1646 conf->scribble_len = scribble_len(newsize);
1647 for_each_present_cpu(cpu) {
1648 struct raid5_percpu *percpu;
1649 void *scribble;
1650
1651 percpu = per_cpu_ptr(conf->percpu, cpu);
1652 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1653
1654 if (scribble) {
1655 kfree(percpu->scribble);
1656 percpu->scribble = scribble;
1657 } else {
1658 err = -ENOMEM;
1659 break;
1660 }
1661 }
1662 put_online_cpus();
1663
NeilBrownad01c9e2006-03-27 01:18:07 -08001664 /* Step 4, return new stripes to service */
1665 while(!list_empty(&newstripes)) {
1666 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1667 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001668
NeilBrownad01c9e2006-03-27 01:18:07 -08001669 for (i=conf->raid_disks; i < newsize; i++)
1670 if (nsh->dev[i].page == NULL) {
1671 struct page *p = alloc_page(GFP_NOIO);
1672 nsh->dev[i].page = p;
1673 if (!p)
1674 err = -ENOMEM;
1675 }
1676 release_stripe(nsh);
1677 }
1678 /* critical section pass, GFP_NOIO no longer needed */
1679
1680 conf->slab_cache = sc;
1681 conf->active_name = 1-conf->active_name;
1682 conf->pool_size = newsize;
1683 return err;
1684}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
NeilBrownd1688a62011-10-11 16:49:52 +11001686static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
1688 struct stripe_head *sh;
1689
NeilBrown3f294f42005-11-08 21:39:25 -08001690 spin_lock_irq(&conf->device_lock);
1691 sh = get_free_stripe(conf);
1692 spin_unlock_irq(&conf->device_lock);
1693 if (!sh)
1694 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001695 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001696 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001697 kmem_cache_free(conf->slab_cache, sh);
1698 atomic_dec(&conf->active_stripes);
1699 return 1;
1700}
1701
NeilBrownd1688a62011-10-11 16:49:52 +11001702static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001703{
1704 while (drop_one_stripe(conf))
1705 ;
1706
NeilBrown29fc7e32006-02-03 03:03:41 -08001707 if (conf->slab_cache)
1708 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 conf->slab_cache = NULL;
1710}
1711
NeilBrown6712ecf2007-09-27 12:47:43 +02001712static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
NeilBrown99c0fb52009-03-31 14:39:38 +11001714 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001715 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001716 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001718 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11001719 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10001720 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
1722 for (i=0 ; i<disks; i++)
1723 if (bi == &sh->dev[i].req)
1724 break;
1725
Dan Williams45b42332007-07-09 11:56:43 -07001726 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1727 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 uptodate);
1729 if (i == disks) {
1730 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001731 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 }
NeilBrown14a75d32011-12-23 10:17:52 +11001733 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11001734 /* If replacement finished while this request was outstanding,
1735 * 'replacement' might be NULL already.
1736 * In that case it moved down to 'rdev'.
1737 * rdev is not removed until all requests are finished.
1738 */
NeilBrown14a75d32011-12-23 10:17:52 +11001739 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001740 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11001741 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
NeilBrown05616be2012-05-21 09:27:00 +10001743 if (use_new_offset(conf, sh))
1744 s = sh->sector + rdev->new_data_offset;
1745 else
1746 s = sh->sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001749 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11001750 /* Note that this cannot happen on a
1751 * replacement device. We just fail those on
1752 * any error
1753 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001754 printk_ratelimited(
1755 KERN_INFO
1756 "md/raid:%s: read error corrected"
1757 " (%lu sectors at %llu on %s)\n",
1758 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10001759 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001760 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001761 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001762 clear_bit(R5_ReadError, &sh->dev[i].flags);
1763 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng3f9e7c12012-07-31 10:04:21 +10001764 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
1765 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1766
NeilBrown14a75d32011-12-23 10:17:52 +11001767 if (atomic_read(&rdev->read_errors))
1768 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11001770 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001771 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10001772 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001775 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11001776 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1777 printk_ratelimited(
1778 KERN_WARNING
1779 "md/raid:%s: read error on replacement device "
1780 "(sector %llu on %s).\n",
1781 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001782 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11001783 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001784 else if (conf->mddev->degraded >= conf->max_degraded) {
1785 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001786 printk_ratelimited(
1787 KERN_WARNING
1788 "md/raid:%s: read error not correctable "
1789 "(sector %llu on %s).\n",
1790 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001791 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001792 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001793 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08001794 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10001795 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001796 printk_ratelimited(
1797 KERN_WARNING
1798 "md/raid:%s: read error NOT corrected!! "
1799 "(sector %llu on %s).\n",
1800 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001801 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001802 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001803 } else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001804 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001805 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001806 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001807 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001808 else
1809 retry = 1;
1810 if (retry)
majianpeng3f9e7c12012-07-31 10:04:21 +10001811 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
1812 set_bit(R5_ReadError, &sh->dev[i].flags);
1813 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1814 } else
1815 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -08001816 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001817 clear_bit(R5_ReadError, &sh->dev[i].flags);
1818 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10001819 if (!(set_bad
1820 && test_bit(In_sync, &rdev->flags)
1821 && rdev_set_badblocks(
1822 rdev, sh->sector, STRIPE_SECTORS, 0)))
1823 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 }
NeilBrown14a75d32011-12-23 10:17:52 +11001826 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1828 set_bit(STRIPE_HANDLE, &sh->state);
1829 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830}
1831
NeilBrownd710e132008-10-13 11:55:12 +11001832static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833{
NeilBrown99c0fb52009-03-31 14:39:38 +11001834 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001835 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001836 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11001837 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001839 sector_t first_bad;
1840 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11001841 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
NeilBrown977df362011-12-23 10:17:53 +11001843 for (i = 0 ; i < disks; i++) {
1844 if (bi == &sh->dev[i].req) {
1845 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 break;
NeilBrown977df362011-12-23 10:17:53 +11001847 }
1848 if (bi == &sh->dev[i].rreq) {
1849 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001850 if (rdev)
1851 replacement = 1;
1852 else
1853 /* rdev was removed and 'replacement'
1854 * replaced it. rdev is not removed
1855 * until all requests are finished.
1856 */
1857 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11001858 break;
1859 }
1860 }
Dan Williams45b42332007-07-09 11:56:43 -07001861 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1863 uptodate);
1864 if (i == disks) {
1865 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001866 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 }
1868
NeilBrown977df362011-12-23 10:17:53 +11001869 if (replacement) {
1870 if (!uptodate)
1871 md_error(conf->mddev, rdev);
1872 else if (is_badblock(rdev, sh->sector,
1873 STRIPE_SECTORS,
1874 &first_bad, &bad_sectors))
1875 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1876 } else {
1877 if (!uptodate) {
1878 set_bit(WriteErrorSeen, &rdev->flags);
1879 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11001880 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1881 set_bit(MD_RECOVERY_NEEDED,
1882 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11001883 } else if (is_badblock(rdev, sh->sector,
1884 STRIPE_SECTORS,
1885 &first_bad, &bad_sectors))
1886 set_bit(R5_MadeGood, &sh->dev[i].flags);
1887 }
1888 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
NeilBrown977df362011-12-23 10:17:53 +11001890 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1891 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001893 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894}
1895
NeilBrown784052e2009-03-31 15:19:07 +11001896static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
NeilBrown784052e2009-03-31 15:19:07 +11001898static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899{
1900 struct r5dev *dev = &sh->dev[i];
1901
1902 bio_init(&dev->req);
1903 dev->req.bi_io_vec = &dev->vec;
1904 dev->req.bi_vcnt++;
1905 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11001907 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
NeilBrown977df362011-12-23 10:17:53 +11001909 bio_init(&dev->rreq);
1910 dev->rreq.bi_io_vec = &dev->rvec;
1911 dev->rreq.bi_vcnt++;
1912 dev->rreq.bi_max_vecs++;
1913 dev->rreq.bi_private = sh;
1914 dev->rvec.bv_page = dev->page;
1915
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001917 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918}
1919
NeilBrownfd01b882011-10-11 16:47:53 +11001920static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921{
1922 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11001923 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11001924 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10001925 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
NeilBrown908f4fb2011-12-23 10:17:50 +11001927 spin_lock_irqsave(&conf->device_lock, flags);
1928 clear_bit(In_sync, &rdev->flags);
1929 mddev->degraded = calc_degraded(conf);
1930 spin_unlock_irqrestore(&conf->device_lock, flags);
1931 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1932
NeilBrownde393cd2011-07-28 11:31:48 +10001933 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001934 set_bit(Faulty, &rdev->flags);
1935 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1936 printk(KERN_ALERT
1937 "md/raid:%s: Disk failure on %s, disabling device.\n"
1938 "md/raid:%s: Operation continuing on %d devices.\n",
1939 mdname(mddev),
1940 bdevname(rdev->bdev, b),
1941 mdname(mddev),
1942 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001943}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
1945/*
1946 * Input: a 'big' sector number,
1947 * Output: index of the data and parity disk, and the sector # in them.
1948 */
NeilBrownd1688a62011-10-11 16:49:52 +11001949static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001950 int previous, int *dd_idx,
1951 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001953 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001954 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001956 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001957 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001959 int algorithm = previous ? conf->prev_algo
1960 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001961 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1962 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001963 int raid_disks = previous ? conf->previous_raid_disks
1964 : conf->raid_disks;
1965 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
1967 /* First compute the information on this sector */
1968
1969 /*
1970 * Compute the chunk number and the sector offset inside the chunk
1971 */
1972 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1973 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
1975 /*
1976 * Compute the stripe number
1977 */
NeilBrown35f2a592010-04-20 14:13:34 +10001978 stripe = chunk_number;
1979 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10001980 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 /*
1982 * Select the parity disk based on the user selected algorithm.
1983 */
NeilBrown84789552011-07-27 11:00:36 +10001984 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07001985 switch(conf->level) {
1986 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001987 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001988 break;
1989 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001990 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001992 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001993 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 (*dd_idx)++;
1995 break;
1996 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001997 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001998 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 (*dd_idx)++;
2000 break;
2001 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002002 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002003 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 break;
2005 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002006 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002007 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002009 case ALGORITHM_PARITY_0:
2010 pd_idx = 0;
2011 (*dd_idx)++;
2012 break;
2013 case ALGORITHM_PARITY_N:
2014 pd_idx = data_disks;
2015 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002017 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002018 }
2019 break;
2020 case 6:
2021
NeilBrowne183eae2009-03-31 15:20:22 +11002022 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002023 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002024 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002025 qd_idx = pd_idx + 1;
2026 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002027 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002028 qd_idx = 0;
2029 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002030 (*dd_idx) += 2; /* D D P Q D */
2031 break;
2032 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002033 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002034 qd_idx = pd_idx + 1;
2035 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002036 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002037 qd_idx = 0;
2038 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002039 (*dd_idx) += 2; /* D D P Q D */
2040 break;
2041 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002042 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002043 qd_idx = (pd_idx + 1) % raid_disks;
2044 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002045 break;
2046 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002047 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002048 qd_idx = (pd_idx + 1) % raid_disks;
2049 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002050 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002051
2052 case ALGORITHM_PARITY_0:
2053 pd_idx = 0;
2054 qd_idx = 1;
2055 (*dd_idx) += 2;
2056 break;
2057 case ALGORITHM_PARITY_N:
2058 pd_idx = data_disks;
2059 qd_idx = data_disks + 1;
2060 break;
2061
2062 case ALGORITHM_ROTATING_ZERO_RESTART:
2063 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2064 * of blocks for computing Q is different.
2065 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002066 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002067 qd_idx = pd_idx + 1;
2068 if (pd_idx == raid_disks-1) {
2069 (*dd_idx)++; /* Q D D D P */
2070 qd_idx = 0;
2071 } else if (*dd_idx >= pd_idx)
2072 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002073 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002074 break;
2075
2076 case ALGORITHM_ROTATING_N_RESTART:
2077 /* Same a left_asymmetric, by first stripe is
2078 * D D D P Q rather than
2079 * Q D D D P
2080 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002081 stripe2 += 1;
2082 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002083 qd_idx = pd_idx + 1;
2084 if (pd_idx == raid_disks-1) {
2085 (*dd_idx)++; /* Q D D D P */
2086 qd_idx = 0;
2087 } else if (*dd_idx >= pd_idx)
2088 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002089 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002090 break;
2091
2092 case ALGORITHM_ROTATING_N_CONTINUE:
2093 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002094 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002095 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2096 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002097 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002098 break;
2099
2100 case ALGORITHM_LEFT_ASYMMETRIC_6:
2101 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002102 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002103 if (*dd_idx >= pd_idx)
2104 (*dd_idx)++;
2105 qd_idx = raid_disks - 1;
2106 break;
2107
2108 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002109 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002110 if (*dd_idx >= pd_idx)
2111 (*dd_idx)++;
2112 qd_idx = raid_disks - 1;
2113 break;
2114
2115 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002116 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002117 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2118 qd_idx = raid_disks - 1;
2119 break;
2120
2121 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002122 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002123 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2124 qd_idx = raid_disks - 1;
2125 break;
2126
2127 case ALGORITHM_PARITY_0_6:
2128 pd_idx = 0;
2129 (*dd_idx)++;
2130 qd_idx = raid_disks - 1;
2131 break;
2132
NeilBrown16a53ec2006-06-26 00:27:38 -07002133 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002134 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002135 }
2136 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 }
2138
NeilBrown911d4ee2009-03-31 14:39:38 +11002139 if (sh) {
2140 sh->pd_idx = pd_idx;
2141 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002142 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 /*
2145 * Finally, compute the new sector number
2146 */
2147 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2148 return new_sector;
2149}
2150
2151
NeilBrown784052e2009-03-31 15:19:07 +11002152static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
NeilBrownd1688a62011-10-11 16:49:52 +11002154 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002155 int raid_disks = sh->disks;
2156 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002158 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2159 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002160 int algorithm = previous ? conf->prev_algo
2161 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 sector_t stripe;
2163 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002164 sector_t chunk_number;
2165 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002167 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
NeilBrown16a53ec2006-06-26 00:27:38 -07002169
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2171 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
NeilBrown16a53ec2006-06-26 00:27:38 -07002173 if (i == sh->pd_idx)
2174 return 0;
2175 switch(conf->level) {
2176 case 4: break;
2177 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002178 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 case ALGORITHM_LEFT_ASYMMETRIC:
2180 case ALGORITHM_RIGHT_ASYMMETRIC:
2181 if (i > sh->pd_idx)
2182 i--;
2183 break;
2184 case ALGORITHM_LEFT_SYMMETRIC:
2185 case ALGORITHM_RIGHT_SYMMETRIC:
2186 if (i < sh->pd_idx)
2187 i += raid_disks;
2188 i -= (sh->pd_idx + 1);
2189 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002190 case ALGORITHM_PARITY_0:
2191 i -= 1;
2192 break;
2193 case ALGORITHM_PARITY_N:
2194 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002196 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002197 }
2198 break;
2199 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002200 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002201 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002202 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002203 case ALGORITHM_LEFT_ASYMMETRIC:
2204 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002205 case ALGORITHM_ROTATING_ZERO_RESTART:
2206 case ALGORITHM_ROTATING_N_RESTART:
2207 if (sh->pd_idx == raid_disks-1)
2208 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002209 else if (i > sh->pd_idx)
2210 i -= 2; /* D D P Q D */
2211 break;
2212 case ALGORITHM_LEFT_SYMMETRIC:
2213 case ALGORITHM_RIGHT_SYMMETRIC:
2214 if (sh->pd_idx == raid_disks-1)
2215 i--; /* Q D D D P */
2216 else {
2217 /* D D P Q D */
2218 if (i < sh->pd_idx)
2219 i += raid_disks;
2220 i -= (sh->pd_idx + 2);
2221 }
2222 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002223 case ALGORITHM_PARITY_0:
2224 i -= 2;
2225 break;
2226 case ALGORITHM_PARITY_N:
2227 break;
2228 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002229 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002230 if (sh->pd_idx == 0)
2231 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002232 else {
2233 /* D D Q P D */
2234 if (i < sh->pd_idx)
2235 i += raid_disks;
2236 i -= (sh->pd_idx + 1);
2237 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002238 break;
2239 case ALGORITHM_LEFT_ASYMMETRIC_6:
2240 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2241 if (i > sh->pd_idx)
2242 i--;
2243 break;
2244 case ALGORITHM_LEFT_SYMMETRIC_6:
2245 case ALGORITHM_RIGHT_SYMMETRIC_6:
2246 if (i < sh->pd_idx)
2247 i += data_disks + 1;
2248 i -= (sh->pd_idx + 1);
2249 break;
2250 case ALGORITHM_PARITY_0_6:
2251 i -= 1;
2252 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002253 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002254 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002255 }
2256 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 }
2258
2259 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002260 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
NeilBrown112bf892009-03-31 14:39:38 +11002262 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002263 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002264 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2265 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002266 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2267 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 return 0;
2269 }
2270 return r_sector;
2271}
2272
2273
Dan Williams600aa102008-06-28 08:32:05 +10002274static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002275schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002276 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002277{
2278 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002279 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002280 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002281
Dan Williamse33129d2007-01-02 13:52:30 -07002282 if (rcw) {
2283 /* if we are not expanding this is a proper write request, and
2284 * there will be bios with new data to be drained into the
2285 * stripe cache
2286 */
2287 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10002288 sh->reconstruct_state = reconstruct_state_drain_run;
2289 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2290 } else
2291 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07002292
Dan Williamsac6b53b2009-07-14 13:40:19 -07002293 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002294
2295 for (i = disks; i--; ) {
2296 struct r5dev *dev = &sh->dev[i];
2297
2298 if (dev->towrite) {
2299 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002300 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002301 if (!expand)
2302 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002303 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002304 }
2305 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002306 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002307 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002308 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002309 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002310 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002311 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2312 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2313
Dan Williamsd8ee0722008-06-28 08:32:06 +10002314 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002315 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2316 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002317 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002318
2319 for (i = disks; i--; ) {
2320 struct r5dev *dev = &sh->dev[i];
2321 if (i == pd_idx)
2322 continue;
2323
Dan Williamse33129d2007-01-02 13:52:30 -07002324 if (dev->towrite &&
2325 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002326 test_bit(R5_Wantcompute, &dev->flags))) {
2327 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002328 set_bit(R5_LOCKED, &dev->flags);
2329 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002330 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002331 }
2332 }
2333 }
2334
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002335 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002336 * are in flight
2337 */
2338 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2339 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002340 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002341
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002342 if (level == 6) {
2343 int qd_idx = sh->qd_idx;
2344 struct r5dev *dev = &sh->dev[qd_idx];
2345
2346 set_bit(R5_LOCKED, &dev->flags);
2347 clear_bit(R5_UPTODATE, &dev->flags);
2348 s->locked++;
2349 }
2350
Dan Williams600aa102008-06-28 08:32:05 +10002351 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002352 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002353 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002354}
NeilBrown16a53ec2006-06-26 00:27:38 -07002355
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356/*
2357 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002358 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 * The bi_next chain must be in order.
2360 */
2361static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2362{
2363 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002364 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002365 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
NeilBrowncbe47ec2011-07-26 11:20:35 +10002367 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 (unsigned long long)bi->bi_sector,
2369 (unsigned long long)sh->sector);
2370
Shaohua Lib17459c2012-07-19 16:01:31 +10002371 /*
2372 * If several bio share a stripe. The bio bi_phys_segments acts as a
2373 * reference count to avoid race. The reference count should already be
2374 * increased before this function is called (for example, in
2375 * make_request()), so other bio sharing this stripe will not free the
2376 * stripe. If a stripe is owned by one stripe, the stripe lock will
2377 * protect it.
2378 */
2379 spin_lock_irq(&sh->stripe_lock);
NeilBrown72626682005-09-09 16:23:54 -07002380 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002382 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07002383 firstwrite = 1;
2384 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 bip = &sh->dev[dd_idx].toread;
2386 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002387 if (bio_end_sector(*bip) > bi->bi_sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 goto overlap;
2389 bip = & (*bip)->bi_next;
2390 }
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002391 if (*bip && (*bip)->bi_sector < bio_end_sector(bi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 goto overlap;
2393
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002394 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 if (*bip)
2396 bi->bi_next = *bip;
2397 *bip = bi;
Shaohua Lie7836bd62012-07-19 16:01:31 +10002398 raid5_inc_bi_active_stripes(bi);
NeilBrown72626682005-09-09 16:23:54 -07002399
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 if (forwrite) {
2401 /* check if page is covered */
2402 sector_t sector = sh->dev[dd_idx].sector;
2403 for (bi=sh->dev[dd_idx].towrite;
2404 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2405 bi && bi->bi_sector <= sector;
2406 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002407 if (bio_end_sector(bi) >= sector)
2408 sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 }
2410 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2411 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2412 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002413
2414 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2415 (unsigned long long)(*bip)->bi_sector,
2416 (unsigned long long)sh->sector, dd_idx);
NeilBrownb97390a2012-10-11 13:50:12 +11002417 spin_unlock_irq(&sh->stripe_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002418
2419 if (conf->mddev->bitmap && firstwrite) {
2420 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2421 STRIPE_SECTORS, 0);
2422 sh->bm_seq = conf->seq_flush+1;
2423 set_bit(STRIPE_BIT_DELAY, &sh->state);
2424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 return 1;
2426
2427 overlap:
2428 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10002429 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 return 0;
2431}
2432
NeilBrownd1688a62011-10-11 16:49:52 +11002433static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002434
NeilBrownd1688a62011-10-11 16:49:52 +11002435static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002436 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002437{
NeilBrown784052e2009-03-31 15:19:07 +11002438 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002439 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002440 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002441 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002442 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002443
NeilBrown112bf892009-03-31 14:39:38 +11002444 raid5_compute_sector(conf,
2445 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002446 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002447 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002448 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002449}
2450
Dan Williamsa4456852007-07-09 11:56:43 -07002451static void
NeilBrownd1688a62011-10-11 16:49:52 +11002452handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002453 struct stripe_head_state *s, int disks,
2454 struct bio **return_bi)
2455{
2456 int i;
2457 for (i = disks; i--; ) {
2458 struct bio *bi;
2459 int bitmap_end = 0;
2460
2461 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002462 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002463 rcu_read_lock();
2464 rdev = rcu_dereference(conf->disks[i].rdev);
2465 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002466 atomic_inc(&rdev->nr_pending);
2467 else
2468 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002469 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002470 if (rdev) {
2471 if (!rdev_set_badblocks(
2472 rdev,
2473 sh->sector,
2474 STRIPE_SECTORS, 0))
2475 md_error(conf->mddev, rdev);
2476 rdev_dec_pending(rdev, conf->mddev);
2477 }
Dan Williamsa4456852007-07-09 11:56:43 -07002478 }
Shaohua Lib17459c2012-07-19 16:01:31 +10002479 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002480 /* fail all writes first */
2481 bi = sh->dev[i].towrite;
2482 sh->dev[i].towrite = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10002483 spin_unlock_irq(&sh->stripe_lock);
NeilBrown1ed850f2012-10-11 13:50:13 +11002484 if (bi)
Dan Williamsa4456852007-07-09 11:56:43 -07002485 bitmap_end = 1;
Dan Williamsa4456852007-07-09 11:56:43 -07002486
2487 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2488 wake_up(&conf->wait_for_overlap);
2489
2490 while (bi && bi->bi_sector <
2491 sh->dev[i].sector + STRIPE_SECTORS) {
2492 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2493 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002494 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002495 md_write_end(conf->mddev);
2496 bi->bi_next = *return_bi;
2497 *return_bi = bi;
2498 }
2499 bi = nextbi;
2500 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002501 if (bitmap_end)
2502 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2503 STRIPE_SECTORS, 0, 0);
2504 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002505 /* and fail all 'written' */
2506 bi = sh->dev[i].written;
2507 sh->dev[i].written = NULL;
2508 if (bi) bitmap_end = 1;
2509 while (bi && bi->bi_sector <
2510 sh->dev[i].sector + STRIPE_SECTORS) {
2511 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2512 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002513 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002514 md_write_end(conf->mddev);
2515 bi->bi_next = *return_bi;
2516 *return_bi = bi;
2517 }
2518 bi = bi2;
2519 }
2520
Dan Williamsb5e98d62007-01-02 13:52:31 -07002521 /* fail any reads if this device is non-operational and
2522 * the data has not reached the cache yet.
2523 */
2524 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2525 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2526 test_bit(R5_ReadError, &sh->dev[i].flags))) {
NeilBrown143c4d02012-10-11 13:50:12 +11002527 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002528 bi = sh->dev[i].toread;
2529 sh->dev[i].toread = NULL;
NeilBrown143c4d02012-10-11 13:50:12 +11002530 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002531 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2532 wake_up(&conf->wait_for_overlap);
Dan Williamsa4456852007-07-09 11:56:43 -07002533 while (bi && bi->bi_sector <
2534 sh->dev[i].sector + STRIPE_SECTORS) {
2535 struct bio *nextbi =
2536 r5_next_bio(bi, sh->dev[i].sector);
2537 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002538 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002539 bi->bi_next = *return_bi;
2540 *return_bi = bi;
2541 }
2542 bi = nextbi;
2543 }
2544 }
Dan Williamsa4456852007-07-09 11:56:43 -07002545 if (bitmap_end)
2546 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2547 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002548 /* If we were in the middle of a write the parity block might
2549 * still be locked - so just clear all R5_LOCKED flags
2550 */
2551 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002552 }
2553
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002554 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2555 if (atomic_dec_and_test(&conf->pending_full_writes))
2556 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002557}
2558
NeilBrown7f0da592011-07-28 11:39:22 +10002559static void
NeilBrownd1688a62011-10-11 16:49:52 +11002560handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002561 struct stripe_head_state *s)
2562{
2563 int abort = 0;
2564 int i;
2565
NeilBrown7f0da592011-07-28 11:39:22 +10002566 clear_bit(STRIPE_SYNCING, &sh->state);
2567 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002568 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002569 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10002570 * Don't even need to abort as that is handled elsewhere
2571 * if needed, and not always wanted e.g. if there is a known
2572 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11002573 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002574 * non-sync devices, or abort the recovery
2575 */
NeilBrown18b98372012-04-01 23:48:38 +10002576 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2577 /* During recovery devices cannot be removed, so
2578 * locking and refcounting of rdevs is not needed
2579 */
2580 for (i = 0; i < conf->raid_disks; i++) {
2581 struct md_rdev *rdev = conf->disks[i].rdev;
2582 if (rdev
2583 && !test_bit(Faulty, &rdev->flags)
2584 && !test_bit(In_sync, &rdev->flags)
2585 && !rdev_set_badblocks(rdev, sh->sector,
2586 STRIPE_SECTORS, 0))
2587 abort = 1;
2588 rdev = conf->disks[i].replacement;
2589 if (rdev
2590 && !test_bit(Faulty, &rdev->flags)
2591 && !test_bit(In_sync, &rdev->flags)
2592 && !rdev_set_badblocks(rdev, sh->sector,
2593 STRIPE_SECTORS, 0))
2594 abort = 1;
2595 }
2596 if (abort)
2597 conf->recovery_disabled =
2598 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10002599 }
NeilBrown18b98372012-04-01 23:48:38 +10002600 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10002601}
2602
NeilBrown9a3e1102011-12-23 10:17:53 +11002603static int want_replace(struct stripe_head *sh, int disk_idx)
2604{
2605 struct md_rdev *rdev;
2606 int rv = 0;
2607 /* Doing recovery so rcu locking not required */
2608 rdev = sh->raid_conf->disks[disk_idx].replacement;
2609 if (rdev
2610 && !test_bit(Faulty, &rdev->flags)
2611 && !test_bit(In_sync, &rdev->flags)
2612 && (rdev->recovery_offset <= sh->sector
2613 || rdev->mddev->recovery_cp <= sh->sector))
2614 rv = 1;
2615
2616 return rv;
2617}
2618
NeilBrown93b3dbc2011-07-27 11:00:36 +10002619/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002620 * to be read or computed to satisfy a request.
2621 *
2622 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002623 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002624 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002625static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2626 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002627{
2628 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002629 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2630 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002631
Dan Williamsf38e1212007-01-02 13:52:30 -07002632 /* is the data in this block needed, and can we get it? */
2633 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002634 !test_bit(R5_UPTODATE, &dev->flags) &&
2635 (dev->toread ||
2636 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2637 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002638 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002639 (s->failed >= 1 && fdev[0]->toread) ||
2640 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002641 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2642 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2643 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002644 /* we would like to get this block, possibly by computing it,
2645 * otherwise read it if the backing disk is insync
2646 */
2647 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2648 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2649 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002650 (s->failed && (disk_idx == s->failed_num[0] ||
2651 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002652 /* have disk failed, and we're requested to fetch it;
2653 * do compute it
2654 */
2655 pr_debug("Computing stripe %llu block %d\n",
2656 (unsigned long long)sh->sector, disk_idx);
2657 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2658 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2659 set_bit(R5_Wantcompute, &dev->flags);
2660 sh->ops.target = disk_idx;
2661 sh->ops.target2 = -1; /* no 2nd target */
2662 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002663 /* Careful: from this point on 'uptodate' is in the eye
2664 * of raid_run_ops which services 'compute' operations
2665 * before writes. R5_Wantcompute flags a block that will
2666 * be R5_UPTODATE by the time it is needed for a
2667 * subsequent operation.
2668 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002669 s->uptodate++;
2670 return 1;
2671 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2672 /* Computing 2-failure is *very* expensive; only
2673 * do it if failed >= 2
2674 */
2675 int other;
2676 for (other = disks; other--; ) {
2677 if (other == disk_idx)
2678 continue;
2679 if (!test_bit(R5_UPTODATE,
2680 &sh->dev[other].flags))
2681 break;
2682 }
2683 BUG_ON(other < 0);
2684 pr_debug("Computing stripe %llu blocks %d,%d\n",
2685 (unsigned long long)sh->sector,
2686 disk_idx, other);
2687 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2688 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2689 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2690 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2691 sh->ops.target = disk_idx;
2692 sh->ops.target2 = other;
2693 s->uptodate += 2;
2694 s->req_compute = 1;
2695 return 1;
2696 } else if (test_bit(R5_Insync, &dev->flags)) {
2697 set_bit(R5_LOCKED, &dev->flags);
2698 set_bit(R5_Wantread, &dev->flags);
2699 s->locked++;
2700 pr_debug("Reading block %d (sync=%d)\n",
2701 disk_idx, s->syncing);
2702 }
2703 }
2704
2705 return 0;
2706}
2707
2708/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002709 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002710 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002711static void handle_stripe_fill(struct stripe_head *sh,
2712 struct stripe_head_state *s,
2713 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002714{
2715 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002716
2717 /* look for blocks to read/compute, skip this if a compute
2718 * is already in flight, or if the stripe contents are in the
2719 * midst of changing due to a write
2720 */
2721 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2722 !sh->reconstruct_state)
2723 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002724 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002725 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002726 set_bit(STRIPE_HANDLE, &sh->state);
2727}
2728
2729
Dan Williams1fe797e2008-06-28 09:16:30 +10002730/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002731 * any written block on an uptodate or failed drive can be returned.
2732 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2733 * never LOCKED, so we don't need to test 'failed' directly.
2734 */
NeilBrownd1688a62011-10-11 16:49:52 +11002735static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002736 struct stripe_head *sh, int disks, struct bio **return_bi)
2737{
2738 int i;
2739 struct r5dev *dev;
2740
2741 for (i = disks; i--; )
2742 if (sh->dev[i].written) {
2743 dev = &sh->dev[i];
2744 if (!test_bit(R5_LOCKED, &dev->flags) &&
Shaohua Li9e4447682012-10-11 13:49:49 +11002745 (test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownca64cae2012-11-21 16:33:40 +11002746 test_bit(R5_Discard, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002747 /* We can return any write requests */
2748 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07002749 pr_debug("Return write for disc %d\n", i);
NeilBrownca64cae2012-11-21 16:33:40 +11002750 if (test_and_clear_bit(R5_Discard, &dev->flags))
2751 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002752 wbi = dev->written;
2753 dev->written = NULL;
2754 while (wbi && wbi->bi_sector <
2755 dev->sector + STRIPE_SECTORS) {
2756 wbi2 = r5_next_bio(wbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002757 if (!raid5_dec_bi_active_stripes(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002758 md_write_end(conf->mddev);
2759 wbi->bi_next = *return_bi;
2760 *return_bi = wbi;
2761 }
2762 wbi = wbi2;
2763 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002764 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2765 STRIPE_SECTORS,
Dan Williamsa4456852007-07-09 11:56:43 -07002766 !test_bit(STRIPE_DEGRADED, &sh->state),
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002767 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002768 }
NeilBrownca64cae2012-11-21 16:33:40 +11002769 } else if (test_bit(R5_Discard, &sh->dev[i].flags))
2770 clear_bit(R5_Discard, &sh->dev[i].flags);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002771
2772 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2773 if (atomic_dec_and_test(&conf->pending_full_writes))
2774 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002775}
2776
NeilBrownd1688a62011-10-11 16:49:52 +11002777static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002778 struct stripe_head *sh,
2779 struct stripe_head_state *s,
2780 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002781{
2782 int rmw = 0, rcw = 0, i;
Alexander Lyakasa7854482012-10-11 13:50:12 +11002783 sector_t recovery_cp = conf->mddev->recovery_cp;
2784
2785 /* RAID6 requires 'rcw' in current implementation.
2786 * Otherwise, check whether resync is now happening or should start.
2787 * If yes, then the array is dirty (after unclean shutdown or
2788 * initial creation), so parity in some stripes might be inconsistent.
2789 * In this case, we need to always do reconstruct-write, to ensure
2790 * that in case of drive failure or read-error correction, we
2791 * generate correct data from the parity.
2792 */
2793 if (conf->max_degraded == 2 ||
2794 (recovery_cp < MaxSector && sh->sector >= recovery_cp)) {
2795 /* Calculate the real rcw later - for now make it
NeilBrownc8ac1802011-07-27 11:00:36 +10002796 * look like rcw is cheaper
2797 */
2798 rcw = 1; rmw = 2;
Alexander Lyakasa7854482012-10-11 13:50:12 +11002799 pr_debug("force RCW max_degraded=%u, recovery_cp=%llu sh->sector=%llu\n",
2800 conf->max_degraded, (unsigned long long)recovery_cp,
2801 (unsigned long long)sh->sector);
NeilBrownc8ac1802011-07-27 11:00:36 +10002802 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002803 /* would I have to read this buffer for read_modify_write */
2804 struct r5dev *dev = &sh->dev[i];
2805 if ((dev->towrite || i == sh->pd_idx) &&
2806 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002807 !(test_bit(R5_UPTODATE, &dev->flags) ||
2808 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002809 if (test_bit(R5_Insync, &dev->flags))
2810 rmw++;
2811 else
2812 rmw += 2*disks; /* cannot read it */
2813 }
2814 /* Would I have to read this buffer for reconstruct_write */
2815 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2816 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002817 !(test_bit(R5_UPTODATE, &dev->flags) ||
2818 test_bit(R5_Wantcompute, &dev->flags))) {
2819 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002820 else
2821 rcw += 2*disks;
2822 }
2823 }
Dan Williams45b42332007-07-09 11:56:43 -07002824 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002825 (unsigned long long)sh->sector, rmw, rcw);
2826 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrowna9add5d2012-10-31 11:59:09 +11002827 if (rmw < rcw && rmw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002828 /* prefer read-modify-write, but need to get some data */
NeilBrowna9add5d2012-10-31 11:59:09 +11002829 blk_add_trace_msg(conf->mddev->queue, "raid5 rmw %llu %d",
2830 (unsigned long long)sh->sector, rmw);
Dan Williamsa4456852007-07-09 11:56:43 -07002831 for (i = disks; i--; ) {
2832 struct r5dev *dev = &sh->dev[i];
2833 if ((dev->towrite || i == sh->pd_idx) &&
2834 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002835 !(test_bit(R5_UPTODATE, &dev->flags) ||
2836 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002837 test_bit(R5_Insync, &dev->flags)) {
2838 if (
2839 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002840 pr_debug("Read_old block "
NeilBrowna9add5d2012-10-31 11:59:09 +11002841 "%d for r-m-w\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002842 set_bit(R5_LOCKED, &dev->flags);
2843 set_bit(R5_Wantread, &dev->flags);
2844 s->locked++;
2845 } else {
2846 set_bit(STRIPE_DELAYED, &sh->state);
2847 set_bit(STRIPE_HANDLE, &sh->state);
2848 }
2849 }
2850 }
NeilBrowna9add5d2012-10-31 11:59:09 +11002851 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002852 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002853 /* want reconstruct write, but need to get some data */
NeilBrowna9add5d2012-10-31 11:59:09 +11002854 int qread =0;
NeilBrownc8ac1802011-07-27 11:00:36 +10002855 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002856 for (i = disks; i--; ) {
2857 struct r5dev *dev = &sh->dev[i];
2858 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002859 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002860 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002861 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002862 test_bit(R5_Wantcompute, &dev->flags))) {
2863 rcw++;
2864 if (!test_bit(R5_Insync, &dev->flags))
2865 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002866 if (
2867 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002868 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002869 "%d for Reconstruct\n", i);
2870 set_bit(R5_LOCKED, &dev->flags);
2871 set_bit(R5_Wantread, &dev->flags);
2872 s->locked++;
NeilBrowna9add5d2012-10-31 11:59:09 +11002873 qread++;
Dan Williamsa4456852007-07-09 11:56:43 -07002874 } else {
2875 set_bit(STRIPE_DELAYED, &sh->state);
2876 set_bit(STRIPE_HANDLE, &sh->state);
2877 }
2878 }
2879 }
NeilBrowna9add5d2012-10-31 11:59:09 +11002880 if (rcw)
2881 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
2882 (unsigned long long)sh->sector,
2883 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
NeilBrownc8ac1802011-07-27 11:00:36 +10002884 }
Dan Williamsa4456852007-07-09 11:56:43 -07002885 /* now if nothing is locked, and if we have enough data,
2886 * we can start a write request
2887 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002888 /* since handle_stripe can be called at any time we need to handle the
2889 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002890 * subsequent call wants to start a write request. raid_run_ops only
2891 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002892 * simultaneously. If this is not the case then new writes need to be
2893 * held off until the compute completes.
2894 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002895 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2896 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2897 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002898 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002899}
2900
NeilBrownd1688a62011-10-11 16:49:52 +11002901static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002902 struct stripe_head_state *s, int disks)
2903{
Dan Williamsecc65c92008-06-28 08:31:57 +10002904 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002905
Dan Williamsbd2ab672008-04-10 21:29:27 -07002906 set_bit(STRIPE_HANDLE, &sh->state);
2907
Dan Williamsecc65c92008-06-28 08:31:57 +10002908 switch (sh->check_state) {
2909 case check_state_idle:
2910 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002911 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002912 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002913 sh->check_state = check_state_run;
2914 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002915 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002916 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002917 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002918 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002919 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002920 /* fall through */
2921 case check_state_compute_result:
2922 sh->check_state = check_state_idle;
2923 if (!dev)
2924 dev = &sh->dev[sh->pd_idx];
2925
2926 /* check that a write has not made the stripe insync */
2927 if (test_bit(STRIPE_INSYNC, &sh->state))
2928 break;
2929
2930 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002931 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2932 BUG_ON(s->uptodate != disks);
2933
2934 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002935 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002936 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002937
Dan Williamsa4456852007-07-09 11:56:43 -07002938 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002939 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002940 break;
2941 case check_state_run:
2942 break; /* we will be called again upon completion */
2943 case check_state_check_result:
2944 sh->check_state = check_state_idle;
2945
2946 /* if a failure occurred during the check operation, leave
2947 * STRIPE_INSYNC not set and let the stripe be handled again
2948 */
2949 if (s->failed)
2950 break;
2951
2952 /* handle a successful check operation, if parity is correct
2953 * we are done. Otherwise update the mismatch count and repair
2954 * parity if !MD_RECOVERY_CHECK
2955 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002956 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002957 /* parity is correct (on disc,
2958 * not in buffer any more)
2959 */
2960 set_bit(STRIPE_INSYNC, &sh->state);
2961 else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11002962 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsecc65c92008-06-28 08:31:57 +10002963 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2964 /* don't try to repair!! */
2965 set_bit(STRIPE_INSYNC, &sh->state);
2966 else {
2967 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002968 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002969 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2970 set_bit(R5_Wantcompute,
2971 &sh->dev[sh->pd_idx].flags);
2972 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002973 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10002974 s->uptodate++;
2975 }
2976 }
2977 break;
2978 case check_state_compute_run:
2979 break;
2980 default:
2981 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2982 __func__, sh->check_state,
2983 (unsigned long long) sh->sector);
2984 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002985 }
2986}
2987
2988
NeilBrownd1688a62011-10-11 16:49:52 +11002989static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07002990 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10002991 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002992{
Dan Williamsa4456852007-07-09 11:56:43 -07002993 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11002994 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07002995 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07002996
2997 set_bit(STRIPE_HANDLE, &sh->state);
2998
2999 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003000
Dan Williamsa4456852007-07-09 11:56:43 -07003001 /* Want to check and possibly repair P and Q.
3002 * However there could be one 'failed' device, in which
3003 * case we can only check one of them, possibly using the
3004 * other to generate missing data
3005 */
3006
Dan Williamsd82dfee2009-07-14 13:40:57 -07003007 switch (sh->check_state) {
3008 case check_state_idle:
3009 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10003010 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003011 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07003012 * makes sense to check P (If anything else were failed,
3013 * we would have used P to recreate it).
3014 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003015 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07003016 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003017 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003018 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07003019 * anything, so it makes sense to check it
3020 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003021 if (sh->check_state == check_state_run)
3022 sh->check_state = check_state_run_pq;
3023 else
3024 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07003025 }
Dan Williams36d1c642009-07-14 11:48:22 -07003026
Dan Williamsd82dfee2009-07-14 13:40:57 -07003027 /* discard potentially stale zero_sum_result */
3028 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07003029
Dan Williamsd82dfee2009-07-14 13:40:57 -07003030 if (sh->check_state == check_state_run) {
3031 /* async_xor_zero_sum destroys the contents of P */
3032 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
3033 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07003034 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003035 if (sh->check_state >= check_state_run &&
3036 sh->check_state <= check_state_run_pq) {
3037 /* async_syndrome_zero_sum preserves P and Q, so
3038 * no need to mark them !uptodate here
3039 */
3040 set_bit(STRIPE_OP_CHECK, &s->ops_request);
3041 break;
3042 }
Dan Williams36d1c642009-07-14 11:48:22 -07003043
Dan Williamsd82dfee2009-07-14 13:40:57 -07003044 /* we have 2-disk failure */
3045 BUG_ON(s->failed != 2);
3046 /* fall through */
3047 case check_state_compute_result:
3048 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07003049
Dan Williamsd82dfee2009-07-14 13:40:57 -07003050 /* check that a write has not made the stripe insync */
3051 if (test_bit(STRIPE_INSYNC, &sh->state))
3052 break;
Dan Williamsa4456852007-07-09 11:56:43 -07003053
3054 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07003055 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07003056 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003057 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07003058 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003059 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07003060 s->locked++;
3061 set_bit(R5_LOCKED, &dev->flags);
3062 set_bit(R5_Wantwrite, &dev->flags);
3063 }
3064 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003065 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07003066 s->locked++;
3067 set_bit(R5_LOCKED, &dev->flags);
3068 set_bit(R5_Wantwrite, &dev->flags);
3069 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003070 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003071 dev = &sh->dev[pd_idx];
3072 s->locked++;
3073 set_bit(R5_LOCKED, &dev->flags);
3074 set_bit(R5_Wantwrite, &dev->flags);
3075 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003076 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003077 dev = &sh->dev[qd_idx];
3078 s->locked++;
3079 set_bit(R5_LOCKED, &dev->flags);
3080 set_bit(R5_Wantwrite, &dev->flags);
3081 }
3082 clear_bit(STRIPE_DEGRADED, &sh->state);
3083
3084 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003085 break;
3086 case check_state_run:
3087 case check_state_run_q:
3088 case check_state_run_pq:
3089 break; /* we will be called again upon completion */
3090 case check_state_check_result:
3091 sh->check_state = check_state_idle;
3092
3093 /* handle a successful check operation, if parity is correct
3094 * we are done. Otherwise update the mismatch count and repair
3095 * parity if !MD_RECOVERY_CHECK
3096 */
3097 if (sh->ops.zero_sum_result == 0) {
3098 /* both parities are correct */
3099 if (!s->failed)
3100 set_bit(STRIPE_INSYNC, &sh->state);
3101 else {
3102 /* in contrast to the raid5 case we can validate
3103 * parity, but still have a failure to write
3104 * back
3105 */
3106 sh->check_state = check_state_compute_result;
3107 /* Returning at this point means that we may go
3108 * off and bring p and/or q uptodate again so
3109 * we make sure to check zero_sum_result again
3110 * to verify if p or q need writeback
3111 */
3112 }
3113 } else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003114 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003115 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3116 /* don't try to repair!! */
3117 set_bit(STRIPE_INSYNC, &sh->state);
3118 else {
3119 int *target = &sh->ops.target;
3120
3121 sh->ops.target = -1;
3122 sh->ops.target2 = -1;
3123 sh->check_state = check_state_compute_run;
3124 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3125 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3126 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3127 set_bit(R5_Wantcompute,
3128 &sh->dev[pd_idx].flags);
3129 *target = pd_idx;
3130 target = &sh->ops.target2;
3131 s->uptodate++;
3132 }
3133 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3134 set_bit(R5_Wantcompute,
3135 &sh->dev[qd_idx].flags);
3136 *target = qd_idx;
3137 s->uptodate++;
3138 }
3139 }
3140 }
3141 break;
3142 case check_state_compute_run:
3143 break;
3144 default:
3145 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3146 __func__, sh->check_state,
3147 (unsigned long long) sh->sector);
3148 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003149 }
3150}
3151
NeilBrownd1688a62011-10-11 16:49:52 +11003152static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003153{
3154 int i;
3155
3156 /* We have read all the blocks in this stripe and now we need to
3157 * copy some of them into a target stripe for expand.
3158 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003159 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003160 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3161 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003162 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003163 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003164 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003165 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003166
NeilBrown784052e2009-03-31 15:19:07 +11003167 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003168 sector_t s = raid5_compute_sector(conf, bn, 0,
3169 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003170 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003171 if (sh2 == NULL)
3172 /* so far only the early blocks of this stripe
3173 * have been requested. When later blocks
3174 * get requested, we will try again
3175 */
3176 continue;
3177 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3178 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3179 /* must have already done this block */
3180 release_stripe(sh2);
3181 continue;
3182 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003183
3184 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003185 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003186 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003187 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003188 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003189
Dan Williamsa4456852007-07-09 11:56:43 -07003190 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3191 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3192 for (j = 0; j < conf->raid_disks; j++)
3193 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003194 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003195 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3196 break;
3197 if (j == conf->raid_disks) {
3198 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3199 set_bit(STRIPE_HANDLE, &sh2->state);
3200 }
3201 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003202
Dan Williamsa4456852007-07-09 11:56:43 -07003203 }
NeilBrowna2e08552007-09-11 15:23:36 -07003204 /* done submitting copies, wait for them to complete */
NeilBrown749586b2012-11-20 14:11:15 +11003205 async_tx_quiesce(&tx);
Dan Williamsa4456852007-07-09 11:56:43 -07003206}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207
3208/*
3209 * handle_stripe - do things to a stripe.
3210 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003211 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3212 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003214 * return some read requests which now have data
3215 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 * schedule a read on some buffers
3217 * schedule a write of some buffers
3218 * return confirmation of parity correctness
3219 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 */
Dan Williamsa4456852007-07-09 11:56:43 -07003221
NeilBrownacfe7262011-07-27 11:00:36 +10003222static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003223{
NeilBrownd1688a62011-10-11 16:49:52 +11003224 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003225 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003226 struct r5dev *dev;
3227 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003228 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003229
NeilBrownacfe7262011-07-27 11:00:36 +10003230 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003231
NeilBrownacfe7262011-07-27 11:00:36 +10003232 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3233 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3234 s->failed_num[0] = -1;
3235 s->failed_num[1] = -1;
3236
3237 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003238 rcu_read_lock();
3239 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003240 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003241 sector_t first_bad;
3242 int bad_sectors;
3243 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003244
NeilBrown16a53ec2006-06-26 00:27:38 -07003245 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003246
Dan Williams45b42332007-07-09 11:56:43 -07003247 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003248 i, dev->flags,
3249 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003250 /* maybe we can reply to a read
3251 *
3252 * new wantfill requests are only permitted while
3253 * ops_complete_biofill is guaranteed to be inactive
3254 */
3255 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3256 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3257 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003258
3259 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003260 if (test_bit(R5_LOCKED, &dev->flags))
3261 s->locked++;
3262 if (test_bit(R5_UPTODATE, &dev->flags))
3263 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003264 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003265 s->compute++;
3266 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003267 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003268
NeilBrownacfe7262011-07-27 11:00:36 +10003269 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003270 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003271 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003272 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003273 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003274 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003275 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003276 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003277 }
Dan Williamsa4456852007-07-09 11:56:43 -07003278 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003279 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003280 /* Prefer to use the replacement for reads, but only
3281 * if it is recovered enough and has no bad blocks.
3282 */
3283 rdev = rcu_dereference(conf->disks[i].replacement);
3284 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3285 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3286 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3287 &first_bad, &bad_sectors))
3288 set_bit(R5_ReadRepl, &dev->flags);
3289 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003290 if (rdev)
3291 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003292 rdev = rcu_dereference(conf->disks[i].rdev);
3293 clear_bit(R5_ReadRepl, &dev->flags);
3294 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003295 if (rdev && test_bit(Faulty, &rdev->flags))
3296 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003297 if (rdev) {
3298 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3299 &first_bad, &bad_sectors);
3300 if (s->blocked_rdev == NULL
3301 && (test_bit(Blocked, &rdev->flags)
3302 || is_bad < 0)) {
3303 if (is_bad < 0)
3304 set_bit(BlockedBadBlocks,
3305 &rdev->flags);
3306 s->blocked_rdev = rdev;
3307 atomic_inc(&rdev->nr_pending);
3308 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003309 }
NeilBrown415e72d2010-06-17 17:25:21 +10003310 clear_bit(R5_Insync, &dev->flags);
3311 if (!rdev)
3312 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003313 else if (is_bad) {
3314 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10003315 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3316 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003317 /* treat as in-sync, but with a read error
3318 * which we can now try to correct
3319 */
3320 set_bit(R5_Insync, &dev->flags);
3321 set_bit(R5_ReadError, &dev->flags);
3322 }
3323 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003324 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003325 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003326 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003327 set_bit(R5_Insync, &dev->flags);
3328 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3329 test_bit(R5_Expanded, &dev->flags))
3330 /* If we've reshaped into here, we assume it is Insync.
3331 * We will shortly update recovery_offset to make
3332 * it official.
3333 */
3334 set_bit(R5_Insync, &dev->flags);
3335
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003336 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003337 /* This flag does not apply to '.replacement'
3338 * only to .rdev, so make sure to check that*/
3339 struct md_rdev *rdev2 = rcu_dereference(
3340 conf->disks[i].rdev);
3341 if (rdev2 == rdev)
3342 clear_bit(R5_Insync, &dev->flags);
3343 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003344 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003345 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003346 } else
3347 clear_bit(R5_WriteError, &dev->flags);
3348 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003349 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003350 /* This flag does not apply to '.replacement'
3351 * only to .rdev, so make sure to check that*/
3352 struct md_rdev *rdev2 = rcu_dereference(
3353 conf->disks[i].rdev);
3354 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003355 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003356 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003357 } else
3358 clear_bit(R5_MadeGood, &dev->flags);
3359 }
NeilBrown977df362011-12-23 10:17:53 +11003360 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3361 struct md_rdev *rdev2 = rcu_dereference(
3362 conf->disks[i].replacement);
3363 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3364 s->handle_bad_blocks = 1;
3365 atomic_inc(&rdev2->nr_pending);
3366 } else
3367 clear_bit(R5_MadeGoodRepl, &dev->flags);
3368 }
NeilBrown415e72d2010-06-17 17:25:21 +10003369 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003370 /* The ReadError flag will just be confusing now */
3371 clear_bit(R5_ReadError, &dev->flags);
3372 clear_bit(R5_ReWrite, &dev->flags);
3373 }
NeilBrown415e72d2010-06-17 17:25:21 +10003374 if (test_bit(R5_ReadError, &dev->flags))
3375 clear_bit(R5_Insync, &dev->flags);
3376 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003377 if (s->failed < 2)
3378 s->failed_num[s->failed] = i;
3379 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003380 if (rdev && !test_bit(Faulty, &rdev->flags))
3381 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003382 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003383 }
NeilBrown9a3e1102011-12-23 10:17:53 +11003384 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3385 /* If there is a failed device being replaced,
3386 * we must be recovering.
3387 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10003388 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11003389 * else we can only be replacing
3390 * sync and recovery both need to read all devices, and so
3391 * use the same flag.
3392 */
3393 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10003394 sh->sector >= conf->mddev->recovery_cp ||
3395 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11003396 s->syncing = 1;
3397 else
3398 s->replacing = 1;
3399 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003400 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003401}
NeilBrownf4168852007-02-28 20:11:53 -08003402
NeilBrowncc940152011-07-26 11:35:35 +10003403static void handle_stripe(struct stripe_head *sh)
3404{
3405 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003406 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003407 int i;
NeilBrown84789552011-07-27 11:00:36 +10003408 int prexor;
3409 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003410 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003411
3412 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003413 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003414 /* already being handled, ensure it gets handled
3415 * again when current action finishes */
3416 set_bit(STRIPE_HANDLE, &sh->state);
3417 return;
3418 }
3419
3420 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3421 set_bit(STRIPE_SYNCING, &sh->state);
3422 clear_bit(STRIPE_INSYNC, &sh->state);
3423 }
3424 clear_bit(STRIPE_DELAYED, &sh->state);
3425
3426 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3427 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3428 (unsigned long long)sh->sector, sh->state,
3429 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3430 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003431
NeilBrownacfe7262011-07-27 11:00:36 +10003432 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003433
NeilBrownbc2607f2011-07-28 11:39:22 +10003434 if (s.handle_bad_blocks) {
3435 set_bit(STRIPE_HANDLE, &sh->state);
3436 goto finish;
3437 }
3438
NeilBrown474af965fe2011-07-27 11:00:36 +10003439 if (unlikely(s.blocked_rdev)) {
3440 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003441 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003442 set_bit(STRIPE_HANDLE, &sh->state);
3443 goto finish;
3444 }
3445 /* There is nothing for the blocked_rdev to block */
3446 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3447 s.blocked_rdev = NULL;
3448 }
3449
3450 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3451 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3452 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3453 }
3454
3455 pr_debug("locked=%d uptodate=%d to_read=%d"
3456 " to_write=%d failed=%d failed_num=%d,%d\n",
3457 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3458 s.failed_num[0], s.failed_num[1]);
3459 /* check if the array has lost more than max_degraded devices and,
3460 * if so, some requests might need to be failed.
3461 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003462 if (s.failed > conf->max_degraded) {
3463 sh->check_state = 0;
3464 sh->reconstruct_state = 0;
3465 if (s.to_read+s.to_write+s.written)
3466 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003467 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003468 handle_failed_sync(conf, sh, &s);
3469 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003470
NeilBrown84789552011-07-27 11:00:36 +10003471 /* Now we check to see if any write operations have recently
3472 * completed
3473 */
3474 prexor = 0;
3475 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3476 prexor = 1;
3477 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3478 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3479 sh->reconstruct_state = reconstruct_state_idle;
3480
3481 /* All the 'written' buffers and the parity block are ready to
3482 * be written back to disk
3483 */
Shaohua Li9e4447682012-10-11 13:49:49 +11003484 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
3485 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003486 BUG_ON(sh->qd_idx >= 0 &&
Shaohua Li9e4447682012-10-11 13:49:49 +11003487 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
3488 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003489 for (i = disks; i--; ) {
3490 struct r5dev *dev = &sh->dev[i];
3491 if (test_bit(R5_LOCKED, &dev->flags) &&
3492 (i == sh->pd_idx || i == sh->qd_idx ||
3493 dev->written)) {
3494 pr_debug("Writing block %d\n", i);
3495 set_bit(R5_Wantwrite, &dev->flags);
3496 if (prexor)
3497 continue;
3498 if (!test_bit(R5_Insync, &dev->flags) ||
3499 ((i == sh->pd_idx || i == sh->qd_idx) &&
3500 s.failed == 0))
3501 set_bit(STRIPE_INSYNC, &sh->state);
3502 }
3503 }
3504 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3505 s.dec_preread_active = 1;
3506 }
3507
NeilBrownef5b7c62012-11-22 09:13:36 +11003508 /*
3509 * might be able to return some write requests if the parity blocks
3510 * are safe, or on a failed drive
3511 */
3512 pdev = &sh->dev[sh->pd_idx];
3513 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3514 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3515 qdev = &sh->dev[sh->qd_idx];
3516 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3517 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3518 || conf->level < 6;
3519
3520 if (s.written &&
3521 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3522 && !test_bit(R5_LOCKED, &pdev->flags)
3523 && (test_bit(R5_UPTODATE, &pdev->flags) ||
3524 test_bit(R5_Discard, &pdev->flags))))) &&
3525 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3526 && !test_bit(R5_LOCKED, &qdev->flags)
3527 && (test_bit(R5_UPTODATE, &qdev->flags) ||
3528 test_bit(R5_Discard, &qdev->flags))))))
3529 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3530
3531 /* Now we might consider reading some blocks, either to check/generate
3532 * parity, or to satisfy requests
3533 * or to load a block that is being partially written.
3534 */
3535 if (s.to_read || s.non_overwrite
3536 || (conf->level == 6 && s.to_write && s.failed)
3537 || (s.syncing && (s.uptodate + s.compute < disks))
3538 || s.replacing
3539 || s.expanding)
3540 handle_stripe_fill(sh, &s, disks);
3541
NeilBrown84789552011-07-27 11:00:36 +10003542 /* Now to consider new write requests and what else, if anything
3543 * should be read. We do not handle new writes when:
3544 * 1/ A 'write' operation (copy+xor) is already in flight.
3545 * 2/ A 'check' operation is in flight, as it may clobber the parity
3546 * block.
3547 */
3548 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3549 handle_stripe_dirtying(conf, sh, &s, disks);
3550
3551 /* maybe we need to check and possibly fix the parity for this stripe
3552 * Any reads will already have been scheduled, so we just see if enough
3553 * data is available. The parity check is held off while parity
3554 * dependent operations are in flight.
3555 */
3556 if (sh->check_state ||
3557 (s.syncing && s.locked == 0 &&
3558 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3559 !test_bit(STRIPE_INSYNC, &sh->state))) {
3560 if (conf->level == 6)
3561 handle_parity_checks6(conf, sh, &s, disks);
3562 else
3563 handle_parity_checks5(conf, sh, &s, disks);
3564 }
NeilBrownc5a31002011-07-27 11:00:36 +10003565
NeilBrown9a3e1102011-12-23 10:17:53 +11003566 if (s.replacing && s.locked == 0
3567 && !test_bit(STRIPE_INSYNC, &sh->state)) {
3568 /* Write out to replacement devices where possible */
3569 for (i = 0; i < conf->raid_disks; i++)
3570 if (test_bit(R5_UPTODATE, &sh->dev[i].flags) &&
3571 test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3572 set_bit(R5_WantReplace, &sh->dev[i].flags);
3573 set_bit(R5_LOCKED, &sh->dev[i].flags);
3574 s.locked++;
3575 }
3576 set_bit(STRIPE_INSYNC, &sh->state);
3577 }
3578 if ((s.syncing || s.replacing) && s.locked == 0 &&
3579 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003580 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3581 clear_bit(STRIPE_SYNCING, &sh->state);
3582 }
3583
3584 /* If the failed drives are just a ReadError, then we might need
3585 * to progress the repair/check process
3586 */
3587 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3588 for (i = 0; i < s.failed; i++) {
3589 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3590 if (test_bit(R5_ReadError, &dev->flags)
3591 && !test_bit(R5_LOCKED, &dev->flags)
3592 && test_bit(R5_UPTODATE, &dev->flags)
3593 ) {
3594 if (!test_bit(R5_ReWrite, &dev->flags)) {
3595 set_bit(R5_Wantwrite, &dev->flags);
3596 set_bit(R5_ReWrite, &dev->flags);
3597 set_bit(R5_LOCKED, &dev->flags);
3598 s.locked++;
3599 } else {
3600 /* let's read it back */
3601 set_bit(R5_Wantread, &dev->flags);
3602 set_bit(R5_LOCKED, &dev->flags);
3603 s.locked++;
3604 }
3605 }
3606 }
3607
3608
NeilBrown3687c062011-07-27 11:00:36 +10003609 /* Finish reconstruct operations initiated by the expansion process */
3610 if (sh->reconstruct_state == reconstruct_state_result) {
3611 struct stripe_head *sh_src
3612 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3613 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3614 /* sh cannot be written until sh_src has been read.
3615 * so arrange for sh to be delayed a little
3616 */
3617 set_bit(STRIPE_DELAYED, &sh->state);
3618 set_bit(STRIPE_HANDLE, &sh->state);
3619 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3620 &sh_src->state))
3621 atomic_inc(&conf->preread_active_stripes);
3622 release_stripe(sh_src);
3623 goto finish;
3624 }
3625 if (sh_src)
3626 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003627
NeilBrown3687c062011-07-27 11:00:36 +10003628 sh->reconstruct_state = reconstruct_state_idle;
3629 clear_bit(STRIPE_EXPANDING, &sh->state);
3630 for (i = conf->raid_disks; i--; ) {
3631 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3632 set_bit(R5_LOCKED, &sh->dev[i].flags);
3633 s.locked++;
3634 }
3635 }
3636
3637 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3638 !sh->reconstruct_state) {
3639 /* Need to write out all blocks after computing parity */
3640 sh->disks = conf->raid_disks;
3641 stripe_set_idx(sh->sector, conf, 0, sh);
3642 schedule_reconstruction(sh, &s, 1, 1);
3643 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3644 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3645 atomic_dec(&conf->reshape_stripes);
3646 wake_up(&conf->wait_for_overlap);
3647 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3648 }
3649
3650 if (s.expanding && s.locked == 0 &&
3651 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3652 handle_stripe_expansion(conf, sh);
3653
3654finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003655 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10003656 if (unlikely(s.blocked_rdev)) {
3657 if (conf->mddev->external)
3658 md_wait_for_blocked_rdev(s.blocked_rdev,
3659 conf->mddev);
3660 else
3661 /* Internal metadata will immediately
3662 * be written by raid5d, so we don't
3663 * need to wait here.
3664 */
3665 rdev_dec_pending(s.blocked_rdev,
3666 conf->mddev);
3667 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003668
NeilBrownbc2607f2011-07-28 11:39:22 +10003669 if (s.handle_bad_blocks)
3670 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003671 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003672 struct r5dev *dev = &sh->dev[i];
3673 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3674 /* We own a safe reference to the rdev */
3675 rdev = conf->disks[i].rdev;
3676 if (!rdev_set_badblocks(rdev, sh->sector,
3677 STRIPE_SECTORS, 0))
3678 md_error(conf->mddev, rdev);
3679 rdev_dec_pending(rdev, conf->mddev);
3680 }
NeilBrownb84db562011-07-28 11:39:23 +10003681 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3682 rdev = conf->disks[i].rdev;
3683 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003684 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10003685 rdev_dec_pending(rdev, conf->mddev);
3686 }
NeilBrown977df362011-12-23 10:17:53 +11003687 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3688 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11003689 if (!rdev)
3690 /* rdev have been moved down */
3691 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11003692 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003693 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11003694 rdev_dec_pending(rdev, conf->mddev);
3695 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003696 }
3697
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003698 if (s.ops_request)
3699 raid_run_ops(sh, s.ops_request);
3700
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003701 ops_run_io(sh, &s);
3702
NeilBrownc5709ef2011-07-26 11:35:20 +10003703 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003704 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003705 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003706 * have actually been submitted.
3707 */
3708 atomic_dec(&conf->preread_active_stripes);
3709 if (atomic_read(&conf->preread_active_stripes) <
3710 IO_THRESHOLD)
3711 md_wakeup_thread(conf->mddev->thread);
3712 }
3713
NeilBrownc5709ef2011-07-26 11:35:20 +10003714 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003715
Dan Williams257a4b42011-11-08 16:22:06 +11003716 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003717}
3718
NeilBrownd1688a62011-10-11 16:49:52 +11003719static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720{
3721 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3722 while (!list_empty(&conf->delayed_list)) {
3723 struct list_head *l = conf->delayed_list.next;
3724 struct stripe_head *sh;
3725 sh = list_entry(l, struct stripe_head, lru);
3726 list_del_init(l);
3727 clear_bit(STRIPE_DELAYED, &sh->state);
3728 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3729 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003730 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 }
NeilBrown482c0832011-04-18 18:25:42 +10003732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733}
3734
NeilBrownd1688a62011-10-11 16:49:52 +11003735static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003736{
3737 /* device_lock is held */
3738 struct list_head head;
3739 list_add(&head, &conf->bitmap_list);
3740 list_del_init(&conf->bitmap_list);
3741 while (!list_empty(&head)) {
3742 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3743 list_del_init(&sh->lru);
3744 atomic_inc(&sh->count);
3745 __release_stripe(conf, sh);
3746 }
3747}
3748
NeilBrownfd01b882011-10-11 16:47:53 +11003749int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003750{
NeilBrownd1688a62011-10-11 16:49:52 +11003751 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003752
3753 /* No difference between reads and writes. Just check
3754 * how busy the stripe_cache is
3755 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003756
NeilBrownf022b2f2006-10-03 01:15:56 -07003757 if (conf->inactive_blocked)
3758 return 1;
3759 if (conf->quiesce)
3760 return 1;
3761 if (list_empty_careful(&conf->inactive_list))
3762 return 1;
3763
3764 return 0;
3765}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003766EXPORT_SYMBOL_GPL(md_raid5_congested);
3767
3768static int raid5_congested(void *data, int bits)
3769{
NeilBrownfd01b882011-10-11 16:47:53 +11003770 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003771
3772 return mddev_congested(mddev, bits) ||
3773 md_raid5_congested(mddev, bits);
3774}
NeilBrownf022b2f2006-10-03 01:15:56 -07003775
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003776/* We want read requests to align with chunks where possible,
3777 * but write requests don't need to.
3778 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003779static int raid5_mergeable_bvec(struct request_queue *q,
3780 struct bvec_merge_data *bvm,
3781 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003782{
NeilBrownfd01b882011-10-11 16:47:53 +11003783 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003784 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003785 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003786 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003787 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003788
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003789 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003790 return biovec->bv_len; /* always allow writes to be mergeable */
3791
Andre Noll664e7c42009-06-18 08:45:27 +10003792 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3793 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003794 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3795 if (max < 0) max = 0;
3796 if (max <= biovec->bv_len && bio_sectors == 0)
3797 return biovec->bv_len;
3798 else
3799 return max;
3800}
3801
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003802
NeilBrownfd01b882011-10-11 16:47:53 +11003803static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003804{
3805 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003806 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003807 unsigned int bio_sectors = bio->bi_size >> 9;
3808
Andre Noll664e7c42009-06-18 08:45:27 +10003809 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3810 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003811 return chunk_sectors >=
3812 ((sector & (chunk_sectors - 1)) + bio_sectors);
3813}
3814
3815/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003816 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3817 * later sampled by raid5d.
3818 */
NeilBrownd1688a62011-10-11 16:49:52 +11003819static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003820{
3821 unsigned long flags;
3822
3823 spin_lock_irqsave(&conf->device_lock, flags);
3824
3825 bi->bi_next = conf->retry_read_aligned_list;
3826 conf->retry_read_aligned_list = bi;
3827
3828 spin_unlock_irqrestore(&conf->device_lock, flags);
3829 md_wakeup_thread(conf->mddev->thread);
3830}
3831
3832
NeilBrownd1688a62011-10-11 16:49:52 +11003833static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003834{
3835 struct bio *bi;
3836
3837 bi = conf->retry_read_aligned;
3838 if (bi) {
3839 conf->retry_read_aligned = NULL;
3840 return bi;
3841 }
3842 bi = conf->retry_read_aligned_list;
3843 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003844 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003845 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003846 /*
3847 * this sets the active strip count to 1 and the processed
3848 * strip count to zero (upper 8 bits)
3849 */
Shaohua Lie7836bd62012-07-19 16:01:31 +10003850 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003851 }
3852
3853 return bi;
3854}
3855
3856
3857/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003858 * The "raid5_align_endio" should check if the read succeeded and if it
3859 * did, call bio_endio on the original bio (having bio_put the new bio
3860 * first).
3861 * If the read failed..
3862 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003863static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003864{
3865 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003866 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003867 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003868 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003869 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003870
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003871 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003872
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003873 rdev = (void*)raid_bi->bi_next;
3874 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003875 mddev = rdev->mddev;
3876 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003877
3878 rdev_dec_pending(rdev, conf->mddev);
3879
3880 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003881 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003882 if (atomic_dec_and_test(&conf->active_aligned_reads))
3883 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003884 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003885 }
3886
3887
Dan Williams45b42332007-07-09 11:56:43 -07003888 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003889
3890 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003891}
3892
Neil Brown387bb172007-02-08 14:20:29 -08003893static int bio_fits_rdev(struct bio *bi)
3894{
Jens Axboe165125e2007-07-24 09:28:11 +02003895 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003896
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003897 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003898 return 0;
3899 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003900 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003901 return 0;
3902
3903 if (q->merge_bvec_fn)
3904 /* it's too hard to apply the merge_bvec_fn at this stage,
3905 * just just give up
3906 */
3907 return 0;
3908
3909 return 1;
3910}
3911
3912
NeilBrownfd01b882011-10-11 16:47:53 +11003913static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003914{
NeilBrownd1688a62011-10-11 16:49:52 +11003915 struct r5conf *conf = mddev->private;
NeilBrown8553fe72009-12-14 12:49:47 +11003916 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003917 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11003918 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11003919 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003920
3921 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003922 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003923 return 0;
3924 }
3925 /*
NeilBrowna167f662010-10-26 18:31:13 +11003926 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003927 */
NeilBrowna167f662010-10-26 18:31:13 +11003928 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003929 if (!align_bi)
3930 return 0;
3931 /*
3932 * set bi_end_io to a new function, and set bi_private to the
3933 * original bio.
3934 */
3935 align_bi->bi_end_io = raid5_align_endio;
3936 align_bi->bi_private = raid_bio;
3937 /*
3938 * compute position
3939 */
NeilBrown112bf892009-03-31 14:39:38 +11003940 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3941 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003942 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003943
Kent Overstreetf73a1c72012-09-25 15:05:12 -07003944 end_sector = bio_end_sector(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003945 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11003946 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
3947 if (!rdev || test_bit(Faulty, &rdev->flags) ||
3948 rdev->recovery_offset < end_sector) {
3949 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3950 if (rdev &&
3951 (test_bit(Faulty, &rdev->flags) ||
3952 !(test_bit(In_sync, &rdev->flags) ||
3953 rdev->recovery_offset >= end_sector)))
3954 rdev = NULL;
3955 }
3956 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10003957 sector_t first_bad;
3958 int bad_sectors;
3959
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003960 atomic_inc(&rdev->nr_pending);
3961 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003962 raid_bio->bi_next = (void*)rdev;
3963 align_bi->bi_bdev = rdev->bdev;
3964 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003965
NeilBrown31c176e2011-07-28 11:39:22 +10003966 if (!bio_fits_rdev(align_bi) ||
3967 is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
3968 &first_bad, &bad_sectors)) {
3969 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08003970 bio_put(align_bi);
3971 rdev_dec_pending(rdev, mddev);
3972 return 0;
3973 }
3974
majianpeng6c0544e2012-06-12 08:31:10 +08003975 /* No reshape active, so we can trust rdev->data_offset */
3976 align_bi->bi_sector += rdev->data_offset;
3977
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003978 spin_lock_irq(&conf->device_lock);
3979 wait_event_lock_irq(conf->wait_for_stripe,
3980 conf->quiesce == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01003981 conf->device_lock);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003982 atomic_inc(&conf->active_aligned_reads);
3983 spin_unlock_irq(&conf->device_lock);
3984
NeilBrowna9add5d2012-10-31 11:59:09 +11003985 trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev),
3986 align_bi, disk_devt(mddev->gendisk),
3987 raid_bio->bi_sector);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003988 generic_make_request(align_bi);
3989 return 1;
3990 } else {
3991 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003992 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003993 return 0;
3994 }
3995}
3996
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003997/* __get_priority_stripe - get the next stripe to process
3998 *
3999 * Full stripe writes are allowed to pass preread active stripes up until
4000 * the bypass_threshold is exceeded. In general the bypass_count
4001 * increments when the handle_list is handled before the hold_list; however, it
4002 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
4003 * stripe with in flight i/o. The bypass_count will be reset when the
4004 * head of the hold_list has changed, i.e. the head was promoted to the
4005 * handle_list.
4006 */
NeilBrownd1688a62011-10-11 16:49:52 +11004007static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004008{
4009 struct stripe_head *sh;
4010
4011 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
4012 __func__,
4013 list_empty(&conf->handle_list) ? "empty" : "busy",
4014 list_empty(&conf->hold_list) ? "empty" : "busy",
4015 atomic_read(&conf->pending_full_writes), conf->bypass_count);
4016
4017 if (!list_empty(&conf->handle_list)) {
4018 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
4019
4020 if (list_empty(&conf->hold_list))
4021 conf->bypass_count = 0;
4022 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
4023 if (conf->hold_list.next == conf->last_hold)
4024 conf->bypass_count++;
4025 else {
4026 conf->last_hold = conf->hold_list.next;
4027 conf->bypass_count -= conf->bypass_threshold;
4028 if (conf->bypass_count < 0)
4029 conf->bypass_count = 0;
4030 }
4031 }
4032 } else if (!list_empty(&conf->hold_list) &&
4033 ((conf->bypass_threshold &&
4034 conf->bypass_count > conf->bypass_threshold) ||
4035 atomic_read(&conf->pending_full_writes) == 0)) {
4036 sh = list_entry(conf->hold_list.next,
4037 typeof(*sh), lru);
4038 conf->bypass_count -= conf->bypass_threshold;
4039 if (conf->bypass_count < 0)
4040 conf->bypass_count = 0;
4041 } else
4042 return NULL;
4043
4044 list_del_init(&sh->lru);
4045 atomic_inc(&sh->count);
4046 BUG_ON(atomic_read(&sh->count) != 1);
4047 return sh;
4048}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004049
Shaohua Li8811b592012-08-02 08:33:00 +10004050struct raid5_plug_cb {
4051 struct blk_plug_cb cb;
4052 struct list_head list;
4053};
4054
4055static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
4056{
4057 struct raid5_plug_cb *cb = container_of(
4058 blk_cb, struct raid5_plug_cb, cb);
4059 struct stripe_head *sh;
4060 struct mddev *mddev = cb->cb.data;
4061 struct r5conf *conf = mddev->private;
NeilBrowna9add5d2012-10-31 11:59:09 +11004062 int cnt = 0;
Shaohua Li8811b592012-08-02 08:33:00 +10004063
4064 if (cb->list.next && !list_empty(&cb->list)) {
4065 spin_lock_irq(&conf->device_lock);
4066 while (!list_empty(&cb->list)) {
4067 sh = list_first_entry(&cb->list, struct stripe_head, lru);
4068 list_del_init(&sh->lru);
4069 /*
4070 * avoid race release_stripe_plug() sees
4071 * STRIPE_ON_UNPLUG_LIST clear but the stripe
4072 * is still in our list
4073 */
4074 smp_mb__before_clear_bit();
4075 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
4076 __release_stripe(conf, sh);
NeilBrowna9add5d2012-10-31 11:59:09 +11004077 cnt++;
Shaohua Li8811b592012-08-02 08:33:00 +10004078 }
4079 spin_unlock_irq(&conf->device_lock);
4080 }
NeilBrowna9add5d2012-10-31 11:59:09 +11004081 trace_block_unplug(mddev->queue, cnt, !from_schedule);
Shaohua Li8811b592012-08-02 08:33:00 +10004082 kfree(cb);
4083}
4084
4085static void release_stripe_plug(struct mddev *mddev,
4086 struct stripe_head *sh)
4087{
4088 struct blk_plug_cb *blk_cb = blk_check_plugged(
4089 raid5_unplug, mddev,
4090 sizeof(struct raid5_plug_cb));
4091 struct raid5_plug_cb *cb;
4092
4093 if (!blk_cb) {
4094 release_stripe(sh);
4095 return;
4096 }
4097
4098 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
4099
4100 if (cb->list.next == NULL)
4101 INIT_LIST_HEAD(&cb->list);
4102
4103 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
4104 list_add_tail(&sh->lru, &cb->list);
4105 else
4106 release_stripe(sh);
4107}
4108
Shaohua Li620125f2012-10-11 13:49:05 +11004109static void make_discard_request(struct mddev *mddev, struct bio *bi)
4110{
4111 struct r5conf *conf = mddev->private;
4112 sector_t logical_sector, last_sector;
4113 struct stripe_head *sh;
4114 int remaining;
4115 int stripe_sectors;
4116
4117 if (mddev->reshape_position != MaxSector)
4118 /* Skip discard while reshape is happening */
4119 return;
4120
4121 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4122 last_sector = bi->bi_sector + (bi->bi_size>>9);
4123
4124 bi->bi_next = NULL;
4125 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
4126
4127 stripe_sectors = conf->chunk_sectors *
4128 (conf->raid_disks - conf->max_degraded);
4129 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
4130 stripe_sectors);
4131 sector_div(last_sector, stripe_sectors);
4132
4133 logical_sector *= conf->chunk_sectors;
4134 last_sector *= conf->chunk_sectors;
4135
4136 for (; logical_sector < last_sector;
4137 logical_sector += STRIPE_SECTORS) {
4138 DEFINE_WAIT(w);
4139 int d;
4140 again:
4141 sh = get_active_stripe(conf, logical_sector, 0, 0, 0);
4142 prepare_to_wait(&conf->wait_for_overlap, &w,
4143 TASK_UNINTERRUPTIBLE);
4144 spin_lock_irq(&sh->stripe_lock);
4145 for (d = 0; d < conf->raid_disks; d++) {
4146 if (d == sh->pd_idx || d == sh->qd_idx)
4147 continue;
4148 if (sh->dev[d].towrite || sh->dev[d].toread) {
4149 set_bit(R5_Overlap, &sh->dev[d].flags);
4150 spin_unlock_irq(&sh->stripe_lock);
4151 release_stripe(sh);
4152 schedule();
4153 goto again;
4154 }
4155 }
4156 finish_wait(&conf->wait_for_overlap, &w);
4157 for (d = 0; d < conf->raid_disks; d++) {
4158 if (d == sh->pd_idx || d == sh->qd_idx)
4159 continue;
4160 sh->dev[d].towrite = bi;
4161 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
4162 raid5_inc_bi_active_stripes(bi);
4163 }
4164 spin_unlock_irq(&sh->stripe_lock);
4165 if (conf->mddev->bitmap) {
4166 for (d = 0;
4167 d < conf->raid_disks - conf->max_degraded;
4168 d++)
4169 bitmap_startwrite(mddev->bitmap,
4170 sh->sector,
4171 STRIPE_SECTORS,
4172 0);
4173 sh->bm_seq = conf->seq_flush + 1;
4174 set_bit(STRIPE_BIT_DELAY, &sh->state);
4175 }
4176
4177 set_bit(STRIPE_HANDLE, &sh->state);
4178 clear_bit(STRIPE_DELAYED, &sh->state);
4179 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4180 atomic_inc(&conf->preread_active_stripes);
4181 release_stripe_plug(mddev, sh);
4182 }
4183
4184 remaining = raid5_dec_bi_active_stripes(bi);
4185 if (remaining == 0) {
4186 md_write_end(mddev);
4187 bio_endio(bi, 0);
4188 }
4189}
4190
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07004191static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192{
NeilBrownd1688a62011-10-11 16:49:52 +11004193 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11004194 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 sector_t new_sector;
4196 sector_t logical_sector, last_sector;
4197 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01004198 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11004199 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200
Tejun Heoe9c74692010-09-03 11:56:18 +02004201 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
4202 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004203 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07004204 }
4205
NeilBrown3d310eb2005-06-21 17:17:26 -07004206 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07004207
NeilBrown802ba062006-12-13 00:34:13 -08004208 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004209 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11004210 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004211 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004212
Shaohua Li620125f2012-10-11 13:49:05 +11004213 if (unlikely(bi->bi_rw & REQ_DISCARD)) {
4214 make_discard_request(mddev, bi);
4215 return;
4216 }
4217
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004219 last_sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 bi->bi_next = NULL;
4221 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07004222
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
4224 DEFINE_WAIT(w);
NeilBrownb5663ba2009-03-31 14:39:38 +11004225 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08004226
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004227 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11004228 previous = 0;
NeilBrownb578d552006-03-27 01:18:12 -08004229 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004230 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11004231 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08004232 * 64bit on a 32bit platform, and so it might be
4233 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02004234 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08004235 * the lock is dropped, so once we get a reference
4236 * to the stripe that we think it is, we will have
4237 * to check again.
4238 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004239 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004240 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004241 ? logical_sector < conf->reshape_progress
4242 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004243 previous = 1;
4244 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10004245 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004246 ? logical_sector < conf->reshape_safe
4247 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08004248 spin_unlock_irq(&conf->device_lock);
4249 schedule();
4250 goto retry;
4251 }
4252 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004253 spin_unlock_irq(&conf->device_lock);
4254 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004255
NeilBrown112bf892009-03-31 14:39:38 +11004256 new_sector = raid5_compute_sector(conf, logical_sector,
4257 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11004258 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10004259 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 (unsigned long long)new_sector,
4261 (unsigned long long)logical_sector);
4262
NeilBrownb5663ba2009-03-31 14:39:38 +11004263 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10004264 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004266 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004267 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08004268 * stripe, so we must do the range check again.
4269 * Expansion could still move past after this
4270 * test, but as we are holding a reference to
4271 * 'sh', we know that if that happens,
4272 * STRIPE_EXPANDING will get set and the expansion
4273 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004274 */
4275 int must_retry = 0;
4276 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004277 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11004278 ? logical_sector >= conf->reshape_progress
4279 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004280 /* mismatch, need to try again */
4281 must_retry = 1;
4282 spin_unlock_irq(&conf->device_lock);
4283 if (must_retry) {
4284 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004285 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004286 goto retry;
4287 }
4288 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004289
Namhyung Kimffd96e32011-07-18 17:38:51 +10004290 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004291 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004292 logical_sector < mddev->suspend_hi) {
4293 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004294 /* As the suspend_* range is controlled by
4295 * userspace, we want an interruptible
4296 * wait.
4297 */
4298 flush_signals(current);
4299 prepare_to_wait(&conf->wait_for_overlap,
4300 &w, TASK_INTERRUPTIBLE);
4301 if (logical_sector >= mddev->suspend_lo &&
4302 logical_sector < mddev->suspend_hi)
4303 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004304 goto retry;
4305 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004306
4307 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004308 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004309 /* Stripe is busy expanding or
4310 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 * and wait a while
4312 */
NeilBrown482c0832011-04-18 18:25:42 +10004313 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 release_stripe(sh);
4315 schedule();
4316 goto retry;
4317 }
4318 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004319 set_bit(STRIPE_HANDLE, &sh->state);
4320 clear_bit(STRIPE_DELAYED, &sh->state);
NeilBrowna852d7b2012-09-19 12:48:30 +10004321 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004322 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4323 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10004324 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 } else {
4326 /* cannot get stripe for read-ahead, just give-up */
4327 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4328 finish_wait(&conf->wait_for_overlap, &w);
4329 break;
4330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004332
Shaohua Lie7836bd62012-07-19 16:01:31 +10004333 remaining = raid5_dec_bi_active_stripes(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004334 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335
NeilBrown16a53ec2006-06-26 00:27:38 -07004336 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004338
Neil Brown0e13fe232008-06-28 08:31:20 +10004339 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341}
4342
NeilBrownfd01b882011-10-11 16:47:53 +11004343static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004344
NeilBrownfd01b882011-10-11 16:47:53 +11004345static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346{
NeilBrown52c03292006-06-26 00:27:43 -07004347 /* reshaping is quite different to recovery/resync so it is
4348 * handled quite separately ... here.
4349 *
4350 * On each call to sync_request, we gather one chunk worth of
4351 * destination stripes and flag them as expanding.
4352 * Then we find all the source stripes and request reads.
4353 * As the reads complete, handle_stripe will copy the data
4354 * into the destination stripe and release that stripe.
4355 */
NeilBrownd1688a62011-10-11 16:49:52 +11004356 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004358 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004359 int raid_disks = conf->previous_raid_disks;
4360 int data_disks = raid_disks - conf->max_degraded;
4361 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004362 int i;
4363 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004364 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004365 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004366 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004367 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004368
NeilBrownfef9c612009-03-31 15:16:46 +11004369 if (sector_nr == 0) {
4370 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10004371 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004372 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4373 sector_nr = raid5_size(mddev, 0, 0)
4374 - conf->reshape_progress;
NeilBrown2c810cd2012-05-21 09:27:00 +10004375 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004376 conf->reshape_progress > 0)
4377 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004378 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004379 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004380 mddev->curr_resync_completed = sector_nr;
4381 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004382 *skipped = 1;
4383 return sector_nr;
4384 }
NeilBrown52c03292006-06-26 00:27:43 -07004385 }
4386
NeilBrown7a661382009-03-31 15:21:40 +11004387 /* We need to process a full chunk at a time.
4388 * If old and new chunk sizes differ, we need to process the
4389 * largest of these
4390 */
Andre Noll664e7c42009-06-18 08:45:27 +10004391 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4392 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004393 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004394 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004395
NeilBrownb5254dd2012-05-21 09:27:01 +10004396 /* We update the metadata at least every 10 seconds, or when
4397 * the data about to be copied would over-write the source of
4398 * the data at the front of the range. i.e. one new_stripe
4399 * along from reshape_progress new_maps to after where
4400 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004401 */
NeilBrownfef9c612009-03-31 15:16:46 +11004402 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004403 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004404 readpos = conf->reshape_progress;
4405 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004406 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004407 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10004408 if (mddev->reshape_backwards) {
NeilBrowned37d832009-05-27 21:39:05 +10004409 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004410 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004411 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004412 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004413 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004414 readpos -= min_t(sector_t, reshape_sectors, readpos);
4415 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004416 }
NeilBrown52c03292006-06-26 00:27:43 -07004417
NeilBrownb5254dd2012-05-21 09:27:01 +10004418 /* Having calculated the 'writepos' possibly use it
4419 * to set 'stripe_addr' which is where we will write to.
4420 */
4421 if (mddev->reshape_backwards) {
4422 BUG_ON(conf->reshape_progress == 0);
4423 stripe_addr = writepos;
4424 BUG_ON((mddev->dev_sectors &
4425 ~((sector_t)reshape_sectors - 1))
4426 - reshape_sectors - stripe_addr
4427 != sector_nr);
4428 } else {
4429 BUG_ON(writepos != sector_nr + reshape_sectors);
4430 stripe_addr = sector_nr;
4431 }
4432
NeilBrownc8f517c2009-03-31 15:28:40 +11004433 /* 'writepos' is the most advanced device address we might write.
4434 * 'readpos' is the least advanced device address we might read.
4435 * 'safepos' is the least address recorded in the metadata as having
4436 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10004437 * If there is a min_offset_diff, these are adjusted either by
4438 * increasing the safepos/readpos if diff is negative, or
4439 * increasing writepos if diff is positive.
4440 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11004441 * ensure safety in the face of a crash - that must be done by userspace
4442 * making a backup of the data. So in that case there is no particular
4443 * rush to update metadata.
4444 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4445 * update the metadata to advance 'safepos' to match 'readpos' so that
4446 * we can be safe in the event of a crash.
4447 * So we insist on updating metadata if safepos is behind writepos and
4448 * readpos is beyond writepos.
4449 * In any case, update the metadata every 10 seconds.
4450 * Maybe that number should be configurable, but I'm not sure it is
4451 * worth it.... maybe it could be a multiple of safemode_delay???
4452 */
NeilBrownb5254dd2012-05-21 09:27:01 +10004453 if (conf->min_offset_diff < 0) {
4454 safepos += -conf->min_offset_diff;
4455 readpos += -conf->min_offset_diff;
4456 } else
4457 writepos += conf->min_offset_diff;
4458
NeilBrown2c810cd2012-05-21 09:27:00 +10004459 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11004460 ? (safepos > writepos && readpos < writepos)
4461 : (safepos < writepos && readpos > writepos)) ||
4462 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004463 /* Cannot proceed until we've updated the superblock... */
4464 wait_event(conf->wait_for_overlap,
4465 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004466 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004467 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004468 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07004469 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004470 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07004471 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004472 kthread_should_stop());
4473 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004474 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004475 spin_unlock_irq(&conf->device_lock);
4476 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004477 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004478 }
4479
NeilBrownab69ae12009-03-31 15:26:47 +11004480 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004481 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004482 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004483 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004484 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004485 set_bit(STRIPE_EXPANDING, &sh->state);
4486 atomic_inc(&conf->reshape_stripes);
4487 /* If any of this stripe is beyond the end of the old
4488 * array, then we need to zero those blocks
4489 */
4490 for (j=sh->disks; j--;) {
4491 sector_t s;
4492 if (j == sh->pd_idx)
4493 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004494 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004495 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004496 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004497 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004498 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004499 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004500 continue;
4501 }
4502 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4503 set_bit(R5_Expanded, &sh->dev[j].flags);
4504 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4505 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004506 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004507 set_bit(STRIPE_EXPAND_READY, &sh->state);
4508 set_bit(STRIPE_HANDLE, &sh->state);
4509 }
NeilBrownab69ae12009-03-31 15:26:47 +11004510 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004511 }
4512 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004513 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11004514 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004515 else
NeilBrown7a661382009-03-31 15:21:40 +11004516 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004517 spin_unlock_irq(&conf->device_lock);
4518 /* Ok, those stripe are ready. We can start scheduling
4519 * reads on the source stripes.
4520 * The source stripes are determined by mapping the first and last
4521 * block on the destination stripes.
4522 */
NeilBrown52c03292006-06-26 00:27:43 -07004523 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004524 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004525 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004526 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004527 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004528 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004529 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004530 if (last_sector >= mddev->dev_sectors)
4531 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004532 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004533 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004534 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4535 set_bit(STRIPE_HANDLE, &sh->state);
4536 release_stripe(sh);
4537 first_sector += STRIPE_SECTORS;
4538 }
NeilBrownab69ae12009-03-31 15:26:47 +11004539 /* Now that the sources are clearly marked, we can release
4540 * the destination stripes
4541 */
4542 while (!list_empty(&stripes)) {
4543 sh = list_entry(stripes.next, struct stripe_head, lru);
4544 list_del_init(&sh->lru);
4545 release_stripe(sh);
4546 }
NeilBrownc6207272008-02-06 01:39:52 -08004547 /* If this takes us to the resync_max point where we have to pause,
4548 * then we need to write out the superblock.
4549 */
NeilBrown7a661382009-03-31 15:21:40 +11004550 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004551 if ((sector_nr - mddev->curr_resync_completed) * 2
4552 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004553 /* Cannot proceed until we've updated the superblock... */
4554 wait_event(conf->wait_for_overlap,
4555 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004556 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004557 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004558 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004559 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4560 md_wakeup_thread(mddev->thread);
4561 wait_event(mddev->sb_wait,
4562 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4563 || kthread_should_stop());
4564 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004565 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004566 spin_unlock_irq(&conf->device_lock);
4567 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004568 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004569 }
NeilBrown7a661382009-03-31 15:21:40 +11004570 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004571}
4572
4573/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004574static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004575{
NeilBrownd1688a62011-10-11 16:49:52 +11004576 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004577 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004578 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004579 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004580 int still_degraded = 0;
4581 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582
NeilBrown72626682005-09-09 16:23:54 -07004583 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004585
NeilBrown29269552006-03-27 01:18:10 -08004586 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4587 end_reshape(conf);
4588 return 0;
4589 }
NeilBrown72626682005-09-09 16:23:54 -07004590
4591 if (mddev->curr_resync < max_sector) /* aborted */
4592 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4593 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004594 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004595 conf->fullsync = 0;
4596 bitmap_close_sync(mddev->bitmap);
4597
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 return 0;
4599 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004600
NeilBrown64bd6602009-08-03 10:59:58 +10004601 /* Allow raid5_quiesce to complete */
4602 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4603
NeilBrown52c03292006-06-26 00:27:43 -07004604 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4605 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004606
NeilBrownc6207272008-02-06 01:39:52 -08004607 /* No need to check resync_max as we never do more than one
4608 * stripe, and as resync_max will always be on a chunk boundary,
4609 * if the check in md_do_sync didn't fire, there is no chance
4610 * of overstepping resync_max here
4611 */
4612
NeilBrown16a53ec2006-06-26 00:27:38 -07004613 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 * to resync, then assert that we are finished, because there is
4615 * nothing we can do.
4616 */
NeilBrown3285edf2006-06-26 00:27:55 -07004617 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004618 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004619 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004620 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 return rv;
4622 }
NeilBrown72626682005-09-09 16:23:54 -07004623 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004624 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004625 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4626 /* we can skip this block, and probably more */
4627 sync_blocks /= STRIPE_SECTORS;
4628 *skipped = 1;
4629 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631
NeilBrownb47490c2008-02-06 01:39:50 -08004632 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4633
NeilBrowna8c906c2009-06-09 14:39:59 +10004634 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004636 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004638 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004640 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004642 /* Need to check if array will still be degraded after recovery/resync
4643 * We don't need to check the 'failed' flag as when that gets set,
4644 * recovery aborts.
4645 */
NeilBrownf001a702009-06-09 14:30:31 +10004646 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004647 if (conf->disks[i].rdev == NULL)
4648 still_degraded = 1;
4649
4650 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4651
NeilBrown83206d62011-07-26 11:19:49 +10004652 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653
NeilBrown14425772009-10-16 15:55:25 +11004654 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 release_stripe(sh);
4656
4657 return STRIPE_SECTORS;
4658}
4659
NeilBrownd1688a62011-10-11 16:49:52 +11004660static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004661{
4662 /* We may not be able to submit a whole bio at once as there
4663 * may not be enough stripe_heads available.
4664 * We cannot pre-allocate enough stripe_heads as we may need
4665 * more than exist in the cache (if we allow ever large chunks).
4666 * So we do one stripe head at a time and record in
4667 * ->bi_hw_segments how many have been done.
4668 *
4669 * We *know* that this entire raid_bio is in one chunk, so
4670 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4671 */
4672 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004673 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004674 sector_t sector, logical_sector, last_sector;
4675 int scnt = 0;
4676 int remaining;
4677 int handled = 0;
4678
4679 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004680 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004681 0, &dd_idx, NULL);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004682 last_sector = bio_end_sector(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004683
4684 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004685 logical_sector += STRIPE_SECTORS,
4686 sector += STRIPE_SECTORS,
4687 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004688
Shaohua Lie7836bd62012-07-19 16:01:31 +10004689 if (scnt < raid5_bi_processed_stripes(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004690 /* already done this stripe */
4691 continue;
4692
NeilBrowna8c906c2009-06-09 14:39:59 +10004693 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004694
4695 if (!sh) {
4696 /* failed to get a stripe - must wait */
Shaohua Lie7836bd62012-07-19 16:01:31 +10004697 raid5_set_bi_processed_stripes(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004698 conf->retry_read_aligned = raid_bio;
4699 return handled;
4700 }
4701
Neil Brown387bb172007-02-08 14:20:29 -08004702 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4703 release_stripe(sh);
Shaohua Lie7836bd62012-07-19 16:01:31 +10004704 raid5_set_bi_processed_stripes(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004705 conf->retry_read_aligned = raid_bio;
4706 return handled;
4707 }
4708
majianpeng3f9e7c12012-07-31 10:04:21 +10004709 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07004710 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004711 release_stripe(sh);
4712 handled++;
4713 }
Shaohua Lie7836bd62012-07-19 16:01:31 +10004714 remaining = raid5_dec_bi_active_stripes(raid_bio);
Tejun Heo3a366e62013-01-11 13:06:33 -08004715 if (remaining == 0)
Neil Brown0e13fe232008-06-28 08:31:20 +10004716 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004717 if (atomic_dec_and_test(&conf->active_aligned_reads))
4718 wake_up(&conf->wait_for_stripe);
4719 return handled;
4720}
4721
Shaohua Li46a06402012-08-02 08:33:15 +10004722#define MAX_STRIPE_BATCH 8
4723static int handle_active_stripes(struct r5conf *conf)
4724{
4725 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
4726 int i, batch_size = 0;
4727
4728 while (batch_size < MAX_STRIPE_BATCH &&
4729 (sh = __get_priority_stripe(conf)) != NULL)
4730 batch[batch_size++] = sh;
4731
4732 if (batch_size == 0)
4733 return batch_size;
4734 spin_unlock_irq(&conf->device_lock);
4735
4736 for (i = 0; i < batch_size; i++)
4737 handle_stripe(batch[i]);
4738
4739 cond_resched();
4740
4741 spin_lock_irq(&conf->device_lock);
4742 for (i = 0; i < batch_size; i++)
4743 __release_stripe(conf, batch[i]);
4744 return batch_size;
4745}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004746
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747/*
4748 * This is our raid5 kernel thread.
4749 *
4750 * We scan the hash table for stripes which can be handled now.
4751 * During the scan, completed stripes are saved for us by the interrupt
4752 * handler, so that they will not have to wait for our next wakeup.
4753 */
Shaohua Li4ed87312012-10-11 13:34:00 +11004754static void raid5d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755{
Shaohua Li4ed87312012-10-11 13:34:00 +11004756 struct mddev *mddev = thread->mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11004757 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004759 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760
Dan Williams45b42332007-07-09 11:56:43 -07004761 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762
4763 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004765 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 handled = 0;
4767 spin_lock_irq(&conf->device_lock);
4768 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004769 struct bio *bio;
Shaohua Li46a06402012-08-02 08:33:15 +10004770 int batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771
NeilBrown0021b7b2012-07-31 09:08:14 +02004772 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10004773 !list_empty(&conf->bitmap_list)) {
4774 /* Now is a good time to flush some bitmap updates */
4775 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004776 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004777 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004778 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004779 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004780 activate_bit_delay(conf);
4781 }
NeilBrown0021b7b2012-07-31 09:08:14 +02004782 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004783
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004784 while ((bio = remove_bio_from_retry(conf))) {
4785 int ok;
4786 spin_unlock_irq(&conf->device_lock);
4787 ok = retry_aligned_read(conf, bio);
4788 spin_lock_irq(&conf->device_lock);
4789 if (!ok)
4790 break;
4791 handled++;
4792 }
4793
Shaohua Li46a06402012-08-02 08:33:15 +10004794 batch_size = handle_active_stripes(conf);
4795 if (!batch_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 break;
Shaohua Li46a06402012-08-02 08:33:15 +10004797 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798
Shaohua Li46a06402012-08-02 08:33:15 +10004799 if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) {
4800 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10004801 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10004802 spin_lock_irq(&conf->device_lock);
4803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 }
Dan Williams45b42332007-07-09 11:56:43 -07004805 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806
4807 spin_unlock_irq(&conf->device_lock);
4808
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004809 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004810 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811
Dan Williams45b42332007-07-09 11:56:43 -07004812 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813}
4814
NeilBrown3f294f42005-11-08 21:39:25 -08004815static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004816raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004817{
NeilBrownd1688a62011-10-11 16:49:52 +11004818 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004819 if (conf)
4820 return sprintf(page, "%d\n", conf->max_nr_stripes);
4821 else
4822 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004823}
4824
NeilBrownc41d4ac2010-06-01 19:37:24 +10004825int
NeilBrownfd01b882011-10-11 16:47:53 +11004826raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004827{
NeilBrownd1688a62011-10-11 16:49:52 +11004828 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004829 int err;
4830
4831 if (size <= 16 || size > 32768)
4832 return -EINVAL;
4833 while (size < conf->max_nr_stripes) {
4834 if (drop_one_stripe(conf))
4835 conf->max_nr_stripes--;
4836 else
4837 break;
4838 }
4839 err = md_allow_write(mddev);
4840 if (err)
4841 return err;
4842 while (size > conf->max_nr_stripes) {
4843 if (grow_one_stripe(conf))
4844 conf->max_nr_stripes++;
4845 else break;
4846 }
4847 return 0;
4848}
4849EXPORT_SYMBOL(raid5_set_cache_size);
4850
NeilBrown3f294f42005-11-08 21:39:25 -08004851static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004852raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004853{
NeilBrownd1688a62011-10-11 16:49:52 +11004854 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004855 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004856 int err;
4857
NeilBrown3f294f42005-11-08 21:39:25 -08004858 if (len >= PAGE_SIZE)
4859 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004860 if (!conf)
4861 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004862
Dan Williams4ef197d82008-04-28 02:15:54 -07004863 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004864 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004865 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004866 if (err)
4867 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004868 return len;
4869}
NeilBrown007583c2005-11-08 21:39:30 -08004870
NeilBrown96de1e62005-11-08 21:39:39 -08004871static struct md_sysfs_entry
4872raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4873 raid5_show_stripe_cache_size,
4874 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004875
4876static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004877raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004878{
NeilBrownd1688a62011-10-11 16:49:52 +11004879 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004880 if (conf)
4881 return sprintf(page, "%d\n", conf->bypass_threshold);
4882 else
4883 return 0;
4884}
4885
4886static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004887raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004888{
NeilBrownd1688a62011-10-11 16:49:52 +11004889 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004890 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004891 if (len >= PAGE_SIZE)
4892 return -EINVAL;
4893 if (!conf)
4894 return -ENODEV;
4895
Dan Williams4ef197d82008-04-28 02:15:54 -07004896 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004897 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004898 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004899 return -EINVAL;
4900 conf->bypass_threshold = new;
4901 return len;
4902}
4903
4904static struct md_sysfs_entry
4905raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4906 S_IRUGO | S_IWUSR,
4907 raid5_show_preread_threshold,
4908 raid5_store_preread_threshold);
4909
4910static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004911stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004912{
NeilBrownd1688a62011-10-11 16:49:52 +11004913 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004914 if (conf)
4915 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4916 else
4917 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004918}
4919
NeilBrown96de1e62005-11-08 21:39:39 -08004920static struct md_sysfs_entry
4921raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004922
NeilBrown007583c2005-11-08 21:39:30 -08004923static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004924 &raid5_stripecache_size.attr,
4925 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004926 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004927 NULL,
4928};
NeilBrown007583c2005-11-08 21:39:30 -08004929static struct attribute_group raid5_attrs_group = {
4930 .name = NULL,
4931 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004932};
4933
Dan Williams80c3a6c2009-03-17 18:10:40 -07004934static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11004935raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07004936{
NeilBrownd1688a62011-10-11 16:49:52 +11004937 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004938
4939 if (!sectors)
4940 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004941 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11004942 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11004943 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004944
Andre Noll9d8f0362009-06-18 08:45:01 +10004945 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004946 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004947 return sectors * (raid_disks - conf->max_degraded);
4948}
4949
NeilBrownd1688a62011-10-11 16:49:52 +11004950static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004951{
4952 struct raid5_percpu *percpu;
4953 unsigned long cpu;
4954
4955 if (!conf->percpu)
4956 return;
4957
4958 get_online_cpus();
4959 for_each_possible_cpu(cpu) {
4960 percpu = per_cpu_ptr(conf->percpu, cpu);
4961 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004962 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004963 }
4964#ifdef CONFIG_HOTPLUG_CPU
4965 unregister_cpu_notifier(&conf->cpu_notify);
4966#endif
4967 put_online_cpus();
4968
4969 free_percpu(conf->percpu);
4970}
4971
NeilBrownd1688a62011-10-11 16:49:52 +11004972static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10004973{
4974 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004975 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10004976 kfree(conf->disks);
4977 kfree(conf->stripe_hashtbl);
4978 kfree(conf);
4979}
4980
Dan Williams36d1c642009-07-14 11:48:22 -07004981#ifdef CONFIG_HOTPLUG_CPU
4982static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4983 void *hcpu)
4984{
NeilBrownd1688a62011-10-11 16:49:52 +11004985 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07004986 long cpu = (long)hcpu;
4987 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4988
4989 switch (action) {
4990 case CPU_UP_PREPARE:
4991 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07004992 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07004993 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004994 if (!percpu->scribble)
4995 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4996
4997 if (!percpu->scribble ||
4998 (conf->level == 6 && !percpu->spare_page)) {
4999 safe_put_page(percpu->spare_page);
5000 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005001 pr_err("%s: failed memory allocation for cpu%ld\n",
5002 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07005003 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07005004 }
5005 break;
5006 case CPU_DEAD:
5007 case CPU_DEAD_FROZEN:
5008 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005009 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005010 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07005011 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07005012 break;
5013 default:
5014 break;
5015 }
5016 return NOTIFY_OK;
5017}
5018#endif
5019
NeilBrownd1688a62011-10-11 16:49:52 +11005020static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07005021{
5022 unsigned long cpu;
5023 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09005024 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07005025 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07005026 int err;
5027
Dan Williams36d1c642009-07-14 11:48:22 -07005028 allcpus = alloc_percpu(struct raid5_percpu);
5029 if (!allcpus)
5030 return -ENOMEM;
5031 conf->percpu = allcpus;
5032
5033 get_online_cpus();
5034 err = 0;
5035 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07005036 if (conf->level == 6) {
5037 spare_page = alloc_page(GFP_KERNEL);
5038 if (!spare_page) {
5039 err = -ENOMEM;
5040 break;
5041 }
5042 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
5043 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11005044 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005045 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07005046 err = -ENOMEM;
5047 break;
5048 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07005049 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07005050 }
5051#ifdef CONFIG_HOTPLUG_CPU
5052 conf->cpu_notify.notifier_call = raid456_cpu_notify;
5053 conf->cpu_notify.priority = 0;
5054 if (err == 0)
5055 err = register_cpu_notifier(&conf->cpu_notify);
5056#endif
5057 put_online_cpus();
5058
5059 return err;
5060}
5061
NeilBrownd1688a62011-10-11 16:49:52 +11005062static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063{
NeilBrownd1688a62011-10-11 16:49:52 +11005064 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005065 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11005066 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10005068 char pers_name[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069
NeilBrown91adb562009-03-31 14:39:39 +11005070 if (mddev->new_level != 5
5071 && mddev->new_level != 4
5072 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10005073 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005074 mdname(mddev), mddev->new_level);
5075 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 }
NeilBrown91adb562009-03-31 14:39:39 +11005077 if ((mddev->new_level == 5
5078 && !algorithm_valid_raid5(mddev->new_layout)) ||
5079 (mddev->new_level == 6
5080 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005081 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11005082 mdname(mddev), mddev->new_layout);
5083 return ERR_PTR(-EIO);
5084 }
5085 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10005086 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005087 mdname(mddev), mddev->raid_disks);
5088 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11005089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090
Andre Noll664e7c42009-06-18 08:45:27 +10005091 if (!mddev->new_chunk_sectors ||
5092 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
5093 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005094 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
5095 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11005096 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11005097 }
5098
NeilBrownd1688a62011-10-11 16:49:52 +11005099 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11005100 if (conf == NULL)
5101 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11005102 spin_lock_init(&conf->device_lock);
5103 init_waitqueue_head(&conf->wait_for_stripe);
5104 init_waitqueue_head(&conf->wait_for_overlap);
5105 INIT_LIST_HEAD(&conf->handle_list);
5106 INIT_LIST_HEAD(&conf->hold_list);
5107 INIT_LIST_HEAD(&conf->delayed_list);
5108 INIT_LIST_HEAD(&conf->bitmap_list);
5109 INIT_LIST_HEAD(&conf->inactive_list);
5110 atomic_set(&conf->active_stripes, 0);
5111 atomic_set(&conf->preread_active_stripes, 0);
5112 atomic_set(&conf->active_aligned_reads, 0);
5113 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11005114 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11005115
5116 conf->raid_disks = mddev->raid_disks;
5117 if (mddev->reshape_position == MaxSector)
5118 conf->previous_raid_disks = mddev->raid_disks;
5119 else
5120 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005121 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
5122 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11005123
NeilBrown5e5e3e72009-10-16 16:35:30 +11005124 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11005125 GFP_KERNEL);
5126 if (!conf->disks)
5127 goto abort;
5128
5129 conf->mddev = mddev;
5130
5131 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
5132 goto abort;
5133
Dan Williams36d1c642009-07-14 11:48:22 -07005134 conf->level = mddev->new_level;
5135 if (raid5_alloc_percpu(conf) != 0)
5136 goto abort;
5137
NeilBrown0c55e022010-05-03 14:09:02 +10005138 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11005139
NeilBrowndafb20f2012-03-19 12:46:39 +11005140 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11005141 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005142 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11005143 || raid_disk < 0)
5144 continue;
5145 disk = conf->disks + raid_disk;
5146
NeilBrown17045f52011-12-23 10:17:53 +11005147 if (test_bit(Replacement, &rdev->flags)) {
5148 if (disk->replacement)
5149 goto abort;
5150 disk->replacement = rdev;
5151 } else {
5152 if (disk->rdev)
5153 goto abort;
5154 disk->rdev = rdev;
5155 }
NeilBrown91adb562009-03-31 14:39:39 +11005156
5157 if (test_bit(In_sync, &rdev->flags)) {
5158 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10005159 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
5160 " disk %d\n",
5161 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05005162 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11005163 /* Cannot rely on bitmap to complete recovery */
5164 conf->fullsync = 1;
5165 }
5166
Andre Noll09c9e5f2009-06-18 08:45:55 +10005167 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11005168 conf->level = mddev->new_level;
5169 if (conf->level == 6)
5170 conf->max_degraded = 2;
5171 else
5172 conf->max_degraded = 1;
5173 conf->algorithm = mddev->new_layout;
5174 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11005175 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11005176 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10005177 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11005178 conf->prev_algo = mddev->layout;
5179 }
NeilBrown91adb562009-03-31 14:39:39 +11005180
5181 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11005182 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11005183 if (grow_stripes(conf, conf->max_nr_stripes)) {
5184 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005185 "md/raid:%s: couldn't allocate %dkB for buffers\n",
5186 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005187 goto abort;
5188 } else
NeilBrown0c55e022010-05-03 14:09:02 +10005189 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
5190 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005191
NeilBrown02326052012-07-03 15:56:52 +10005192 sprintf(pers_name, "raid%d", mddev->new_level);
5193 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11005194 if (!conf->thread) {
5195 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005196 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11005197 mdname(mddev));
5198 goto abort;
5199 }
5200
5201 return conf;
5202
5203 abort:
5204 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10005205 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11005206 return ERR_PTR(-EIO);
5207 } else
5208 return ERR_PTR(-ENOMEM);
5209}
5210
NeilBrownc148ffd2009-11-13 17:47:00 +11005211
5212static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
5213{
5214 switch (algo) {
5215 case ALGORITHM_PARITY_0:
5216 if (raid_disk < max_degraded)
5217 return 1;
5218 break;
5219 case ALGORITHM_PARITY_N:
5220 if (raid_disk >= raid_disks - max_degraded)
5221 return 1;
5222 break;
5223 case ALGORITHM_PARITY_0_6:
5224 if (raid_disk == 0 ||
5225 raid_disk == raid_disks - 1)
5226 return 1;
5227 break;
5228 case ALGORITHM_LEFT_ASYMMETRIC_6:
5229 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5230 case ALGORITHM_LEFT_SYMMETRIC_6:
5231 case ALGORITHM_RIGHT_SYMMETRIC_6:
5232 if (raid_disk == raid_disks - 1)
5233 return 1;
5234 }
5235 return 0;
5236}
5237
NeilBrownfd01b882011-10-11 16:47:53 +11005238static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11005239{
NeilBrownd1688a62011-10-11 16:49:52 +11005240 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10005241 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11005242 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11005243 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11005244 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11005245 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10005246 long long min_offset_diff = 0;
5247 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11005248
Andre Noll8c6ac862009-06-18 08:48:06 +10005249 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10005250 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10005251 " -- starting background reconstruction\n",
5252 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10005253
5254 rdev_for_each(rdev, mddev) {
5255 long long diff;
5256 if (rdev->raid_disk < 0)
5257 continue;
5258 diff = (rdev->new_data_offset - rdev->data_offset);
5259 if (first) {
5260 min_offset_diff = diff;
5261 first = 0;
5262 } else if (mddev->reshape_backwards &&
5263 diff < min_offset_diff)
5264 min_offset_diff = diff;
5265 else if (!mddev->reshape_backwards &&
5266 diff > min_offset_diff)
5267 min_offset_diff = diff;
5268 }
5269
NeilBrownf6705572006-03-27 01:18:11 -08005270 if (mddev->reshape_position != MaxSector) {
5271 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10005272 * Difficulties arise if the stripe we would write to
5273 * next is at or after the stripe we would read from next.
5274 * For a reshape that changes the number of devices, this
5275 * is only possible for a very short time, and mdadm makes
5276 * sure that time appears to have past before assembling
5277 * the array. So we fail if that time hasn't passed.
5278 * For a reshape that keeps the number of devices the same
5279 * mdadm must be monitoring the reshape can keeping the
5280 * critical areas read-only and backed up. It will start
5281 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08005282 */
5283 sector_t here_new, here_old;
5284 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11005285 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08005286
NeilBrown88ce4932009-03-31 15:24:23 +11005287 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10005288 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08005289 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08005290 mdname(mddev));
5291 return -EINVAL;
5292 }
NeilBrownf6705572006-03-27 01:18:11 -08005293 old_disks = mddev->raid_disks - mddev->delta_disks;
5294 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08005295 * further up in new geometry must map after here in old
5296 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08005297 */
5298 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10005299 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005300 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005301 printk(KERN_ERR "md/raid:%s: reshape_position not "
5302 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005303 return -EINVAL;
5304 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005305 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005306 /* here_new is the stripe we will write to */
5307 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10005308 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005309 (old_disks-max_degraded));
5310 /* here_old is the first stripe that we might need to read
5311 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10005312 if (mddev->delta_disks == 0) {
NeilBrownb5254dd2012-05-21 09:27:01 +10005313 if ((here_new * mddev->new_chunk_sectors !=
5314 here_old * mddev->chunk_sectors)) {
5315 printk(KERN_ERR "md/raid:%s: reshape position is"
5316 " confused - aborting\n", mdname(mddev));
5317 return -EINVAL;
5318 }
NeilBrown67ac6012009-08-13 10:06:24 +10005319 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10005320 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10005321 * and taking constant backups.
5322 * mdadm always starts a situation like this in
5323 * readonly mode so it can take control before
5324 * allowing any writes. So just check for that.
5325 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005326 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
5327 abs(min_offset_diff) >= mddev->new_chunk_sectors)
5328 /* not really in-place - so OK */;
5329 else if (mddev->ro == 0) {
5330 printk(KERN_ERR "md/raid:%s: in-place reshape "
5331 "must be started in read-only mode "
5332 "- aborting\n",
NeilBrown0c55e022010-05-03 14:09:02 +10005333 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005334 return -EINVAL;
5335 }
NeilBrown2c810cd2012-05-21 09:27:00 +10005336 } else if (mddev->reshape_backwards
NeilBrownb5254dd2012-05-21 09:27:01 +10005337 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
NeilBrown67ac6012009-08-13 10:06:24 +10005338 here_old * mddev->chunk_sectors)
5339 : (here_new * mddev->new_chunk_sectors >=
NeilBrownb5254dd2012-05-21 09:27:01 +10005340 here_old * mddev->chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08005341 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005342 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5343 "auto-recovery - aborting.\n",
5344 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005345 return -EINVAL;
5346 }
NeilBrown0c55e022010-05-03 14:09:02 +10005347 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5348 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005349 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005350 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005351 BUG_ON(mddev->level != mddev->new_level);
5352 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005353 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005354 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005355 }
5356
NeilBrown245f46c2009-03-31 14:39:39 +11005357 if (mddev->private == NULL)
5358 conf = setup_conf(mddev);
5359 else
5360 conf = mddev->private;
5361
NeilBrown91adb562009-03-31 14:39:39 +11005362 if (IS_ERR(conf))
5363 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005364
NeilBrownb5254dd2012-05-21 09:27:01 +10005365 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11005366 mddev->thread = conf->thread;
5367 conf->thread = NULL;
5368 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369
NeilBrown17045f52011-12-23 10:17:53 +11005370 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5371 i++) {
5372 rdev = conf->disks[i].rdev;
5373 if (!rdev && conf->disks[i].replacement) {
5374 /* The replacement is all we have yet */
5375 rdev = conf->disks[i].replacement;
5376 conf->disks[i].replacement = NULL;
5377 clear_bit(Replacement, &rdev->flags);
5378 conf->disks[i].rdev = rdev;
5379 }
5380 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11005381 continue;
NeilBrown17045f52011-12-23 10:17:53 +11005382 if (conf->disks[i].replacement &&
5383 conf->reshape_progress != MaxSector) {
5384 /* replacements and reshape simply do not mix. */
5385 printk(KERN_ERR "md: cannot handle concurrent "
5386 "replacement and reshape.\n");
5387 goto abort;
5388 }
NeilBrown2f115882010-06-17 17:41:03 +10005389 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005390 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005391 continue;
5392 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005393 /* This disc is not fully in-sync. However if it
5394 * just stored parity (beyond the recovery_offset),
5395 * when we don't need to be concerned about the
5396 * array being dirty.
5397 * When reshape goes 'backwards', we never have
5398 * partially completed devices, so we only need
5399 * to worry about reshape going forwards.
5400 */
5401 /* Hack because v0.91 doesn't store recovery_offset properly. */
5402 if (mddev->major_version == 0 &&
5403 mddev->minor_version > 90)
5404 rdev->recovery_offset = reshape_offset;
5405
NeilBrownc148ffd2009-11-13 17:47:00 +11005406 if (rdev->recovery_offset < reshape_offset) {
5407 /* We need to check old and new layout */
5408 if (!only_parity(rdev->raid_disk,
5409 conf->algorithm,
5410 conf->raid_disks,
5411 conf->max_degraded))
5412 continue;
5413 }
5414 if (!only_parity(rdev->raid_disk,
5415 conf->prev_algo,
5416 conf->previous_raid_disks,
5417 conf->max_degraded))
5418 continue;
5419 dirty_parity_disks++;
5420 }
NeilBrown91adb562009-03-31 14:39:39 +11005421
NeilBrown17045f52011-12-23 10:17:53 +11005422 /*
5423 * 0 for a fully functional array, 1 or 2 for a degraded array.
5424 */
NeilBrown908f4fb2011-12-23 10:17:50 +11005425 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426
NeilBrown674806d2010-06-16 17:17:53 +10005427 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005428 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07005430 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 goto abort;
5432 }
5433
NeilBrown91adb562009-03-31 14:39:39 +11005434 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10005435 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11005436 mddev->resync_max_sectors = mddev->dev_sectors;
5437
NeilBrownc148ffd2009-11-13 17:47:00 +11005438 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005440 if (mddev->ok_start_degraded)
5441 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10005442 "md/raid:%s: starting dirty degraded array"
5443 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005444 mdname(mddev));
5445 else {
5446 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005447 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005448 mdname(mddev));
5449 goto abort;
5450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 }
5452
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10005454 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5455 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11005456 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5457 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 else
NeilBrown0c55e022010-05-03 14:09:02 +10005459 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5460 " out of %d devices, algorithm %d\n",
5461 mdname(mddev), conf->level,
5462 mddev->raid_disks - mddev->degraded,
5463 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464
5465 print_raid5_conf(conf);
5466
NeilBrownfef9c612009-03-31 15:16:46 +11005467 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11005468 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005469 atomic_set(&conf->reshape_stripes, 0);
5470 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5471 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5472 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5473 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5474 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005475 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005476 }
5477
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478
5479 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005480 if (mddev->to_remove == &raid5_attrs_group)
5481 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005482 else if (mddev->kobj.sd &&
5483 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005484 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10005485 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08005486 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10005487 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5488
5489 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005490 int chunk_size;
Shaohua Li620125f2012-10-11 13:49:05 +11005491 bool discard_supported = true;
NeilBrown4a5add42010-06-01 19:37:28 +10005492 /* read-ahead size must cover two whole stripes, which
5493 * is 2 * (datadisks) * chunksize where 'n' is the
5494 * number of raid devices
5495 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5497 int stripe = data_disks *
5498 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5499 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5500 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10005501
5502 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005503
5504 mddev->queue->backing_dev_info.congested_data = mddev;
5505 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10005506
5507 chunk_size = mddev->chunk_sectors << 9;
5508 blk_queue_io_min(mddev->queue, chunk_size);
5509 blk_queue_io_opt(mddev->queue, chunk_size *
5510 (conf->raid_disks - conf->max_degraded));
Shaohua Li620125f2012-10-11 13:49:05 +11005511 /*
5512 * We can only discard a whole stripe. It doesn't make sense to
5513 * discard data disk but write parity disk
5514 */
5515 stripe = stripe * PAGE_SIZE;
NeilBrown4ac68752012-11-19 13:11:26 +11005516 /* Round up to power of 2, as discard handling
5517 * currently assumes that */
5518 while ((stripe-1) & stripe)
5519 stripe = (stripe | (stripe-1)) + 1;
Shaohua Li620125f2012-10-11 13:49:05 +11005520 mddev->queue->limits.discard_alignment = stripe;
5521 mddev->queue->limits.discard_granularity = stripe;
5522 /*
5523 * unaligned part of discard request will be ignored, so can't
5524 * guarantee discard_zerors_data
5525 */
5526 mddev->queue->limits.discard_zeroes_data = 0;
NeilBrown9f7c2222010-07-26 12:04:13 +10005527
NeilBrown05616be2012-05-21 09:27:00 +10005528 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005529 disk_stack_limits(mddev->gendisk, rdev->bdev,
5530 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10005531 disk_stack_limits(mddev->gendisk, rdev->bdev,
5532 rdev->new_data_offset << 9);
Shaohua Li620125f2012-10-11 13:49:05 +11005533 /*
5534 * discard_zeroes_data is required, otherwise data
5535 * could be lost. Consider a scenario: discard a stripe
5536 * (the stripe could be inconsistent if
5537 * discard_zeroes_data is 0); write one disk of the
5538 * stripe (the stripe could be inconsistent again
5539 * depending on which disks are used to calculate
5540 * parity); the disk is broken; The stripe data of this
5541 * disk is lost.
5542 */
5543 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
5544 !bdev_get_queue(rdev->bdev)->
5545 limits.discard_zeroes_data)
5546 discard_supported = false;
NeilBrown05616be2012-05-21 09:27:00 +10005547 }
Shaohua Li620125f2012-10-11 13:49:05 +11005548
5549 if (discard_supported &&
5550 mddev->queue->limits.max_discard_sectors >= stripe &&
5551 mddev->queue->limits.discard_granularity >= stripe)
5552 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
5553 mddev->queue);
5554 else
5555 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
5556 mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557 }
5558
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 return 0;
5560abort:
NeilBrown01f96c02011-09-21 15:30:20 +10005561 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11005562 print_raid5_conf(conf);
5563 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005565 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 return -EIO;
5567}
5568
NeilBrownfd01b882011-10-11 16:47:53 +11005569static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570{
NeilBrownd1688a62011-10-11 16:49:52 +11005571 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572
NeilBrown01f96c02011-09-21 15:30:20 +10005573 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005574 if (mddev->queue)
5575 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10005576 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005577 mddev->private = NULL;
5578 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 return 0;
5580}
5581
NeilBrownfd01b882011-10-11 16:47:53 +11005582static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583{
NeilBrownd1688a62011-10-11 16:49:52 +11005584 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 int i;
5586
Andre Noll9d8f0362009-06-18 08:45:01 +10005587 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5588 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005589 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590 for (i = 0; i < conf->raid_disks; i++)
5591 seq_printf (seq, "%s",
5592 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005593 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595}
5596
NeilBrownd1688a62011-10-11 16:49:52 +11005597static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598{
5599 int i;
5600 struct disk_info *tmp;
5601
NeilBrown0c55e022010-05-03 14:09:02 +10005602 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 if (!conf) {
5604 printk("(conf==NULL)\n");
5605 return;
5606 }
NeilBrown0c55e022010-05-03 14:09:02 +10005607 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5608 conf->raid_disks,
5609 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610
5611 for (i = 0; i < conf->raid_disks; i++) {
5612 char b[BDEVNAME_SIZE];
5613 tmp = conf->disks + i;
5614 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005615 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5616 i, !test_bit(Faulty, &tmp->rdev->flags),
5617 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 }
5619}
5620
NeilBrownfd01b882011-10-11 16:47:53 +11005621static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622{
5623 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005624 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005626 int count = 0;
5627 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628
5629 for (i = 0; i < conf->raid_disks; i++) {
5630 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11005631 if (tmp->replacement
5632 && tmp->replacement->recovery_offset == MaxSector
5633 && !test_bit(Faulty, &tmp->replacement->flags)
5634 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
5635 /* Replacement has just become active. */
5636 if (!tmp->rdev
5637 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
5638 count++;
5639 if (tmp->rdev) {
5640 /* Replaced device not technically faulty,
5641 * but we need to be sure it gets removed
5642 * and never re-added.
5643 */
5644 set_bit(Faulty, &tmp->rdev->flags);
5645 sysfs_notify_dirent_safe(
5646 tmp->rdev->sysfs_state);
5647 }
5648 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
5649 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005650 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005651 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005652 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005653 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005654 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 }
5656 }
NeilBrown6b965622010-08-18 11:56:59 +10005657 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005658 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005659 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005661 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662}
5663
NeilBrownb8321b62011-12-23 10:17:51 +11005664static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665{
NeilBrownd1688a62011-10-11 16:49:52 +11005666 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11005668 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11005669 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 struct disk_info *p = conf->disks + number;
5671
5672 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11005673 if (rdev == p->rdev)
5674 rdevp = &p->rdev;
5675 else if (rdev == p->replacement)
5676 rdevp = &p->replacement;
5677 else
5678 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11005679
NeilBrown657e3e42011-12-23 10:17:52 +11005680 if (number >= conf->raid_disks &&
5681 conf->reshape_progress == MaxSector)
5682 clear_bit(In_sync, &rdev->flags);
5683
5684 if (test_bit(In_sync, &rdev->flags) ||
5685 atomic_read(&rdev->nr_pending)) {
5686 err = -EBUSY;
5687 goto abort;
5688 }
5689 /* Only remove non-faulty devices if recovery
5690 * isn't possible.
5691 */
5692 if (!test_bit(Faulty, &rdev->flags) &&
5693 mddev->recovery_disabled != conf->recovery_disabled &&
5694 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11005695 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11005696 number < conf->raid_disks) {
5697 err = -EBUSY;
5698 goto abort;
5699 }
5700 *rdevp = NULL;
5701 synchronize_rcu();
5702 if (atomic_read(&rdev->nr_pending)) {
5703 /* lost the race, try later */
5704 err = -EBUSY;
5705 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11005706 } else if (p->replacement) {
5707 /* We must have just cleared 'rdev' */
5708 p->rdev = p->replacement;
5709 clear_bit(Replacement, &p->replacement->flags);
5710 smp_mb(); /* Make sure other CPUs may see both as identical
5711 * but will never see neither - if they are careful
5712 */
5713 p->replacement = NULL;
5714 clear_bit(WantReplacement, &rdev->flags);
5715 } else
5716 /* We might have just removed the Replacement as faulty-
5717 * clear the bit just in case
5718 */
5719 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720abort:
5721
5722 print_raid5_conf(conf);
5723 return err;
5724}
5725
NeilBrownfd01b882011-10-11 16:47:53 +11005726static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727{
NeilBrownd1688a62011-10-11 16:49:52 +11005728 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005729 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 int disk;
5731 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005732 int first = 0;
5733 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734
NeilBrown7f0da592011-07-28 11:39:22 +10005735 if (mddev->recovery_disabled == conf->recovery_disabled)
5736 return -EBUSY;
5737
NeilBrowndc10c642012-03-19 12:46:37 +11005738 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005740 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741
Neil Brown6c2fce22008-06-28 08:31:31 +10005742 if (rdev->raid_disk >= 0)
5743 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744
5745 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005746 * find the disk ... but prefer rdev->saved_raid_disk
5747 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005749 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005750 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005751 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10005752 first = rdev->saved_raid_disk;
5753
5754 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11005755 p = conf->disks + disk;
5756 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005757 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005759 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005760 if (rdev->saved_raid_disk != disk)
5761 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005762 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10005763 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005765 }
5766 for (disk = first; disk <= last; disk++) {
5767 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11005768 if (test_bit(WantReplacement, &p->rdev->flags) &&
5769 p->replacement == NULL) {
5770 clear_bit(In_sync, &rdev->flags);
5771 set_bit(Replacement, &rdev->flags);
5772 rdev->raid_disk = disk;
5773 err = 0;
5774 conf->fullsync = 1;
5775 rcu_assign_pointer(p->replacement, rdev);
5776 break;
5777 }
5778 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005779out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005781 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782}
5783
NeilBrownfd01b882011-10-11 16:47:53 +11005784static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785{
5786 /* no resync is happening, and there is enough space
5787 * on all devices, so we can resize.
5788 * We need to make sure resync covers any new space.
5789 * If the array is shrinking we should possibly wait until
5790 * any io in the removed space completes, but it hardly seems
5791 * worth it.
5792 */
NeilBrowna4a61252012-05-22 13:55:27 +10005793 sector_t newsize;
Andre Noll9d8f0362009-06-18 08:45:01 +10005794 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10005795 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
5796 if (mddev->external_size &&
5797 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11005798 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10005799 if (mddev->bitmap) {
5800 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
5801 if (ret)
5802 return ret;
5803 }
5804 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10005805 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005806 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005807 if (sectors > mddev->dev_sectors &&
5808 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005809 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5811 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005812 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005813 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814 return 0;
5815}
5816
NeilBrownfd01b882011-10-11 16:47:53 +11005817static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005818{
5819 /* Can only proceed if there are plenty of stripe_heads.
5820 * We need a minimum of one full stripe,, and for sensible progress
5821 * it is best to have about 4 times that.
5822 * If we require 4 times, then the default 256 4K stripe_heads will
5823 * allow for chunk sizes up to 256K, which is probably OK.
5824 * If the chunk size is greater, user-space should request more
5825 * stripe_heads first.
5826 */
NeilBrownd1688a62011-10-11 16:49:52 +11005827 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005828 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5829 > conf->max_nr_stripes ||
5830 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5831 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005832 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5833 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005834 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5835 / STRIPE_SIZE)*4);
5836 return 0;
5837 }
5838 return 1;
5839}
5840
NeilBrownfd01b882011-10-11 16:47:53 +11005841static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005842{
NeilBrownd1688a62011-10-11 16:49:52 +11005843 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005844
NeilBrown88ce4932009-03-31 15:24:23 +11005845 if (mddev->delta_disks == 0 &&
5846 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005847 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005848 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10005849 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005850 return -EINVAL;
5851 if (mddev->delta_disks < 0) {
5852 /* We might be able to shrink, but the devices must
5853 * be made bigger first.
5854 * For raid6, 4 is the minimum size.
5855 * Otherwise 2 is the minimum
5856 */
5857 int min = 2;
5858 if (mddev->level == 6)
5859 min = 4;
5860 if (mddev->raid_disks + mddev->delta_disks < min)
5861 return -EINVAL;
5862 }
NeilBrown29269552006-03-27 01:18:10 -08005863
NeilBrown01ee22b2009-06-18 08:47:20 +10005864 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005865 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005866
NeilBrowne56108d62012-10-11 14:24:13 +11005867 return resize_stripes(conf, (conf->previous_raid_disks
5868 + mddev->delta_disks));
NeilBrown63c70c42006-03-27 01:18:13 -08005869}
5870
NeilBrownfd01b882011-10-11 16:47:53 +11005871static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005872{
NeilBrownd1688a62011-10-11 16:49:52 +11005873 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005874 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005875 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005876 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005877
NeilBrownf4168852007-02-28 20:11:53 -08005878 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005879 return -EBUSY;
5880
NeilBrown01ee22b2009-06-18 08:47:20 +10005881 if (!check_stripe_cache(mddev))
5882 return -ENOSPC;
5883
NeilBrown30b67642012-05-22 13:55:28 +10005884 if (has_failed(conf))
5885 return -EINVAL;
5886
NeilBrownc6563a82012-05-21 09:27:00 +10005887 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11005888 if (!test_bit(In_sync, &rdev->flags)
5889 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005890 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10005891 }
NeilBrown63c70c42006-03-27 01:18:13 -08005892
NeilBrownf4168852007-02-28 20:11:53 -08005893 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005894 /* Not enough devices even to make a degraded array
5895 * of that size
5896 */
5897 return -EINVAL;
5898
NeilBrownec32a2b2009-03-31 15:17:38 +11005899 /* Refuse to reduce size of the array. Any reductions in
5900 * array size must be through explicit setting of array_size
5901 * attribute.
5902 */
5903 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5904 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10005905 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11005906 "before number of disks\n", mdname(mddev));
5907 return -EINVAL;
5908 }
5909
NeilBrownf6705572006-03-27 01:18:11 -08005910 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005911 spin_lock_irq(&conf->device_lock);
5912 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005913 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005914 conf->prev_chunk_sectors = conf->chunk_sectors;
5915 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005916 conf->prev_algo = conf->algorithm;
5917 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10005918 conf->generation++;
5919 /* Code that selects data_offset needs to see the generation update
5920 * if reshape_progress has been set - so a memory barrier needed.
5921 */
5922 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10005923 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11005924 conf->reshape_progress = raid5_size(mddev, 0, 0);
5925 else
5926 conf->reshape_progress = 0;
5927 conf->reshape_safe = conf->reshape_progress;
NeilBrown29269552006-03-27 01:18:10 -08005928 spin_unlock_irq(&conf->device_lock);
5929
5930 /* Add some new drives, as many as will fit.
5931 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10005932 * Don't add devices if we are reducing the number of
5933 * devices in the array. This is because it is not possible
5934 * to correctly record the "partially reconstructed" state of
5935 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08005936 */
NeilBrown87a8dec2011-01-31 11:57:43 +11005937 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11005938 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11005939 if (rdev->raid_disk < 0 &&
5940 !test_bit(Faulty, &rdev->flags)) {
5941 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11005942 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11005943 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11005944 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11005945 else
NeilBrown87a8dec2011-01-31 11:57:43 +11005946 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10005947
5948 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11005949 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11005950 }
NeilBrown87a8dec2011-01-31 11:57:43 +11005951 } else if (rdev->raid_disk >= conf->previous_raid_disks
5952 && !test_bit(Faulty, &rdev->flags)) {
5953 /* This is a spare that was manually added */
5954 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11005955 }
NeilBrown29269552006-03-27 01:18:10 -08005956
NeilBrown87a8dec2011-01-31 11:57:43 +11005957 /* When a reshape changes the number of devices,
5958 * ->degraded is measured against the larger of the
5959 * pre and post number of devices.
5960 */
NeilBrownec32a2b2009-03-31 15:17:38 +11005961 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005962 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11005963 spin_unlock_irqrestore(&conf->device_lock, flags);
5964 }
NeilBrown63c70c42006-03-27 01:18:13 -08005965 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10005966 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07005967 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005968
NeilBrown29269552006-03-27 01:18:10 -08005969 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5970 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5971 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5972 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5973 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005974 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08005975 if (!mddev->sync_thread) {
5976 mddev->recovery = 0;
5977 spin_lock_irq(&conf->device_lock);
5978 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10005979 rdev_for_each(rdev, mddev)
5980 rdev->new_data_offset = rdev->data_offset;
5981 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11005982 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11005983 mddev->reshape_position = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005984 spin_unlock_irq(&conf->device_lock);
5985 return -EAGAIN;
5986 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005987 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005988 md_wakeup_thread(mddev->sync_thread);
5989 md_new_event(mddev);
5990 return 0;
5991}
NeilBrown29269552006-03-27 01:18:10 -08005992
NeilBrownec32a2b2009-03-31 15:17:38 +11005993/* This is called from the reshape thread and should make any
5994 * changes needed in 'conf'
5995 */
NeilBrownd1688a62011-10-11 16:49:52 +11005996static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08005997{
NeilBrown29269552006-03-27 01:18:10 -08005998
NeilBrownf6705572006-03-27 01:18:11 -08005999 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10006000 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07006001
NeilBrownf6705572006-03-27 01:18:11 -08006002 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11006003 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10006004 rdev_for_each(rdev, conf->mddev)
6005 rdev->data_offset = rdev->new_data_offset;
6006 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11006007 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08006008 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11006009 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07006010
6011 /* read-ahead size must cover two whole stripes, which is
6012 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
6013 */
NeilBrown4a5add42010-06-01 19:37:28 +10006014 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11006015 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006016 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11006017 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07006018 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
6019 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
6020 }
NeilBrown29269552006-03-27 01:18:10 -08006021 }
NeilBrown29269552006-03-27 01:18:10 -08006022}
6023
NeilBrownec32a2b2009-03-31 15:17:38 +11006024/* This is called from the raid5d thread with mddev_lock held.
6025 * It makes config changes to the device.
6026 */
NeilBrownfd01b882011-10-11 16:47:53 +11006027static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11006028{
NeilBrownd1688a62011-10-11 16:49:52 +11006029 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11006030
6031 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6032
NeilBrownec32a2b2009-03-31 15:17:38 +11006033 if (mddev->delta_disks > 0) {
6034 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
6035 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006036 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11006037 } else {
6038 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11006039 spin_lock_irq(&conf->device_lock);
6040 mddev->degraded = calc_degraded(conf);
6041 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11006042 for (d = conf->raid_disks ;
6043 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10006044 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11006045 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10006046 if (rdev)
6047 clear_bit(In_sync, &rdev->flags);
6048 rdev = conf->disks[d].replacement;
6049 if (rdev)
6050 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10006051 }
NeilBrowncea9c222009-03-31 15:15:05 +11006052 }
NeilBrown88ce4932009-03-31 15:24:23 +11006053 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006054 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11006055 mddev->reshape_position = MaxSector;
6056 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006057 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11006058 }
6059}
6060
NeilBrownfd01b882011-10-11 16:47:53 +11006061static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07006062{
NeilBrownd1688a62011-10-11 16:49:52 +11006063 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07006064
6065 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08006066 case 2: /* resume for a suspend */
6067 wake_up(&conf->wait_for_overlap);
6068 break;
6069
NeilBrown72626682005-09-09 16:23:54 -07006070 case 1: /* stop all writes */
6071 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006072 /* '2' tells resync/reshape to pause so that all
6073 * active stripes can drain
6074 */
6075 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07006076 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006077 atomic_read(&conf->active_stripes) == 0 &&
6078 atomic_read(&conf->active_aligned_reads) == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01006079 conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006080 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07006081 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006082 /* allow reshape to continue */
6083 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006084 break;
6085
6086 case 0: /* re-enable writes */
6087 spin_lock_irq(&conf->device_lock);
6088 conf->quiesce = 0;
6089 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08006090 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006091 spin_unlock_irq(&conf->device_lock);
6092 break;
6093 }
NeilBrown72626682005-09-09 16:23:54 -07006094}
NeilBrownb15c2e52006-01-06 00:20:16 -08006095
NeilBrownd562b0c2009-03-31 14:39:39 +11006096
NeilBrownfd01b882011-10-11 16:47:53 +11006097static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11006098{
NeilBrowne373ab12011-10-11 16:48:59 +11006099 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07006100 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11006101
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006102 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11006103 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10006104 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
6105 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006106 return ERR_PTR(-EINVAL);
6107 }
6108
NeilBrowne373ab12011-10-11 16:48:59 +11006109 sectors = raid0_conf->strip_zone[0].zone_end;
6110 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10006111 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006112 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11006113 mddev->new_layout = ALGORITHM_PARITY_N;
6114 mddev->new_chunk_sectors = mddev->chunk_sectors;
6115 mddev->raid_disks += 1;
6116 mddev->delta_disks = 1;
6117 /* make sure it will be not marked as dirty */
6118 mddev->recovery_cp = MaxSector;
6119
6120 return setup_conf(mddev);
6121}
6122
6123
NeilBrownfd01b882011-10-11 16:47:53 +11006124static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006125{
6126 int chunksect;
6127
6128 if (mddev->raid_disks != 2 ||
6129 mddev->degraded > 1)
6130 return ERR_PTR(-EINVAL);
6131
6132 /* Should check if there are write-behind devices? */
6133
6134 chunksect = 64*2; /* 64K by default */
6135
6136 /* The array must be an exact multiple of chunksize */
6137 while (chunksect && (mddev->array_sectors & (chunksect-1)))
6138 chunksect >>= 1;
6139
6140 if ((chunksect<<9) < STRIPE_SIZE)
6141 /* array size does not allow a suitable chunk size */
6142 return ERR_PTR(-EINVAL);
6143
6144 mddev->new_level = 5;
6145 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10006146 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11006147
6148 return setup_conf(mddev);
6149}
6150
NeilBrownfd01b882011-10-11 16:47:53 +11006151static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11006152{
6153 int new_layout;
6154
6155 switch (mddev->layout) {
6156 case ALGORITHM_LEFT_ASYMMETRIC_6:
6157 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
6158 break;
6159 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6160 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
6161 break;
6162 case ALGORITHM_LEFT_SYMMETRIC_6:
6163 new_layout = ALGORITHM_LEFT_SYMMETRIC;
6164 break;
6165 case ALGORITHM_RIGHT_SYMMETRIC_6:
6166 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
6167 break;
6168 case ALGORITHM_PARITY_0_6:
6169 new_layout = ALGORITHM_PARITY_0;
6170 break;
6171 case ALGORITHM_PARITY_N:
6172 new_layout = ALGORITHM_PARITY_N;
6173 break;
6174 default:
6175 return ERR_PTR(-EINVAL);
6176 }
6177 mddev->new_level = 5;
6178 mddev->new_layout = new_layout;
6179 mddev->delta_disks = -1;
6180 mddev->raid_disks -= 1;
6181 return setup_conf(mddev);
6182}
6183
NeilBrownd562b0c2009-03-31 14:39:39 +11006184
NeilBrownfd01b882011-10-11 16:47:53 +11006185static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11006186{
NeilBrown88ce4932009-03-31 15:24:23 +11006187 /* For a 2-drive array, the layout and chunk size can be changed
6188 * immediately as not restriping is needed.
6189 * For larger arrays we record the new value - after validation
6190 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11006191 */
NeilBrownd1688a62011-10-11 16:49:52 +11006192 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10006193 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11006194
NeilBrown597a7112009-06-18 08:47:42 +10006195 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11006196 return -EINVAL;
6197 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006198 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11006199 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006200 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11006201 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006202 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11006203 /* not factor of array size */
6204 return -EINVAL;
6205 }
6206
6207 /* They look valid */
6208
NeilBrown88ce4932009-03-31 15:24:23 +11006209 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10006210 /* can make the change immediately */
6211 if (mddev->new_layout >= 0) {
6212 conf->algorithm = mddev->new_layout;
6213 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11006214 }
6215 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10006216 conf->chunk_sectors = new_chunk ;
6217 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11006218 }
6219 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6220 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11006221 }
NeilBrown50ac1682009-06-18 08:47:55 +10006222 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11006223}
6224
NeilBrownfd01b882011-10-11 16:47:53 +11006225static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11006226{
NeilBrown597a7112009-06-18 08:47:42 +10006227 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10006228
NeilBrown597a7112009-06-18 08:47:42 +10006229 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11006230 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006231 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006232 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11006233 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006234 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11006235 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006236 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11006237 /* not factor of array size */
6238 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006239 }
NeilBrown88ce4932009-03-31 15:24:23 +11006240
6241 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10006242 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11006243}
6244
NeilBrownfd01b882011-10-11 16:47:53 +11006245static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006246{
6247 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006248 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006249 * raid1 - if there are two drives. We need to know the chunk size
6250 * raid4 - trivial - just use a raid4 layout.
6251 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006252 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006253 if (mddev->level == 0)
6254 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11006255 if (mddev->level == 1)
6256 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11006257 if (mddev->level == 4) {
6258 mddev->new_layout = ALGORITHM_PARITY_N;
6259 mddev->new_level = 5;
6260 return setup_conf(mddev);
6261 }
NeilBrownfc9739c2009-03-31 14:57:20 +11006262 if (mddev->level == 6)
6263 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11006264
6265 return ERR_PTR(-EINVAL);
6266}
6267
NeilBrownfd01b882011-10-11 16:47:53 +11006268static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11006269{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006270 /* raid4 can take over:
6271 * raid0 - if there is only one strip zone
6272 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11006273 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006274 if (mddev->level == 0)
6275 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11006276 if (mddev->level == 5 &&
6277 mddev->layout == ALGORITHM_PARITY_N) {
6278 mddev->new_layout = 0;
6279 mddev->new_level = 4;
6280 return setup_conf(mddev);
6281 }
6282 return ERR_PTR(-EINVAL);
6283}
NeilBrownd562b0c2009-03-31 14:39:39 +11006284
NeilBrown84fc4b52011-10-11 16:49:58 +11006285static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11006286
NeilBrownfd01b882011-10-11 16:47:53 +11006287static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11006288{
6289 /* Currently can only take over a raid5. We map the
6290 * personality to an equivalent raid6 personality
6291 * with the Q block at the end.
6292 */
6293 int new_layout;
6294
6295 if (mddev->pers != &raid5_personality)
6296 return ERR_PTR(-EINVAL);
6297 if (mddev->degraded > 1)
6298 return ERR_PTR(-EINVAL);
6299 if (mddev->raid_disks > 253)
6300 return ERR_PTR(-EINVAL);
6301 if (mddev->raid_disks < 3)
6302 return ERR_PTR(-EINVAL);
6303
6304 switch (mddev->layout) {
6305 case ALGORITHM_LEFT_ASYMMETRIC:
6306 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
6307 break;
6308 case ALGORITHM_RIGHT_ASYMMETRIC:
6309 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
6310 break;
6311 case ALGORITHM_LEFT_SYMMETRIC:
6312 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
6313 break;
6314 case ALGORITHM_RIGHT_SYMMETRIC:
6315 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
6316 break;
6317 case ALGORITHM_PARITY_0:
6318 new_layout = ALGORITHM_PARITY_0_6;
6319 break;
6320 case ALGORITHM_PARITY_N:
6321 new_layout = ALGORITHM_PARITY_N;
6322 break;
6323 default:
6324 return ERR_PTR(-EINVAL);
6325 }
6326 mddev->new_level = 6;
6327 mddev->new_layout = new_layout;
6328 mddev->delta_disks = 1;
6329 mddev->raid_disks += 1;
6330 return setup_conf(mddev);
6331}
6332
6333
NeilBrown84fc4b52011-10-11 16:49:58 +11006334static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07006335{
6336 .name = "raid6",
6337 .level = 6,
6338 .owner = THIS_MODULE,
6339 .make_request = make_request,
6340 .run = run,
6341 .stop = stop,
6342 .status = status,
6343 .error_handler = error,
6344 .hot_add_disk = raid5_add_disk,
6345 .hot_remove_disk= raid5_remove_disk,
6346 .spare_active = raid5_spare_active,
6347 .sync_request = sync_request,
6348 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006349 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10006350 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08006351 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006352 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07006353 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11006354 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07006355};
NeilBrown84fc4b52011-10-11 16:49:58 +11006356static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357{
6358 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08006359 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360 .owner = THIS_MODULE,
6361 .make_request = make_request,
6362 .run = run,
6363 .stop = stop,
6364 .status = status,
6365 .error_handler = error,
6366 .hot_add_disk = raid5_add_disk,
6367 .hot_remove_disk= raid5_remove_disk,
6368 .spare_active = raid5_spare_active,
6369 .sync_request = sync_request,
6370 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006371 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08006372 .check_reshape = raid5_check_reshape,
6373 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006374 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07006375 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11006376 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377};
6378
NeilBrown84fc4b52011-10-11 16:49:58 +11006379static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380{
NeilBrown2604b702006-01-06 00:20:36 -08006381 .name = "raid4",
6382 .level = 4,
6383 .owner = THIS_MODULE,
6384 .make_request = make_request,
6385 .run = run,
6386 .stop = stop,
6387 .status = status,
6388 .error_handler = error,
6389 .hot_add_disk = raid5_add_disk,
6390 .hot_remove_disk= raid5_remove_disk,
6391 .spare_active = raid5_spare_active,
6392 .sync_request = sync_request,
6393 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006394 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08006395 .check_reshape = raid5_check_reshape,
6396 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006397 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08006398 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11006399 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08006400};
6401
6402static int __init raid5_init(void)
6403{
NeilBrown16a53ec2006-06-26 00:27:38 -07006404 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006405 register_md_personality(&raid5_personality);
6406 register_md_personality(&raid4_personality);
6407 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408}
6409
NeilBrown2604b702006-01-06 00:20:36 -08006410static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006411{
NeilBrown16a53ec2006-06-26 00:27:38 -07006412 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006413 unregister_md_personality(&raid5_personality);
6414 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006415}
6416
6417module_init(raid5_init);
6418module_exit(raid5_exit);
6419MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11006420MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08006422MODULE_ALIAS("md-raid5");
6423MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08006424MODULE_ALIAS("md-level-5");
6425MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07006426MODULE_ALIAS("md-personality-8"); /* RAID6 */
6427MODULE_ALIAS("md-raid6");
6428MODULE_ALIAS("md-level-6");
6429
6430/* This used to be two separate modules, they were: */
6431MODULE_ALIAS("raid5");
6432MODULE_ALIAS("raid6");