blob: 7bbd28546214c4a9be5a4e08bc98df4ee4d5616d [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{
Kent Overstreetaa8b57a2013-02-05 15:19:29 -080093 int sectors = bio_sectors(bio);
NeilBrowndb298e12011-10-07 14:23:00 +110094 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 Li9e444762012-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
Dan Williams91c00922007-01-02 13:52:30 -0700570 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100571 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100572 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
573 rdev = rcu_dereference(conf->disks[i].rdev);
574 if (!rdev) {
575 rdev = rrdev;
576 rrdev = NULL;
577 }
NeilBrown9a3e1102011-12-23 10:17:53 +1100578 if (rw & WRITE) {
579 if (replace_only)
580 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100581 if (rdev == rrdev)
582 /* We raced and saw duplicates */
583 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100584 } else {
NeilBrowndd054fc2011-12-23 10:17:53 +1100585 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100586 rdev = rrdev;
587 rrdev = NULL;
588 }
NeilBrown977df362011-12-23 10:17:53 +1100589
Dan Williams91c00922007-01-02 13:52:30 -0700590 if (rdev && test_bit(Faulty, &rdev->flags))
591 rdev = NULL;
592 if (rdev)
593 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100594 if (rrdev && test_bit(Faulty, &rrdev->flags))
595 rrdev = NULL;
596 if (rrdev)
597 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700598 rcu_read_unlock();
599
NeilBrown73e92e52011-07-28 11:39:22 +1000600 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100601 * need to check for writes. We never accept write errors
602 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000603 */
604 while ((rw & WRITE) && rdev &&
605 test_bit(WriteErrorSeen, &rdev->flags)) {
606 sector_t first_bad;
607 int bad_sectors;
608 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
609 &first_bad, &bad_sectors);
610 if (!bad)
611 break;
612
613 if (bad < 0) {
614 set_bit(BlockedBadBlocks, &rdev->flags);
615 if (!conf->mddev->external &&
616 conf->mddev->flags) {
617 /* It is very unlikely, but we might
618 * still need to write out the
619 * bad block log - better give it
620 * a chance*/
621 md_check_recovery(conf->mddev);
622 }
majianpeng18507532012-07-03 12:11:54 +1000623 /*
624 * Because md_wait_for_blocked_rdev
625 * will dec nr_pending, we must
626 * increment it first.
627 */
628 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +1000629 md_wait_for_blocked_rdev(rdev, conf->mddev);
630 } else {
631 /* Acknowledged bad block - skip the write */
632 rdev_dec_pending(rdev, conf->mddev);
633 rdev = NULL;
634 }
635 }
636
Dan Williams91c00922007-01-02 13:52:30 -0700637 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100638 if (s->syncing || s->expanding || s->expanded
639 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700640 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
641
Dan Williams2b7497f2008-06-28 08:31:52 +1000642 set_bit(STRIPE_IO_STARTED, &sh->state);
643
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700644 bio_reset(bi);
Dan Williams91c00922007-01-02 13:52:30 -0700645 bi->bi_bdev = rdev->bdev;
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700646 bi->bi_rw = rw;
647 bi->bi_end_io = (rw & WRITE)
648 ? raid5_end_write_request
649 : raid5_end_read_request;
650 bi->bi_private = sh;
651
Dan Williams91c00922007-01-02 13:52:30 -0700652 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700653 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700654 bi->bi_rw, i);
655 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000656 if (use_new_offset(conf, sh))
657 bi->bi_sector = (sh->sector
658 + rdev->new_data_offset);
659 else
660 bi->bi_sector = (sh->sector
661 + rdev->data_offset);
majianpeng3f9e7c12012-07-31 10:04:21 +1000662 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
663 bi->bi_rw |= REQ_FLUSH;
664
Dan Williams91c00922007-01-02 13:52:30 -0700665 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
666 bi->bi_io_vec[0].bv_offset = 0;
667 bi->bi_size = STRIPE_SIZE;
NeilBrown977df362011-12-23 10:17:53 +1100668 if (rrdev)
669 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
NeilBrowna9add5d2012-10-31 11:59:09 +1100670 trace_block_bio_remap(bdev_get_queue(bi->bi_bdev),
671 bi, disk_devt(conf->mddev->gendisk),
672 sh->dev[i].sector);
Dan Williams91c00922007-01-02 13:52:30 -0700673 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100674 }
675 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100676 if (s->syncing || s->expanding || s->expanded
677 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +1100678 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
679
680 set_bit(STRIPE_IO_STARTED, &sh->state);
681
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700682 bio_reset(rbi);
NeilBrown977df362011-12-23 10:17:53 +1100683 rbi->bi_bdev = rrdev->bdev;
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700684 rbi->bi_rw = rw;
685 BUG_ON(!(rw & WRITE));
686 rbi->bi_end_io = raid5_end_write_request;
687 rbi->bi_private = sh;
688
NeilBrown977df362011-12-23 10:17:53 +1100689 pr_debug("%s: for %llu schedule op %ld on "
690 "replacement disc %d\n",
691 __func__, (unsigned long long)sh->sector,
692 rbi->bi_rw, i);
693 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000694 if (use_new_offset(conf, sh))
695 rbi->bi_sector = (sh->sector
696 + rrdev->new_data_offset);
697 else
698 rbi->bi_sector = (sh->sector
699 + rrdev->data_offset);
NeilBrown977df362011-12-23 10:17:53 +1100700 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
701 rbi->bi_io_vec[0].bv_offset = 0;
702 rbi->bi_size = STRIPE_SIZE;
NeilBrowna9add5d2012-10-31 11:59:09 +1100703 trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev),
704 rbi, disk_devt(conf->mddev->gendisk),
705 sh->dev[i].sector);
NeilBrown977df362011-12-23 10:17:53 +1100706 generic_make_request(rbi);
707 }
708 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000709 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700710 set_bit(STRIPE_DEGRADED, &sh->state);
711 pr_debug("skip op %ld on disc %d for sector %llu\n",
712 bi->bi_rw, i, (unsigned long long)sh->sector);
713 clear_bit(R5_LOCKED, &sh->dev[i].flags);
714 set_bit(STRIPE_HANDLE, &sh->state);
715 }
716 }
717}
718
719static struct dma_async_tx_descriptor *
720async_copy_data(int frombio, struct bio *bio, struct page *page,
721 sector_t sector, struct dma_async_tx_descriptor *tx)
722{
723 struct bio_vec *bvl;
724 struct page *bio_page;
725 int i;
726 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700727 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700728 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700729
730 if (bio->bi_sector >= sector)
731 page_offset = (signed)(bio->bi_sector - sector) * 512;
732 else
733 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700734
Dan Williams0403e382009-09-08 17:42:50 -0700735 if (frombio)
736 flags |= ASYNC_TX_FENCE;
737 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
738
Dan Williams91c00922007-01-02 13:52:30 -0700739 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000740 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700741 int clen;
742 int b_offset = 0;
743
744 if (page_offset < 0) {
745 b_offset = -page_offset;
746 page_offset += b_offset;
747 len -= b_offset;
748 }
749
750 if (len > 0 && page_offset + len > STRIPE_SIZE)
751 clen = STRIPE_SIZE - page_offset;
752 else
753 clen = len;
754
755 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000756 b_offset += bvl->bv_offset;
757 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700758 if (frombio)
759 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700760 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700761 else
762 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700763 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700764 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700765 /* chain the operations */
766 submit.depend_tx = tx;
767
Dan Williams91c00922007-01-02 13:52:30 -0700768 if (clen < len) /* hit end of page */
769 break;
770 page_offset += len;
771 }
772
773 return tx;
774}
775
776static void ops_complete_biofill(void *stripe_head_ref)
777{
778 struct stripe_head *sh = stripe_head_ref;
779 struct bio *return_bi = NULL;
Dan Williamse4d84902007-09-24 10:06:13 -0700780 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700781
Harvey Harrisone46b2722008-04-28 02:15:50 -0700782 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700783 (unsigned long long)sh->sector);
784
785 /* clear completed biofills */
786 for (i = sh->disks; i--; ) {
787 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700788
789 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700790 /* and check if we need to reply to a read request,
791 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000792 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700793 */
794 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700795 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700796
Dan Williams91c00922007-01-02 13:52:30 -0700797 BUG_ON(!dev->read);
798 rbi = dev->read;
799 dev->read = NULL;
800 while (rbi && rbi->bi_sector <
801 dev->sector + STRIPE_SECTORS) {
802 rbi2 = r5_next_bio(rbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +1000803 if (!raid5_dec_bi_active_stripes(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700804 rbi->bi_next = return_bi;
805 return_bi = rbi;
806 }
Dan Williams91c00922007-01-02 13:52:30 -0700807 rbi = rbi2;
808 }
809 }
810 }
Dan Williams83de75c2008-06-28 08:31:58 +1000811 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700812
813 return_io(return_bi);
814
Dan Williamse4d84902007-09-24 10:06:13 -0700815 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700816 release_stripe(sh);
817}
818
819static void ops_run_biofill(struct stripe_head *sh)
820{
821 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa08abd82009-06-03 11:43:59 -0700822 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700823 int i;
824
Harvey Harrisone46b2722008-04-28 02:15:50 -0700825 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700826 (unsigned long long)sh->sector);
827
828 for (i = sh->disks; i--; ) {
829 struct r5dev *dev = &sh->dev[i];
830 if (test_bit(R5_Wantfill, &dev->flags)) {
831 struct bio *rbi;
Shaohua Lib17459c2012-07-19 16:01:31 +1000832 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700833 dev->read = rbi = dev->toread;
834 dev->toread = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +1000835 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700836 while (rbi && rbi->bi_sector <
837 dev->sector + STRIPE_SECTORS) {
838 tx = async_copy_data(0, rbi, dev->page,
839 dev->sector, tx);
840 rbi = r5_next_bio(rbi, dev->sector);
841 }
842 }
843 }
844
845 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700846 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
847 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700848}
849
Dan Williams4e7d2c02009-08-29 19:13:11 -0700850static void mark_target_uptodate(struct stripe_head *sh, int target)
851{
852 struct r5dev *tgt;
853
854 if (target < 0)
855 return;
856
857 tgt = &sh->dev[target];
858 set_bit(R5_UPTODATE, &tgt->flags);
859 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
860 clear_bit(R5_Wantcompute, &tgt->flags);
861}
862
Dan Williamsac6b53b2009-07-14 13:40:19 -0700863static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700864{
865 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700866
Harvey Harrisone46b2722008-04-28 02:15:50 -0700867 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700868 (unsigned long long)sh->sector);
869
Dan Williamsac6b53b2009-07-14 13:40:19 -0700870 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700871 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700872 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700873
Dan Williamsecc65c92008-06-28 08:31:57 +1000874 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
875 if (sh->check_state == check_state_compute_run)
876 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700877 set_bit(STRIPE_HANDLE, &sh->state);
878 release_stripe(sh);
879}
880
Dan Williamsd6f38f32009-07-14 11:50:52 -0700881/* return a pointer to the address conversion region of the scribble buffer */
882static addr_conv_t *to_addr_conv(struct stripe_head *sh,
883 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700884{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700885 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
886}
887
888static struct dma_async_tx_descriptor *
889ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
890{
Dan Williams91c00922007-01-02 13:52:30 -0700891 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700892 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700893 int target = sh->ops.target;
894 struct r5dev *tgt = &sh->dev[target];
895 struct page *xor_dest = tgt->page;
896 int count = 0;
897 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700898 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700899 int i;
900
901 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700902 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700903 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
904
905 for (i = disks; i--; )
906 if (i != target)
907 xor_srcs[count++] = sh->dev[i].page;
908
909 atomic_inc(&sh->count);
910
Dan Williams0403e382009-09-08 17:42:50 -0700911 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700912 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700913 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700914 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700915 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700916 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700917
Dan Williams91c00922007-01-02 13:52:30 -0700918 return tx;
919}
920
Dan Williamsac6b53b2009-07-14 13:40:19 -0700921/* set_syndrome_sources - populate source buffers for gen_syndrome
922 * @srcs - (struct page *) array of size sh->disks
923 * @sh - stripe_head to parse
924 *
925 * Populates srcs in proper layout order for the stripe and returns the
926 * 'count' of sources to be used in a call to async_gen_syndrome. The P
927 * destination buffer is recorded in srcs[count] and the Q destination
928 * is recorded in srcs[count+1]].
929 */
930static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
931{
932 int disks = sh->disks;
933 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
934 int d0_idx = raid6_d0(sh);
935 int count;
936 int i;
937
938 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100939 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700940
941 count = 0;
942 i = d0_idx;
943 do {
944 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
945
946 srcs[slot] = sh->dev[i].page;
947 i = raid6_next_disk(i, disks);
948 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700949
NeilBrowne4424fe2009-10-16 16:27:34 +1100950 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700951}
952
953static struct dma_async_tx_descriptor *
954ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
955{
956 int disks = sh->disks;
957 struct page **blocks = percpu->scribble;
958 int target;
959 int qd_idx = sh->qd_idx;
960 struct dma_async_tx_descriptor *tx;
961 struct async_submit_ctl submit;
962 struct r5dev *tgt;
963 struct page *dest;
964 int i;
965 int count;
966
967 if (sh->ops.target < 0)
968 target = sh->ops.target2;
969 else if (sh->ops.target2 < 0)
970 target = sh->ops.target;
971 else
972 /* we should only have one valid target */
973 BUG();
974 BUG_ON(target < 0);
975 pr_debug("%s: stripe %llu block: %d\n",
976 __func__, (unsigned long long)sh->sector, target);
977
978 tgt = &sh->dev[target];
979 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
980 dest = tgt->page;
981
982 atomic_inc(&sh->count);
983
984 if (target == qd_idx) {
985 count = set_syndrome_sources(blocks, sh);
986 blocks[count] = NULL; /* regenerating p is not necessary */
987 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700988 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
989 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700990 to_addr_conv(sh, percpu));
991 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
992 } else {
993 /* Compute any data- or p-drive using XOR */
994 count = 0;
995 for (i = disks; i-- ; ) {
996 if (i == target || i == qd_idx)
997 continue;
998 blocks[count++] = sh->dev[i].page;
999 }
1000
Dan Williams0403e382009-09-08 17:42:50 -07001001 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1002 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001003 to_addr_conv(sh, percpu));
1004 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
1005 }
1006
1007 return tx;
1008}
1009
1010static struct dma_async_tx_descriptor *
1011ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1012{
1013 int i, count, disks = sh->disks;
1014 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1015 int d0_idx = raid6_d0(sh);
1016 int faila = -1, failb = -1;
1017 int target = sh->ops.target;
1018 int target2 = sh->ops.target2;
1019 struct r5dev *tgt = &sh->dev[target];
1020 struct r5dev *tgt2 = &sh->dev[target2];
1021 struct dma_async_tx_descriptor *tx;
1022 struct page **blocks = percpu->scribble;
1023 struct async_submit_ctl submit;
1024
1025 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1026 __func__, (unsigned long long)sh->sector, target, target2);
1027 BUG_ON(target < 0 || target2 < 0);
1028 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1029 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1030
Dan Williams6c910a72009-09-16 12:24:54 -07001031 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001032 * slot number conversion for 'faila' and 'failb'
1033 */
1034 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001035 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001036 count = 0;
1037 i = d0_idx;
1038 do {
1039 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1040
1041 blocks[slot] = sh->dev[i].page;
1042
1043 if (i == target)
1044 faila = slot;
1045 if (i == target2)
1046 failb = slot;
1047 i = raid6_next_disk(i, disks);
1048 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001049
1050 BUG_ON(faila == failb);
1051 if (failb < faila)
1052 swap(faila, failb);
1053 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1054 __func__, (unsigned long long)sh->sector, faila, failb);
1055
1056 atomic_inc(&sh->count);
1057
1058 if (failb == syndrome_disks+1) {
1059 /* Q disk is one of the missing disks */
1060 if (faila == syndrome_disks) {
1061 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001062 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1063 ops_complete_compute, sh,
1064 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +11001065 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001066 STRIPE_SIZE, &submit);
1067 } else {
1068 struct page *dest;
1069 int data_target;
1070 int qd_idx = sh->qd_idx;
1071
1072 /* Missing D+Q: recompute D from P, then recompute Q */
1073 if (target == qd_idx)
1074 data_target = target2;
1075 else
1076 data_target = target;
1077
1078 count = 0;
1079 for (i = disks; i-- ; ) {
1080 if (i == data_target || i == qd_idx)
1081 continue;
1082 blocks[count++] = sh->dev[i].page;
1083 }
1084 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001085 init_async_submit(&submit,
1086 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1087 NULL, NULL, NULL,
1088 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001089 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1090 &submit);
1091
1092 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001093 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1094 ops_complete_compute, sh,
1095 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001096 return async_gen_syndrome(blocks, 0, count+2,
1097 STRIPE_SIZE, &submit);
1098 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001099 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001100 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1101 ops_complete_compute, sh,
1102 to_addr_conv(sh, percpu));
1103 if (failb == syndrome_disks) {
1104 /* We're missing D+P. */
1105 return async_raid6_datap_recov(syndrome_disks+2,
1106 STRIPE_SIZE, faila,
1107 blocks, &submit);
1108 } else {
1109 /* We're missing D+D. */
1110 return async_raid6_2data_recov(syndrome_disks+2,
1111 STRIPE_SIZE, faila, failb,
1112 blocks, &submit);
1113 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001114 }
1115}
1116
1117
Dan Williams91c00922007-01-02 13:52:30 -07001118static void ops_complete_prexor(void *stripe_head_ref)
1119{
1120 struct stripe_head *sh = stripe_head_ref;
1121
Harvey Harrisone46b2722008-04-28 02:15:50 -07001122 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001123 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001124}
1125
1126static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001127ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1128 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001129{
Dan Williams91c00922007-01-02 13:52:30 -07001130 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001131 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001132 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001133 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001134
1135 /* existing parity data subtracted */
1136 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1137
Harvey Harrisone46b2722008-04-28 02:15:50 -07001138 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001139 (unsigned long long)sh->sector);
1140
1141 for (i = disks; i--; ) {
1142 struct r5dev *dev = &sh->dev[i];
1143 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001144 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001145 xor_srcs[count++] = dev->page;
1146 }
1147
Dan Williams0403e382009-09-08 17:42:50 -07001148 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001149 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001150 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001151
1152 return tx;
1153}
1154
1155static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001156ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001157{
1158 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001159 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001160
Harvey Harrisone46b2722008-04-28 02:15:50 -07001161 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001162 (unsigned long long)sh->sector);
1163
1164 for (i = disks; i--; ) {
1165 struct r5dev *dev = &sh->dev[i];
1166 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001167
Dan Williamsd8ee0722008-06-28 08:32:06 +10001168 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001169 struct bio *wbi;
1170
Shaohua Lib17459c2012-07-19 16:01:31 +10001171 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001172 chosen = dev->towrite;
1173 dev->towrite = NULL;
1174 BUG_ON(dev->written);
1175 wbi = dev->written = chosen;
Shaohua Lib17459c2012-07-19 16:01:31 +10001176 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001177
1178 while (wbi && wbi->bi_sector <
1179 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001180 if (wbi->bi_rw & REQ_FUA)
1181 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001182 if (wbi->bi_rw & REQ_SYNC)
1183 set_bit(R5_SyncIO, &dev->flags);
Shaohua Li9e444762012-10-11 13:49:49 +11001184 if (wbi->bi_rw & REQ_DISCARD)
Shaohua Li620125f2012-10-11 13:49:05 +11001185 set_bit(R5_Discard, &dev->flags);
Shaohua Li9e444762012-10-11 13:49:49 +11001186 else
Shaohua Li620125f2012-10-11 13:49:05 +11001187 tx = async_copy_data(1, wbi, dev->page,
1188 dev->sector, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001189 wbi = r5_next_bio(wbi, dev->sector);
1190 }
1191 }
1192 }
1193
1194 return tx;
1195}
1196
Dan Williamsac6b53b2009-07-14 13:40:19 -07001197static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001198{
1199 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001200 int disks = sh->disks;
1201 int pd_idx = sh->pd_idx;
1202 int qd_idx = sh->qd_idx;
1203 int i;
Shaohua Li9e444762012-10-11 13:49:49 +11001204 bool fua = false, sync = false, discard = false;
Dan Williams91c00922007-01-02 13:52:30 -07001205
Harvey Harrisone46b2722008-04-28 02:15:50 -07001206 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001207 (unsigned long long)sh->sector);
1208
Shaohua Libc0934f2012-05-22 13:55:05 +10001209 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001210 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001211 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
Shaohua Li9e444762012-10-11 13:49:49 +11001212 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001213 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001214
Dan Williams91c00922007-01-02 13:52:30 -07001215 for (i = disks; i--; ) {
1216 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001217
Tejun Heoe9c74692010-09-03 11:56:18 +02001218 if (dev->written || i == pd_idx || i == qd_idx) {
Shaohua Li9e444762012-10-11 13:49:49 +11001219 if (!discard)
1220 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001221 if (fua)
1222 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001223 if (sync)
1224 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001225 }
Dan Williams91c00922007-01-02 13:52:30 -07001226 }
1227
Dan Williamsd8ee0722008-06-28 08:32:06 +10001228 if (sh->reconstruct_state == reconstruct_state_drain_run)
1229 sh->reconstruct_state = reconstruct_state_drain_result;
1230 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1231 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1232 else {
1233 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1234 sh->reconstruct_state = reconstruct_state_result;
1235 }
Dan Williams91c00922007-01-02 13:52:30 -07001236
1237 set_bit(STRIPE_HANDLE, &sh->state);
1238 release_stripe(sh);
1239}
1240
1241static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001242ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1243 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001244{
Dan Williams91c00922007-01-02 13:52:30 -07001245 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001246 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001247 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001248 int count = 0, pd_idx = sh->pd_idx, i;
1249 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001250 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001251 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001252
Harvey Harrisone46b2722008-04-28 02:15:50 -07001253 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001254 (unsigned long long)sh->sector);
1255
Shaohua Li620125f2012-10-11 13:49:05 +11001256 for (i = 0; i < sh->disks; i++) {
1257 if (pd_idx == i)
1258 continue;
1259 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1260 break;
1261 }
1262 if (i >= sh->disks) {
1263 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001264 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1265 ops_complete_reconstruct(sh);
1266 return;
1267 }
Dan Williams91c00922007-01-02 13:52:30 -07001268 /* check if prexor is active which means only process blocks
1269 * that are part of a read-modify-write (written)
1270 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001271 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1272 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001273 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1274 for (i = disks; i--; ) {
1275 struct r5dev *dev = &sh->dev[i];
1276 if (dev->written)
1277 xor_srcs[count++] = dev->page;
1278 }
1279 } else {
1280 xor_dest = sh->dev[pd_idx].page;
1281 for (i = disks; i--; ) {
1282 struct r5dev *dev = &sh->dev[i];
1283 if (i != pd_idx)
1284 xor_srcs[count++] = dev->page;
1285 }
1286 }
1287
Dan Williams91c00922007-01-02 13:52:30 -07001288 /* 1/ if we prexor'd then the dest is reused as a source
1289 * 2/ if we did not prexor then we are redoing the parity
1290 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1291 * for the synchronous xor case
1292 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001293 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001294 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1295
1296 atomic_inc(&sh->count);
1297
Dan Williamsac6b53b2009-07-14 13:40:19 -07001298 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001299 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001300 if (unlikely(count == 1))
1301 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1302 else
1303 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001304}
1305
Dan Williamsac6b53b2009-07-14 13:40:19 -07001306static void
1307ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1308 struct dma_async_tx_descriptor *tx)
1309{
1310 struct async_submit_ctl submit;
1311 struct page **blocks = percpu->scribble;
Shaohua Li620125f2012-10-11 13:49:05 +11001312 int count, i;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001313
1314 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1315
Shaohua Li620125f2012-10-11 13:49:05 +11001316 for (i = 0; i < sh->disks; i++) {
1317 if (sh->pd_idx == i || sh->qd_idx == i)
1318 continue;
1319 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1320 break;
1321 }
1322 if (i >= sh->disks) {
1323 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001324 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1325 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1326 ops_complete_reconstruct(sh);
1327 return;
1328 }
1329
Dan Williamsac6b53b2009-07-14 13:40:19 -07001330 count = set_syndrome_sources(blocks, sh);
1331
1332 atomic_inc(&sh->count);
1333
1334 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1335 sh, to_addr_conv(sh, percpu));
1336 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001337}
1338
1339static void ops_complete_check(void *stripe_head_ref)
1340{
1341 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001342
Harvey Harrisone46b2722008-04-28 02:15:50 -07001343 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001344 (unsigned long long)sh->sector);
1345
Dan Williamsecc65c92008-06-28 08:31:57 +10001346 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001347 set_bit(STRIPE_HANDLE, &sh->state);
1348 release_stripe(sh);
1349}
1350
Dan Williamsac6b53b2009-07-14 13:40:19 -07001351static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001352{
Dan Williams91c00922007-01-02 13:52:30 -07001353 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001354 int pd_idx = sh->pd_idx;
1355 int qd_idx = sh->qd_idx;
1356 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001357 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001358 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001359 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001360 int count;
1361 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001362
Harvey Harrisone46b2722008-04-28 02:15:50 -07001363 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001364 (unsigned long long)sh->sector);
1365
Dan Williamsac6b53b2009-07-14 13:40:19 -07001366 count = 0;
1367 xor_dest = sh->dev[pd_idx].page;
1368 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001369 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001370 if (i == pd_idx || i == qd_idx)
1371 continue;
1372 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001373 }
1374
Dan Williamsd6f38f32009-07-14 11:50:52 -07001375 init_async_submit(&submit, 0, NULL, NULL, NULL,
1376 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001377 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001378 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001379
Dan Williams91c00922007-01-02 13:52:30 -07001380 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001381 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1382 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001383}
1384
Dan Williamsac6b53b2009-07-14 13:40:19 -07001385static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1386{
1387 struct page **srcs = percpu->scribble;
1388 struct async_submit_ctl submit;
1389 int count;
1390
1391 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1392 (unsigned long long)sh->sector, checkp);
1393
1394 count = set_syndrome_sources(srcs, sh);
1395 if (!checkp)
1396 srcs[count] = NULL;
1397
1398 atomic_inc(&sh->count);
1399 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1400 sh, to_addr_conv(sh, percpu));
1401 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1402 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1403}
1404
NeilBrown51acbce2013-02-28 09:08:34 +11001405static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001406{
1407 int overlap_clear = 0, i, disks = sh->disks;
1408 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001409 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001410 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001411 struct raid5_percpu *percpu;
1412 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001413
Dan Williamsd6f38f32009-07-14 11:50:52 -07001414 cpu = get_cpu();
1415 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001416 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001417 ops_run_biofill(sh);
1418 overlap_clear++;
1419 }
1420
Dan Williams7b3a8712008-06-28 08:32:09 +10001421 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001422 if (level < 6)
1423 tx = ops_run_compute5(sh, percpu);
1424 else {
1425 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1426 tx = ops_run_compute6_1(sh, percpu);
1427 else
1428 tx = ops_run_compute6_2(sh, percpu);
1429 }
1430 /* terminate the chain if reconstruct is not set to be run */
1431 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001432 async_tx_ack(tx);
1433 }
Dan Williams91c00922007-01-02 13:52:30 -07001434
Dan Williams600aa102008-06-28 08:32:05 +10001435 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001436 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001437
Dan Williams600aa102008-06-28 08:32:05 +10001438 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001439 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001440 overlap_clear++;
1441 }
1442
Dan Williamsac6b53b2009-07-14 13:40:19 -07001443 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1444 if (level < 6)
1445 ops_run_reconstruct5(sh, percpu, tx);
1446 else
1447 ops_run_reconstruct6(sh, percpu, tx);
1448 }
Dan Williams91c00922007-01-02 13:52:30 -07001449
Dan Williamsac6b53b2009-07-14 13:40:19 -07001450 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1451 if (sh->check_state == check_state_run)
1452 ops_run_check_p(sh, percpu);
1453 else if (sh->check_state == check_state_run_q)
1454 ops_run_check_pq(sh, percpu, 0);
1455 else if (sh->check_state == check_state_run_pq)
1456 ops_run_check_pq(sh, percpu, 1);
1457 else
1458 BUG();
1459 }
Dan Williams91c00922007-01-02 13:52:30 -07001460
Dan Williams91c00922007-01-02 13:52:30 -07001461 if (overlap_clear)
1462 for (i = disks; i--; ) {
1463 struct r5dev *dev = &sh->dev[i];
1464 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1465 wake_up(&sh->raid_conf->wait_for_overlap);
1466 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001467 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001468}
1469
NeilBrownd1688a62011-10-11 16:49:52 +11001470static int grow_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
1472 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001473 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001474 if (!sh)
1475 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001476
NeilBrown3f294f42005-11-08 21:39:25 -08001477 sh->raid_conf = conf;
NeilBrown3f294f42005-11-08 21:39:25 -08001478
Shaohua Lib17459c2012-07-19 16:01:31 +10001479 spin_lock_init(&sh->stripe_lock);
1480
NeilBrowne4e11e32010-06-16 16:45:16 +10001481 if (grow_buffers(sh)) {
1482 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001483 kmem_cache_free(conf->slab_cache, sh);
1484 return 0;
1485 }
1486 /* we just created an active stripe so... */
1487 atomic_set(&sh->count, 1);
1488 atomic_inc(&conf->active_stripes);
1489 INIT_LIST_HEAD(&sh->lru);
1490 release_stripe(sh);
1491 return 1;
1492}
1493
NeilBrownd1688a62011-10-11 16:49:52 +11001494static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001495{
Christoph Lametere18b8902006-12-06 20:33:20 -08001496 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001497 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
NeilBrownf4be6b42010-06-01 19:37:25 +10001499 if (conf->mddev->gendisk)
1500 sprintf(conf->cache_name[0],
1501 "raid%d-%s", conf->level, mdname(conf->mddev));
1502 else
1503 sprintf(conf->cache_name[0],
1504 "raid%d-%p", conf->level, conf->mddev);
1505 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1506
NeilBrownad01c9e2006-03-27 01:18:07 -08001507 conf->active_name = 0;
1508 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001510 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 if (!sc)
1512 return 1;
1513 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001514 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001515 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001516 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 return 0;
1519}
NeilBrown29269552006-03-27 01:18:10 -08001520
Dan Williamsd6f38f32009-07-14 11:50:52 -07001521/**
1522 * scribble_len - return the required size of the scribble region
1523 * @num - total number of disks in the array
1524 *
1525 * The size must be enough to contain:
1526 * 1/ a struct page pointer for each device in the array +2
1527 * 2/ room to convert each entry in (1) to its corresponding dma
1528 * (dma_map_page()) or page (page_address()) address.
1529 *
1530 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1531 * calculate over all devices (not just the data blocks), using zeros in place
1532 * of the P and Q blocks.
1533 */
1534static size_t scribble_len(int num)
1535{
1536 size_t len;
1537
1538 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1539
1540 return len;
1541}
1542
NeilBrownd1688a62011-10-11 16:49:52 +11001543static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001544{
1545 /* Make all the stripes able to hold 'newsize' devices.
1546 * New slots in each stripe get 'page' set to a new page.
1547 *
1548 * This happens in stages:
1549 * 1/ create a new kmem_cache and allocate the required number of
1550 * stripe_heads.
Masanari Iida83f0d772012-10-30 00:18:08 +09001551 * 2/ gather all the old stripe_heads and transfer the pages across
NeilBrownad01c9e2006-03-27 01:18:07 -08001552 * to the new stripe_heads. This will have the side effect of
1553 * freezing the array as once all stripe_heads have been collected,
1554 * no IO will be possible. Old stripe heads are freed once their
1555 * pages have been transferred over, and the old kmem_cache is
1556 * freed when all stripes are done.
1557 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1558 * we simple return a failre status - no need to clean anything up.
1559 * 4/ allocate new pages for the new slots in the new stripe_heads.
1560 * If this fails, we don't bother trying the shrink the
1561 * stripe_heads down again, we just leave them as they are.
1562 * As each stripe_head is processed the new one is released into
1563 * active service.
1564 *
1565 * Once step2 is started, we cannot afford to wait for a write,
1566 * so we use GFP_NOIO allocations.
1567 */
1568 struct stripe_head *osh, *nsh;
1569 LIST_HEAD(newstripes);
1570 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001571 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001572 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001573 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001574 int i;
1575
1576 if (newsize <= conf->pool_size)
1577 return 0; /* never bother to shrink */
1578
Dan Williamsb5470dc2008-06-27 21:44:04 -07001579 err = md_allow_write(conf->mddev);
1580 if (err)
1581 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001582
NeilBrownad01c9e2006-03-27 01:18:07 -08001583 /* Step 1 */
1584 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1585 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001586 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001587 if (!sc)
1588 return -ENOMEM;
1589
1590 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001591 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001592 if (!nsh)
1593 break;
1594
NeilBrownad01c9e2006-03-27 01:18:07 -08001595 nsh->raid_conf = conf;
NeilBrowncb13ff62012-09-24 16:27:20 +10001596 spin_lock_init(&nsh->stripe_lock);
NeilBrownad01c9e2006-03-27 01:18:07 -08001597
1598 list_add(&nsh->lru, &newstripes);
1599 }
1600 if (i) {
1601 /* didn't get enough, give up */
1602 while (!list_empty(&newstripes)) {
1603 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1604 list_del(&nsh->lru);
1605 kmem_cache_free(sc, nsh);
1606 }
1607 kmem_cache_destroy(sc);
1608 return -ENOMEM;
1609 }
1610 /* Step 2 - Must use GFP_NOIO now.
1611 * OK, we have enough stripes, start collecting inactive
1612 * stripes and copying them over
1613 */
1614 list_for_each_entry(nsh, &newstripes, lru) {
1615 spin_lock_irq(&conf->device_lock);
1616 wait_event_lock_irq(conf->wait_for_stripe,
1617 !list_empty(&conf->inactive_list),
Lukas Czernereed8c022012-11-30 11:42:40 +01001618 conf->device_lock);
NeilBrownad01c9e2006-03-27 01:18:07 -08001619 osh = get_free_stripe(conf);
1620 spin_unlock_irq(&conf->device_lock);
1621 atomic_set(&nsh->count, 1);
1622 for(i=0; i<conf->pool_size; i++)
1623 nsh->dev[i].page = osh->dev[i].page;
1624 for( ; i<newsize; i++)
1625 nsh->dev[i].page = NULL;
1626 kmem_cache_free(conf->slab_cache, osh);
1627 }
1628 kmem_cache_destroy(conf->slab_cache);
1629
1630 /* Step 3.
1631 * At this point, we are holding all the stripes so the array
1632 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001633 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001634 */
1635 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1636 if (ndisks) {
1637 for (i=0; i<conf->raid_disks; i++)
1638 ndisks[i] = conf->disks[i];
1639 kfree(conf->disks);
1640 conf->disks = ndisks;
1641 } else
1642 err = -ENOMEM;
1643
Dan Williamsd6f38f32009-07-14 11:50:52 -07001644 get_online_cpus();
1645 conf->scribble_len = scribble_len(newsize);
1646 for_each_present_cpu(cpu) {
1647 struct raid5_percpu *percpu;
1648 void *scribble;
1649
1650 percpu = per_cpu_ptr(conf->percpu, cpu);
1651 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1652
1653 if (scribble) {
1654 kfree(percpu->scribble);
1655 percpu->scribble = scribble;
1656 } else {
1657 err = -ENOMEM;
1658 break;
1659 }
1660 }
1661 put_online_cpus();
1662
NeilBrownad01c9e2006-03-27 01:18:07 -08001663 /* Step 4, return new stripes to service */
1664 while(!list_empty(&newstripes)) {
1665 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1666 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001667
NeilBrownad01c9e2006-03-27 01:18:07 -08001668 for (i=conf->raid_disks; i < newsize; i++)
1669 if (nsh->dev[i].page == NULL) {
1670 struct page *p = alloc_page(GFP_NOIO);
1671 nsh->dev[i].page = p;
1672 if (!p)
1673 err = -ENOMEM;
1674 }
1675 release_stripe(nsh);
1676 }
1677 /* critical section pass, GFP_NOIO no longer needed */
1678
1679 conf->slab_cache = sc;
1680 conf->active_name = 1-conf->active_name;
1681 conf->pool_size = newsize;
1682 return err;
1683}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
NeilBrownd1688a62011-10-11 16:49:52 +11001685static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686{
1687 struct stripe_head *sh;
1688
NeilBrown3f294f42005-11-08 21:39:25 -08001689 spin_lock_irq(&conf->device_lock);
1690 sh = get_free_stripe(conf);
1691 spin_unlock_irq(&conf->device_lock);
1692 if (!sh)
1693 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001694 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001695 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001696 kmem_cache_free(conf->slab_cache, sh);
1697 atomic_dec(&conf->active_stripes);
1698 return 1;
1699}
1700
NeilBrownd1688a62011-10-11 16:49:52 +11001701static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001702{
1703 while (drop_one_stripe(conf))
1704 ;
1705
NeilBrown29fc7e32006-02-03 03:03:41 -08001706 if (conf->slab_cache)
1707 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 conf->slab_cache = NULL;
1709}
1710
NeilBrown6712ecf2007-09-27 12:47:43 +02001711static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
NeilBrown99c0fb52009-03-31 14:39:38 +11001713 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001714 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001715 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001717 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11001718 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10001719 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
1721 for (i=0 ; i<disks; i++)
1722 if (bi == &sh->dev[i].req)
1723 break;
1724
Dan Williams45b42332007-07-09 11:56:43 -07001725 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1726 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 uptodate);
1728 if (i == disks) {
1729 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001730 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 }
NeilBrown14a75d32011-12-23 10:17:52 +11001732 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11001733 /* If replacement finished while this request was outstanding,
1734 * 'replacement' might be NULL already.
1735 * In that case it moved down to 'rdev'.
1736 * rdev is not removed until all requests are finished.
1737 */
NeilBrown14a75d32011-12-23 10:17:52 +11001738 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001739 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11001740 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
NeilBrown05616be2012-05-21 09:27:00 +10001742 if (use_new_offset(conf, sh))
1743 s = sh->sector + rdev->new_data_offset;
1744 else
1745 s = sh->sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001748 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11001749 /* Note that this cannot happen on a
1750 * replacement device. We just fail those on
1751 * any error
1752 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001753 printk_ratelimited(
1754 KERN_INFO
1755 "md/raid:%s: read error corrected"
1756 " (%lu sectors at %llu on %s)\n",
1757 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10001758 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001759 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001760 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001761 clear_bit(R5_ReadError, &sh->dev[i].flags);
1762 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng3f9e7c12012-07-31 10:04:21 +10001763 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
1764 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1765
NeilBrown14a75d32011-12-23 10:17:52 +11001766 if (atomic_read(&rdev->read_errors))
1767 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11001769 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001770 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10001771 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001774 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11001775 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1776 printk_ratelimited(
1777 KERN_WARNING
1778 "md/raid:%s: read error on replacement device "
1779 "(sector %llu on %s).\n",
1780 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001781 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11001782 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001783 else if (conf->mddev->degraded >= conf->max_degraded) {
1784 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001785 printk_ratelimited(
1786 KERN_WARNING
1787 "md/raid:%s: read error not correctable "
1788 "(sector %llu on %s).\n",
1789 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001790 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001791 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001792 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08001793 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10001794 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001795 printk_ratelimited(
1796 KERN_WARNING
1797 "md/raid:%s: read error NOT corrected!! "
1798 "(sector %llu on %s).\n",
1799 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001800 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001801 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001802 } else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001803 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001804 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001805 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001806 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001807 else
1808 retry = 1;
1809 if (retry)
majianpeng3f9e7c12012-07-31 10:04:21 +10001810 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
1811 set_bit(R5_ReadError, &sh->dev[i].flags);
1812 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1813 } else
1814 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -08001815 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001816 clear_bit(R5_ReadError, &sh->dev[i].flags);
1817 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10001818 if (!(set_bad
1819 && test_bit(In_sync, &rdev->flags)
1820 && rdev_set_badblocks(
1821 rdev, sh->sector, STRIPE_SECTORS, 0)))
1822 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 }
NeilBrown14a75d32011-12-23 10:17:52 +11001825 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1827 set_bit(STRIPE_HANDLE, &sh->state);
1828 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829}
1830
NeilBrownd710e132008-10-13 11:55:12 +11001831static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832{
NeilBrown99c0fb52009-03-31 14:39:38 +11001833 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001834 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001835 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11001836 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001838 sector_t first_bad;
1839 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11001840 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
NeilBrown977df362011-12-23 10:17:53 +11001842 for (i = 0 ; i < disks; i++) {
1843 if (bi == &sh->dev[i].req) {
1844 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 break;
NeilBrown977df362011-12-23 10:17:53 +11001846 }
1847 if (bi == &sh->dev[i].rreq) {
1848 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001849 if (rdev)
1850 replacement = 1;
1851 else
1852 /* rdev was removed and 'replacement'
1853 * replaced it. rdev is not removed
1854 * until all requests are finished.
1855 */
1856 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11001857 break;
1858 }
1859 }
Dan Williams45b42332007-07-09 11:56:43 -07001860 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1862 uptodate);
1863 if (i == disks) {
1864 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001865 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 }
1867
NeilBrown977df362011-12-23 10:17:53 +11001868 if (replacement) {
1869 if (!uptodate)
1870 md_error(conf->mddev, rdev);
1871 else if (is_badblock(rdev, sh->sector,
1872 STRIPE_SECTORS,
1873 &first_bad, &bad_sectors))
1874 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1875 } else {
1876 if (!uptodate) {
1877 set_bit(WriteErrorSeen, &rdev->flags);
1878 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11001879 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1880 set_bit(MD_RECOVERY_NEEDED,
1881 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11001882 } else if (is_badblock(rdev, sh->sector,
1883 STRIPE_SECTORS,
1884 &first_bad, &bad_sectors))
1885 set_bit(R5_MadeGood, &sh->dev[i].flags);
1886 }
1887 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
NeilBrown977df362011-12-23 10:17:53 +11001889 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1890 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001892 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893}
1894
NeilBrown784052e2009-03-31 15:19:07 +11001895static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
NeilBrown784052e2009-03-31 15:19:07 +11001897static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898{
1899 struct r5dev *dev = &sh->dev[i];
1900
1901 bio_init(&dev->req);
1902 dev->req.bi_io_vec = &dev->vec;
1903 dev->req.bi_vcnt++;
1904 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11001906 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
NeilBrown977df362011-12-23 10:17:53 +11001908 bio_init(&dev->rreq);
1909 dev->rreq.bi_io_vec = &dev->rvec;
1910 dev->rreq.bi_vcnt++;
1911 dev->rreq.bi_max_vecs++;
1912 dev->rreq.bi_private = sh;
1913 dev->rvec.bv_page = dev->page;
1914
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001916 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917}
1918
NeilBrownfd01b882011-10-11 16:47:53 +11001919static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920{
1921 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11001922 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11001923 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10001924 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
NeilBrown908f4fb2011-12-23 10:17:50 +11001926 spin_lock_irqsave(&conf->device_lock, flags);
1927 clear_bit(In_sync, &rdev->flags);
1928 mddev->degraded = calc_degraded(conf);
1929 spin_unlock_irqrestore(&conf->device_lock, flags);
1930 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1931
NeilBrownde393cd2011-07-28 11:31:48 +10001932 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001933 set_bit(Faulty, &rdev->flags);
1934 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1935 printk(KERN_ALERT
1936 "md/raid:%s: Disk failure on %s, disabling device.\n"
1937 "md/raid:%s: Operation continuing on %d devices.\n",
1938 mdname(mddev),
1939 bdevname(rdev->bdev, b),
1940 mdname(mddev),
1941 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001942}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
1944/*
1945 * Input: a 'big' sector number,
1946 * Output: index of the data and parity disk, and the sector # in them.
1947 */
NeilBrownd1688a62011-10-11 16:49:52 +11001948static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001949 int previous, int *dd_idx,
1950 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001952 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001953 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001955 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001956 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001958 int algorithm = previous ? conf->prev_algo
1959 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001960 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1961 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001962 int raid_disks = previous ? conf->previous_raid_disks
1963 : conf->raid_disks;
1964 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
1966 /* First compute the information on this sector */
1967
1968 /*
1969 * Compute the chunk number and the sector offset inside the chunk
1970 */
1971 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1972 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
1974 /*
1975 * Compute the stripe number
1976 */
NeilBrown35f2a592010-04-20 14:13:34 +10001977 stripe = chunk_number;
1978 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10001979 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 /*
1981 * Select the parity disk based on the user selected algorithm.
1982 */
NeilBrown84789552011-07-27 11:00:36 +10001983 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07001984 switch(conf->level) {
1985 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001986 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001987 break;
1988 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001989 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001991 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001992 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 (*dd_idx)++;
1994 break;
1995 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001996 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001997 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 (*dd_idx)++;
1999 break;
2000 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002001 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002002 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 break;
2004 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002005 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002006 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002008 case ALGORITHM_PARITY_0:
2009 pd_idx = 0;
2010 (*dd_idx)++;
2011 break;
2012 case ALGORITHM_PARITY_N:
2013 pd_idx = data_disks;
2014 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002016 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002017 }
2018 break;
2019 case 6:
2020
NeilBrowne183eae2009-03-31 15:20:22 +11002021 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002022 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002023 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002024 qd_idx = pd_idx + 1;
2025 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002026 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002027 qd_idx = 0;
2028 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002029 (*dd_idx) += 2; /* D D P Q D */
2030 break;
2031 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002032 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002033 qd_idx = pd_idx + 1;
2034 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002035 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002036 qd_idx = 0;
2037 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002038 (*dd_idx) += 2; /* D D P Q D */
2039 break;
2040 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002041 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002042 qd_idx = (pd_idx + 1) % raid_disks;
2043 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002044 break;
2045 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002046 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002047 qd_idx = (pd_idx + 1) % raid_disks;
2048 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002049 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002050
2051 case ALGORITHM_PARITY_0:
2052 pd_idx = 0;
2053 qd_idx = 1;
2054 (*dd_idx) += 2;
2055 break;
2056 case ALGORITHM_PARITY_N:
2057 pd_idx = data_disks;
2058 qd_idx = data_disks + 1;
2059 break;
2060
2061 case ALGORITHM_ROTATING_ZERO_RESTART:
2062 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2063 * of blocks for computing Q is different.
2064 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002065 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002066 qd_idx = pd_idx + 1;
2067 if (pd_idx == raid_disks-1) {
2068 (*dd_idx)++; /* Q D D D P */
2069 qd_idx = 0;
2070 } else if (*dd_idx >= pd_idx)
2071 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002072 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002073 break;
2074
2075 case ALGORITHM_ROTATING_N_RESTART:
2076 /* Same a left_asymmetric, by first stripe is
2077 * D D D P Q rather than
2078 * Q D D D P
2079 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002080 stripe2 += 1;
2081 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002082 qd_idx = pd_idx + 1;
2083 if (pd_idx == raid_disks-1) {
2084 (*dd_idx)++; /* Q D D D P */
2085 qd_idx = 0;
2086 } else if (*dd_idx >= pd_idx)
2087 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002088 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002089 break;
2090
2091 case ALGORITHM_ROTATING_N_CONTINUE:
2092 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002093 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002094 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2095 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002096 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002097 break;
2098
2099 case ALGORITHM_LEFT_ASYMMETRIC_6:
2100 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002101 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002102 if (*dd_idx >= pd_idx)
2103 (*dd_idx)++;
2104 qd_idx = raid_disks - 1;
2105 break;
2106
2107 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002108 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002109 if (*dd_idx >= pd_idx)
2110 (*dd_idx)++;
2111 qd_idx = raid_disks - 1;
2112 break;
2113
2114 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002115 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002116 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2117 qd_idx = raid_disks - 1;
2118 break;
2119
2120 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002121 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002122 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2123 qd_idx = raid_disks - 1;
2124 break;
2125
2126 case ALGORITHM_PARITY_0_6:
2127 pd_idx = 0;
2128 (*dd_idx)++;
2129 qd_idx = raid_disks - 1;
2130 break;
2131
NeilBrown16a53ec2006-06-26 00:27:38 -07002132 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002133 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002134 }
2135 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 }
2137
NeilBrown911d4ee2009-03-31 14:39:38 +11002138 if (sh) {
2139 sh->pd_idx = pd_idx;
2140 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002141 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 /*
2144 * Finally, compute the new sector number
2145 */
2146 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2147 return new_sector;
2148}
2149
2150
NeilBrown784052e2009-03-31 15:19:07 +11002151static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152{
NeilBrownd1688a62011-10-11 16:49:52 +11002153 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002154 int raid_disks = sh->disks;
2155 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002157 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2158 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002159 int algorithm = previous ? conf->prev_algo
2160 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 sector_t stripe;
2162 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002163 sector_t chunk_number;
2164 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002166 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
NeilBrown16a53ec2006-06-26 00:27:38 -07002168
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2170 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
NeilBrown16a53ec2006-06-26 00:27:38 -07002172 if (i == sh->pd_idx)
2173 return 0;
2174 switch(conf->level) {
2175 case 4: break;
2176 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002177 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 case ALGORITHM_LEFT_ASYMMETRIC:
2179 case ALGORITHM_RIGHT_ASYMMETRIC:
2180 if (i > sh->pd_idx)
2181 i--;
2182 break;
2183 case ALGORITHM_LEFT_SYMMETRIC:
2184 case ALGORITHM_RIGHT_SYMMETRIC:
2185 if (i < sh->pd_idx)
2186 i += raid_disks;
2187 i -= (sh->pd_idx + 1);
2188 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002189 case ALGORITHM_PARITY_0:
2190 i -= 1;
2191 break;
2192 case ALGORITHM_PARITY_N:
2193 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002195 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002196 }
2197 break;
2198 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002199 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002200 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002201 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002202 case ALGORITHM_LEFT_ASYMMETRIC:
2203 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002204 case ALGORITHM_ROTATING_ZERO_RESTART:
2205 case ALGORITHM_ROTATING_N_RESTART:
2206 if (sh->pd_idx == raid_disks-1)
2207 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002208 else if (i > sh->pd_idx)
2209 i -= 2; /* D D P Q D */
2210 break;
2211 case ALGORITHM_LEFT_SYMMETRIC:
2212 case ALGORITHM_RIGHT_SYMMETRIC:
2213 if (sh->pd_idx == raid_disks-1)
2214 i--; /* Q D D D P */
2215 else {
2216 /* D D P Q D */
2217 if (i < sh->pd_idx)
2218 i += raid_disks;
2219 i -= (sh->pd_idx + 2);
2220 }
2221 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002222 case ALGORITHM_PARITY_0:
2223 i -= 2;
2224 break;
2225 case ALGORITHM_PARITY_N:
2226 break;
2227 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002228 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002229 if (sh->pd_idx == 0)
2230 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002231 else {
2232 /* D D Q P D */
2233 if (i < sh->pd_idx)
2234 i += raid_disks;
2235 i -= (sh->pd_idx + 1);
2236 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002237 break;
2238 case ALGORITHM_LEFT_ASYMMETRIC_6:
2239 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2240 if (i > sh->pd_idx)
2241 i--;
2242 break;
2243 case ALGORITHM_LEFT_SYMMETRIC_6:
2244 case ALGORITHM_RIGHT_SYMMETRIC_6:
2245 if (i < sh->pd_idx)
2246 i += data_disks + 1;
2247 i -= (sh->pd_idx + 1);
2248 break;
2249 case ALGORITHM_PARITY_0_6:
2250 i -= 1;
2251 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002252 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002253 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002254 }
2255 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 }
2257
2258 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002259 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
NeilBrown112bf892009-03-31 14:39:38 +11002261 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002262 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002263 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2264 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002265 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2266 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 return 0;
2268 }
2269 return r_sector;
2270}
2271
2272
Dan Williams600aa102008-06-28 08:32:05 +10002273static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002274schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002275 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002276{
2277 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002278 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002279 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002280
Dan Williamse33129d2007-01-02 13:52:30 -07002281 if (rcw) {
2282 /* if we are not expanding this is a proper write request, and
2283 * there will be bios with new data to be drained into the
2284 * stripe cache
2285 */
2286 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10002287 sh->reconstruct_state = reconstruct_state_drain_run;
2288 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2289 } else
2290 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07002291
Dan Williamsac6b53b2009-07-14 13:40:19 -07002292 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002293
2294 for (i = disks; i--; ) {
2295 struct r5dev *dev = &sh->dev[i];
2296
2297 if (dev->towrite) {
2298 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002299 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002300 if (!expand)
2301 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002302 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002303 }
2304 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002305 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002306 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002307 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002308 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002309 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002310 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2311 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2312
Dan Williamsd8ee0722008-06-28 08:32:06 +10002313 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002314 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2315 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002316 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002317
2318 for (i = disks; i--; ) {
2319 struct r5dev *dev = &sh->dev[i];
2320 if (i == pd_idx)
2321 continue;
2322
Dan Williamse33129d2007-01-02 13:52:30 -07002323 if (dev->towrite &&
2324 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002325 test_bit(R5_Wantcompute, &dev->flags))) {
2326 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002327 set_bit(R5_LOCKED, &dev->flags);
2328 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002329 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002330 }
2331 }
2332 }
2333
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002334 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002335 * are in flight
2336 */
2337 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2338 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002339 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002340
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002341 if (level == 6) {
2342 int qd_idx = sh->qd_idx;
2343 struct r5dev *dev = &sh->dev[qd_idx];
2344
2345 set_bit(R5_LOCKED, &dev->flags);
2346 clear_bit(R5_UPTODATE, &dev->flags);
2347 s->locked++;
2348 }
2349
Dan Williams600aa102008-06-28 08:32:05 +10002350 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002351 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002352 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002353}
NeilBrown16a53ec2006-06-26 00:27:38 -07002354
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355/*
2356 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002357 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 * The bi_next chain must be in order.
2359 */
2360static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2361{
2362 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002363 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002364 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
NeilBrowncbe47ec2011-07-26 11:20:35 +10002366 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 (unsigned long long)bi->bi_sector,
2368 (unsigned long long)sh->sector);
2369
Shaohua Lib17459c2012-07-19 16:01:31 +10002370 /*
2371 * If several bio share a stripe. The bio bi_phys_segments acts as a
2372 * reference count to avoid race. The reference count should already be
2373 * increased before this function is called (for example, in
2374 * make_request()), so other bio sharing this stripe will not free the
2375 * stripe. If a stripe is owned by one stripe, the stripe lock will
2376 * protect it.
2377 */
2378 spin_lock_irq(&sh->stripe_lock);
NeilBrown72626682005-09-09 16:23:54 -07002379 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002381 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07002382 firstwrite = 1;
2383 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 bip = &sh->dev[dd_idx].toread;
2385 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002386 if (bio_end_sector(*bip) > bi->bi_sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 goto overlap;
2388 bip = & (*bip)->bi_next;
2389 }
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002390 if (*bip && (*bip)->bi_sector < bio_end_sector(bi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 goto overlap;
2392
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002393 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 if (*bip)
2395 bi->bi_next = *bip;
2396 *bip = bi;
Shaohua Lie7836bd62012-07-19 16:01:31 +10002397 raid5_inc_bi_active_stripes(bi);
NeilBrown72626682005-09-09 16:23:54 -07002398
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 if (forwrite) {
2400 /* check if page is covered */
2401 sector_t sector = sh->dev[dd_idx].sector;
2402 for (bi=sh->dev[dd_idx].towrite;
2403 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2404 bi && bi->bi_sector <= sector;
2405 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002406 if (bio_end_sector(bi) >= sector)
2407 sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 }
2409 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2410 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2411 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002412
2413 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2414 (unsigned long long)(*bip)->bi_sector,
2415 (unsigned long long)sh->sector, dd_idx);
NeilBrownb97390a2012-10-11 13:50:12 +11002416 spin_unlock_irq(&sh->stripe_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002417
2418 if (conf->mddev->bitmap && firstwrite) {
2419 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2420 STRIPE_SECTORS, 0);
2421 sh->bm_seq = conf->seq_flush+1;
2422 set_bit(STRIPE_BIT_DELAY, &sh->state);
2423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 return 1;
2425
2426 overlap:
2427 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10002428 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 return 0;
2430}
2431
NeilBrownd1688a62011-10-11 16:49:52 +11002432static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002433
NeilBrownd1688a62011-10-11 16:49:52 +11002434static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002435 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002436{
NeilBrown784052e2009-03-31 15:19:07 +11002437 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002438 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002439 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002440 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002441 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002442
NeilBrown112bf892009-03-31 14:39:38 +11002443 raid5_compute_sector(conf,
2444 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002445 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002446 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002447 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002448}
2449
Dan Williamsa4456852007-07-09 11:56:43 -07002450static void
NeilBrownd1688a62011-10-11 16:49:52 +11002451handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002452 struct stripe_head_state *s, int disks,
2453 struct bio **return_bi)
2454{
2455 int i;
2456 for (i = disks; i--; ) {
2457 struct bio *bi;
2458 int bitmap_end = 0;
2459
2460 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002461 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002462 rcu_read_lock();
2463 rdev = rcu_dereference(conf->disks[i].rdev);
2464 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002465 atomic_inc(&rdev->nr_pending);
2466 else
2467 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002468 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002469 if (rdev) {
2470 if (!rdev_set_badblocks(
2471 rdev,
2472 sh->sector,
2473 STRIPE_SECTORS, 0))
2474 md_error(conf->mddev, rdev);
2475 rdev_dec_pending(rdev, conf->mddev);
2476 }
Dan Williamsa4456852007-07-09 11:56:43 -07002477 }
Shaohua Lib17459c2012-07-19 16:01:31 +10002478 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002479 /* fail all writes first */
2480 bi = sh->dev[i].towrite;
2481 sh->dev[i].towrite = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10002482 spin_unlock_irq(&sh->stripe_lock);
NeilBrown1ed850f2012-10-11 13:50:13 +11002483 if (bi)
Dan Williamsa4456852007-07-09 11:56:43 -07002484 bitmap_end = 1;
Dan Williamsa4456852007-07-09 11:56:43 -07002485
2486 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2487 wake_up(&conf->wait_for_overlap);
2488
2489 while (bi && bi->bi_sector <
2490 sh->dev[i].sector + STRIPE_SECTORS) {
2491 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2492 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002493 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002494 md_write_end(conf->mddev);
2495 bi->bi_next = *return_bi;
2496 *return_bi = bi;
2497 }
2498 bi = nextbi;
2499 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002500 if (bitmap_end)
2501 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2502 STRIPE_SECTORS, 0, 0);
2503 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002504 /* and fail all 'written' */
2505 bi = sh->dev[i].written;
2506 sh->dev[i].written = NULL;
2507 if (bi) bitmap_end = 1;
2508 while (bi && bi->bi_sector <
2509 sh->dev[i].sector + STRIPE_SECTORS) {
2510 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2511 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002512 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002513 md_write_end(conf->mddev);
2514 bi->bi_next = *return_bi;
2515 *return_bi = bi;
2516 }
2517 bi = bi2;
2518 }
2519
Dan Williamsb5e98d62007-01-02 13:52:31 -07002520 /* fail any reads if this device is non-operational and
2521 * the data has not reached the cache yet.
2522 */
2523 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2524 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2525 test_bit(R5_ReadError, &sh->dev[i].flags))) {
NeilBrown143c4d02012-10-11 13:50:12 +11002526 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002527 bi = sh->dev[i].toread;
2528 sh->dev[i].toread = NULL;
NeilBrown143c4d02012-10-11 13:50:12 +11002529 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002530 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2531 wake_up(&conf->wait_for_overlap);
Dan Williamsa4456852007-07-09 11:56:43 -07002532 while (bi && bi->bi_sector <
2533 sh->dev[i].sector + STRIPE_SECTORS) {
2534 struct bio *nextbi =
2535 r5_next_bio(bi, sh->dev[i].sector);
2536 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002537 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002538 bi->bi_next = *return_bi;
2539 *return_bi = bi;
2540 }
2541 bi = nextbi;
2542 }
2543 }
Dan Williamsa4456852007-07-09 11:56:43 -07002544 if (bitmap_end)
2545 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2546 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002547 /* If we were in the middle of a write the parity block might
2548 * still be locked - so just clear all R5_LOCKED flags
2549 */
2550 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002551 }
2552
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002553 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2554 if (atomic_dec_and_test(&conf->pending_full_writes))
2555 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002556}
2557
NeilBrown7f0da592011-07-28 11:39:22 +10002558static void
NeilBrownd1688a62011-10-11 16:49:52 +11002559handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002560 struct stripe_head_state *s)
2561{
2562 int abort = 0;
2563 int i;
2564
NeilBrown7f0da592011-07-28 11:39:22 +10002565 clear_bit(STRIPE_SYNCING, &sh->state);
2566 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002567 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002568 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10002569 * Don't even need to abort as that is handled elsewhere
2570 * if needed, and not always wanted e.g. if there is a known
2571 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11002572 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002573 * non-sync devices, or abort the recovery
2574 */
NeilBrown18b98372012-04-01 23:48:38 +10002575 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2576 /* During recovery devices cannot be removed, so
2577 * locking and refcounting of rdevs is not needed
2578 */
2579 for (i = 0; i < conf->raid_disks; i++) {
2580 struct md_rdev *rdev = conf->disks[i].rdev;
2581 if (rdev
2582 && !test_bit(Faulty, &rdev->flags)
2583 && !test_bit(In_sync, &rdev->flags)
2584 && !rdev_set_badblocks(rdev, sh->sector,
2585 STRIPE_SECTORS, 0))
2586 abort = 1;
2587 rdev = conf->disks[i].replacement;
2588 if (rdev
2589 && !test_bit(Faulty, &rdev->flags)
2590 && !test_bit(In_sync, &rdev->flags)
2591 && !rdev_set_badblocks(rdev, sh->sector,
2592 STRIPE_SECTORS, 0))
2593 abort = 1;
2594 }
2595 if (abort)
2596 conf->recovery_disabled =
2597 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10002598 }
NeilBrown18b98372012-04-01 23:48:38 +10002599 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10002600}
2601
NeilBrown9a3e1102011-12-23 10:17:53 +11002602static int want_replace(struct stripe_head *sh, int disk_idx)
2603{
2604 struct md_rdev *rdev;
2605 int rv = 0;
2606 /* Doing recovery so rcu locking not required */
2607 rdev = sh->raid_conf->disks[disk_idx].replacement;
2608 if (rdev
2609 && !test_bit(Faulty, &rdev->flags)
2610 && !test_bit(In_sync, &rdev->flags)
2611 && (rdev->recovery_offset <= sh->sector
2612 || rdev->mddev->recovery_cp <= sh->sector))
2613 rv = 1;
2614
2615 return rv;
2616}
2617
NeilBrown93b3dbc2011-07-27 11:00:36 +10002618/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002619 * to be read or computed to satisfy a request.
2620 *
2621 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002622 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002623 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002624static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2625 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002626{
2627 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002628 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2629 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002630
Dan Williamsf38e1212007-01-02 13:52:30 -07002631 /* is the data in this block needed, and can we get it? */
2632 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002633 !test_bit(R5_UPTODATE, &dev->flags) &&
2634 (dev->toread ||
2635 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2636 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002637 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002638 (s->failed >= 1 && fdev[0]->toread) ||
2639 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002640 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2641 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2642 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002643 /* we would like to get this block, possibly by computing it,
2644 * otherwise read it if the backing disk is insync
2645 */
2646 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2647 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2648 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002649 (s->failed && (disk_idx == s->failed_num[0] ||
2650 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002651 /* have disk failed, and we're requested to fetch it;
2652 * do compute it
2653 */
2654 pr_debug("Computing stripe %llu block %d\n",
2655 (unsigned long long)sh->sector, disk_idx);
2656 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2657 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2658 set_bit(R5_Wantcompute, &dev->flags);
2659 sh->ops.target = disk_idx;
2660 sh->ops.target2 = -1; /* no 2nd target */
2661 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002662 /* Careful: from this point on 'uptodate' is in the eye
2663 * of raid_run_ops which services 'compute' operations
2664 * before writes. R5_Wantcompute flags a block that will
2665 * be R5_UPTODATE by the time it is needed for a
2666 * subsequent operation.
2667 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002668 s->uptodate++;
2669 return 1;
2670 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2671 /* Computing 2-failure is *very* expensive; only
2672 * do it if failed >= 2
2673 */
2674 int other;
2675 for (other = disks; other--; ) {
2676 if (other == disk_idx)
2677 continue;
2678 if (!test_bit(R5_UPTODATE,
2679 &sh->dev[other].flags))
2680 break;
2681 }
2682 BUG_ON(other < 0);
2683 pr_debug("Computing stripe %llu blocks %d,%d\n",
2684 (unsigned long long)sh->sector,
2685 disk_idx, other);
2686 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2687 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2688 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2689 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2690 sh->ops.target = disk_idx;
2691 sh->ops.target2 = other;
2692 s->uptodate += 2;
2693 s->req_compute = 1;
2694 return 1;
2695 } else if (test_bit(R5_Insync, &dev->flags)) {
2696 set_bit(R5_LOCKED, &dev->flags);
2697 set_bit(R5_Wantread, &dev->flags);
2698 s->locked++;
2699 pr_debug("Reading block %d (sync=%d)\n",
2700 disk_idx, s->syncing);
2701 }
2702 }
2703
2704 return 0;
2705}
2706
2707/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002708 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002709 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002710static void handle_stripe_fill(struct stripe_head *sh,
2711 struct stripe_head_state *s,
2712 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002713{
2714 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002715
2716 /* look for blocks to read/compute, skip this if a compute
2717 * is already in flight, or if the stripe contents are in the
2718 * midst of changing due to a write
2719 */
2720 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2721 !sh->reconstruct_state)
2722 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002723 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002724 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002725 set_bit(STRIPE_HANDLE, &sh->state);
2726}
2727
2728
Dan Williams1fe797e2008-06-28 09:16:30 +10002729/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002730 * any written block on an uptodate or failed drive can be returned.
2731 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2732 * never LOCKED, so we don't need to test 'failed' directly.
2733 */
NeilBrownd1688a62011-10-11 16:49:52 +11002734static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002735 struct stripe_head *sh, int disks, struct bio **return_bi)
2736{
2737 int i;
2738 struct r5dev *dev;
2739
2740 for (i = disks; i--; )
2741 if (sh->dev[i].written) {
2742 dev = &sh->dev[i];
2743 if (!test_bit(R5_LOCKED, &dev->flags) &&
Shaohua Li9e444762012-10-11 13:49:49 +11002744 (test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownca64cae2012-11-21 16:33:40 +11002745 test_bit(R5_Discard, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002746 /* We can return any write requests */
2747 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07002748 pr_debug("Return write for disc %d\n", i);
NeilBrownca64cae2012-11-21 16:33:40 +11002749 if (test_and_clear_bit(R5_Discard, &dev->flags))
2750 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002751 wbi = dev->written;
2752 dev->written = NULL;
2753 while (wbi && wbi->bi_sector <
2754 dev->sector + STRIPE_SECTORS) {
2755 wbi2 = r5_next_bio(wbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002756 if (!raid5_dec_bi_active_stripes(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002757 md_write_end(conf->mddev);
2758 wbi->bi_next = *return_bi;
2759 *return_bi = wbi;
2760 }
2761 wbi = wbi2;
2762 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002763 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2764 STRIPE_SECTORS,
Dan Williamsa4456852007-07-09 11:56:43 -07002765 !test_bit(STRIPE_DEGRADED, &sh->state),
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002766 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002767 }
NeilBrownca64cae2012-11-21 16:33:40 +11002768 } else if (test_bit(R5_Discard, &sh->dev[i].flags))
2769 clear_bit(R5_Discard, &sh->dev[i].flags);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002770
2771 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2772 if (atomic_dec_and_test(&conf->pending_full_writes))
2773 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002774}
2775
NeilBrownd1688a62011-10-11 16:49:52 +11002776static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002777 struct stripe_head *sh,
2778 struct stripe_head_state *s,
2779 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002780{
2781 int rmw = 0, rcw = 0, i;
Alexander Lyakasa7854482012-10-11 13:50:12 +11002782 sector_t recovery_cp = conf->mddev->recovery_cp;
2783
2784 /* RAID6 requires 'rcw' in current implementation.
2785 * Otherwise, check whether resync is now happening or should start.
2786 * If yes, then the array is dirty (after unclean shutdown or
2787 * initial creation), so parity in some stripes might be inconsistent.
2788 * In this case, we need to always do reconstruct-write, to ensure
2789 * that in case of drive failure or read-error correction, we
2790 * generate correct data from the parity.
2791 */
2792 if (conf->max_degraded == 2 ||
2793 (recovery_cp < MaxSector && sh->sector >= recovery_cp)) {
2794 /* Calculate the real rcw later - for now make it
NeilBrownc8ac1802011-07-27 11:00:36 +10002795 * look like rcw is cheaper
2796 */
2797 rcw = 1; rmw = 2;
Alexander Lyakasa7854482012-10-11 13:50:12 +11002798 pr_debug("force RCW max_degraded=%u, recovery_cp=%llu sh->sector=%llu\n",
2799 conf->max_degraded, (unsigned long long)recovery_cp,
2800 (unsigned long long)sh->sector);
NeilBrownc8ac1802011-07-27 11:00:36 +10002801 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002802 /* would I have to read this buffer for read_modify_write */
2803 struct r5dev *dev = &sh->dev[i];
2804 if ((dev->towrite || i == sh->pd_idx) &&
2805 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002806 !(test_bit(R5_UPTODATE, &dev->flags) ||
2807 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002808 if (test_bit(R5_Insync, &dev->flags))
2809 rmw++;
2810 else
2811 rmw += 2*disks; /* cannot read it */
2812 }
2813 /* Would I have to read this buffer for reconstruct_write */
2814 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2815 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002816 !(test_bit(R5_UPTODATE, &dev->flags) ||
2817 test_bit(R5_Wantcompute, &dev->flags))) {
2818 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002819 else
2820 rcw += 2*disks;
2821 }
2822 }
Dan Williams45b42332007-07-09 11:56:43 -07002823 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002824 (unsigned long long)sh->sector, rmw, rcw);
2825 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrowna9add5d2012-10-31 11:59:09 +11002826 if (rmw < rcw && rmw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002827 /* prefer read-modify-write, but need to get some data */
NeilBrowna9add5d2012-10-31 11:59:09 +11002828 blk_add_trace_msg(conf->mddev->queue, "raid5 rmw %llu %d",
2829 (unsigned long long)sh->sector, rmw);
Dan Williamsa4456852007-07-09 11:56:43 -07002830 for (i = disks; i--; ) {
2831 struct r5dev *dev = &sh->dev[i];
2832 if ((dev->towrite || i == sh->pd_idx) &&
2833 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002834 !(test_bit(R5_UPTODATE, &dev->flags) ||
2835 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002836 test_bit(R5_Insync, &dev->flags)) {
2837 if (
2838 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002839 pr_debug("Read_old block "
NeilBrowna9add5d2012-10-31 11:59:09 +11002840 "%d for r-m-w\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002841 set_bit(R5_LOCKED, &dev->flags);
2842 set_bit(R5_Wantread, &dev->flags);
2843 s->locked++;
2844 } else {
2845 set_bit(STRIPE_DELAYED, &sh->state);
2846 set_bit(STRIPE_HANDLE, &sh->state);
2847 }
2848 }
2849 }
NeilBrowna9add5d2012-10-31 11:59:09 +11002850 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002851 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002852 /* want reconstruct write, but need to get some data */
NeilBrowna9add5d2012-10-31 11:59:09 +11002853 int qread =0;
NeilBrownc8ac1802011-07-27 11:00:36 +10002854 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002855 for (i = disks; i--; ) {
2856 struct r5dev *dev = &sh->dev[i];
2857 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002858 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002859 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002860 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002861 test_bit(R5_Wantcompute, &dev->flags))) {
2862 rcw++;
2863 if (!test_bit(R5_Insync, &dev->flags))
2864 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002865 if (
2866 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002867 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002868 "%d for Reconstruct\n", i);
2869 set_bit(R5_LOCKED, &dev->flags);
2870 set_bit(R5_Wantread, &dev->flags);
2871 s->locked++;
NeilBrowna9add5d2012-10-31 11:59:09 +11002872 qread++;
Dan Williamsa4456852007-07-09 11:56:43 -07002873 } else {
2874 set_bit(STRIPE_DELAYED, &sh->state);
2875 set_bit(STRIPE_HANDLE, &sh->state);
2876 }
2877 }
2878 }
NeilBrowna9add5d2012-10-31 11:59:09 +11002879 if (rcw)
2880 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
2881 (unsigned long long)sh->sector,
2882 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
NeilBrownc8ac1802011-07-27 11:00:36 +10002883 }
Dan Williamsa4456852007-07-09 11:56:43 -07002884 /* now if nothing is locked, and if we have enough data,
2885 * we can start a write request
2886 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002887 /* since handle_stripe can be called at any time we need to handle the
2888 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002889 * subsequent call wants to start a write request. raid_run_ops only
2890 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002891 * simultaneously. If this is not the case then new writes need to be
2892 * held off until the compute completes.
2893 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002894 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2895 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2896 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002897 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002898}
2899
NeilBrownd1688a62011-10-11 16:49:52 +11002900static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002901 struct stripe_head_state *s, int disks)
2902{
Dan Williamsecc65c92008-06-28 08:31:57 +10002903 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002904
Dan Williamsbd2ab672008-04-10 21:29:27 -07002905 set_bit(STRIPE_HANDLE, &sh->state);
2906
Dan Williamsecc65c92008-06-28 08:31:57 +10002907 switch (sh->check_state) {
2908 case check_state_idle:
2909 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002910 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002911 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002912 sh->check_state = check_state_run;
2913 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002914 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002915 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002916 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002917 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002918 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002919 /* fall through */
2920 case check_state_compute_result:
2921 sh->check_state = check_state_idle;
2922 if (!dev)
2923 dev = &sh->dev[sh->pd_idx];
2924
2925 /* check that a write has not made the stripe insync */
2926 if (test_bit(STRIPE_INSYNC, &sh->state))
2927 break;
2928
2929 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002930 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2931 BUG_ON(s->uptodate != disks);
2932
2933 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002934 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002935 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002936
Dan Williamsa4456852007-07-09 11:56:43 -07002937 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002938 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002939 break;
2940 case check_state_run:
2941 break; /* we will be called again upon completion */
2942 case check_state_check_result:
2943 sh->check_state = check_state_idle;
2944
2945 /* if a failure occurred during the check operation, leave
2946 * STRIPE_INSYNC not set and let the stripe be handled again
2947 */
2948 if (s->failed)
2949 break;
2950
2951 /* handle a successful check operation, if parity is correct
2952 * we are done. Otherwise update the mismatch count and repair
2953 * parity if !MD_RECOVERY_CHECK
2954 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002955 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002956 /* parity is correct (on disc,
2957 * not in buffer any more)
2958 */
2959 set_bit(STRIPE_INSYNC, &sh->state);
2960 else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11002961 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsecc65c92008-06-28 08:31:57 +10002962 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2963 /* don't try to repair!! */
2964 set_bit(STRIPE_INSYNC, &sh->state);
2965 else {
2966 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002967 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002968 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2969 set_bit(R5_Wantcompute,
2970 &sh->dev[sh->pd_idx].flags);
2971 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002972 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10002973 s->uptodate++;
2974 }
2975 }
2976 break;
2977 case check_state_compute_run:
2978 break;
2979 default:
2980 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2981 __func__, sh->check_state,
2982 (unsigned long long) sh->sector);
2983 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002984 }
2985}
2986
2987
NeilBrownd1688a62011-10-11 16:49:52 +11002988static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07002989 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10002990 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002991{
Dan Williamsa4456852007-07-09 11:56:43 -07002992 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11002993 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07002994 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07002995
2996 set_bit(STRIPE_HANDLE, &sh->state);
2997
2998 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002999
Dan Williamsa4456852007-07-09 11:56:43 -07003000 /* Want to check and possibly repair P and Q.
3001 * However there could be one 'failed' device, in which
3002 * case we can only check one of them, possibly using the
3003 * other to generate missing data
3004 */
3005
Dan Williamsd82dfee2009-07-14 13:40:57 -07003006 switch (sh->check_state) {
3007 case check_state_idle:
3008 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10003009 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003010 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07003011 * makes sense to check P (If anything else were failed,
3012 * we would have used P to recreate it).
3013 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003014 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07003015 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003016 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003017 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07003018 * anything, so it makes sense to check it
3019 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003020 if (sh->check_state == check_state_run)
3021 sh->check_state = check_state_run_pq;
3022 else
3023 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07003024 }
Dan Williams36d1c642009-07-14 11:48:22 -07003025
Dan Williamsd82dfee2009-07-14 13:40:57 -07003026 /* discard potentially stale zero_sum_result */
3027 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07003028
Dan Williamsd82dfee2009-07-14 13:40:57 -07003029 if (sh->check_state == check_state_run) {
3030 /* async_xor_zero_sum destroys the contents of P */
3031 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
3032 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07003033 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003034 if (sh->check_state >= check_state_run &&
3035 sh->check_state <= check_state_run_pq) {
3036 /* async_syndrome_zero_sum preserves P and Q, so
3037 * no need to mark them !uptodate here
3038 */
3039 set_bit(STRIPE_OP_CHECK, &s->ops_request);
3040 break;
3041 }
Dan Williams36d1c642009-07-14 11:48:22 -07003042
Dan Williamsd82dfee2009-07-14 13:40:57 -07003043 /* we have 2-disk failure */
3044 BUG_ON(s->failed != 2);
3045 /* fall through */
3046 case check_state_compute_result:
3047 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07003048
Dan Williamsd82dfee2009-07-14 13:40:57 -07003049 /* check that a write has not made the stripe insync */
3050 if (test_bit(STRIPE_INSYNC, &sh->state))
3051 break;
Dan Williamsa4456852007-07-09 11:56:43 -07003052
3053 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07003054 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07003055 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003056 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07003057 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003058 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07003059 s->locked++;
3060 set_bit(R5_LOCKED, &dev->flags);
3061 set_bit(R5_Wantwrite, &dev->flags);
3062 }
3063 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003064 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07003065 s->locked++;
3066 set_bit(R5_LOCKED, &dev->flags);
3067 set_bit(R5_Wantwrite, &dev->flags);
3068 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003069 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003070 dev = &sh->dev[pd_idx];
3071 s->locked++;
3072 set_bit(R5_LOCKED, &dev->flags);
3073 set_bit(R5_Wantwrite, &dev->flags);
3074 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003075 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003076 dev = &sh->dev[qd_idx];
3077 s->locked++;
3078 set_bit(R5_LOCKED, &dev->flags);
3079 set_bit(R5_Wantwrite, &dev->flags);
3080 }
3081 clear_bit(STRIPE_DEGRADED, &sh->state);
3082
3083 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003084 break;
3085 case check_state_run:
3086 case check_state_run_q:
3087 case check_state_run_pq:
3088 break; /* we will be called again upon completion */
3089 case check_state_check_result:
3090 sh->check_state = check_state_idle;
3091
3092 /* handle a successful check operation, if parity is correct
3093 * we are done. Otherwise update the mismatch count and repair
3094 * parity if !MD_RECOVERY_CHECK
3095 */
3096 if (sh->ops.zero_sum_result == 0) {
3097 /* both parities are correct */
3098 if (!s->failed)
3099 set_bit(STRIPE_INSYNC, &sh->state);
3100 else {
3101 /* in contrast to the raid5 case we can validate
3102 * parity, but still have a failure to write
3103 * back
3104 */
3105 sh->check_state = check_state_compute_result;
3106 /* Returning at this point means that we may go
3107 * off and bring p and/or q uptodate again so
3108 * we make sure to check zero_sum_result again
3109 * to verify if p or q need writeback
3110 */
3111 }
3112 } else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003113 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003114 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3115 /* don't try to repair!! */
3116 set_bit(STRIPE_INSYNC, &sh->state);
3117 else {
3118 int *target = &sh->ops.target;
3119
3120 sh->ops.target = -1;
3121 sh->ops.target2 = -1;
3122 sh->check_state = check_state_compute_run;
3123 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3124 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3125 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3126 set_bit(R5_Wantcompute,
3127 &sh->dev[pd_idx].flags);
3128 *target = pd_idx;
3129 target = &sh->ops.target2;
3130 s->uptodate++;
3131 }
3132 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3133 set_bit(R5_Wantcompute,
3134 &sh->dev[qd_idx].flags);
3135 *target = qd_idx;
3136 s->uptodate++;
3137 }
3138 }
3139 }
3140 break;
3141 case check_state_compute_run:
3142 break;
3143 default:
3144 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3145 __func__, sh->check_state,
3146 (unsigned long long) sh->sector);
3147 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003148 }
3149}
3150
NeilBrownd1688a62011-10-11 16:49:52 +11003151static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003152{
3153 int i;
3154
3155 /* We have read all the blocks in this stripe and now we need to
3156 * copy some of them into a target stripe for expand.
3157 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003158 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003159 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3160 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003161 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003162 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003163 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003164 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003165
NeilBrown784052e2009-03-31 15:19:07 +11003166 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003167 sector_t s = raid5_compute_sector(conf, bn, 0,
3168 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003169 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003170 if (sh2 == NULL)
3171 /* so far only the early blocks of this stripe
3172 * have been requested. When later blocks
3173 * get requested, we will try again
3174 */
3175 continue;
3176 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3177 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3178 /* must have already done this block */
3179 release_stripe(sh2);
3180 continue;
3181 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003182
3183 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003184 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003185 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003186 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003187 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003188
Dan Williamsa4456852007-07-09 11:56:43 -07003189 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3190 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3191 for (j = 0; j < conf->raid_disks; j++)
3192 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003193 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003194 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3195 break;
3196 if (j == conf->raid_disks) {
3197 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3198 set_bit(STRIPE_HANDLE, &sh2->state);
3199 }
3200 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003201
Dan Williamsa4456852007-07-09 11:56:43 -07003202 }
NeilBrowna2e08552007-09-11 15:23:36 -07003203 /* done submitting copies, wait for them to complete */
NeilBrown749586b2012-11-20 14:11:15 +11003204 async_tx_quiesce(&tx);
Dan Williamsa4456852007-07-09 11:56:43 -07003205}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206
3207/*
3208 * handle_stripe - do things to a stripe.
3209 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003210 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3211 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003213 * return some read requests which now have data
3214 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 * schedule a read on some buffers
3216 * schedule a write of some buffers
3217 * return confirmation of parity correctness
3218 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 */
Dan Williamsa4456852007-07-09 11:56:43 -07003220
NeilBrownacfe7262011-07-27 11:00:36 +10003221static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003222{
NeilBrownd1688a62011-10-11 16:49:52 +11003223 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003224 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003225 struct r5dev *dev;
3226 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003227 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003228
NeilBrownacfe7262011-07-27 11:00:36 +10003229 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003230
NeilBrownacfe7262011-07-27 11:00:36 +10003231 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3232 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3233 s->failed_num[0] = -1;
3234 s->failed_num[1] = -1;
3235
3236 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003237 rcu_read_lock();
3238 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003239 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003240 sector_t first_bad;
3241 int bad_sectors;
3242 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003243
NeilBrown16a53ec2006-06-26 00:27:38 -07003244 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003245
Dan Williams45b42332007-07-09 11:56:43 -07003246 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003247 i, dev->flags,
3248 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003249 /* maybe we can reply to a read
3250 *
3251 * new wantfill requests are only permitted while
3252 * ops_complete_biofill is guaranteed to be inactive
3253 */
3254 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3255 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3256 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003257
3258 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003259 if (test_bit(R5_LOCKED, &dev->flags))
3260 s->locked++;
3261 if (test_bit(R5_UPTODATE, &dev->flags))
3262 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003263 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003264 s->compute++;
3265 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003266 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003267
NeilBrownacfe7262011-07-27 11:00:36 +10003268 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003269 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003270 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003271 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003272 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003273 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003274 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003275 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003276 }
Dan Williamsa4456852007-07-09 11:56:43 -07003277 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003278 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003279 /* Prefer to use the replacement for reads, but only
3280 * if it is recovered enough and has no bad blocks.
3281 */
3282 rdev = rcu_dereference(conf->disks[i].replacement);
3283 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3284 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3285 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3286 &first_bad, &bad_sectors))
3287 set_bit(R5_ReadRepl, &dev->flags);
3288 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003289 if (rdev)
3290 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003291 rdev = rcu_dereference(conf->disks[i].rdev);
3292 clear_bit(R5_ReadRepl, &dev->flags);
3293 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003294 if (rdev && test_bit(Faulty, &rdev->flags))
3295 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003296 if (rdev) {
3297 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3298 &first_bad, &bad_sectors);
3299 if (s->blocked_rdev == NULL
3300 && (test_bit(Blocked, &rdev->flags)
3301 || is_bad < 0)) {
3302 if (is_bad < 0)
3303 set_bit(BlockedBadBlocks,
3304 &rdev->flags);
3305 s->blocked_rdev = rdev;
3306 atomic_inc(&rdev->nr_pending);
3307 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003308 }
NeilBrown415e72d2010-06-17 17:25:21 +10003309 clear_bit(R5_Insync, &dev->flags);
3310 if (!rdev)
3311 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003312 else if (is_bad) {
3313 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10003314 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3315 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003316 /* treat as in-sync, but with a read error
3317 * which we can now try to correct
3318 */
3319 set_bit(R5_Insync, &dev->flags);
3320 set_bit(R5_ReadError, &dev->flags);
3321 }
3322 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003323 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003324 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003325 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003326 set_bit(R5_Insync, &dev->flags);
3327 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3328 test_bit(R5_Expanded, &dev->flags))
3329 /* If we've reshaped into here, we assume it is Insync.
3330 * We will shortly update recovery_offset to make
3331 * it official.
3332 */
3333 set_bit(R5_Insync, &dev->flags);
3334
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003335 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003336 /* This flag does not apply to '.replacement'
3337 * only to .rdev, so make sure to check that*/
3338 struct md_rdev *rdev2 = rcu_dereference(
3339 conf->disks[i].rdev);
3340 if (rdev2 == rdev)
3341 clear_bit(R5_Insync, &dev->flags);
3342 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003343 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003344 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003345 } else
3346 clear_bit(R5_WriteError, &dev->flags);
3347 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003348 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003349 /* This flag does not apply to '.replacement'
3350 * only to .rdev, so make sure to check that*/
3351 struct md_rdev *rdev2 = rcu_dereference(
3352 conf->disks[i].rdev);
3353 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003354 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003355 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003356 } else
3357 clear_bit(R5_MadeGood, &dev->flags);
3358 }
NeilBrown977df362011-12-23 10:17:53 +11003359 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3360 struct md_rdev *rdev2 = rcu_dereference(
3361 conf->disks[i].replacement);
3362 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3363 s->handle_bad_blocks = 1;
3364 atomic_inc(&rdev2->nr_pending);
3365 } else
3366 clear_bit(R5_MadeGoodRepl, &dev->flags);
3367 }
NeilBrown415e72d2010-06-17 17:25:21 +10003368 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003369 /* The ReadError flag will just be confusing now */
3370 clear_bit(R5_ReadError, &dev->flags);
3371 clear_bit(R5_ReWrite, &dev->flags);
3372 }
NeilBrown415e72d2010-06-17 17:25:21 +10003373 if (test_bit(R5_ReadError, &dev->flags))
3374 clear_bit(R5_Insync, &dev->flags);
3375 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003376 if (s->failed < 2)
3377 s->failed_num[s->failed] = i;
3378 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003379 if (rdev && !test_bit(Faulty, &rdev->flags))
3380 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003381 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003382 }
NeilBrown9a3e1102011-12-23 10:17:53 +11003383 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3384 /* If there is a failed device being replaced,
3385 * we must be recovering.
3386 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10003387 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11003388 * else we can only be replacing
3389 * sync and recovery both need to read all devices, and so
3390 * use the same flag.
3391 */
3392 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10003393 sh->sector >= conf->mddev->recovery_cp ||
3394 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11003395 s->syncing = 1;
3396 else
3397 s->replacing = 1;
3398 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003399 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003400}
NeilBrownf4168852007-02-28 20:11:53 -08003401
NeilBrowncc940152011-07-26 11:35:35 +10003402static void handle_stripe(struct stripe_head *sh)
3403{
3404 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003405 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003406 int i;
NeilBrown84789552011-07-27 11:00:36 +10003407 int prexor;
3408 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003409 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003410
3411 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003412 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003413 /* already being handled, ensure it gets handled
3414 * again when current action finishes */
3415 set_bit(STRIPE_HANDLE, &sh->state);
3416 return;
3417 }
3418
3419 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3420 set_bit(STRIPE_SYNCING, &sh->state);
3421 clear_bit(STRIPE_INSYNC, &sh->state);
3422 }
3423 clear_bit(STRIPE_DELAYED, &sh->state);
3424
3425 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3426 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3427 (unsigned long long)sh->sector, sh->state,
3428 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3429 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003430
NeilBrownacfe7262011-07-27 11:00:36 +10003431 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003432
NeilBrownbc2607f2011-07-28 11:39:22 +10003433 if (s.handle_bad_blocks) {
3434 set_bit(STRIPE_HANDLE, &sh->state);
3435 goto finish;
3436 }
3437
NeilBrown474af965fe2011-07-27 11:00:36 +10003438 if (unlikely(s.blocked_rdev)) {
3439 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003440 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003441 set_bit(STRIPE_HANDLE, &sh->state);
3442 goto finish;
3443 }
3444 /* There is nothing for the blocked_rdev to block */
3445 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3446 s.blocked_rdev = NULL;
3447 }
3448
3449 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3450 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3451 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3452 }
3453
3454 pr_debug("locked=%d uptodate=%d to_read=%d"
3455 " to_write=%d failed=%d failed_num=%d,%d\n",
3456 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3457 s.failed_num[0], s.failed_num[1]);
3458 /* check if the array has lost more than max_degraded devices and,
3459 * if so, some requests might need to be failed.
3460 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003461 if (s.failed > conf->max_degraded) {
3462 sh->check_state = 0;
3463 sh->reconstruct_state = 0;
3464 if (s.to_read+s.to_write+s.written)
3465 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003466 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003467 handle_failed_sync(conf, sh, &s);
3468 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003469
NeilBrown84789552011-07-27 11:00:36 +10003470 /* Now we check to see if any write operations have recently
3471 * completed
3472 */
3473 prexor = 0;
3474 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3475 prexor = 1;
3476 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3477 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3478 sh->reconstruct_state = reconstruct_state_idle;
3479
3480 /* All the 'written' buffers and the parity block are ready to
3481 * be written back to disk
3482 */
Shaohua Li9e444762012-10-11 13:49:49 +11003483 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
3484 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003485 BUG_ON(sh->qd_idx >= 0 &&
Shaohua Li9e444762012-10-11 13:49:49 +11003486 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
3487 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003488 for (i = disks; i--; ) {
3489 struct r5dev *dev = &sh->dev[i];
3490 if (test_bit(R5_LOCKED, &dev->flags) &&
3491 (i == sh->pd_idx || i == sh->qd_idx ||
3492 dev->written)) {
3493 pr_debug("Writing block %d\n", i);
3494 set_bit(R5_Wantwrite, &dev->flags);
3495 if (prexor)
3496 continue;
3497 if (!test_bit(R5_Insync, &dev->flags) ||
3498 ((i == sh->pd_idx || i == sh->qd_idx) &&
3499 s.failed == 0))
3500 set_bit(STRIPE_INSYNC, &sh->state);
3501 }
3502 }
3503 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3504 s.dec_preread_active = 1;
3505 }
3506
NeilBrownef5b7c62012-11-22 09:13:36 +11003507 /*
3508 * might be able to return some write requests if the parity blocks
3509 * are safe, or on a failed drive
3510 */
3511 pdev = &sh->dev[sh->pd_idx];
3512 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3513 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3514 qdev = &sh->dev[sh->qd_idx];
3515 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3516 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3517 || conf->level < 6;
3518
3519 if (s.written &&
3520 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3521 && !test_bit(R5_LOCKED, &pdev->flags)
3522 && (test_bit(R5_UPTODATE, &pdev->flags) ||
3523 test_bit(R5_Discard, &pdev->flags))))) &&
3524 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3525 && !test_bit(R5_LOCKED, &qdev->flags)
3526 && (test_bit(R5_UPTODATE, &qdev->flags) ||
3527 test_bit(R5_Discard, &qdev->flags))))))
3528 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3529
3530 /* Now we might consider reading some blocks, either to check/generate
3531 * parity, or to satisfy requests
3532 * or to load a block that is being partially written.
3533 */
3534 if (s.to_read || s.non_overwrite
3535 || (conf->level == 6 && s.to_write && s.failed)
3536 || (s.syncing && (s.uptodate + s.compute < disks))
3537 || s.replacing
3538 || s.expanding)
3539 handle_stripe_fill(sh, &s, disks);
3540
NeilBrown84789552011-07-27 11:00:36 +10003541 /* Now to consider new write requests and what else, if anything
3542 * should be read. We do not handle new writes when:
3543 * 1/ A 'write' operation (copy+xor) is already in flight.
3544 * 2/ A 'check' operation is in flight, as it may clobber the parity
3545 * block.
3546 */
3547 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3548 handle_stripe_dirtying(conf, sh, &s, disks);
3549
3550 /* maybe we need to check and possibly fix the parity for this stripe
3551 * Any reads will already have been scheduled, so we just see if enough
3552 * data is available. The parity check is held off while parity
3553 * dependent operations are in flight.
3554 */
3555 if (sh->check_state ||
3556 (s.syncing && s.locked == 0 &&
3557 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3558 !test_bit(STRIPE_INSYNC, &sh->state))) {
3559 if (conf->level == 6)
3560 handle_parity_checks6(conf, sh, &s, disks);
3561 else
3562 handle_parity_checks5(conf, sh, &s, disks);
3563 }
NeilBrownc5a31002011-07-27 11:00:36 +10003564
NeilBrown9a3e1102011-12-23 10:17:53 +11003565 if (s.replacing && s.locked == 0
3566 && !test_bit(STRIPE_INSYNC, &sh->state)) {
3567 /* Write out to replacement devices where possible */
3568 for (i = 0; i < conf->raid_disks; i++)
3569 if (test_bit(R5_UPTODATE, &sh->dev[i].flags) &&
3570 test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3571 set_bit(R5_WantReplace, &sh->dev[i].flags);
3572 set_bit(R5_LOCKED, &sh->dev[i].flags);
3573 s.locked++;
3574 }
3575 set_bit(STRIPE_INSYNC, &sh->state);
3576 }
3577 if ((s.syncing || s.replacing) && s.locked == 0 &&
3578 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003579 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3580 clear_bit(STRIPE_SYNCING, &sh->state);
3581 }
3582
3583 /* If the failed drives are just a ReadError, then we might need
3584 * to progress the repair/check process
3585 */
3586 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3587 for (i = 0; i < s.failed; i++) {
3588 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3589 if (test_bit(R5_ReadError, &dev->flags)
3590 && !test_bit(R5_LOCKED, &dev->flags)
3591 && test_bit(R5_UPTODATE, &dev->flags)
3592 ) {
3593 if (!test_bit(R5_ReWrite, &dev->flags)) {
3594 set_bit(R5_Wantwrite, &dev->flags);
3595 set_bit(R5_ReWrite, &dev->flags);
3596 set_bit(R5_LOCKED, &dev->flags);
3597 s.locked++;
3598 } else {
3599 /* let's read it back */
3600 set_bit(R5_Wantread, &dev->flags);
3601 set_bit(R5_LOCKED, &dev->flags);
3602 s.locked++;
3603 }
3604 }
3605 }
3606
3607
NeilBrown3687c062011-07-27 11:00:36 +10003608 /* Finish reconstruct operations initiated by the expansion process */
3609 if (sh->reconstruct_state == reconstruct_state_result) {
3610 struct stripe_head *sh_src
3611 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3612 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3613 /* sh cannot be written until sh_src has been read.
3614 * so arrange for sh to be delayed a little
3615 */
3616 set_bit(STRIPE_DELAYED, &sh->state);
3617 set_bit(STRIPE_HANDLE, &sh->state);
3618 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3619 &sh_src->state))
3620 atomic_inc(&conf->preread_active_stripes);
3621 release_stripe(sh_src);
3622 goto finish;
3623 }
3624 if (sh_src)
3625 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003626
NeilBrown3687c062011-07-27 11:00:36 +10003627 sh->reconstruct_state = reconstruct_state_idle;
3628 clear_bit(STRIPE_EXPANDING, &sh->state);
3629 for (i = conf->raid_disks; i--; ) {
3630 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3631 set_bit(R5_LOCKED, &sh->dev[i].flags);
3632 s.locked++;
3633 }
3634 }
3635
3636 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3637 !sh->reconstruct_state) {
3638 /* Need to write out all blocks after computing parity */
3639 sh->disks = conf->raid_disks;
3640 stripe_set_idx(sh->sector, conf, 0, sh);
3641 schedule_reconstruction(sh, &s, 1, 1);
3642 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3643 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3644 atomic_dec(&conf->reshape_stripes);
3645 wake_up(&conf->wait_for_overlap);
3646 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3647 }
3648
3649 if (s.expanding && s.locked == 0 &&
3650 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3651 handle_stripe_expansion(conf, sh);
3652
3653finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003654 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10003655 if (unlikely(s.blocked_rdev)) {
3656 if (conf->mddev->external)
3657 md_wait_for_blocked_rdev(s.blocked_rdev,
3658 conf->mddev);
3659 else
3660 /* Internal metadata will immediately
3661 * be written by raid5d, so we don't
3662 * need to wait here.
3663 */
3664 rdev_dec_pending(s.blocked_rdev,
3665 conf->mddev);
3666 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003667
NeilBrownbc2607f2011-07-28 11:39:22 +10003668 if (s.handle_bad_blocks)
3669 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003670 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003671 struct r5dev *dev = &sh->dev[i];
3672 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3673 /* We own a safe reference to the rdev */
3674 rdev = conf->disks[i].rdev;
3675 if (!rdev_set_badblocks(rdev, sh->sector,
3676 STRIPE_SECTORS, 0))
3677 md_error(conf->mddev, rdev);
3678 rdev_dec_pending(rdev, conf->mddev);
3679 }
NeilBrownb84db562011-07-28 11:39:23 +10003680 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3681 rdev = conf->disks[i].rdev;
3682 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003683 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10003684 rdev_dec_pending(rdev, conf->mddev);
3685 }
NeilBrown977df362011-12-23 10:17:53 +11003686 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3687 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11003688 if (!rdev)
3689 /* rdev have been moved down */
3690 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11003691 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003692 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11003693 rdev_dec_pending(rdev, conf->mddev);
3694 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003695 }
3696
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003697 if (s.ops_request)
3698 raid_run_ops(sh, s.ops_request);
3699
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003700 ops_run_io(sh, &s);
3701
NeilBrownc5709ef2011-07-26 11:35:20 +10003702 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003703 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003704 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003705 * have actually been submitted.
3706 */
3707 atomic_dec(&conf->preread_active_stripes);
3708 if (atomic_read(&conf->preread_active_stripes) <
3709 IO_THRESHOLD)
3710 md_wakeup_thread(conf->mddev->thread);
3711 }
3712
NeilBrownc5709ef2011-07-26 11:35:20 +10003713 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003714
Dan Williams257a4b42011-11-08 16:22:06 +11003715 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003716}
3717
NeilBrownd1688a62011-10-11 16:49:52 +11003718static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719{
3720 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3721 while (!list_empty(&conf->delayed_list)) {
3722 struct list_head *l = conf->delayed_list.next;
3723 struct stripe_head *sh;
3724 sh = list_entry(l, struct stripe_head, lru);
3725 list_del_init(l);
3726 clear_bit(STRIPE_DELAYED, &sh->state);
3727 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3728 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003729 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 }
NeilBrown482c0832011-04-18 18:25:42 +10003731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732}
3733
NeilBrownd1688a62011-10-11 16:49:52 +11003734static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003735{
3736 /* device_lock is held */
3737 struct list_head head;
3738 list_add(&head, &conf->bitmap_list);
3739 list_del_init(&conf->bitmap_list);
3740 while (!list_empty(&head)) {
3741 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3742 list_del_init(&sh->lru);
3743 atomic_inc(&sh->count);
3744 __release_stripe(conf, sh);
3745 }
3746}
3747
NeilBrownfd01b882011-10-11 16:47:53 +11003748int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003749{
NeilBrownd1688a62011-10-11 16:49:52 +11003750 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003751
3752 /* No difference between reads and writes. Just check
3753 * how busy the stripe_cache is
3754 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003755
NeilBrownf022b2f2006-10-03 01:15:56 -07003756 if (conf->inactive_blocked)
3757 return 1;
3758 if (conf->quiesce)
3759 return 1;
3760 if (list_empty_careful(&conf->inactive_list))
3761 return 1;
3762
3763 return 0;
3764}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003765EXPORT_SYMBOL_GPL(md_raid5_congested);
3766
3767static int raid5_congested(void *data, int bits)
3768{
NeilBrownfd01b882011-10-11 16:47:53 +11003769 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003770
3771 return mddev_congested(mddev, bits) ||
3772 md_raid5_congested(mddev, bits);
3773}
NeilBrownf022b2f2006-10-03 01:15:56 -07003774
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003775/* We want read requests to align with chunks where possible,
3776 * but write requests don't need to.
3777 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003778static int raid5_mergeable_bvec(struct request_queue *q,
3779 struct bvec_merge_data *bvm,
3780 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003781{
NeilBrownfd01b882011-10-11 16:47:53 +11003782 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003783 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003784 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003785 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003786 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003787
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003788 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003789 return biovec->bv_len; /* always allow writes to be mergeable */
3790
Andre Noll664e7c42009-06-18 08:45:27 +10003791 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3792 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003793 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3794 if (max < 0) max = 0;
3795 if (max <= biovec->bv_len && bio_sectors == 0)
3796 return biovec->bv_len;
3797 else
3798 return max;
3799}
3800
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003801
NeilBrownfd01b882011-10-11 16:47:53 +11003802static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003803{
3804 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003805 unsigned int chunk_sectors = mddev->chunk_sectors;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08003806 unsigned int bio_sectors = bio_sectors(bio);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003807
Andre Noll664e7c42009-06-18 08:45:27 +10003808 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3809 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003810 return chunk_sectors >=
3811 ((sector & (chunk_sectors - 1)) + bio_sectors);
3812}
3813
3814/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003815 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3816 * later sampled by raid5d.
3817 */
NeilBrownd1688a62011-10-11 16:49:52 +11003818static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003819{
3820 unsigned long flags;
3821
3822 spin_lock_irqsave(&conf->device_lock, flags);
3823
3824 bi->bi_next = conf->retry_read_aligned_list;
3825 conf->retry_read_aligned_list = bi;
3826
3827 spin_unlock_irqrestore(&conf->device_lock, flags);
3828 md_wakeup_thread(conf->mddev->thread);
3829}
3830
3831
NeilBrownd1688a62011-10-11 16:49:52 +11003832static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003833{
3834 struct bio *bi;
3835
3836 bi = conf->retry_read_aligned;
3837 if (bi) {
3838 conf->retry_read_aligned = NULL;
3839 return bi;
3840 }
3841 bi = conf->retry_read_aligned_list;
3842 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003843 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003844 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003845 /*
3846 * this sets the active strip count to 1 and the processed
3847 * strip count to zero (upper 8 bits)
3848 */
Shaohua Lie7836bd62012-07-19 16:01:31 +10003849 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003850 }
3851
3852 return bi;
3853}
3854
3855
3856/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003857 * The "raid5_align_endio" should check if the read succeeded and if it
3858 * did, call bio_endio on the original bio (having bio_put the new bio
3859 * first).
3860 * If the read failed..
3861 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003862static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003863{
3864 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003865 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003866 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003867 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003868 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003869
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003870 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003871
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003872 rdev = (void*)raid_bi->bi_next;
3873 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003874 mddev = rdev->mddev;
3875 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003876
3877 rdev_dec_pending(rdev, conf->mddev);
3878
3879 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003880 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003881 if (atomic_dec_and_test(&conf->active_aligned_reads))
3882 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003883 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003884 }
3885
3886
Dan Williams45b42332007-07-09 11:56:43 -07003887 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003888
3889 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003890}
3891
Neil Brown387bb172007-02-08 14:20:29 -08003892static int bio_fits_rdev(struct bio *bi)
3893{
Jens Axboe165125e2007-07-24 09:28:11 +02003894 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003895
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08003896 if (bio_sectors(bi) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003897 return 0;
3898 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003899 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003900 return 0;
3901
3902 if (q->merge_bvec_fn)
3903 /* it's too hard to apply the merge_bvec_fn at this stage,
3904 * just just give up
3905 */
3906 return 0;
3907
3908 return 1;
3909}
3910
3911
NeilBrownfd01b882011-10-11 16:47:53 +11003912static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003913{
NeilBrownd1688a62011-10-11 16:49:52 +11003914 struct r5conf *conf = mddev->private;
NeilBrown8553fe72009-12-14 12:49:47 +11003915 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003916 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11003917 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11003918 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003919
3920 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003921 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003922 return 0;
3923 }
3924 /*
NeilBrowna167f662010-10-26 18:31:13 +11003925 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003926 */
NeilBrowna167f662010-10-26 18:31:13 +11003927 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003928 if (!align_bi)
3929 return 0;
3930 /*
3931 * set bi_end_io to a new function, and set bi_private to the
3932 * original bio.
3933 */
3934 align_bi->bi_end_io = raid5_align_endio;
3935 align_bi->bi_private = raid_bio;
3936 /*
3937 * compute position
3938 */
NeilBrown112bf892009-03-31 14:39:38 +11003939 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3940 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003941 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003942
Kent Overstreetf73a1c72012-09-25 15:05:12 -07003943 end_sector = bio_end_sector(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003944 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11003945 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
3946 if (!rdev || test_bit(Faulty, &rdev->flags) ||
3947 rdev->recovery_offset < end_sector) {
3948 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3949 if (rdev &&
3950 (test_bit(Faulty, &rdev->flags) ||
3951 !(test_bit(In_sync, &rdev->flags) ||
3952 rdev->recovery_offset >= end_sector)))
3953 rdev = NULL;
3954 }
3955 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10003956 sector_t first_bad;
3957 int bad_sectors;
3958
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003959 atomic_inc(&rdev->nr_pending);
3960 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003961 raid_bio->bi_next = (void*)rdev;
3962 align_bi->bi_bdev = rdev->bdev;
3963 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003964
NeilBrown31c176e2011-07-28 11:39:22 +10003965 if (!bio_fits_rdev(align_bi) ||
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08003966 is_badblock(rdev, align_bi->bi_sector, bio_sectors(align_bi),
NeilBrown31c176e2011-07-28 11:39:22 +10003967 &first_bad, &bad_sectors)) {
3968 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08003969 bio_put(align_bi);
3970 rdev_dec_pending(rdev, mddev);
3971 return 0;
3972 }
3973
majianpeng6c0544e2012-06-12 08:31:10 +08003974 /* No reshape active, so we can trust rdev->data_offset */
3975 align_bi->bi_sector += rdev->data_offset;
3976
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003977 spin_lock_irq(&conf->device_lock);
3978 wait_event_lock_irq(conf->wait_for_stripe,
3979 conf->quiesce == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01003980 conf->device_lock);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003981 atomic_inc(&conf->active_aligned_reads);
3982 spin_unlock_irq(&conf->device_lock);
3983
NeilBrowna9add5d2012-10-31 11:59:09 +11003984 trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev),
3985 align_bi, disk_devt(mddev->gendisk),
3986 raid_bio->bi_sector);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003987 generic_make_request(align_bi);
3988 return 1;
3989 } else {
3990 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003991 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003992 return 0;
3993 }
3994}
3995
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003996/* __get_priority_stripe - get the next stripe to process
3997 *
3998 * Full stripe writes are allowed to pass preread active stripes up until
3999 * the bypass_threshold is exceeded. In general the bypass_count
4000 * increments when the handle_list is handled before the hold_list; however, it
4001 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
4002 * stripe with in flight i/o. The bypass_count will be reset when the
4003 * head of the hold_list has changed, i.e. the head was promoted to the
4004 * handle_list.
4005 */
NeilBrownd1688a62011-10-11 16:49:52 +11004006static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004007{
4008 struct stripe_head *sh;
4009
4010 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
4011 __func__,
4012 list_empty(&conf->handle_list) ? "empty" : "busy",
4013 list_empty(&conf->hold_list) ? "empty" : "busy",
4014 atomic_read(&conf->pending_full_writes), conf->bypass_count);
4015
4016 if (!list_empty(&conf->handle_list)) {
4017 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
4018
4019 if (list_empty(&conf->hold_list))
4020 conf->bypass_count = 0;
4021 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
4022 if (conf->hold_list.next == conf->last_hold)
4023 conf->bypass_count++;
4024 else {
4025 conf->last_hold = conf->hold_list.next;
4026 conf->bypass_count -= conf->bypass_threshold;
4027 if (conf->bypass_count < 0)
4028 conf->bypass_count = 0;
4029 }
4030 }
4031 } else if (!list_empty(&conf->hold_list) &&
4032 ((conf->bypass_threshold &&
4033 conf->bypass_count > conf->bypass_threshold) ||
4034 atomic_read(&conf->pending_full_writes) == 0)) {
4035 sh = list_entry(conf->hold_list.next,
4036 typeof(*sh), lru);
4037 conf->bypass_count -= conf->bypass_threshold;
4038 if (conf->bypass_count < 0)
4039 conf->bypass_count = 0;
4040 } else
4041 return NULL;
4042
4043 list_del_init(&sh->lru);
4044 atomic_inc(&sh->count);
4045 BUG_ON(atomic_read(&sh->count) != 1);
4046 return sh;
4047}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004048
Shaohua Li8811b592012-08-02 08:33:00 +10004049struct raid5_plug_cb {
4050 struct blk_plug_cb cb;
4051 struct list_head list;
4052};
4053
4054static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
4055{
4056 struct raid5_plug_cb *cb = container_of(
4057 blk_cb, struct raid5_plug_cb, cb);
4058 struct stripe_head *sh;
4059 struct mddev *mddev = cb->cb.data;
4060 struct r5conf *conf = mddev->private;
NeilBrowna9add5d2012-10-31 11:59:09 +11004061 int cnt = 0;
Shaohua Li8811b592012-08-02 08:33:00 +10004062
4063 if (cb->list.next && !list_empty(&cb->list)) {
4064 spin_lock_irq(&conf->device_lock);
4065 while (!list_empty(&cb->list)) {
4066 sh = list_first_entry(&cb->list, struct stripe_head, lru);
4067 list_del_init(&sh->lru);
4068 /*
4069 * avoid race release_stripe_plug() sees
4070 * STRIPE_ON_UNPLUG_LIST clear but the stripe
4071 * is still in our list
4072 */
4073 smp_mb__before_clear_bit();
4074 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
4075 __release_stripe(conf, sh);
NeilBrowna9add5d2012-10-31 11:59:09 +11004076 cnt++;
Shaohua Li8811b592012-08-02 08:33:00 +10004077 }
4078 spin_unlock_irq(&conf->device_lock);
4079 }
NeilBrowna9add5d2012-10-31 11:59:09 +11004080 trace_block_unplug(mddev->queue, cnt, !from_schedule);
Shaohua Li8811b592012-08-02 08:33:00 +10004081 kfree(cb);
4082}
4083
4084static void release_stripe_plug(struct mddev *mddev,
4085 struct stripe_head *sh)
4086{
4087 struct blk_plug_cb *blk_cb = blk_check_plugged(
4088 raid5_unplug, mddev,
4089 sizeof(struct raid5_plug_cb));
4090 struct raid5_plug_cb *cb;
4091
4092 if (!blk_cb) {
4093 release_stripe(sh);
4094 return;
4095 }
4096
4097 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
4098
4099 if (cb->list.next == NULL)
4100 INIT_LIST_HEAD(&cb->list);
4101
4102 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
4103 list_add_tail(&sh->lru, &cb->list);
4104 else
4105 release_stripe(sh);
4106}
4107
Shaohua Li620125f2012-10-11 13:49:05 +11004108static void make_discard_request(struct mddev *mddev, struct bio *bi)
4109{
4110 struct r5conf *conf = mddev->private;
4111 sector_t logical_sector, last_sector;
4112 struct stripe_head *sh;
4113 int remaining;
4114 int stripe_sectors;
4115
4116 if (mddev->reshape_position != MaxSector)
4117 /* Skip discard while reshape is happening */
4118 return;
4119
4120 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4121 last_sector = bi->bi_sector + (bi->bi_size>>9);
4122
4123 bi->bi_next = NULL;
4124 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
4125
4126 stripe_sectors = conf->chunk_sectors *
4127 (conf->raid_disks - conf->max_degraded);
4128 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
4129 stripe_sectors);
4130 sector_div(last_sector, stripe_sectors);
4131
4132 logical_sector *= conf->chunk_sectors;
4133 last_sector *= conf->chunk_sectors;
4134
4135 for (; logical_sector < last_sector;
4136 logical_sector += STRIPE_SECTORS) {
4137 DEFINE_WAIT(w);
4138 int d;
4139 again:
4140 sh = get_active_stripe(conf, logical_sector, 0, 0, 0);
4141 prepare_to_wait(&conf->wait_for_overlap, &w,
4142 TASK_UNINTERRUPTIBLE);
4143 spin_lock_irq(&sh->stripe_lock);
4144 for (d = 0; d < conf->raid_disks; d++) {
4145 if (d == sh->pd_idx || d == sh->qd_idx)
4146 continue;
4147 if (sh->dev[d].towrite || sh->dev[d].toread) {
4148 set_bit(R5_Overlap, &sh->dev[d].flags);
4149 spin_unlock_irq(&sh->stripe_lock);
4150 release_stripe(sh);
4151 schedule();
4152 goto again;
4153 }
4154 }
4155 finish_wait(&conf->wait_for_overlap, &w);
4156 for (d = 0; d < conf->raid_disks; d++) {
4157 if (d == sh->pd_idx || d == sh->qd_idx)
4158 continue;
4159 sh->dev[d].towrite = bi;
4160 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
4161 raid5_inc_bi_active_stripes(bi);
4162 }
4163 spin_unlock_irq(&sh->stripe_lock);
4164 if (conf->mddev->bitmap) {
4165 for (d = 0;
4166 d < conf->raid_disks - conf->max_degraded;
4167 d++)
4168 bitmap_startwrite(mddev->bitmap,
4169 sh->sector,
4170 STRIPE_SECTORS,
4171 0);
4172 sh->bm_seq = conf->seq_flush + 1;
4173 set_bit(STRIPE_BIT_DELAY, &sh->state);
4174 }
4175
4176 set_bit(STRIPE_HANDLE, &sh->state);
4177 clear_bit(STRIPE_DELAYED, &sh->state);
4178 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4179 atomic_inc(&conf->preread_active_stripes);
4180 release_stripe_plug(mddev, sh);
4181 }
4182
4183 remaining = raid5_dec_bi_active_stripes(bi);
4184 if (remaining == 0) {
4185 md_write_end(mddev);
4186 bio_endio(bi, 0);
4187 }
4188}
4189
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07004190static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191{
NeilBrownd1688a62011-10-11 16:49:52 +11004192 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11004193 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 sector_t new_sector;
4195 sector_t logical_sector, last_sector;
4196 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01004197 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11004198 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199
Tejun Heoe9c74692010-09-03 11:56:18 +02004200 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
4201 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004202 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07004203 }
4204
NeilBrown3d310eb2005-06-21 17:17:26 -07004205 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07004206
NeilBrown802ba062006-12-13 00:34:13 -08004207 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004208 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11004209 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004210 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004211
Shaohua Li620125f2012-10-11 13:49:05 +11004212 if (unlikely(bi->bi_rw & REQ_DISCARD)) {
4213 make_discard_request(mddev, bi);
4214 return;
4215 }
4216
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004218 last_sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 bi->bi_next = NULL;
4220 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07004221
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
4223 DEFINE_WAIT(w);
NeilBrownb5663ba2009-03-31 14:39:38 +11004224 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08004225
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004226 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11004227 previous = 0;
NeilBrownb578d552006-03-27 01:18:12 -08004228 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004229 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11004230 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08004231 * 64bit on a 32bit platform, and so it might be
4232 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02004233 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08004234 * the lock is dropped, so once we get a reference
4235 * to the stripe that we think it is, we will have
4236 * to check again.
4237 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004238 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004239 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004240 ? logical_sector < conf->reshape_progress
4241 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004242 previous = 1;
4243 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10004244 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004245 ? logical_sector < conf->reshape_safe
4246 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08004247 spin_unlock_irq(&conf->device_lock);
4248 schedule();
4249 goto retry;
4250 }
4251 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004252 spin_unlock_irq(&conf->device_lock);
4253 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004254
NeilBrown112bf892009-03-31 14:39:38 +11004255 new_sector = raid5_compute_sector(conf, logical_sector,
4256 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11004257 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10004258 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 (unsigned long long)new_sector,
4260 (unsigned long long)logical_sector);
4261
NeilBrownb5663ba2009-03-31 14:39:38 +11004262 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10004263 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004265 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004266 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08004267 * stripe, so we must do the range check again.
4268 * Expansion could still move past after this
4269 * test, but as we are holding a reference to
4270 * 'sh', we know that if that happens,
4271 * STRIPE_EXPANDING will get set and the expansion
4272 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004273 */
4274 int must_retry = 0;
4275 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004276 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11004277 ? logical_sector >= conf->reshape_progress
4278 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004279 /* mismatch, need to try again */
4280 must_retry = 1;
4281 spin_unlock_irq(&conf->device_lock);
4282 if (must_retry) {
4283 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004284 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004285 goto retry;
4286 }
4287 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004288
Namhyung Kimffd96e32011-07-18 17:38:51 +10004289 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004290 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004291 logical_sector < mddev->suspend_hi) {
4292 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004293 /* As the suspend_* range is controlled by
4294 * userspace, we want an interruptible
4295 * wait.
4296 */
4297 flush_signals(current);
4298 prepare_to_wait(&conf->wait_for_overlap,
4299 &w, TASK_INTERRUPTIBLE);
4300 if (logical_sector >= mddev->suspend_lo &&
4301 logical_sector < mddev->suspend_hi)
4302 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004303 goto retry;
4304 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004305
4306 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004307 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004308 /* Stripe is busy expanding or
4309 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 * and wait a while
4311 */
NeilBrown482c0832011-04-18 18:25:42 +10004312 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 release_stripe(sh);
4314 schedule();
4315 goto retry;
4316 }
4317 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004318 set_bit(STRIPE_HANDLE, &sh->state);
4319 clear_bit(STRIPE_DELAYED, &sh->state);
NeilBrowna852d7b2012-09-19 12:48:30 +10004320 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004321 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4322 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10004323 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 } else {
4325 /* cannot get stripe for read-ahead, just give-up */
4326 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4327 finish_wait(&conf->wait_for_overlap, &w);
4328 break;
4329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004331
Shaohua Lie7836bd62012-07-19 16:01:31 +10004332 remaining = raid5_dec_bi_active_stripes(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004333 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334
NeilBrown16a53ec2006-06-26 00:27:38 -07004335 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004337
Neil Brown0e13fe232008-06-28 08:31:20 +10004338 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340}
4341
NeilBrownfd01b882011-10-11 16:47:53 +11004342static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004343
NeilBrownfd01b882011-10-11 16:47:53 +11004344static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345{
NeilBrown52c03292006-06-26 00:27:43 -07004346 /* reshaping is quite different to recovery/resync so it is
4347 * handled quite separately ... here.
4348 *
4349 * On each call to sync_request, we gather one chunk worth of
4350 * destination stripes and flag them as expanding.
4351 * Then we find all the source stripes and request reads.
4352 * As the reads complete, handle_stripe will copy the data
4353 * into the destination stripe and release that stripe.
4354 */
NeilBrownd1688a62011-10-11 16:49:52 +11004355 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004357 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004358 int raid_disks = conf->previous_raid_disks;
4359 int data_disks = raid_disks - conf->max_degraded;
4360 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004361 int i;
4362 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004363 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004364 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004365 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004366 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004367
NeilBrownfef9c612009-03-31 15:16:46 +11004368 if (sector_nr == 0) {
4369 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10004370 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004371 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4372 sector_nr = raid5_size(mddev, 0, 0)
4373 - conf->reshape_progress;
NeilBrown2c810cd2012-05-21 09:27:00 +10004374 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004375 conf->reshape_progress > 0)
4376 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004377 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004378 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004379 mddev->curr_resync_completed = sector_nr;
4380 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004381 *skipped = 1;
4382 return sector_nr;
4383 }
NeilBrown52c03292006-06-26 00:27:43 -07004384 }
4385
NeilBrown7a661382009-03-31 15:21:40 +11004386 /* We need to process a full chunk at a time.
4387 * If old and new chunk sizes differ, we need to process the
4388 * largest of these
4389 */
Andre Noll664e7c42009-06-18 08:45:27 +10004390 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4391 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004392 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004393 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004394
NeilBrownb5254dd2012-05-21 09:27:01 +10004395 /* We update the metadata at least every 10 seconds, or when
4396 * the data about to be copied would over-write the source of
4397 * the data at the front of the range. i.e. one new_stripe
4398 * along from reshape_progress new_maps to after where
4399 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004400 */
NeilBrownfef9c612009-03-31 15:16:46 +11004401 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004402 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004403 readpos = conf->reshape_progress;
4404 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004405 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004406 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10004407 if (mddev->reshape_backwards) {
NeilBrowned37d832009-05-27 21:39:05 +10004408 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004409 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004410 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004411 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004412 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004413 readpos -= min_t(sector_t, reshape_sectors, readpos);
4414 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004415 }
NeilBrown52c03292006-06-26 00:27:43 -07004416
NeilBrownb5254dd2012-05-21 09:27:01 +10004417 /* Having calculated the 'writepos' possibly use it
4418 * to set 'stripe_addr' which is where we will write to.
4419 */
4420 if (mddev->reshape_backwards) {
4421 BUG_ON(conf->reshape_progress == 0);
4422 stripe_addr = writepos;
4423 BUG_ON((mddev->dev_sectors &
4424 ~((sector_t)reshape_sectors - 1))
4425 - reshape_sectors - stripe_addr
4426 != sector_nr);
4427 } else {
4428 BUG_ON(writepos != sector_nr + reshape_sectors);
4429 stripe_addr = sector_nr;
4430 }
4431
NeilBrownc8f517c2009-03-31 15:28:40 +11004432 /* 'writepos' is the most advanced device address we might write.
4433 * 'readpos' is the least advanced device address we might read.
4434 * 'safepos' is the least address recorded in the metadata as having
4435 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10004436 * If there is a min_offset_diff, these are adjusted either by
4437 * increasing the safepos/readpos if diff is negative, or
4438 * increasing writepos if diff is positive.
4439 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11004440 * ensure safety in the face of a crash - that must be done by userspace
4441 * making a backup of the data. So in that case there is no particular
4442 * rush to update metadata.
4443 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4444 * update the metadata to advance 'safepos' to match 'readpos' so that
4445 * we can be safe in the event of a crash.
4446 * So we insist on updating metadata if safepos is behind writepos and
4447 * readpos is beyond writepos.
4448 * In any case, update the metadata every 10 seconds.
4449 * Maybe that number should be configurable, but I'm not sure it is
4450 * worth it.... maybe it could be a multiple of safemode_delay???
4451 */
NeilBrownb5254dd2012-05-21 09:27:01 +10004452 if (conf->min_offset_diff < 0) {
4453 safepos += -conf->min_offset_diff;
4454 readpos += -conf->min_offset_diff;
4455 } else
4456 writepos += conf->min_offset_diff;
4457
NeilBrown2c810cd2012-05-21 09:27:00 +10004458 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11004459 ? (safepos > writepos && readpos < writepos)
4460 : (safepos < writepos && readpos > writepos)) ||
4461 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004462 /* Cannot proceed until we've updated the superblock... */
4463 wait_event(conf->wait_for_overlap,
4464 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004465 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004466 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004467 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07004468 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004469 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07004470 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004471 kthread_should_stop());
4472 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004473 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004474 spin_unlock_irq(&conf->device_lock);
4475 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004476 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004477 }
4478
NeilBrownab69ae12009-03-31 15:26:47 +11004479 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004480 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004481 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004482 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004483 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004484 set_bit(STRIPE_EXPANDING, &sh->state);
4485 atomic_inc(&conf->reshape_stripes);
4486 /* If any of this stripe is beyond the end of the old
4487 * array, then we need to zero those blocks
4488 */
4489 for (j=sh->disks; j--;) {
4490 sector_t s;
4491 if (j == sh->pd_idx)
4492 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004493 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004494 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004495 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004496 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004497 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004498 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004499 continue;
4500 }
4501 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4502 set_bit(R5_Expanded, &sh->dev[j].flags);
4503 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4504 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004505 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004506 set_bit(STRIPE_EXPAND_READY, &sh->state);
4507 set_bit(STRIPE_HANDLE, &sh->state);
4508 }
NeilBrownab69ae12009-03-31 15:26:47 +11004509 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004510 }
4511 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004512 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11004513 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004514 else
NeilBrown7a661382009-03-31 15:21:40 +11004515 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004516 spin_unlock_irq(&conf->device_lock);
4517 /* Ok, those stripe are ready. We can start scheduling
4518 * reads on the source stripes.
4519 * The source stripes are determined by mapping the first and last
4520 * block on the destination stripes.
4521 */
NeilBrown52c03292006-06-26 00:27:43 -07004522 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004523 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004524 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004525 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004526 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004527 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004528 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004529 if (last_sector >= mddev->dev_sectors)
4530 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004531 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004532 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004533 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4534 set_bit(STRIPE_HANDLE, &sh->state);
4535 release_stripe(sh);
4536 first_sector += STRIPE_SECTORS;
4537 }
NeilBrownab69ae12009-03-31 15:26:47 +11004538 /* Now that the sources are clearly marked, we can release
4539 * the destination stripes
4540 */
4541 while (!list_empty(&stripes)) {
4542 sh = list_entry(stripes.next, struct stripe_head, lru);
4543 list_del_init(&sh->lru);
4544 release_stripe(sh);
4545 }
NeilBrownc6207272008-02-06 01:39:52 -08004546 /* If this takes us to the resync_max point where we have to pause,
4547 * then we need to write out the superblock.
4548 */
NeilBrown7a661382009-03-31 15:21:40 +11004549 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004550 if ((sector_nr - mddev->curr_resync_completed) * 2
4551 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004552 /* Cannot proceed until we've updated the superblock... */
4553 wait_event(conf->wait_for_overlap,
4554 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004555 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004556 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004557 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004558 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4559 md_wakeup_thread(mddev->thread);
4560 wait_event(mddev->sb_wait,
4561 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4562 || kthread_should_stop());
4563 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004564 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004565 spin_unlock_irq(&conf->device_lock);
4566 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004567 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004568 }
NeilBrown7a661382009-03-31 15:21:40 +11004569 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004570}
4571
4572/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004573static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004574{
NeilBrownd1688a62011-10-11 16:49:52 +11004575 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004576 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004577 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004578 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004579 int still_degraded = 0;
4580 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581
NeilBrown72626682005-09-09 16:23:54 -07004582 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004584
NeilBrown29269552006-03-27 01:18:10 -08004585 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4586 end_reshape(conf);
4587 return 0;
4588 }
NeilBrown72626682005-09-09 16:23:54 -07004589
4590 if (mddev->curr_resync < max_sector) /* aborted */
4591 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4592 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004593 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004594 conf->fullsync = 0;
4595 bitmap_close_sync(mddev->bitmap);
4596
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 return 0;
4598 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004599
NeilBrown64bd6602009-08-03 10:59:58 +10004600 /* Allow raid5_quiesce to complete */
4601 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4602
NeilBrown52c03292006-06-26 00:27:43 -07004603 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4604 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004605
NeilBrownc6207272008-02-06 01:39:52 -08004606 /* No need to check resync_max as we never do more than one
4607 * stripe, and as resync_max will always be on a chunk boundary,
4608 * if the check in md_do_sync didn't fire, there is no chance
4609 * of overstepping resync_max here
4610 */
4611
NeilBrown16a53ec2006-06-26 00:27:38 -07004612 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 * to resync, then assert that we are finished, because there is
4614 * nothing we can do.
4615 */
NeilBrown3285edf2006-06-26 00:27:55 -07004616 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004617 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004618 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004619 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 return rv;
4621 }
NeilBrown72626682005-09-09 16:23:54 -07004622 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004623 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004624 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4625 /* we can skip this block, and probably more */
4626 sync_blocks /= STRIPE_SECTORS;
4627 *skipped = 1;
4628 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630
NeilBrownb47490c2008-02-06 01:39:50 -08004631 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4632
NeilBrowna8c906c2009-06-09 14:39:59 +10004633 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004635 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004637 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004639 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004641 /* Need to check if array will still be degraded after recovery/resync
4642 * We don't need to check the 'failed' flag as when that gets set,
4643 * recovery aborts.
4644 */
NeilBrownf001a702009-06-09 14:30:31 +10004645 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004646 if (conf->disks[i].rdev == NULL)
4647 still_degraded = 1;
4648
4649 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4650
NeilBrown83206d62011-07-26 11:19:49 +10004651 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652
NeilBrown14425772009-10-16 15:55:25 +11004653 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 release_stripe(sh);
4655
4656 return STRIPE_SECTORS;
4657}
4658
NeilBrownd1688a62011-10-11 16:49:52 +11004659static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004660{
4661 /* We may not be able to submit a whole bio at once as there
4662 * may not be enough stripe_heads available.
4663 * We cannot pre-allocate enough stripe_heads as we may need
4664 * more than exist in the cache (if we allow ever large chunks).
4665 * So we do one stripe head at a time and record in
4666 * ->bi_hw_segments how many have been done.
4667 *
4668 * We *know* that this entire raid_bio is in one chunk, so
4669 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4670 */
4671 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004672 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004673 sector_t sector, logical_sector, last_sector;
4674 int scnt = 0;
4675 int remaining;
4676 int handled = 0;
4677
4678 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004679 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004680 0, &dd_idx, NULL);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004681 last_sector = bio_end_sector(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004682
4683 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004684 logical_sector += STRIPE_SECTORS,
4685 sector += STRIPE_SECTORS,
4686 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004687
Shaohua Lie7836bd62012-07-19 16:01:31 +10004688 if (scnt < raid5_bi_processed_stripes(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004689 /* already done this stripe */
4690 continue;
4691
NeilBrowna8c906c2009-06-09 14:39:59 +10004692 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004693
4694 if (!sh) {
4695 /* failed to get a stripe - must wait */
Shaohua Lie7836bd62012-07-19 16:01:31 +10004696 raid5_set_bi_processed_stripes(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004697 conf->retry_read_aligned = raid_bio;
4698 return handled;
4699 }
4700
Neil Brown387bb172007-02-08 14:20:29 -08004701 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4702 release_stripe(sh);
Shaohua Lie7836bd62012-07-19 16:01:31 +10004703 raid5_set_bi_processed_stripes(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004704 conf->retry_read_aligned = raid_bio;
4705 return handled;
4706 }
4707
majianpeng3f9e7c12012-07-31 10:04:21 +10004708 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07004709 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004710 release_stripe(sh);
4711 handled++;
4712 }
Shaohua Lie7836bd62012-07-19 16:01:31 +10004713 remaining = raid5_dec_bi_active_stripes(raid_bio);
Tejun Heo3a366e62013-01-11 13:06:33 -08004714 if (remaining == 0)
Neil Brown0e13fe232008-06-28 08:31:20 +10004715 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004716 if (atomic_dec_and_test(&conf->active_aligned_reads))
4717 wake_up(&conf->wait_for_stripe);
4718 return handled;
4719}
4720
Shaohua Li46a06402012-08-02 08:33:15 +10004721#define MAX_STRIPE_BATCH 8
4722static int handle_active_stripes(struct r5conf *conf)
4723{
4724 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
4725 int i, batch_size = 0;
4726
4727 while (batch_size < MAX_STRIPE_BATCH &&
4728 (sh = __get_priority_stripe(conf)) != NULL)
4729 batch[batch_size++] = sh;
4730
4731 if (batch_size == 0)
4732 return batch_size;
4733 spin_unlock_irq(&conf->device_lock);
4734
4735 for (i = 0; i < batch_size; i++)
4736 handle_stripe(batch[i]);
4737
4738 cond_resched();
4739
4740 spin_lock_irq(&conf->device_lock);
4741 for (i = 0; i < batch_size; i++)
4742 __release_stripe(conf, batch[i]);
4743 return batch_size;
4744}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004745
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746/*
4747 * This is our raid5 kernel thread.
4748 *
4749 * We scan the hash table for stripes which can be handled now.
4750 * During the scan, completed stripes are saved for us by the interrupt
4751 * handler, so that they will not have to wait for our next wakeup.
4752 */
Shaohua Li4ed87312012-10-11 13:34:00 +11004753static void raid5d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754{
Shaohua Li4ed87312012-10-11 13:34:00 +11004755 struct mddev *mddev = thread->mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11004756 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004758 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759
Dan Williams45b42332007-07-09 11:56:43 -07004760 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761
4762 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004764 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 handled = 0;
4766 spin_lock_irq(&conf->device_lock);
4767 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004768 struct bio *bio;
Shaohua Li46a06402012-08-02 08:33:15 +10004769 int batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770
NeilBrown0021b7b2012-07-31 09:08:14 +02004771 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10004772 !list_empty(&conf->bitmap_list)) {
4773 /* Now is a good time to flush some bitmap updates */
4774 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004775 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004776 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004777 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004778 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004779 activate_bit_delay(conf);
4780 }
NeilBrown0021b7b2012-07-31 09:08:14 +02004781 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004782
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004783 while ((bio = remove_bio_from_retry(conf))) {
4784 int ok;
4785 spin_unlock_irq(&conf->device_lock);
4786 ok = retry_aligned_read(conf, bio);
4787 spin_lock_irq(&conf->device_lock);
4788 if (!ok)
4789 break;
4790 handled++;
4791 }
4792
Shaohua Li46a06402012-08-02 08:33:15 +10004793 batch_size = handle_active_stripes(conf);
4794 if (!batch_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 break;
Shaohua Li46a06402012-08-02 08:33:15 +10004796 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797
Shaohua Li46a06402012-08-02 08:33:15 +10004798 if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) {
4799 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10004800 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10004801 spin_lock_irq(&conf->device_lock);
4802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 }
Dan Williams45b42332007-07-09 11:56:43 -07004804 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805
4806 spin_unlock_irq(&conf->device_lock);
4807
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004808 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004809 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810
Dan Williams45b42332007-07-09 11:56:43 -07004811 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812}
4813
NeilBrown3f294f42005-11-08 21:39:25 -08004814static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004815raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004816{
NeilBrownd1688a62011-10-11 16:49:52 +11004817 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004818 if (conf)
4819 return sprintf(page, "%d\n", conf->max_nr_stripes);
4820 else
4821 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004822}
4823
NeilBrownc41d4ac2010-06-01 19:37:24 +10004824int
NeilBrownfd01b882011-10-11 16:47:53 +11004825raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004826{
NeilBrownd1688a62011-10-11 16:49:52 +11004827 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004828 int err;
4829
4830 if (size <= 16 || size > 32768)
4831 return -EINVAL;
4832 while (size < conf->max_nr_stripes) {
4833 if (drop_one_stripe(conf))
4834 conf->max_nr_stripes--;
4835 else
4836 break;
4837 }
4838 err = md_allow_write(mddev);
4839 if (err)
4840 return err;
4841 while (size > conf->max_nr_stripes) {
4842 if (grow_one_stripe(conf))
4843 conf->max_nr_stripes++;
4844 else break;
4845 }
4846 return 0;
4847}
4848EXPORT_SYMBOL(raid5_set_cache_size);
4849
NeilBrown3f294f42005-11-08 21:39:25 -08004850static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004851raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004852{
NeilBrownd1688a62011-10-11 16:49:52 +11004853 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004854 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004855 int err;
4856
NeilBrown3f294f42005-11-08 21:39:25 -08004857 if (len >= PAGE_SIZE)
4858 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004859 if (!conf)
4860 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004861
Dan Williams4ef197d82008-04-28 02:15:54 -07004862 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004863 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004864 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004865 if (err)
4866 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004867 return len;
4868}
NeilBrown007583c2005-11-08 21:39:30 -08004869
NeilBrown96de1e62005-11-08 21:39:39 -08004870static struct md_sysfs_entry
4871raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4872 raid5_show_stripe_cache_size,
4873 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004874
4875static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004876raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004877{
NeilBrownd1688a62011-10-11 16:49:52 +11004878 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004879 if (conf)
4880 return sprintf(page, "%d\n", conf->bypass_threshold);
4881 else
4882 return 0;
4883}
4884
4885static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004886raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004887{
NeilBrownd1688a62011-10-11 16:49:52 +11004888 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004889 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004890 if (len >= PAGE_SIZE)
4891 return -EINVAL;
4892 if (!conf)
4893 return -ENODEV;
4894
Dan Williams4ef197d82008-04-28 02:15:54 -07004895 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004896 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004897 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004898 return -EINVAL;
4899 conf->bypass_threshold = new;
4900 return len;
4901}
4902
4903static struct md_sysfs_entry
4904raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4905 S_IRUGO | S_IWUSR,
4906 raid5_show_preread_threshold,
4907 raid5_store_preread_threshold);
4908
4909static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004910stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004911{
NeilBrownd1688a62011-10-11 16:49:52 +11004912 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004913 if (conf)
4914 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4915 else
4916 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004917}
4918
NeilBrown96de1e62005-11-08 21:39:39 -08004919static struct md_sysfs_entry
4920raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004921
NeilBrown007583c2005-11-08 21:39:30 -08004922static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004923 &raid5_stripecache_size.attr,
4924 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004925 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004926 NULL,
4927};
NeilBrown007583c2005-11-08 21:39:30 -08004928static struct attribute_group raid5_attrs_group = {
4929 .name = NULL,
4930 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004931};
4932
Dan Williams80c3a6c2009-03-17 18:10:40 -07004933static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11004934raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07004935{
NeilBrownd1688a62011-10-11 16:49:52 +11004936 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004937
4938 if (!sectors)
4939 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004940 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11004941 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11004942 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004943
Andre Noll9d8f0362009-06-18 08:45:01 +10004944 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004945 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004946 return sectors * (raid_disks - conf->max_degraded);
4947}
4948
NeilBrownd1688a62011-10-11 16:49:52 +11004949static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004950{
4951 struct raid5_percpu *percpu;
4952 unsigned long cpu;
4953
4954 if (!conf->percpu)
4955 return;
4956
4957 get_online_cpus();
4958 for_each_possible_cpu(cpu) {
4959 percpu = per_cpu_ptr(conf->percpu, cpu);
4960 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004961 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004962 }
4963#ifdef CONFIG_HOTPLUG_CPU
4964 unregister_cpu_notifier(&conf->cpu_notify);
4965#endif
4966 put_online_cpus();
4967
4968 free_percpu(conf->percpu);
4969}
4970
NeilBrownd1688a62011-10-11 16:49:52 +11004971static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10004972{
4973 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004974 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10004975 kfree(conf->disks);
4976 kfree(conf->stripe_hashtbl);
4977 kfree(conf);
4978}
4979
Dan Williams36d1c642009-07-14 11:48:22 -07004980#ifdef CONFIG_HOTPLUG_CPU
4981static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4982 void *hcpu)
4983{
NeilBrownd1688a62011-10-11 16:49:52 +11004984 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07004985 long cpu = (long)hcpu;
4986 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4987
4988 switch (action) {
4989 case CPU_UP_PREPARE:
4990 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07004991 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07004992 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004993 if (!percpu->scribble)
4994 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4995
4996 if (!percpu->scribble ||
4997 (conf->level == 6 && !percpu->spare_page)) {
4998 safe_put_page(percpu->spare_page);
4999 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005000 pr_err("%s: failed memory allocation for cpu%ld\n",
5001 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07005002 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07005003 }
5004 break;
5005 case CPU_DEAD:
5006 case CPU_DEAD_FROZEN:
5007 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005008 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005009 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07005010 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07005011 break;
5012 default:
5013 break;
5014 }
5015 return NOTIFY_OK;
5016}
5017#endif
5018
NeilBrownd1688a62011-10-11 16:49:52 +11005019static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07005020{
5021 unsigned long cpu;
5022 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09005023 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07005024 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07005025 int err;
5026
Dan Williams36d1c642009-07-14 11:48:22 -07005027 allcpus = alloc_percpu(struct raid5_percpu);
5028 if (!allcpus)
5029 return -ENOMEM;
5030 conf->percpu = allcpus;
5031
5032 get_online_cpus();
5033 err = 0;
5034 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07005035 if (conf->level == 6) {
5036 spare_page = alloc_page(GFP_KERNEL);
5037 if (!spare_page) {
5038 err = -ENOMEM;
5039 break;
5040 }
5041 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
5042 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11005043 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005044 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07005045 err = -ENOMEM;
5046 break;
5047 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07005048 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07005049 }
5050#ifdef CONFIG_HOTPLUG_CPU
5051 conf->cpu_notify.notifier_call = raid456_cpu_notify;
5052 conf->cpu_notify.priority = 0;
5053 if (err == 0)
5054 err = register_cpu_notifier(&conf->cpu_notify);
5055#endif
5056 put_online_cpus();
5057
5058 return err;
5059}
5060
NeilBrownd1688a62011-10-11 16:49:52 +11005061static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062{
NeilBrownd1688a62011-10-11 16:49:52 +11005063 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005064 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11005065 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10005067 char pers_name[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068
NeilBrown91adb562009-03-31 14:39:39 +11005069 if (mddev->new_level != 5
5070 && mddev->new_level != 4
5071 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10005072 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005073 mdname(mddev), mddev->new_level);
5074 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 }
NeilBrown91adb562009-03-31 14:39:39 +11005076 if ((mddev->new_level == 5
5077 && !algorithm_valid_raid5(mddev->new_layout)) ||
5078 (mddev->new_level == 6
5079 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005080 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11005081 mdname(mddev), mddev->new_layout);
5082 return ERR_PTR(-EIO);
5083 }
5084 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10005085 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005086 mdname(mddev), mddev->raid_disks);
5087 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11005088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089
Andre Noll664e7c42009-06-18 08:45:27 +10005090 if (!mddev->new_chunk_sectors ||
5091 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
5092 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005093 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
5094 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11005095 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11005096 }
5097
NeilBrownd1688a62011-10-11 16:49:52 +11005098 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11005099 if (conf == NULL)
5100 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11005101 spin_lock_init(&conf->device_lock);
5102 init_waitqueue_head(&conf->wait_for_stripe);
5103 init_waitqueue_head(&conf->wait_for_overlap);
5104 INIT_LIST_HEAD(&conf->handle_list);
5105 INIT_LIST_HEAD(&conf->hold_list);
5106 INIT_LIST_HEAD(&conf->delayed_list);
5107 INIT_LIST_HEAD(&conf->bitmap_list);
5108 INIT_LIST_HEAD(&conf->inactive_list);
5109 atomic_set(&conf->active_stripes, 0);
5110 atomic_set(&conf->preread_active_stripes, 0);
5111 atomic_set(&conf->active_aligned_reads, 0);
5112 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11005113 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11005114
5115 conf->raid_disks = mddev->raid_disks;
5116 if (mddev->reshape_position == MaxSector)
5117 conf->previous_raid_disks = mddev->raid_disks;
5118 else
5119 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005120 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
5121 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11005122
NeilBrown5e5e3e72009-10-16 16:35:30 +11005123 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11005124 GFP_KERNEL);
5125 if (!conf->disks)
5126 goto abort;
5127
5128 conf->mddev = mddev;
5129
5130 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
5131 goto abort;
5132
Dan Williams36d1c642009-07-14 11:48:22 -07005133 conf->level = mddev->new_level;
5134 if (raid5_alloc_percpu(conf) != 0)
5135 goto abort;
5136
NeilBrown0c55e022010-05-03 14:09:02 +10005137 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11005138
NeilBrowndafb20f2012-03-19 12:46:39 +11005139 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11005140 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005141 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11005142 || raid_disk < 0)
5143 continue;
5144 disk = conf->disks + raid_disk;
5145
NeilBrown17045f52011-12-23 10:17:53 +11005146 if (test_bit(Replacement, &rdev->flags)) {
5147 if (disk->replacement)
5148 goto abort;
5149 disk->replacement = rdev;
5150 } else {
5151 if (disk->rdev)
5152 goto abort;
5153 disk->rdev = rdev;
5154 }
NeilBrown91adb562009-03-31 14:39:39 +11005155
5156 if (test_bit(In_sync, &rdev->flags)) {
5157 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10005158 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
5159 " disk %d\n",
5160 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05005161 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11005162 /* Cannot rely on bitmap to complete recovery */
5163 conf->fullsync = 1;
5164 }
5165
Andre Noll09c9e5f2009-06-18 08:45:55 +10005166 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11005167 conf->level = mddev->new_level;
5168 if (conf->level == 6)
5169 conf->max_degraded = 2;
5170 else
5171 conf->max_degraded = 1;
5172 conf->algorithm = mddev->new_layout;
5173 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11005174 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11005175 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10005176 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11005177 conf->prev_algo = mddev->layout;
5178 }
NeilBrown91adb562009-03-31 14:39:39 +11005179
5180 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11005181 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11005182 if (grow_stripes(conf, conf->max_nr_stripes)) {
5183 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005184 "md/raid:%s: couldn't allocate %dkB for buffers\n",
5185 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005186 goto abort;
5187 } else
NeilBrown0c55e022010-05-03 14:09:02 +10005188 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
5189 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005190
NeilBrown02326052012-07-03 15:56:52 +10005191 sprintf(pers_name, "raid%d", mddev->new_level);
5192 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11005193 if (!conf->thread) {
5194 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005195 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11005196 mdname(mddev));
5197 goto abort;
5198 }
5199
5200 return conf;
5201
5202 abort:
5203 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10005204 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11005205 return ERR_PTR(-EIO);
5206 } else
5207 return ERR_PTR(-ENOMEM);
5208}
5209
NeilBrownc148ffd2009-11-13 17:47:00 +11005210
5211static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
5212{
5213 switch (algo) {
5214 case ALGORITHM_PARITY_0:
5215 if (raid_disk < max_degraded)
5216 return 1;
5217 break;
5218 case ALGORITHM_PARITY_N:
5219 if (raid_disk >= raid_disks - max_degraded)
5220 return 1;
5221 break;
5222 case ALGORITHM_PARITY_0_6:
5223 if (raid_disk == 0 ||
5224 raid_disk == raid_disks - 1)
5225 return 1;
5226 break;
5227 case ALGORITHM_LEFT_ASYMMETRIC_6:
5228 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5229 case ALGORITHM_LEFT_SYMMETRIC_6:
5230 case ALGORITHM_RIGHT_SYMMETRIC_6:
5231 if (raid_disk == raid_disks - 1)
5232 return 1;
5233 }
5234 return 0;
5235}
5236
NeilBrownfd01b882011-10-11 16:47:53 +11005237static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11005238{
NeilBrownd1688a62011-10-11 16:49:52 +11005239 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10005240 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11005241 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11005242 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11005243 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11005244 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10005245 long long min_offset_diff = 0;
5246 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11005247
Andre Noll8c6ac862009-06-18 08:48:06 +10005248 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10005249 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10005250 " -- starting background reconstruction\n",
5251 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10005252
5253 rdev_for_each(rdev, mddev) {
5254 long long diff;
5255 if (rdev->raid_disk < 0)
5256 continue;
5257 diff = (rdev->new_data_offset - rdev->data_offset);
5258 if (first) {
5259 min_offset_diff = diff;
5260 first = 0;
5261 } else if (mddev->reshape_backwards &&
5262 diff < min_offset_diff)
5263 min_offset_diff = diff;
5264 else if (!mddev->reshape_backwards &&
5265 diff > min_offset_diff)
5266 min_offset_diff = diff;
5267 }
5268
NeilBrownf6705572006-03-27 01:18:11 -08005269 if (mddev->reshape_position != MaxSector) {
5270 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10005271 * Difficulties arise if the stripe we would write to
5272 * next is at or after the stripe we would read from next.
5273 * For a reshape that changes the number of devices, this
5274 * is only possible for a very short time, and mdadm makes
5275 * sure that time appears to have past before assembling
5276 * the array. So we fail if that time hasn't passed.
5277 * For a reshape that keeps the number of devices the same
5278 * mdadm must be monitoring the reshape can keeping the
5279 * critical areas read-only and backed up. It will start
5280 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08005281 */
5282 sector_t here_new, here_old;
5283 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11005284 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08005285
NeilBrown88ce4932009-03-31 15:24:23 +11005286 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10005287 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08005288 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08005289 mdname(mddev));
5290 return -EINVAL;
5291 }
NeilBrownf6705572006-03-27 01:18:11 -08005292 old_disks = mddev->raid_disks - mddev->delta_disks;
5293 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08005294 * further up in new geometry must map after here in old
5295 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08005296 */
5297 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10005298 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005299 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005300 printk(KERN_ERR "md/raid:%s: reshape_position not "
5301 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005302 return -EINVAL;
5303 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005304 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005305 /* here_new is the stripe we will write to */
5306 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10005307 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005308 (old_disks-max_degraded));
5309 /* here_old is the first stripe that we might need to read
5310 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10005311 if (mddev->delta_disks == 0) {
NeilBrownb5254dd2012-05-21 09:27:01 +10005312 if ((here_new * mddev->new_chunk_sectors !=
5313 here_old * mddev->chunk_sectors)) {
5314 printk(KERN_ERR "md/raid:%s: reshape position is"
5315 " confused - aborting\n", mdname(mddev));
5316 return -EINVAL;
5317 }
NeilBrown67ac6012009-08-13 10:06:24 +10005318 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10005319 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10005320 * and taking constant backups.
5321 * mdadm always starts a situation like this in
5322 * readonly mode so it can take control before
5323 * allowing any writes. So just check for that.
5324 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005325 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
5326 abs(min_offset_diff) >= mddev->new_chunk_sectors)
5327 /* not really in-place - so OK */;
5328 else if (mddev->ro == 0) {
5329 printk(KERN_ERR "md/raid:%s: in-place reshape "
5330 "must be started in read-only mode "
5331 "- aborting\n",
NeilBrown0c55e022010-05-03 14:09:02 +10005332 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005333 return -EINVAL;
5334 }
NeilBrown2c810cd2012-05-21 09:27:00 +10005335 } else if (mddev->reshape_backwards
NeilBrownb5254dd2012-05-21 09:27:01 +10005336 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
NeilBrown67ac6012009-08-13 10:06:24 +10005337 here_old * mddev->chunk_sectors)
5338 : (here_new * mddev->new_chunk_sectors >=
NeilBrownb5254dd2012-05-21 09:27:01 +10005339 here_old * mddev->chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08005340 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005341 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5342 "auto-recovery - aborting.\n",
5343 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005344 return -EINVAL;
5345 }
NeilBrown0c55e022010-05-03 14:09:02 +10005346 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5347 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005348 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005349 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005350 BUG_ON(mddev->level != mddev->new_level);
5351 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005352 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005353 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005354 }
5355
NeilBrown245f46c2009-03-31 14:39:39 +11005356 if (mddev->private == NULL)
5357 conf = setup_conf(mddev);
5358 else
5359 conf = mddev->private;
5360
NeilBrown91adb562009-03-31 14:39:39 +11005361 if (IS_ERR(conf))
5362 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005363
NeilBrownb5254dd2012-05-21 09:27:01 +10005364 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11005365 mddev->thread = conf->thread;
5366 conf->thread = NULL;
5367 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368
NeilBrown17045f52011-12-23 10:17:53 +11005369 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5370 i++) {
5371 rdev = conf->disks[i].rdev;
5372 if (!rdev && conf->disks[i].replacement) {
5373 /* The replacement is all we have yet */
5374 rdev = conf->disks[i].replacement;
5375 conf->disks[i].replacement = NULL;
5376 clear_bit(Replacement, &rdev->flags);
5377 conf->disks[i].rdev = rdev;
5378 }
5379 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11005380 continue;
NeilBrown17045f52011-12-23 10:17:53 +11005381 if (conf->disks[i].replacement &&
5382 conf->reshape_progress != MaxSector) {
5383 /* replacements and reshape simply do not mix. */
5384 printk(KERN_ERR "md: cannot handle concurrent "
5385 "replacement and reshape.\n");
5386 goto abort;
5387 }
NeilBrown2f115882010-06-17 17:41:03 +10005388 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005389 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005390 continue;
5391 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005392 /* This disc is not fully in-sync. However if it
5393 * just stored parity (beyond the recovery_offset),
5394 * when we don't need to be concerned about the
5395 * array being dirty.
5396 * When reshape goes 'backwards', we never have
5397 * partially completed devices, so we only need
5398 * to worry about reshape going forwards.
5399 */
5400 /* Hack because v0.91 doesn't store recovery_offset properly. */
5401 if (mddev->major_version == 0 &&
5402 mddev->minor_version > 90)
5403 rdev->recovery_offset = reshape_offset;
5404
NeilBrownc148ffd2009-11-13 17:47:00 +11005405 if (rdev->recovery_offset < reshape_offset) {
5406 /* We need to check old and new layout */
5407 if (!only_parity(rdev->raid_disk,
5408 conf->algorithm,
5409 conf->raid_disks,
5410 conf->max_degraded))
5411 continue;
5412 }
5413 if (!only_parity(rdev->raid_disk,
5414 conf->prev_algo,
5415 conf->previous_raid_disks,
5416 conf->max_degraded))
5417 continue;
5418 dirty_parity_disks++;
5419 }
NeilBrown91adb562009-03-31 14:39:39 +11005420
NeilBrown17045f52011-12-23 10:17:53 +11005421 /*
5422 * 0 for a fully functional array, 1 or 2 for a degraded array.
5423 */
NeilBrown908f4fb2011-12-23 10:17:50 +11005424 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425
NeilBrown674806d2010-06-16 17:17:53 +10005426 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005427 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07005429 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 goto abort;
5431 }
5432
NeilBrown91adb562009-03-31 14:39:39 +11005433 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10005434 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11005435 mddev->resync_max_sectors = mddev->dev_sectors;
5436
NeilBrownc148ffd2009-11-13 17:47:00 +11005437 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005439 if (mddev->ok_start_degraded)
5440 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10005441 "md/raid:%s: starting dirty degraded array"
5442 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005443 mdname(mddev));
5444 else {
5445 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005446 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005447 mdname(mddev));
5448 goto abort;
5449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 }
5451
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10005453 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5454 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11005455 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5456 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 else
NeilBrown0c55e022010-05-03 14:09:02 +10005458 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5459 " out of %d devices, algorithm %d\n",
5460 mdname(mddev), conf->level,
5461 mddev->raid_disks - mddev->degraded,
5462 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463
5464 print_raid5_conf(conf);
5465
NeilBrownfef9c612009-03-31 15:16:46 +11005466 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11005467 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005468 atomic_set(&conf->reshape_stripes, 0);
5469 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5470 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5471 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5472 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5473 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005474 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005475 }
5476
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477
5478 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005479 if (mddev->to_remove == &raid5_attrs_group)
5480 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005481 else if (mddev->kobj.sd &&
5482 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005483 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10005484 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08005485 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10005486 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5487
5488 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005489 int chunk_size;
Shaohua Li620125f2012-10-11 13:49:05 +11005490 bool discard_supported = true;
NeilBrown4a5add42010-06-01 19:37:28 +10005491 /* read-ahead size must cover two whole stripes, which
5492 * is 2 * (datadisks) * chunksize where 'n' is the
5493 * number of raid devices
5494 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5496 int stripe = data_disks *
5497 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5498 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5499 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10005500
5501 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005502
5503 mddev->queue->backing_dev_info.congested_data = mddev;
5504 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10005505
5506 chunk_size = mddev->chunk_sectors << 9;
5507 blk_queue_io_min(mddev->queue, chunk_size);
5508 blk_queue_io_opt(mddev->queue, chunk_size *
5509 (conf->raid_disks - conf->max_degraded));
Shaohua Li620125f2012-10-11 13:49:05 +11005510 /*
5511 * We can only discard a whole stripe. It doesn't make sense to
5512 * discard data disk but write parity disk
5513 */
5514 stripe = stripe * PAGE_SIZE;
NeilBrown4ac68752012-11-19 13:11:26 +11005515 /* Round up to power of 2, as discard handling
5516 * currently assumes that */
5517 while ((stripe-1) & stripe)
5518 stripe = (stripe | (stripe-1)) + 1;
Shaohua Li620125f2012-10-11 13:49:05 +11005519 mddev->queue->limits.discard_alignment = stripe;
5520 mddev->queue->limits.discard_granularity = stripe;
5521 /*
5522 * unaligned part of discard request will be ignored, so can't
5523 * guarantee discard_zerors_data
5524 */
5525 mddev->queue->limits.discard_zeroes_data = 0;
NeilBrown9f7c2222010-07-26 12:04:13 +10005526
NeilBrown05616be2012-05-21 09:27:00 +10005527 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005528 disk_stack_limits(mddev->gendisk, rdev->bdev,
5529 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10005530 disk_stack_limits(mddev->gendisk, rdev->bdev,
5531 rdev->new_data_offset << 9);
Shaohua Li620125f2012-10-11 13:49:05 +11005532 /*
5533 * discard_zeroes_data is required, otherwise data
5534 * could be lost. Consider a scenario: discard a stripe
5535 * (the stripe could be inconsistent if
5536 * discard_zeroes_data is 0); write one disk of the
5537 * stripe (the stripe could be inconsistent again
5538 * depending on which disks are used to calculate
5539 * parity); the disk is broken; The stripe data of this
5540 * disk is lost.
5541 */
5542 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
5543 !bdev_get_queue(rdev->bdev)->
5544 limits.discard_zeroes_data)
5545 discard_supported = false;
NeilBrown05616be2012-05-21 09:27:00 +10005546 }
Shaohua Li620125f2012-10-11 13:49:05 +11005547
5548 if (discard_supported &&
5549 mddev->queue->limits.max_discard_sectors >= stripe &&
5550 mddev->queue->limits.discard_granularity >= stripe)
5551 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
5552 mddev->queue);
5553 else
5554 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
5555 mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 }
5557
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558 return 0;
5559abort:
NeilBrown01f96c02011-09-21 15:30:20 +10005560 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11005561 print_raid5_conf(conf);
5562 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005564 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 return -EIO;
5566}
5567
NeilBrownfd01b882011-10-11 16:47:53 +11005568static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569{
NeilBrownd1688a62011-10-11 16:49:52 +11005570 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571
NeilBrown01f96c02011-09-21 15:30:20 +10005572 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005573 if (mddev->queue)
5574 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10005575 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005576 mddev->private = NULL;
5577 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 return 0;
5579}
5580
NeilBrownfd01b882011-10-11 16:47:53 +11005581static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582{
NeilBrownd1688a62011-10-11 16:49:52 +11005583 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 int i;
5585
Andre Noll9d8f0362009-06-18 08:45:01 +10005586 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5587 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005588 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589 for (i = 0; i < conf->raid_disks; i++)
5590 seq_printf (seq, "%s",
5591 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005592 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594}
5595
NeilBrownd1688a62011-10-11 16:49:52 +11005596static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597{
5598 int i;
5599 struct disk_info *tmp;
5600
NeilBrown0c55e022010-05-03 14:09:02 +10005601 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 if (!conf) {
5603 printk("(conf==NULL)\n");
5604 return;
5605 }
NeilBrown0c55e022010-05-03 14:09:02 +10005606 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5607 conf->raid_disks,
5608 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609
5610 for (i = 0; i < conf->raid_disks; i++) {
5611 char b[BDEVNAME_SIZE];
5612 tmp = conf->disks + i;
5613 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005614 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5615 i, !test_bit(Faulty, &tmp->rdev->flags),
5616 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 }
5618}
5619
NeilBrownfd01b882011-10-11 16:47:53 +11005620static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621{
5622 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005623 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005625 int count = 0;
5626 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627
5628 for (i = 0; i < conf->raid_disks; i++) {
5629 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11005630 if (tmp->replacement
5631 && tmp->replacement->recovery_offset == MaxSector
5632 && !test_bit(Faulty, &tmp->replacement->flags)
5633 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
5634 /* Replacement has just become active. */
5635 if (!tmp->rdev
5636 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
5637 count++;
5638 if (tmp->rdev) {
5639 /* Replaced device not technically faulty,
5640 * but we need to be sure it gets removed
5641 * and never re-added.
5642 */
5643 set_bit(Faulty, &tmp->rdev->flags);
5644 sysfs_notify_dirent_safe(
5645 tmp->rdev->sysfs_state);
5646 }
5647 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
5648 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005649 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005650 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005651 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005652 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005653 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 }
5655 }
NeilBrown6b965622010-08-18 11:56:59 +10005656 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005657 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005658 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005660 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661}
5662
NeilBrownb8321b62011-12-23 10:17:51 +11005663static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664{
NeilBrownd1688a62011-10-11 16:49:52 +11005665 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11005667 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11005668 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 struct disk_info *p = conf->disks + number;
5670
5671 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11005672 if (rdev == p->rdev)
5673 rdevp = &p->rdev;
5674 else if (rdev == p->replacement)
5675 rdevp = &p->replacement;
5676 else
5677 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11005678
NeilBrown657e3e42011-12-23 10:17:52 +11005679 if (number >= conf->raid_disks &&
5680 conf->reshape_progress == MaxSector)
5681 clear_bit(In_sync, &rdev->flags);
5682
5683 if (test_bit(In_sync, &rdev->flags) ||
5684 atomic_read(&rdev->nr_pending)) {
5685 err = -EBUSY;
5686 goto abort;
5687 }
5688 /* Only remove non-faulty devices if recovery
5689 * isn't possible.
5690 */
5691 if (!test_bit(Faulty, &rdev->flags) &&
5692 mddev->recovery_disabled != conf->recovery_disabled &&
5693 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11005694 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11005695 number < conf->raid_disks) {
5696 err = -EBUSY;
5697 goto abort;
5698 }
5699 *rdevp = NULL;
5700 synchronize_rcu();
5701 if (atomic_read(&rdev->nr_pending)) {
5702 /* lost the race, try later */
5703 err = -EBUSY;
5704 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11005705 } else if (p->replacement) {
5706 /* We must have just cleared 'rdev' */
5707 p->rdev = p->replacement;
5708 clear_bit(Replacement, &p->replacement->flags);
5709 smp_mb(); /* Make sure other CPUs may see both as identical
5710 * but will never see neither - if they are careful
5711 */
5712 p->replacement = NULL;
5713 clear_bit(WantReplacement, &rdev->flags);
5714 } else
5715 /* We might have just removed the Replacement as faulty-
5716 * clear the bit just in case
5717 */
5718 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719abort:
5720
5721 print_raid5_conf(conf);
5722 return err;
5723}
5724
NeilBrownfd01b882011-10-11 16:47:53 +11005725static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726{
NeilBrownd1688a62011-10-11 16:49:52 +11005727 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005728 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729 int disk;
5730 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005731 int first = 0;
5732 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733
NeilBrown7f0da592011-07-28 11:39:22 +10005734 if (mddev->recovery_disabled == conf->recovery_disabled)
5735 return -EBUSY;
5736
NeilBrowndc10c642012-03-19 12:46:37 +11005737 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005739 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740
Neil Brown6c2fce22008-06-28 08:31:31 +10005741 if (rdev->raid_disk >= 0)
5742 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743
5744 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005745 * find the disk ... but prefer rdev->saved_raid_disk
5746 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005748 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005749 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005750 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10005751 first = rdev->saved_raid_disk;
5752
5753 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11005754 p = conf->disks + disk;
5755 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005756 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005758 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005759 if (rdev->saved_raid_disk != disk)
5760 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005761 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10005762 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005764 }
5765 for (disk = first; disk <= last; disk++) {
5766 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11005767 if (test_bit(WantReplacement, &p->rdev->flags) &&
5768 p->replacement == NULL) {
5769 clear_bit(In_sync, &rdev->flags);
5770 set_bit(Replacement, &rdev->flags);
5771 rdev->raid_disk = disk;
5772 err = 0;
5773 conf->fullsync = 1;
5774 rcu_assign_pointer(p->replacement, rdev);
5775 break;
5776 }
5777 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005778out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005780 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781}
5782
NeilBrownfd01b882011-10-11 16:47:53 +11005783static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784{
5785 /* no resync is happening, and there is enough space
5786 * on all devices, so we can resize.
5787 * We need to make sure resync covers any new space.
5788 * If the array is shrinking we should possibly wait until
5789 * any io in the removed space completes, but it hardly seems
5790 * worth it.
5791 */
NeilBrowna4a61252012-05-22 13:55:27 +10005792 sector_t newsize;
Andre Noll9d8f0362009-06-18 08:45:01 +10005793 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10005794 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
5795 if (mddev->external_size &&
5796 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11005797 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10005798 if (mddev->bitmap) {
5799 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
5800 if (ret)
5801 return ret;
5802 }
5803 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10005804 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005805 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005806 if (sectors > mddev->dev_sectors &&
5807 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005808 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5810 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005811 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005812 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813 return 0;
5814}
5815
NeilBrownfd01b882011-10-11 16:47:53 +11005816static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005817{
5818 /* Can only proceed if there are plenty of stripe_heads.
5819 * We need a minimum of one full stripe,, and for sensible progress
5820 * it is best to have about 4 times that.
5821 * If we require 4 times, then the default 256 4K stripe_heads will
5822 * allow for chunk sizes up to 256K, which is probably OK.
5823 * If the chunk size is greater, user-space should request more
5824 * stripe_heads first.
5825 */
NeilBrownd1688a62011-10-11 16:49:52 +11005826 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005827 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5828 > conf->max_nr_stripes ||
5829 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5830 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005831 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5832 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005833 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5834 / STRIPE_SIZE)*4);
5835 return 0;
5836 }
5837 return 1;
5838}
5839
NeilBrownfd01b882011-10-11 16:47:53 +11005840static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005841{
NeilBrownd1688a62011-10-11 16:49:52 +11005842 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005843
NeilBrown88ce4932009-03-31 15:24:23 +11005844 if (mddev->delta_disks == 0 &&
5845 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005846 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005847 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10005848 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005849 return -EINVAL;
5850 if (mddev->delta_disks < 0) {
5851 /* We might be able to shrink, but the devices must
5852 * be made bigger first.
5853 * For raid6, 4 is the minimum size.
5854 * Otherwise 2 is the minimum
5855 */
5856 int min = 2;
5857 if (mddev->level == 6)
5858 min = 4;
5859 if (mddev->raid_disks + mddev->delta_disks < min)
5860 return -EINVAL;
5861 }
NeilBrown29269552006-03-27 01:18:10 -08005862
NeilBrown01ee22b2009-06-18 08:47:20 +10005863 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005864 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005865
NeilBrowne56108d62012-10-11 14:24:13 +11005866 return resize_stripes(conf, (conf->previous_raid_disks
5867 + mddev->delta_disks));
NeilBrown63c70c42006-03-27 01:18:13 -08005868}
5869
NeilBrownfd01b882011-10-11 16:47:53 +11005870static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005871{
NeilBrownd1688a62011-10-11 16:49:52 +11005872 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005873 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005874 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005875 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005876
NeilBrownf4168852007-02-28 20:11:53 -08005877 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005878 return -EBUSY;
5879
NeilBrown01ee22b2009-06-18 08:47:20 +10005880 if (!check_stripe_cache(mddev))
5881 return -ENOSPC;
5882
NeilBrown30b67642012-05-22 13:55:28 +10005883 if (has_failed(conf))
5884 return -EINVAL;
5885
NeilBrownc6563a82012-05-21 09:27:00 +10005886 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11005887 if (!test_bit(In_sync, &rdev->flags)
5888 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005889 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10005890 }
NeilBrown63c70c42006-03-27 01:18:13 -08005891
NeilBrownf4168852007-02-28 20:11:53 -08005892 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005893 /* Not enough devices even to make a degraded array
5894 * of that size
5895 */
5896 return -EINVAL;
5897
NeilBrownec32a2b2009-03-31 15:17:38 +11005898 /* Refuse to reduce size of the array. Any reductions in
5899 * array size must be through explicit setting of array_size
5900 * attribute.
5901 */
5902 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5903 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10005904 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11005905 "before number of disks\n", mdname(mddev));
5906 return -EINVAL;
5907 }
5908
NeilBrownf6705572006-03-27 01:18:11 -08005909 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005910 spin_lock_irq(&conf->device_lock);
5911 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005912 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005913 conf->prev_chunk_sectors = conf->chunk_sectors;
5914 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005915 conf->prev_algo = conf->algorithm;
5916 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10005917 conf->generation++;
5918 /* Code that selects data_offset needs to see the generation update
5919 * if reshape_progress has been set - so a memory barrier needed.
5920 */
5921 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10005922 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11005923 conf->reshape_progress = raid5_size(mddev, 0, 0);
5924 else
5925 conf->reshape_progress = 0;
5926 conf->reshape_safe = conf->reshape_progress;
NeilBrown29269552006-03-27 01:18:10 -08005927 spin_unlock_irq(&conf->device_lock);
5928
5929 /* Add some new drives, as many as will fit.
5930 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10005931 * Don't add devices if we are reducing the number of
5932 * devices in the array. This is because it is not possible
5933 * to correctly record the "partially reconstructed" state of
5934 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08005935 */
NeilBrown87a8dec2011-01-31 11:57:43 +11005936 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11005937 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11005938 if (rdev->raid_disk < 0 &&
5939 !test_bit(Faulty, &rdev->flags)) {
5940 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11005941 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11005942 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11005943 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11005944 else
NeilBrown87a8dec2011-01-31 11:57:43 +11005945 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10005946
5947 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11005948 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11005949 }
NeilBrown87a8dec2011-01-31 11:57:43 +11005950 } else if (rdev->raid_disk >= conf->previous_raid_disks
5951 && !test_bit(Faulty, &rdev->flags)) {
5952 /* This is a spare that was manually added */
5953 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11005954 }
NeilBrown29269552006-03-27 01:18:10 -08005955
NeilBrown87a8dec2011-01-31 11:57:43 +11005956 /* When a reshape changes the number of devices,
5957 * ->degraded is measured against the larger of the
5958 * pre and post number of devices.
5959 */
NeilBrownec32a2b2009-03-31 15:17:38 +11005960 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005961 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11005962 spin_unlock_irqrestore(&conf->device_lock, flags);
5963 }
NeilBrown63c70c42006-03-27 01:18:13 -08005964 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10005965 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07005966 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005967
NeilBrown29269552006-03-27 01:18:10 -08005968 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5969 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5970 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5971 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5972 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005973 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08005974 if (!mddev->sync_thread) {
5975 mddev->recovery = 0;
5976 spin_lock_irq(&conf->device_lock);
5977 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10005978 rdev_for_each(rdev, mddev)
5979 rdev->new_data_offset = rdev->data_offset;
5980 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11005981 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11005982 mddev->reshape_position = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005983 spin_unlock_irq(&conf->device_lock);
5984 return -EAGAIN;
5985 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005986 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005987 md_wakeup_thread(mddev->sync_thread);
5988 md_new_event(mddev);
5989 return 0;
5990}
NeilBrown29269552006-03-27 01:18:10 -08005991
NeilBrownec32a2b2009-03-31 15:17:38 +11005992/* This is called from the reshape thread and should make any
5993 * changes needed in 'conf'
5994 */
NeilBrownd1688a62011-10-11 16:49:52 +11005995static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08005996{
NeilBrown29269552006-03-27 01:18:10 -08005997
NeilBrownf6705572006-03-27 01:18:11 -08005998 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10005999 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07006000
NeilBrownf6705572006-03-27 01:18:11 -08006001 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11006002 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10006003 rdev_for_each(rdev, conf->mddev)
6004 rdev->data_offset = rdev->new_data_offset;
6005 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11006006 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08006007 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11006008 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07006009
6010 /* read-ahead size must cover two whole stripes, which is
6011 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
6012 */
NeilBrown4a5add42010-06-01 19:37:28 +10006013 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11006014 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006015 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11006016 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07006017 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
6018 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
6019 }
NeilBrown29269552006-03-27 01:18:10 -08006020 }
NeilBrown29269552006-03-27 01:18:10 -08006021}
6022
NeilBrownec32a2b2009-03-31 15:17:38 +11006023/* This is called from the raid5d thread with mddev_lock held.
6024 * It makes config changes to the device.
6025 */
NeilBrownfd01b882011-10-11 16:47:53 +11006026static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11006027{
NeilBrownd1688a62011-10-11 16:49:52 +11006028 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11006029
6030 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6031
NeilBrownec32a2b2009-03-31 15:17:38 +11006032 if (mddev->delta_disks > 0) {
6033 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
6034 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006035 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11006036 } else {
6037 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11006038 spin_lock_irq(&conf->device_lock);
6039 mddev->degraded = calc_degraded(conf);
6040 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11006041 for (d = conf->raid_disks ;
6042 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10006043 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11006044 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10006045 if (rdev)
6046 clear_bit(In_sync, &rdev->flags);
6047 rdev = conf->disks[d].replacement;
6048 if (rdev)
6049 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10006050 }
NeilBrowncea9c222009-03-31 15:15:05 +11006051 }
NeilBrown88ce4932009-03-31 15:24:23 +11006052 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006053 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11006054 mddev->reshape_position = MaxSector;
6055 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006056 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11006057 }
6058}
6059
NeilBrownfd01b882011-10-11 16:47:53 +11006060static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07006061{
NeilBrownd1688a62011-10-11 16:49:52 +11006062 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07006063
6064 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08006065 case 2: /* resume for a suspend */
6066 wake_up(&conf->wait_for_overlap);
6067 break;
6068
NeilBrown72626682005-09-09 16:23:54 -07006069 case 1: /* stop all writes */
6070 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006071 /* '2' tells resync/reshape to pause so that all
6072 * active stripes can drain
6073 */
6074 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07006075 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006076 atomic_read(&conf->active_stripes) == 0 &&
6077 atomic_read(&conf->active_aligned_reads) == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01006078 conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006079 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07006080 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006081 /* allow reshape to continue */
6082 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006083 break;
6084
6085 case 0: /* re-enable writes */
6086 spin_lock_irq(&conf->device_lock);
6087 conf->quiesce = 0;
6088 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08006089 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006090 spin_unlock_irq(&conf->device_lock);
6091 break;
6092 }
NeilBrown72626682005-09-09 16:23:54 -07006093}
NeilBrownb15c2e52006-01-06 00:20:16 -08006094
NeilBrownd562b0c2009-03-31 14:39:39 +11006095
NeilBrownfd01b882011-10-11 16:47:53 +11006096static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11006097{
NeilBrowne373ab12011-10-11 16:48:59 +11006098 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07006099 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11006100
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006101 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11006102 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10006103 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
6104 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006105 return ERR_PTR(-EINVAL);
6106 }
6107
NeilBrowne373ab12011-10-11 16:48:59 +11006108 sectors = raid0_conf->strip_zone[0].zone_end;
6109 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10006110 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006111 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11006112 mddev->new_layout = ALGORITHM_PARITY_N;
6113 mddev->new_chunk_sectors = mddev->chunk_sectors;
6114 mddev->raid_disks += 1;
6115 mddev->delta_disks = 1;
6116 /* make sure it will be not marked as dirty */
6117 mddev->recovery_cp = MaxSector;
6118
6119 return setup_conf(mddev);
6120}
6121
6122
NeilBrownfd01b882011-10-11 16:47:53 +11006123static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006124{
6125 int chunksect;
6126
6127 if (mddev->raid_disks != 2 ||
6128 mddev->degraded > 1)
6129 return ERR_PTR(-EINVAL);
6130
6131 /* Should check if there are write-behind devices? */
6132
6133 chunksect = 64*2; /* 64K by default */
6134
6135 /* The array must be an exact multiple of chunksize */
6136 while (chunksect && (mddev->array_sectors & (chunksect-1)))
6137 chunksect >>= 1;
6138
6139 if ((chunksect<<9) < STRIPE_SIZE)
6140 /* array size does not allow a suitable chunk size */
6141 return ERR_PTR(-EINVAL);
6142
6143 mddev->new_level = 5;
6144 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10006145 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11006146
6147 return setup_conf(mddev);
6148}
6149
NeilBrownfd01b882011-10-11 16:47:53 +11006150static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11006151{
6152 int new_layout;
6153
6154 switch (mddev->layout) {
6155 case ALGORITHM_LEFT_ASYMMETRIC_6:
6156 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
6157 break;
6158 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6159 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
6160 break;
6161 case ALGORITHM_LEFT_SYMMETRIC_6:
6162 new_layout = ALGORITHM_LEFT_SYMMETRIC;
6163 break;
6164 case ALGORITHM_RIGHT_SYMMETRIC_6:
6165 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
6166 break;
6167 case ALGORITHM_PARITY_0_6:
6168 new_layout = ALGORITHM_PARITY_0;
6169 break;
6170 case ALGORITHM_PARITY_N:
6171 new_layout = ALGORITHM_PARITY_N;
6172 break;
6173 default:
6174 return ERR_PTR(-EINVAL);
6175 }
6176 mddev->new_level = 5;
6177 mddev->new_layout = new_layout;
6178 mddev->delta_disks = -1;
6179 mddev->raid_disks -= 1;
6180 return setup_conf(mddev);
6181}
6182
NeilBrownd562b0c2009-03-31 14:39:39 +11006183
NeilBrownfd01b882011-10-11 16:47:53 +11006184static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11006185{
NeilBrown88ce4932009-03-31 15:24:23 +11006186 /* For a 2-drive array, the layout and chunk size can be changed
6187 * immediately as not restriping is needed.
6188 * For larger arrays we record the new value - after validation
6189 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11006190 */
NeilBrownd1688a62011-10-11 16:49:52 +11006191 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10006192 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11006193
NeilBrown597a7112009-06-18 08:47:42 +10006194 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11006195 return -EINVAL;
6196 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006197 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11006198 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006199 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11006200 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006201 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11006202 /* not factor of array size */
6203 return -EINVAL;
6204 }
6205
6206 /* They look valid */
6207
NeilBrown88ce4932009-03-31 15:24:23 +11006208 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10006209 /* can make the change immediately */
6210 if (mddev->new_layout >= 0) {
6211 conf->algorithm = mddev->new_layout;
6212 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11006213 }
6214 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10006215 conf->chunk_sectors = new_chunk ;
6216 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11006217 }
6218 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6219 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11006220 }
NeilBrown50ac1682009-06-18 08:47:55 +10006221 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11006222}
6223
NeilBrownfd01b882011-10-11 16:47:53 +11006224static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11006225{
NeilBrown597a7112009-06-18 08:47:42 +10006226 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10006227
NeilBrown597a7112009-06-18 08:47:42 +10006228 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11006229 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006230 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006231 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11006232 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006233 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11006234 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006235 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11006236 /* not factor of array size */
6237 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006238 }
NeilBrown88ce4932009-03-31 15:24:23 +11006239
6240 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10006241 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11006242}
6243
NeilBrownfd01b882011-10-11 16:47:53 +11006244static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006245{
6246 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006247 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006248 * raid1 - if there are two drives. We need to know the chunk size
6249 * raid4 - trivial - just use a raid4 layout.
6250 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006251 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006252 if (mddev->level == 0)
6253 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11006254 if (mddev->level == 1)
6255 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11006256 if (mddev->level == 4) {
6257 mddev->new_layout = ALGORITHM_PARITY_N;
6258 mddev->new_level = 5;
6259 return setup_conf(mddev);
6260 }
NeilBrownfc9739c2009-03-31 14:57:20 +11006261 if (mddev->level == 6)
6262 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11006263
6264 return ERR_PTR(-EINVAL);
6265}
6266
NeilBrownfd01b882011-10-11 16:47:53 +11006267static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11006268{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006269 /* raid4 can take over:
6270 * raid0 - if there is only one strip zone
6271 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11006272 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006273 if (mddev->level == 0)
6274 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11006275 if (mddev->level == 5 &&
6276 mddev->layout == ALGORITHM_PARITY_N) {
6277 mddev->new_layout = 0;
6278 mddev->new_level = 4;
6279 return setup_conf(mddev);
6280 }
6281 return ERR_PTR(-EINVAL);
6282}
NeilBrownd562b0c2009-03-31 14:39:39 +11006283
NeilBrown84fc4b52011-10-11 16:49:58 +11006284static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11006285
NeilBrownfd01b882011-10-11 16:47:53 +11006286static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11006287{
6288 /* Currently can only take over a raid5. We map the
6289 * personality to an equivalent raid6 personality
6290 * with the Q block at the end.
6291 */
6292 int new_layout;
6293
6294 if (mddev->pers != &raid5_personality)
6295 return ERR_PTR(-EINVAL);
6296 if (mddev->degraded > 1)
6297 return ERR_PTR(-EINVAL);
6298 if (mddev->raid_disks > 253)
6299 return ERR_PTR(-EINVAL);
6300 if (mddev->raid_disks < 3)
6301 return ERR_PTR(-EINVAL);
6302
6303 switch (mddev->layout) {
6304 case ALGORITHM_LEFT_ASYMMETRIC:
6305 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
6306 break;
6307 case ALGORITHM_RIGHT_ASYMMETRIC:
6308 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
6309 break;
6310 case ALGORITHM_LEFT_SYMMETRIC:
6311 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
6312 break;
6313 case ALGORITHM_RIGHT_SYMMETRIC:
6314 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
6315 break;
6316 case ALGORITHM_PARITY_0:
6317 new_layout = ALGORITHM_PARITY_0_6;
6318 break;
6319 case ALGORITHM_PARITY_N:
6320 new_layout = ALGORITHM_PARITY_N;
6321 break;
6322 default:
6323 return ERR_PTR(-EINVAL);
6324 }
6325 mddev->new_level = 6;
6326 mddev->new_layout = new_layout;
6327 mddev->delta_disks = 1;
6328 mddev->raid_disks += 1;
6329 return setup_conf(mddev);
6330}
6331
6332
NeilBrown84fc4b52011-10-11 16:49:58 +11006333static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07006334{
6335 .name = "raid6",
6336 .level = 6,
6337 .owner = THIS_MODULE,
6338 .make_request = make_request,
6339 .run = run,
6340 .stop = stop,
6341 .status = status,
6342 .error_handler = error,
6343 .hot_add_disk = raid5_add_disk,
6344 .hot_remove_disk= raid5_remove_disk,
6345 .spare_active = raid5_spare_active,
6346 .sync_request = sync_request,
6347 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006348 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10006349 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08006350 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006351 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07006352 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11006353 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07006354};
NeilBrown84fc4b52011-10-11 16:49:58 +11006355static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356{
6357 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08006358 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359 .owner = THIS_MODULE,
6360 .make_request = make_request,
6361 .run = run,
6362 .stop = stop,
6363 .status = status,
6364 .error_handler = error,
6365 .hot_add_disk = raid5_add_disk,
6366 .hot_remove_disk= raid5_remove_disk,
6367 .spare_active = raid5_spare_active,
6368 .sync_request = sync_request,
6369 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006370 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08006371 .check_reshape = raid5_check_reshape,
6372 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006373 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07006374 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11006375 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376};
6377
NeilBrown84fc4b52011-10-11 16:49:58 +11006378static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379{
NeilBrown2604b702006-01-06 00:20:36 -08006380 .name = "raid4",
6381 .level = 4,
6382 .owner = THIS_MODULE,
6383 .make_request = make_request,
6384 .run = run,
6385 .stop = stop,
6386 .status = status,
6387 .error_handler = error,
6388 .hot_add_disk = raid5_add_disk,
6389 .hot_remove_disk= raid5_remove_disk,
6390 .spare_active = raid5_spare_active,
6391 .sync_request = sync_request,
6392 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006393 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08006394 .check_reshape = raid5_check_reshape,
6395 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006396 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08006397 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11006398 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08006399};
6400
6401static int __init raid5_init(void)
6402{
NeilBrown16a53ec2006-06-26 00:27:38 -07006403 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006404 register_md_personality(&raid5_personality);
6405 register_md_personality(&raid4_personality);
6406 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407}
6408
NeilBrown2604b702006-01-06 00:20:36 -08006409static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410{
NeilBrown16a53ec2006-06-26 00:27:38 -07006411 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006412 unregister_md_personality(&raid5_personality);
6413 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006414}
6415
6416module_init(raid5_init);
6417module_exit(raid5_exit);
6418MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11006419MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006420MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08006421MODULE_ALIAS("md-raid5");
6422MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08006423MODULE_ALIAS("md-level-5");
6424MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07006425MODULE_ALIAS("md-personality-8"); /* RAID6 */
6426MODULE_ALIAS("md-raid6");
6427MODULE_ALIAS("md-level-6");
6428
6429/* This used to be two separate modules, they were: */
6430MODULE_ALIAS("raid5");
6431MODULE_ALIAS("raid6");