blob: 9ab506df42daf84c7ad4397552e958ec3fa9bddf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * raid5.c : Multiple Devices driver for Linux
3 * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
4 * Copyright (C) 1999, 2000 Ingo Molnar
NeilBrown16a53ec2006-06-26 00:27:38 -07005 * Copyright (C) 2002, 2003 H. Peter Anvin
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
NeilBrown16a53ec2006-06-26 00:27:38 -07007 * RAID-4/5/6 management functions.
8 * Thanks to Penguin Computing for making the RAID-6 development possible
9 * by donating a test server!
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
NeilBrownae3c20c2006-07-10 04:44:17 -070021/*
22 * BITMAP UNPLUGGING:
23 *
24 * The sequencing for updating the bitmap reliably is a little
25 * subtle (and I got it wrong the first time) so it deserves some
26 * explanation.
27 *
28 * We group bitmap updates into batches. Each batch has a number.
29 * We may write out several batches at once, but that isn't very important.
NeilBrown7c13edc2011-04-18 18:25:43 +100030 * conf->seq_write is the number of the last batch successfully written.
31 * conf->seq_flush is the number of the last batch that was closed to
NeilBrownae3c20c2006-07-10 04:44:17 -070032 * new additions.
33 * When we discover that we will need to write to any block in a stripe
34 * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq
NeilBrown7c13edc2011-04-18 18:25:43 +100035 * the number of the batch it will be in. This is seq_flush+1.
NeilBrownae3c20c2006-07-10 04:44:17 -070036 * When we are ready to do a write, if that batch hasn't been written yet,
37 * we plug the array and queue the stripe for later.
38 * When an unplug happens, we increment bm_flush, thus closing the current
39 * batch.
40 * When we notice that bm_flush > bm_write, we write out all pending updates
41 * to the bitmap, and advance bm_write to where bm_flush was.
42 * This may occasionally write a bit out twice, but is sure never to
43 * miss any bits.
44 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
NeilBrownbff61972009-03-31 14:33:13 +110046#include <linux/blkdev.h>
NeilBrownf6705572006-03-27 01:18:11 -080047#include <linux/kthread.h>
Dan Williamsf701d582009-03-31 15:09:39 +110048#include <linux/raid/pq.h>
Dan Williams91c00922007-01-02 13:52:30 -070049#include <linux/async_tx.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040050#include <linux/module.h>
Dan Williams07a3b412009-08-29 19:13:13 -070051#include <linux/async.h>
NeilBrownbff61972009-03-31 14:33:13 +110052#include <linux/seq_file.h>
Dan Williams36d1c642009-07-14 11:48:22 -070053#include <linux/cpu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090054#include <linux/slab.h>
Christian Dietrich8bda4702011-07-27 11:00:36 +100055#include <linux/ratelimit.h>
NeilBrowna9add5d2012-10-31 11:59:09 +110056#include <trace/events/block.h>
57
NeilBrown43b2e5d2009-03-31 14:33:13 +110058#include "md.h"
NeilBrownbff61972009-03-31 14:33:13 +110059#include "raid5.h"
Trela Maciej54071b32010-03-08 16:02:42 +110060#include "raid0.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110061#include "bitmap.h"
NeilBrown72626682005-09-09 16:23:54 -070062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/*
64 * Stripe cache
65 */
66
67#define NR_STRIPES 256
68#define STRIPE_SIZE PAGE_SIZE
69#define STRIPE_SHIFT (PAGE_SHIFT - 9)
70#define STRIPE_SECTORS (STRIPE_SIZE>>9)
71#define IO_THRESHOLD 1
Dan Williams8b3e6cd2008-04-28 02:15:53 -070072#define BYPASS_THRESHOLD 1
NeilBrownfccddba2006-01-06 00:20:33 -080073#define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define HASH_MASK (NR_HASH - 1)
75
NeilBrownd1688a62011-10-11 16:49:52 +110076static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
NeilBrowndb298e12011-10-07 14:23:00 +110077{
78 int hash = (sect >> STRIPE_SHIFT) & HASH_MASK;
79 return &conf->stripe_hashtbl[hash];
80}
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82/* bio's attached to a stripe+device for I/O are linked together in bi_sector
83 * order without overlap. There may be several bio's per stripe+device, and
84 * a bio could span several devices.
85 * When walking this list for a particular stripe+device, we must never proceed
86 * beyond a bio that extends past this device, as the next bio might no longer
87 * be valid.
NeilBrowndb298e12011-10-07 14:23:00 +110088 * This function is used to determine the 'next' bio in the list, given the sector
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 * of the current stripe+device
90 */
NeilBrowndb298e12011-10-07 14:23:00 +110091static inline struct bio *r5_next_bio(struct bio *bio, sector_t sector)
92{
93 int sectors = bio->bi_size >> 9;
94 if (bio->bi_sector + sectors < sector + STRIPE_SECTORS)
95 return bio->bi_next;
96 else
97 return NULL;
98}
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Jens Axboe960e7392008-08-15 10:41:18 +0200100/*
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200101 * We maintain a biased count of active stripes in the bottom 16 bits of
102 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
Jens Axboe960e7392008-08-15 10:41:18 +0200103 */
Shaohua Lie7836bd62012-07-19 16:01:31 +1000104static inline int raid5_bi_processed_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200105{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000106 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
107 return (atomic_read(segments) >> 16) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200108}
109
Shaohua Lie7836bd62012-07-19 16:01:31 +1000110static inline int raid5_dec_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200111{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000112 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
113 return atomic_sub_return(1, segments) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200114}
115
Shaohua Lie7836bd62012-07-19 16:01:31 +1000116static inline void raid5_inc_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200117{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000118 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
119 atomic_inc(segments);
Jens Axboe960e7392008-08-15 10:41:18 +0200120}
121
Shaohua Lie7836bd62012-07-19 16:01:31 +1000122static inline void raid5_set_bi_processed_stripes(struct bio *bio,
123 unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200124{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000125 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
126 int old, new;
Jens Axboe960e7392008-08-15 10:41:18 +0200127
Shaohua Lie7836bd62012-07-19 16:01:31 +1000128 do {
129 old = atomic_read(segments);
130 new = (old & 0xffff) | (cnt << 16);
131 } while (atomic_cmpxchg(segments, old, new) != old);
Jens Axboe960e7392008-08-15 10:41:18 +0200132}
133
Shaohua Lie7836bd62012-07-19 16:01:31 +1000134static inline void raid5_set_bi_stripes(struct bio *bio, unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200135{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000136 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
137 atomic_set(segments, cnt);
Jens Axboe960e7392008-08-15 10:41:18 +0200138}
139
NeilBrownd0dabf72009-03-31 14:39:38 +1100140/* Find first data disk in a raid6 stripe */
141static inline int raid6_d0(struct stripe_head *sh)
142{
NeilBrown67cc2b82009-03-31 14:39:38 +1100143 if (sh->ddf_layout)
144 /* ddf always start from first device */
145 return 0;
146 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100147 if (sh->qd_idx == sh->disks - 1)
148 return 0;
149 else
150 return sh->qd_idx + 1;
151}
NeilBrown16a53ec2006-06-26 00:27:38 -0700152static inline int raid6_next_disk(int disk, int raid_disks)
153{
154 disk++;
155 return (disk < raid_disks) ? disk : 0;
156}
Dan Williamsa4456852007-07-09 11:56:43 -0700157
NeilBrownd0dabf72009-03-31 14:39:38 +1100158/* When walking through the disks in a raid5, starting at raid6_d0,
159 * We need to map each disk to a 'slot', where the data disks are slot
160 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
161 * is raid_disks-1. This help does that mapping.
162 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100163static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
164 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100165{
Dan Williams66295422009-10-19 18:09:32 -0700166 int slot = *count;
NeilBrown67cc2b82009-03-31 14:39:38 +1100167
NeilBrowne4424fe2009-10-16 16:27:34 +1100168 if (sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700169 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100170 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100171 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100172 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100173 return syndrome_disks + 1;
NeilBrowne4424fe2009-10-16 16:27:34 +1100174 if (!sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700175 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100176 return slot;
177}
178
Dan Williamsa4456852007-07-09 11:56:43 -0700179static void return_io(struct bio *return_bi)
180{
181 struct bio *bi = return_bi;
182 while (bi) {
Dan Williamsa4456852007-07-09 11:56:43 -0700183
184 return_bi = bi->bi_next;
185 bi->bi_next = NULL;
186 bi->bi_size = 0;
Neil Brown0e13fe232008-06-28 08:31:20 +1000187 bio_endio(bi, 0);
Dan Williamsa4456852007-07-09 11:56:43 -0700188 bi = return_bi;
189 }
190}
191
NeilBrownd1688a62011-10-11 16:49:52 +1100192static void print_raid5_conf (struct r5conf *conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Dan Williams600aa102008-06-28 08:32:05 +1000194static int stripe_operations_active(struct stripe_head *sh)
195{
196 return sh->check_state || sh->reconstruct_state ||
197 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
198 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
199}
200
Shaohua Li4eb788d2012-07-19 16:01:31 +1000201static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Shaohua Li4eb788d2012-07-19 16:01:31 +1000203 BUG_ON(!list_empty(&sh->lru));
204 BUG_ON(atomic_read(&conf->active_stripes)==0);
205 if (test_bit(STRIPE_HANDLE, &sh->state)) {
206 if (test_bit(STRIPE_DELAYED, &sh->state) &&
207 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
208 list_add_tail(&sh->lru, &conf->delayed_list);
209 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
210 sh->bm_seq - conf->seq_write > 0)
211 list_add_tail(&sh->lru, &conf->bitmap_list);
212 else {
213 clear_bit(STRIPE_DELAYED, &sh->state);
214 clear_bit(STRIPE_BIT_DELAY, &sh->state);
215 list_add_tail(&sh->lru, &conf->handle_list);
216 }
217 md_wakeup_thread(conf->mddev->thread);
218 } else {
219 BUG_ON(stripe_operations_active(sh));
220 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
221 if (atomic_dec_return(&conf->preread_active_stripes)
222 < IO_THRESHOLD)
223 md_wakeup_thread(conf->mddev->thread);
224 atomic_dec(&conf->active_stripes);
225 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
226 list_add_tail(&sh->lru, &conf->inactive_list);
227 wake_up(&conf->wait_for_stripe);
228 if (conf->retry_read_aligned)
229 md_wakeup_thread(conf->mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
231 }
232}
NeilBrownd0dabf72009-03-31 14:39:38 +1100233
Shaohua Li4eb788d2012-07-19 16:01:31 +1000234static void __release_stripe(struct r5conf *conf, struct stripe_head *sh)
235{
236 if (atomic_dec_and_test(&sh->count))
237 do_release_stripe(conf, sh);
238}
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240static void release_stripe(struct stripe_head *sh)
241{
NeilBrownd1688a62011-10-11 16:49:52 +1100242 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 unsigned long flags;
NeilBrown16a53ec2006-06-26 00:27:38 -0700244
Shaohua Li4eb788d2012-07-19 16:01:31 +1000245 local_irq_save(flags);
246 if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) {
247 do_release_stripe(conf, sh);
248 spin_unlock(&conf->device_lock);
249 }
250 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
NeilBrownfccddba2006-01-06 00:20:33 -0800253static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Dan Williams45b42332007-07-09 11:56:43 -0700255 pr_debug("remove_hash(), stripe %llu\n",
256 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
NeilBrownfccddba2006-01-06 00:20:33 -0800258 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
NeilBrownd1688a62011-10-11 16:49:52 +1100261static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
NeilBrownfccddba2006-01-06 00:20:33 -0800263 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Dan Williams45b42332007-07-09 11:56:43 -0700265 pr_debug("insert_hash(), stripe %llu\n",
266 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
NeilBrownfccddba2006-01-06 00:20:33 -0800268 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
271
272/* find an idle stripe, make sure it is unhashed, and return it. */
NeilBrownd1688a62011-10-11 16:49:52 +1100273static struct stripe_head *get_free_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 struct stripe_head *sh = NULL;
276 struct list_head *first;
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (list_empty(&conf->inactive_list))
279 goto out;
280 first = conf->inactive_list.next;
281 sh = list_entry(first, struct stripe_head, lru);
282 list_del_init(first);
283 remove_hash(sh);
284 atomic_inc(&conf->active_stripes);
285out:
286 return sh;
287}
288
NeilBrowne4e11e32010-06-16 16:45:16 +1000289static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 struct page *p;
292 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000293 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
NeilBrowne4e11e32010-06-16 16:45:16 +1000295 for (i = 0; i < num ; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 p = sh->dev[i].page;
297 if (!p)
298 continue;
299 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800300 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302}
303
NeilBrowne4e11e32010-06-16 16:45:16 +1000304static int grow_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
306 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000307 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
NeilBrowne4e11e32010-06-16 16:45:16 +1000309 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 struct page *page;
311
312 if (!(page = alloc_page(GFP_KERNEL))) {
313 return 1;
314 }
315 sh->dev[i].page = page;
316 }
317 return 0;
318}
319
NeilBrown784052e2009-03-31 15:19:07 +1100320static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrownd1688a62011-10-11 16:49:52 +1100321static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100322 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
NeilBrownb5663ba2009-03-31 14:39:38 +1100324static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
NeilBrownd1688a62011-10-11 16:49:52 +1100326 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800327 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200329 BUG_ON(atomic_read(&sh->count) != 0);
330 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000331 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700332
Dan Williams45b42332007-07-09 11:56:43 -0700333 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 (unsigned long long)sh->sector);
335
336 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700337
NeilBrown86b42c72009-03-31 15:19:03 +1100338 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100339 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100341 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 sh->state = 0;
343
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800344
345 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 struct r5dev *dev = &sh->dev[i];
347
Dan Williamsd84e0f12007-01-02 13:52:30 -0700348 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700350 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700352 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000354 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
356 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100357 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
359 insert_hash(conf, sh);
360}
361
NeilBrownd1688a62011-10-11 16:49:52 +1100362static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100363 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
365 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -0800366 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Dan Williams45b42332007-07-09 11:56:43 -0700368 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
NeilBrownfccddba2006-01-06 00:20:33 -0800369 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100370 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700372 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return NULL;
374}
375
NeilBrown674806d2010-06-16 17:17:53 +1000376/*
377 * Need to check if array has failed when deciding whether to:
378 * - start an array
379 * - remove non-faulty devices
380 * - add a spare
381 * - allow a reshape
382 * This determination is simple when no reshape is happening.
383 * However if there is a reshape, we need to carefully check
384 * both the before and after sections.
385 * This is because some failed devices may only affect one
386 * of the two sections, and some non-in_sync devices may
387 * be insync in the section most affected by failed devices.
388 */
NeilBrown908f4fb2011-12-23 10:17:50 +1100389static int calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000390{
NeilBrown908f4fb2011-12-23 10:17:50 +1100391 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000392 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000393
394 rcu_read_lock();
395 degraded = 0;
396 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100397 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000398 if (rdev && test_bit(Faulty, &rdev->flags))
399 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000400 if (!rdev || test_bit(Faulty, &rdev->flags))
401 degraded++;
402 else if (test_bit(In_sync, &rdev->flags))
403 ;
404 else
405 /* not in-sync or faulty.
406 * If the reshape increases the number of devices,
407 * this is being recovered by the reshape, so
408 * this 'previous' section is not in_sync.
409 * If the number of devices is being reduced however,
410 * the device can only be part of the array if
411 * we are reverting a reshape, so this section will
412 * be in-sync.
413 */
414 if (conf->raid_disks >= conf->previous_raid_disks)
415 degraded++;
416 }
417 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100418 if (conf->raid_disks == conf->previous_raid_disks)
419 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000420 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100421 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000422 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100423 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000424 if (rdev && test_bit(Faulty, &rdev->flags))
425 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000426 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100427 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000428 else if (test_bit(In_sync, &rdev->flags))
429 ;
430 else
431 /* not in-sync or faulty.
432 * If reshape increases the number of devices, this
433 * section has already been recovered, else it
434 * almost certainly hasn't.
435 */
436 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100437 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000438 }
439 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100440 if (degraded2 > degraded)
441 return degraded2;
442 return degraded;
443}
444
445static int has_failed(struct r5conf *conf)
446{
447 int degraded;
448
449 if (conf->mddev->reshape_position == MaxSector)
450 return conf->mddev->degraded > conf->max_degraded;
451
452 degraded = calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000453 if (degraded > conf->max_degraded)
454 return 1;
455 return 0;
456}
457
NeilBrownb5663ba2009-03-31 14:39:38 +1100458static struct stripe_head *
NeilBrownd1688a62011-10-11 16:49:52 +1100459get_active_stripe(struct r5conf *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000460 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
462 struct stripe_head *sh;
463
Dan Williams45b42332007-07-09 11:56:43 -0700464 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 spin_lock_irq(&conf->device_lock);
467
468 do {
NeilBrown72626682005-09-09 16:23:54 -0700469 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000470 conf->quiesce == 0 || noquiesce,
Lukas Czernereed8c022012-11-30 11:42:40 +0100471 conf->device_lock);
NeilBrown86b42c72009-03-31 15:19:03 +1100472 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (!sh) {
474 if (!conf->inactive_blocked)
475 sh = get_free_stripe(conf);
476 if (noblock && sh == NULL)
477 break;
478 if (!sh) {
479 conf->inactive_blocked = 1;
480 wait_event_lock_irq(conf->wait_for_stripe,
481 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800482 (atomic_read(&conf->active_stripes)
483 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 || !conf->inactive_blocked),
Lukas Czernereed8c022012-11-30 11:42:40 +0100485 conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 conf->inactive_blocked = 0;
487 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100488 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 } else {
490 if (atomic_read(&sh->count)) {
NeilBrownab69ae12009-03-31 15:26:47 +1100491 BUG_ON(!list_empty(&sh->lru)
Shaohua Li8811b592012-08-02 08:33:00 +1000492 && !test_bit(STRIPE_EXPANDING, &sh->state)
493 && !test_bit(STRIPE_ON_UNPLUG_LIST, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 } else {
495 if (!test_bit(STRIPE_HANDLE, &sh->state))
496 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700497 if (list_empty(&sh->lru) &&
498 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700499 BUG();
500 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
502 }
503 } while (sh == NULL);
504
505 if (sh)
506 atomic_inc(&sh->count);
507
508 spin_unlock_irq(&conf->device_lock);
509 return sh;
510}
511
NeilBrown05616be2012-05-21 09:27:00 +1000512/* Determine if 'data_offset' or 'new_data_offset' should be used
513 * in this stripe_head.
514 */
515static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
516{
517 sector_t progress = conf->reshape_progress;
518 /* Need a memory barrier to make sure we see the value
519 * of conf->generation, or ->data_offset that was set before
520 * reshape_progress was updated.
521 */
522 smp_rmb();
523 if (progress == MaxSector)
524 return 0;
525 if (sh->generation == conf->generation - 1)
526 return 0;
527 /* We are in a reshape, and this is a new-generation stripe,
528 * so use new_data_offset.
529 */
530 return 1;
531}
532
NeilBrown6712ecf2007-09-27 12:47:43 +0200533static void
534raid5_end_read_request(struct bio *bi, int error);
535static void
536raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700537
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000538static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700539{
NeilBrownd1688a62011-10-11 16:49:52 +1100540 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700541 int i, disks = sh->disks;
542
543 might_sleep();
544
545 for (i = disks; i--; ) {
546 int rw;
NeilBrown9a3e1102011-12-23 10:17:53 +1100547 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100548 struct bio *bi, *rbi;
549 struct md_rdev *rdev, *rrdev = NULL;
Tejun Heoe9c74692010-09-03 11:56:18 +0200550 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
551 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
552 rw = WRITE_FUA;
553 else
554 rw = WRITE;
Shaohua Li9e4447682012-10-11 13:49:49 +1100555 if (test_bit(R5_Discard, &sh->dev[i].flags))
Shaohua Li620125f2012-10-11 13:49:05 +1100556 rw |= REQ_DISCARD;
Tejun Heoe9c74692010-09-03 11:56:18 +0200557 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700558 rw = READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100559 else if (test_and_clear_bit(R5_WantReplace,
560 &sh->dev[i].flags)) {
561 rw = WRITE;
562 replace_only = 1;
563 } else
Dan Williams91c00922007-01-02 13:52:30 -0700564 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +1000565 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
566 rw |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -0700567
568 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100569 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700570
571 bi->bi_rw = rw;
NeilBrown977df362011-12-23 10:17:53 +1100572 rbi->bi_rw = rw;
573 if (rw & WRITE) {
Dan Williams91c00922007-01-02 13:52:30 -0700574 bi->bi_end_io = raid5_end_write_request;
NeilBrown977df362011-12-23 10:17:53 +1100575 rbi->bi_end_io = raid5_end_write_request;
576 } else
Dan Williams91c00922007-01-02 13:52:30 -0700577 bi->bi_end_io = raid5_end_read_request;
578
579 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100580 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100581 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
582 rdev = rcu_dereference(conf->disks[i].rdev);
583 if (!rdev) {
584 rdev = rrdev;
585 rrdev = NULL;
586 }
NeilBrown9a3e1102011-12-23 10:17:53 +1100587 if (rw & WRITE) {
588 if (replace_only)
589 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100590 if (rdev == rrdev)
591 /* We raced and saw duplicates */
592 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100593 } else {
NeilBrowndd054fc2011-12-23 10:17:53 +1100594 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100595 rdev = rrdev;
596 rrdev = NULL;
597 }
NeilBrown977df362011-12-23 10:17:53 +1100598
Dan Williams91c00922007-01-02 13:52:30 -0700599 if (rdev && test_bit(Faulty, &rdev->flags))
600 rdev = NULL;
601 if (rdev)
602 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100603 if (rrdev && test_bit(Faulty, &rrdev->flags))
604 rrdev = NULL;
605 if (rrdev)
606 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700607 rcu_read_unlock();
608
NeilBrown73e92e52011-07-28 11:39:22 +1000609 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100610 * need to check for writes. We never accept write errors
611 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000612 */
613 while ((rw & WRITE) && rdev &&
614 test_bit(WriteErrorSeen, &rdev->flags)) {
615 sector_t first_bad;
616 int bad_sectors;
617 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
618 &first_bad, &bad_sectors);
619 if (!bad)
620 break;
621
622 if (bad < 0) {
623 set_bit(BlockedBadBlocks, &rdev->flags);
624 if (!conf->mddev->external &&
625 conf->mddev->flags) {
626 /* It is very unlikely, but we might
627 * still need to write out the
628 * bad block log - better give it
629 * a chance*/
630 md_check_recovery(conf->mddev);
631 }
majianpeng18507532012-07-03 12:11:54 +1000632 /*
633 * Because md_wait_for_blocked_rdev
634 * will dec nr_pending, we must
635 * increment it first.
636 */
637 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +1000638 md_wait_for_blocked_rdev(rdev, conf->mddev);
639 } else {
640 /* Acknowledged bad block - skip the write */
641 rdev_dec_pending(rdev, conf->mddev);
642 rdev = NULL;
643 }
644 }
645
Dan Williams91c00922007-01-02 13:52:30 -0700646 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100647 if (s->syncing || s->expanding || s->expanded
648 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700649 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
650
Dan Williams2b7497f2008-06-28 08:31:52 +1000651 set_bit(STRIPE_IO_STARTED, &sh->state);
652
Dan Williams91c00922007-01-02 13:52:30 -0700653 bi->bi_bdev = rdev->bdev;
654 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700655 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700656 bi->bi_rw, i);
657 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000658 if (use_new_offset(conf, sh))
659 bi->bi_sector = (sh->sector
660 + rdev->new_data_offset);
661 else
662 bi->bi_sector = (sh->sector
663 + rdev->data_offset);
majianpeng3f9e7c12012-07-31 10:04:21 +1000664 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
665 bi->bi_rw |= REQ_FLUSH;
666
Dan Williams91c00922007-01-02 13:52:30 -0700667 bi->bi_flags = 1 << BIO_UPTODATE;
Dan Williams91c00922007-01-02 13:52:30 -0700668 bi->bi_idx = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700669 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
670 bi->bi_io_vec[0].bv_offset = 0;
671 bi->bi_size = STRIPE_SIZE;
672 bi->bi_next = NULL;
NeilBrown977df362011-12-23 10:17:53 +1100673 if (rrdev)
674 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
NeilBrowna9add5d2012-10-31 11:59:09 +1100675 trace_block_bio_remap(bdev_get_queue(bi->bi_bdev),
676 bi, disk_devt(conf->mddev->gendisk),
677 sh->dev[i].sector);
Dan Williams91c00922007-01-02 13:52:30 -0700678 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100679 }
680 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100681 if (s->syncing || s->expanding || s->expanded
682 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +1100683 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
684
685 set_bit(STRIPE_IO_STARTED, &sh->state);
686
687 rbi->bi_bdev = rrdev->bdev;
688 pr_debug("%s: for %llu schedule op %ld on "
689 "replacement disc %d\n",
690 __func__, (unsigned long long)sh->sector,
691 rbi->bi_rw, i);
692 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000693 if (use_new_offset(conf, sh))
694 rbi->bi_sector = (sh->sector
695 + rrdev->new_data_offset);
696 else
697 rbi->bi_sector = (sh->sector
698 + rrdev->data_offset);
NeilBrown977df362011-12-23 10:17:53 +1100699 rbi->bi_flags = 1 << BIO_UPTODATE;
700 rbi->bi_idx = 0;
701 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
702 rbi->bi_io_vec[0].bv_offset = 0;
703 rbi->bi_size = STRIPE_SIZE;
704 rbi->bi_next = NULL;
NeilBrowna9add5d2012-10-31 11:59:09 +1100705 trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev),
706 rbi, disk_devt(conf->mddev->gendisk),
707 sh->dev[i].sector);
NeilBrown977df362011-12-23 10:17:53 +1100708 generic_make_request(rbi);
709 }
710 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000711 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700712 set_bit(STRIPE_DEGRADED, &sh->state);
713 pr_debug("skip op %ld on disc %d for sector %llu\n",
714 bi->bi_rw, i, (unsigned long long)sh->sector);
715 clear_bit(R5_LOCKED, &sh->dev[i].flags);
716 set_bit(STRIPE_HANDLE, &sh->state);
717 }
718 }
719}
720
721static struct dma_async_tx_descriptor *
722async_copy_data(int frombio, struct bio *bio, struct page *page,
723 sector_t sector, struct dma_async_tx_descriptor *tx)
724{
725 struct bio_vec *bvl;
726 struct page *bio_page;
727 int i;
728 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700729 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700730 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700731
732 if (bio->bi_sector >= sector)
733 page_offset = (signed)(bio->bi_sector - sector) * 512;
734 else
735 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700736
Dan Williams0403e382009-09-08 17:42:50 -0700737 if (frombio)
738 flags |= ASYNC_TX_FENCE;
739 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
740
Dan Williams91c00922007-01-02 13:52:30 -0700741 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000742 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700743 int clen;
744 int b_offset = 0;
745
746 if (page_offset < 0) {
747 b_offset = -page_offset;
748 page_offset += b_offset;
749 len -= b_offset;
750 }
751
752 if (len > 0 && page_offset + len > STRIPE_SIZE)
753 clen = STRIPE_SIZE - page_offset;
754 else
755 clen = len;
756
757 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000758 b_offset += bvl->bv_offset;
759 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700760 if (frombio)
761 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700762 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700763 else
764 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700765 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700766 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700767 /* chain the operations */
768 submit.depend_tx = tx;
769
Dan Williams91c00922007-01-02 13:52:30 -0700770 if (clen < len) /* hit end of page */
771 break;
772 page_offset += len;
773 }
774
775 return tx;
776}
777
778static void ops_complete_biofill(void *stripe_head_ref)
779{
780 struct stripe_head *sh = stripe_head_ref;
781 struct bio *return_bi = NULL;
Dan Williamse4d84902007-09-24 10:06:13 -0700782 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700783
Harvey Harrisone46b2722008-04-28 02:15:50 -0700784 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700785 (unsigned long long)sh->sector);
786
787 /* clear completed biofills */
788 for (i = sh->disks; i--; ) {
789 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700790
791 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700792 /* and check if we need to reply to a read request,
793 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000794 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700795 */
796 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700797 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700798
Dan Williams91c00922007-01-02 13:52:30 -0700799 BUG_ON(!dev->read);
800 rbi = dev->read;
801 dev->read = NULL;
802 while (rbi && rbi->bi_sector <
803 dev->sector + STRIPE_SECTORS) {
804 rbi2 = r5_next_bio(rbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +1000805 if (!raid5_dec_bi_active_stripes(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700806 rbi->bi_next = return_bi;
807 return_bi = rbi;
808 }
Dan Williams91c00922007-01-02 13:52:30 -0700809 rbi = rbi2;
810 }
811 }
812 }
Dan Williams83de75c2008-06-28 08:31:58 +1000813 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700814
815 return_io(return_bi);
816
Dan Williamse4d84902007-09-24 10:06:13 -0700817 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700818 release_stripe(sh);
819}
820
821static void ops_run_biofill(struct stripe_head *sh)
822{
823 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa08abd82009-06-03 11:43:59 -0700824 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700825 int i;
826
Harvey Harrisone46b2722008-04-28 02:15:50 -0700827 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700828 (unsigned long long)sh->sector);
829
830 for (i = sh->disks; i--; ) {
831 struct r5dev *dev = &sh->dev[i];
832 if (test_bit(R5_Wantfill, &dev->flags)) {
833 struct bio *rbi;
Shaohua Lib17459c2012-07-19 16:01:31 +1000834 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700835 dev->read = rbi = dev->toread;
836 dev->toread = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +1000837 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700838 while (rbi && rbi->bi_sector <
839 dev->sector + STRIPE_SECTORS) {
840 tx = async_copy_data(0, rbi, dev->page,
841 dev->sector, tx);
842 rbi = r5_next_bio(rbi, dev->sector);
843 }
844 }
845 }
846
847 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700848 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
849 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700850}
851
Dan Williams4e7d2c02009-08-29 19:13:11 -0700852static void mark_target_uptodate(struct stripe_head *sh, int target)
853{
854 struct r5dev *tgt;
855
856 if (target < 0)
857 return;
858
859 tgt = &sh->dev[target];
860 set_bit(R5_UPTODATE, &tgt->flags);
861 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
862 clear_bit(R5_Wantcompute, &tgt->flags);
863}
864
Dan Williamsac6b53b2009-07-14 13:40:19 -0700865static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700866{
867 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700868
Harvey Harrisone46b2722008-04-28 02:15:50 -0700869 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700870 (unsigned long long)sh->sector);
871
Dan Williamsac6b53b2009-07-14 13:40:19 -0700872 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700873 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700874 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700875
Dan Williamsecc65c92008-06-28 08:31:57 +1000876 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
877 if (sh->check_state == check_state_compute_run)
878 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700879 set_bit(STRIPE_HANDLE, &sh->state);
880 release_stripe(sh);
881}
882
Dan Williamsd6f38f32009-07-14 11:50:52 -0700883/* return a pointer to the address conversion region of the scribble buffer */
884static addr_conv_t *to_addr_conv(struct stripe_head *sh,
885 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700886{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700887 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
888}
889
890static struct dma_async_tx_descriptor *
891ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
892{
Dan Williams91c00922007-01-02 13:52:30 -0700893 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700894 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700895 int target = sh->ops.target;
896 struct r5dev *tgt = &sh->dev[target];
897 struct page *xor_dest = tgt->page;
898 int count = 0;
899 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700900 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700901 int i;
902
903 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700904 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700905 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
906
907 for (i = disks; i--; )
908 if (i != target)
909 xor_srcs[count++] = sh->dev[i].page;
910
911 atomic_inc(&sh->count);
912
Dan Williams0403e382009-09-08 17:42:50 -0700913 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700914 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700915 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700916 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700917 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700918 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700919
Dan Williams91c00922007-01-02 13:52:30 -0700920 return tx;
921}
922
Dan Williamsac6b53b2009-07-14 13:40:19 -0700923/* set_syndrome_sources - populate source buffers for gen_syndrome
924 * @srcs - (struct page *) array of size sh->disks
925 * @sh - stripe_head to parse
926 *
927 * Populates srcs in proper layout order for the stripe and returns the
928 * 'count' of sources to be used in a call to async_gen_syndrome. The P
929 * destination buffer is recorded in srcs[count] and the Q destination
930 * is recorded in srcs[count+1]].
931 */
932static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
933{
934 int disks = sh->disks;
935 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
936 int d0_idx = raid6_d0(sh);
937 int count;
938 int i;
939
940 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100941 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700942
943 count = 0;
944 i = d0_idx;
945 do {
946 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
947
948 srcs[slot] = sh->dev[i].page;
949 i = raid6_next_disk(i, disks);
950 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700951
NeilBrowne4424fe2009-10-16 16:27:34 +1100952 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700953}
954
955static struct dma_async_tx_descriptor *
956ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
957{
958 int disks = sh->disks;
959 struct page **blocks = percpu->scribble;
960 int target;
961 int qd_idx = sh->qd_idx;
962 struct dma_async_tx_descriptor *tx;
963 struct async_submit_ctl submit;
964 struct r5dev *tgt;
965 struct page *dest;
966 int i;
967 int count;
968
969 if (sh->ops.target < 0)
970 target = sh->ops.target2;
971 else if (sh->ops.target2 < 0)
972 target = sh->ops.target;
973 else
974 /* we should only have one valid target */
975 BUG();
976 BUG_ON(target < 0);
977 pr_debug("%s: stripe %llu block: %d\n",
978 __func__, (unsigned long long)sh->sector, target);
979
980 tgt = &sh->dev[target];
981 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
982 dest = tgt->page;
983
984 atomic_inc(&sh->count);
985
986 if (target == qd_idx) {
987 count = set_syndrome_sources(blocks, sh);
988 blocks[count] = NULL; /* regenerating p is not necessary */
989 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700990 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
991 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700992 to_addr_conv(sh, percpu));
993 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
994 } else {
995 /* Compute any data- or p-drive using XOR */
996 count = 0;
997 for (i = disks; i-- ; ) {
998 if (i == target || i == qd_idx)
999 continue;
1000 blocks[count++] = sh->dev[i].page;
1001 }
1002
Dan Williams0403e382009-09-08 17:42:50 -07001003 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1004 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001005 to_addr_conv(sh, percpu));
1006 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
1007 }
1008
1009 return tx;
1010}
1011
1012static struct dma_async_tx_descriptor *
1013ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1014{
1015 int i, count, disks = sh->disks;
1016 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1017 int d0_idx = raid6_d0(sh);
1018 int faila = -1, failb = -1;
1019 int target = sh->ops.target;
1020 int target2 = sh->ops.target2;
1021 struct r5dev *tgt = &sh->dev[target];
1022 struct r5dev *tgt2 = &sh->dev[target2];
1023 struct dma_async_tx_descriptor *tx;
1024 struct page **blocks = percpu->scribble;
1025 struct async_submit_ctl submit;
1026
1027 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1028 __func__, (unsigned long long)sh->sector, target, target2);
1029 BUG_ON(target < 0 || target2 < 0);
1030 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1031 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1032
Dan Williams6c910a72009-09-16 12:24:54 -07001033 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001034 * slot number conversion for 'faila' and 'failb'
1035 */
1036 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001037 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001038 count = 0;
1039 i = d0_idx;
1040 do {
1041 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1042
1043 blocks[slot] = sh->dev[i].page;
1044
1045 if (i == target)
1046 faila = slot;
1047 if (i == target2)
1048 failb = slot;
1049 i = raid6_next_disk(i, disks);
1050 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001051
1052 BUG_ON(faila == failb);
1053 if (failb < faila)
1054 swap(faila, failb);
1055 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1056 __func__, (unsigned long long)sh->sector, faila, failb);
1057
1058 atomic_inc(&sh->count);
1059
1060 if (failb == syndrome_disks+1) {
1061 /* Q disk is one of the missing disks */
1062 if (faila == syndrome_disks) {
1063 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001064 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1065 ops_complete_compute, sh,
1066 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +11001067 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001068 STRIPE_SIZE, &submit);
1069 } else {
1070 struct page *dest;
1071 int data_target;
1072 int qd_idx = sh->qd_idx;
1073
1074 /* Missing D+Q: recompute D from P, then recompute Q */
1075 if (target == qd_idx)
1076 data_target = target2;
1077 else
1078 data_target = target;
1079
1080 count = 0;
1081 for (i = disks; i-- ; ) {
1082 if (i == data_target || i == qd_idx)
1083 continue;
1084 blocks[count++] = sh->dev[i].page;
1085 }
1086 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001087 init_async_submit(&submit,
1088 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1089 NULL, NULL, NULL,
1090 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001091 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1092 &submit);
1093
1094 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001095 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1096 ops_complete_compute, sh,
1097 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001098 return async_gen_syndrome(blocks, 0, count+2,
1099 STRIPE_SIZE, &submit);
1100 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001101 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001102 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1103 ops_complete_compute, sh,
1104 to_addr_conv(sh, percpu));
1105 if (failb == syndrome_disks) {
1106 /* We're missing D+P. */
1107 return async_raid6_datap_recov(syndrome_disks+2,
1108 STRIPE_SIZE, faila,
1109 blocks, &submit);
1110 } else {
1111 /* We're missing D+D. */
1112 return async_raid6_2data_recov(syndrome_disks+2,
1113 STRIPE_SIZE, faila, failb,
1114 blocks, &submit);
1115 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001116 }
1117}
1118
1119
Dan Williams91c00922007-01-02 13:52:30 -07001120static void ops_complete_prexor(void *stripe_head_ref)
1121{
1122 struct stripe_head *sh = stripe_head_ref;
1123
Harvey Harrisone46b2722008-04-28 02:15:50 -07001124 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001125 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001126}
1127
1128static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001129ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1130 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001131{
Dan Williams91c00922007-01-02 13:52:30 -07001132 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001133 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001134 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001135 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001136
1137 /* existing parity data subtracted */
1138 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1139
Harvey Harrisone46b2722008-04-28 02:15:50 -07001140 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001141 (unsigned long long)sh->sector);
1142
1143 for (i = disks; i--; ) {
1144 struct r5dev *dev = &sh->dev[i];
1145 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001146 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001147 xor_srcs[count++] = dev->page;
1148 }
1149
Dan Williams0403e382009-09-08 17:42:50 -07001150 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001151 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001152 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001153
1154 return tx;
1155}
1156
1157static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001158ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001159{
1160 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001161 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001162
Harvey Harrisone46b2722008-04-28 02:15:50 -07001163 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001164 (unsigned long long)sh->sector);
1165
1166 for (i = disks; i--; ) {
1167 struct r5dev *dev = &sh->dev[i];
1168 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001169
Dan Williamsd8ee0722008-06-28 08:32:06 +10001170 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001171 struct bio *wbi;
1172
Shaohua Lib17459c2012-07-19 16:01:31 +10001173 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001174 chosen = dev->towrite;
1175 dev->towrite = NULL;
1176 BUG_ON(dev->written);
1177 wbi = dev->written = chosen;
Shaohua Lib17459c2012-07-19 16:01:31 +10001178 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001179
1180 while (wbi && wbi->bi_sector <
1181 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001182 if (wbi->bi_rw & REQ_FUA)
1183 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001184 if (wbi->bi_rw & REQ_SYNC)
1185 set_bit(R5_SyncIO, &dev->flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001186 if (wbi->bi_rw & REQ_DISCARD)
Shaohua Li620125f2012-10-11 13:49:05 +11001187 set_bit(R5_Discard, &dev->flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001188 else
Shaohua Li620125f2012-10-11 13:49:05 +11001189 tx = async_copy_data(1, wbi, dev->page,
1190 dev->sector, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001191 wbi = r5_next_bio(wbi, dev->sector);
1192 }
1193 }
1194 }
1195
1196 return tx;
1197}
1198
Dan Williamsac6b53b2009-07-14 13:40:19 -07001199static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001200{
1201 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001202 int disks = sh->disks;
1203 int pd_idx = sh->pd_idx;
1204 int qd_idx = sh->qd_idx;
1205 int i;
Shaohua Li9e4447682012-10-11 13:49:49 +11001206 bool fua = false, sync = false, discard = false;
Dan Williams91c00922007-01-02 13:52:30 -07001207
Harvey Harrisone46b2722008-04-28 02:15:50 -07001208 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001209 (unsigned long long)sh->sector);
1210
Shaohua Libc0934f2012-05-22 13:55:05 +10001211 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001212 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001213 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001214 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001215 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001216
Dan Williams91c00922007-01-02 13:52:30 -07001217 for (i = disks; i--; ) {
1218 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001219
Tejun Heoe9c74692010-09-03 11:56:18 +02001220 if (dev->written || i == pd_idx || i == qd_idx) {
Shaohua Li9e4447682012-10-11 13:49:49 +11001221 if (!discard)
1222 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001223 if (fua)
1224 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001225 if (sync)
1226 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001227 }
Dan Williams91c00922007-01-02 13:52:30 -07001228 }
1229
Dan Williamsd8ee0722008-06-28 08:32:06 +10001230 if (sh->reconstruct_state == reconstruct_state_drain_run)
1231 sh->reconstruct_state = reconstruct_state_drain_result;
1232 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1233 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1234 else {
1235 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1236 sh->reconstruct_state = reconstruct_state_result;
1237 }
Dan Williams91c00922007-01-02 13:52:30 -07001238
1239 set_bit(STRIPE_HANDLE, &sh->state);
1240 release_stripe(sh);
1241}
1242
1243static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001244ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1245 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001246{
Dan Williams91c00922007-01-02 13:52:30 -07001247 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001248 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001249 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001250 int count = 0, pd_idx = sh->pd_idx, i;
1251 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001252 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001253 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001254
Harvey Harrisone46b2722008-04-28 02:15:50 -07001255 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001256 (unsigned long long)sh->sector);
1257
Shaohua Li620125f2012-10-11 13:49:05 +11001258 for (i = 0; i < sh->disks; i++) {
1259 if (pd_idx == i)
1260 continue;
1261 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1262 break;
1263 }
1264 if (i >= sh->disks) {
1265 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001266 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1267 ops_complete_reconstruct(sh);
1268 return;
1269 }
Dan Williams91c00922007-01-02 13:52:30 -07001270 /* check if prexor is active which means only process blocks
1271 * that are part of a read-modify-write (written)
1272 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001273 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1274 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001275 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1276 for (i = disks; i--; ) {
1277 struct r5dev *dev = &sh->dev[i];
1278 if (dev->written)
1279 xor_srcs[count++] = dev->page;
1280 }
1281 } else {
1282 xor_dest = sh->dev[pd_idx].page;
1283 for (i = disks; i--; ) {
1284 struct r5dev *dev = &sh->dev[i];
1285 if (i != pd_idx)
1286 xor_srcs[count++] = dev->page;
1287 }
1288 }
1289
Dan Williams91c00922007-01-02 13:52:30 -07001290 /* 1/ if we prexor'd then the dest is reused as a source
1291 * 2/ if we did not prexor then we are redoing the parity
1292 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1293 * for the synchronous xor case
1294 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001295 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001296 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1297
1298 atomic_inc(&sh->count);
1299
Dan Williamsac6b53b2009-07-14 13:40:19 -07001300 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001301 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001302 if (unlikely(count == 1))
1303 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1304 else
1305 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001306}
1307
Dan Williamsac6b53b2009-07-14 13:40:19 -07001308static void
1309ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1310 struct dma_async_tx_descriptor *tx)
1311{
1312 struct async_submit_ctl submit;
1313 struct page **blocks = percpu->scribble;
Shaohua Li620125f2012-10-11 13:49:05 +11001314 int count, i;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001315
1316 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1317
Shaohua Li620125f2012-10-11 13:49:05 +11001318 for (i = 0; i < sh->disks; i++) {
1319 if (sh->pd_idx == i || sh->qd_idx == i)
1320 continue;
1321 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1322 break;
1323 }
1324 if (i >= sh->disks) {
1325 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001326 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1327 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1328 ops_complete_reconstruct(sh);
1329 return;
1330 }
1331
Dan Williamsac6b53b2009-07-14 13:40:19 -07001332 count = set_syndrome_sources(blocks, sh);
1333
1334 atomic_inc(&sh->count);
1335
1336 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1337 sh, to_addr_conv(sh, percpu));
1338 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001339}
1340
1341static void ops_complete_check(void *stripe_head_ref)
1342{
1343 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001344
Harvey Harrisone46b2722008-04-28 02:15:50 -07001345 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001346 (unsigned long long)sh->sector);
1347
Dan Williamsecc65c92008-06-28 08:31:57 +10001348 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001349 set_bit(STRIPE_HANDLE, &sh->state);
1350 release_stripe(sh);
1351}
1352
Dan Williamsac6b53b2009-07-14 13:40:19 -07001353static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001354{
Dan Williams91c00922007-01-02 13:52:30 -07001355 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001356 int pd_idx = sh->pd_idx;
1357 int qd_idx = sh->qd_idx;
1358 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001359 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001360 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001361 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001362 int count;
1363 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001364
Harvey Harrisone46b2722008-04-28 02:15:50 -07001365 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001366 (unsigned long long)sh->sector);
1367
Dan Williamsac6b53b2009-07-14 13:40:19 -07001368 count = 0;
1369 xor_dest = sh->dev[pd_idx].page;
1370 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001371 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001372 if (i == pd_idx || i == qd_idx)
1373 continue;
1374 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001375 }
1376
Dan Williamsd6f38f32009-07-14 11:50:52 -07001377 init_async_submit(&submit, 0, NULL, NULL, NULL,
1378 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001379 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001380 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001381
Dan Williams91c00922007-01-02 13:52:30 -07001382 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001383 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1384 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001385}
1386
Dan Williamsac6b53b2009-07-14 13:40:19 -07001387static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1388{
1389 struct page **srcs = percpu->scribble;
1390 struct async_submit_ctl submit;
1391 int count;
1392
1393 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1394 (unsigned long long)sh->sector, checkp);
1395
1396 count = set_syndrome_sources(srcs, sh);
1397 if (!checkp)
1398 srcs[count] = NULL;
1399
1400 atomic_inc(&sh->count);
1401 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1402 sh, to_addr_conv(sh, percpu));
1403 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1404 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1405}
1406
Dan Williams417b8d42009-10-16 16:25:22 +11001407static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001408{
1409 int overlap_clear = 0, i, disks = sh->disks;
1410 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001411 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001412 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001413 struct raid5_percpu *percpu;
1414 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001415
Dan Williamsd6f38f32009-07-14 11:50:52 -07001416 cpu = get_cpu();
1417 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001418 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001419 ops_run_biofill(sh);
1420 overlap_clear++;
1421 }
1422
Dan Williams7b3a8712008-06-28 08:32:09 +10001423 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001424 if (level < 6)
1425 tx = ops_run_compute5(sh, percpu);
1426 else {
1427 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1428 tx = ops_run_compute6_1(sh, percpu);
1429 else
1430 tx = ops_run_compute6_2(sh, percpu);
1431 }
1432 /* terminate the chain if reconstruct is not set to be run */
1433 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001434 async_tx_ack(tx);
1435 }
Dan Williams91c00922007-01-02 13:52:30 -07001436
Dan Williams600aa102008-06-28 08:32:05 +10001437 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001438 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001439
Dan Williams600aa102008-06-28 08:32:05 +10001440 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001441 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001442 overlap_clear++;
1443 }
1444
Dan Williamsac6b53b2009-07-14 13:40:19 -07001445 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1446 if (level < 6)
1447 ops_run_reconstruct5(sh, percpu, tx);
1448 else
1449 ops_run_reconstruct6(sh, percpu, tx);
1450 }
Dan Williams91c00922007-01-02 13:52:30 -07001451
Dan Williamsac6b53b2009-07-14 13:40:19 -07001452 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1453 if (sh->check_state == check_state_run)
1454 ops_run_check_p(sh, percpu);
1455 else if (sh->check_state == check_state_run_q)
1456 ops_run_check_pq(sh, percpu, 0);
1457 else if (sh->check_state == check_state_run_pq)
1458 ops_run_check_pq(sh, percpu, 1);
1459 else
1460 BUG();
1461 }
Dan Williams91c00922007-01-02 13:52:30 -07001462
Dan Williams91c00922007-01-02 13:52:30 -07001463 if (overlap_clear)
1464 for (i = disks; i--; ) {
1465 struct r5dev *dev = &sh->dev[i];
1466 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1467 wake_up(&sh->raid_conf->wait_for_overlap);
1468 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001469 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001470}
1471
Dan Williams417b8d42009-10-16 16:25:22 +11001472#ifdef CONFIG_MULTICORE_RAID456
1473static void async_run_ops(void *param, async_cookie_t cookie)
1474{
1475 struct stripe_head *sh = param;
1476 unsigned long ops_request = sh->ops.request;
1477
1478 clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
1479 wake_up(&sh->ops.wait_for_ops);
1480
1481 __raid_run_ops(sh, ops_request);
1482 release_stripe(sh);
1483}
1484
1485static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1486{
1487 /* since handle_stripe can be called outside of raid5d context
1488 * we need to ensure sh->ops.request is de-staged before another
1489 * request arrives
1490 */
1491 wait_event(sh->ops.wait_for_ops,
1492 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
1493 sh->ops.request = ops_request;
1494
1495 atomic_inc(&sh->count);
1496 async_schedule(async_run_ops, sh);
1497}
1498#else
1499#define raid_run_ops __raid_run_ops
1500#endif
1501
NeilBrownd1688a62011-10-11 16:49:52 +11001502static int grow_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
1504 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001505 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001506 if (!sh)
1507 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001508
NeilBrown3f294f42005-11-08 21:39:25 -08001509 sh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001510 #ifdef CONFIG_MULTICORE_RAID456
1511 init_waitqueue_head(&sh->ops.wait_for_ops);
1512 #endif
NeilBrown3f294f42005-11-08 21:39:25 -08001513
Shaohua Lib17459c2012-07-19 16:01:31 +10001514 spin_lock_init(&sh->stripe_lock);
1515
NeilBrowne4e11e32010-06-16 16:45:16 +10001516 if (grow_buffers(sh)) {
1517 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001518 kmem_cache_free(conf->slab_cache, sh);
1519 return 0;
1520 }
1521 /* we just created an active stripe so... */
1522 atomic_set(&sh->count, 1);
1523 atomic_inc(&conf->active_stripes);
1524 INIT_LIST_HEAD(&sh->lru);
1525 release_stripe(sh);
1526 return 1;
1527}
1528
NeilBrownd1688a62011-10-11 16:49:52 +11001529static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001530{
Christoph Lametere18b8902006-12-06 20:33:20 -08001531 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001532 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
NeilBrownf4be6b42010-06-01 19:37:25 +10001534 if (conf->mddev->gendisk)
1535 sprintf(conf->cache_name[0],
1536 "raid%d-%s", conf->level, mdname(conf->mddev));
1537 else
1538 sprintf(conf->cache_name[0],
1539 "raid%d-%p", conf->level, conf->mddev);
1540 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1541
NeilBrownad01c9e2006-03-27 01:18:07 -08001542 conf->active_name = 0;
1543 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001545 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 if (!sc)
1547 return 1;
1548 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001549 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001550 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001551 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 return 0;
1554}
NeilBrown29269552006-03-27 01:18:10 -08001555
Dan Williamsd6f38f32009-07-14 11:50:52 -07001556/**
1557 * scribble_len - return the required size of the scribble region
1558 * @num - total number of disks in the array
1559 *
1560 * The size must be enough to contain:
1561 * 1/ a struct page pointer for each device in the array +2
1562 * 2/ room to convert each entry in (1) to its corresponding dma
1563 * (dma_map_page()) or page (page_address()) address.
1564 *
1565 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1566 * calculate over all devices (not just the data blocks), using zeros in place
1567 * of the P and Q blocks.
1568 */
1569static size_t scribble_len(int num)
1570{
1571 size_t len;
1572
1573 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1574
1575 return len;
1576}
1577
NeilBrownd1688a62011-10-11 16:49:52 +11001578static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001579{
1580 /* Make all the stripes able to hold 'newsize' devices.
1581 * New slots in each stripe get 'page' set to a new page.
1582 *
1583 * This happens in stages:
1584 * 1/ create a new kmem_cache and allocate the required number of
1585 * stripe_heads.
Masanari Iida83f0d772012-10-30 00:18:08 +09001586 * 2/ gather all the old stripe_heads and transfer the pages across
NeilBrownad01c9e2006-03-27 01:18:07 -08001587 * to the new stripe_heads. This will have the side effect of
1588 * freezing the array as once all stripe_heads have been collected,
1589 * no IO will be possible. Old stripe heads are freed once their
1590 * pages have been transferred over, and the old kmem_cache is
1591 * freed when all stripes are done.
1592 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1593 * we simple return a failre status - no need to clean anything up.
1594 * 4/ allocate new pages for the new slots in the new stripe_heads.
1595 * If this fails, we don't bother trying the shrink the
1596 * stripe_heads down again, we just leave them as they are.
1597 * As each stripe_head is processed the new one is released into
1598 * active service.
1599 *
1600 * Once step2 is started, we cannot afford to wait for a write,
1601 * so we use GFP_NOIO allocations.
1602 */
1603 struct stripe_head *osh, *nsh;
1604 LIST_HEAD(newstripes);
1605 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001606 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001607 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001608 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001609 int i;
1610
1611 if (newsize <= conf->pool_size)
1612 return 0; /* never bother to shrink */
1613
Dan Williamsb5470dc2008-06-27 21:44:04 -07001614 err = md_allow_write(conf->mddev);
1615 if (err)
1616 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001617
NeilBrownad01c9e2006-03-27 01:18:07 -08001618 /* Step 1 */
1619 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1620 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001621 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001622 if (!sc)
1623 return -ENOMEM;
1624
1625 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001626 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001627 if (!nsh)
1628 break;
1629
NeilBrownad01c9e2006-03-27 01:18:07 -08001630 nsh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001631 #ifdef CONFIG_MULTICORE_RAID456
1632 init_waitqueue_head(&nsh->ops.wait_for_ops);
1633 #endif
NeilBrowncb13ff62012-09-24 16:27:20 +10001634 spin_lock_init(&nsh->stripe_lock);
NeilBrownad01c9e2006-03-27 01:18:07 -08001635
1636 list_add(&nsh->lru, &newstripes);
1637 }
1638 if (i) {
1639 /* didn't get enough, give up */
1640 while (!list_empty(&newstripes)) {
1641 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1642 list_del(&nsh->lru);
1643 kmem_cache_free(sc, nsh);
1644 }
1645 kmem_cache_destroy(sc);
1646 return -ENOMEM;
1647 }
1648 /* Step 2 - Must use GFP_NOIO now.
1649 * OK, we have enough stripes, start collecting inactive
1650 * stripes and copying them over
1651 */
1652 list_for_each_entry(nsh, &newstripes, lru) {
1653 spin_lock_irq(&conf->device_lock);
1654 wait_event_lock_irq(conf->wait_for_stripe,
1655 !list_empty(&conf->inactive_list),
Lukas Czernereed8c022012-11-30 11:42:40 +01001656 conf->device_lock);
NeilBrownad01c9e2006-03-27 01:18:07 -08001657 osh = get_free_stripe(conf);
1658 spin_unlock_irq(&conf->device_lock);
1659 atomic_set(&nsh->count, 1);
1660 for(i=0; i<conf->pool_size; i++)
1661 nsh->dev[i].page = osh->dev[i].page;
1662 for( ; i<newsize; i++)
1663 nsh->dev[i].page = NULL;
1664 kmem_cache_free(conf->slab_cache, osh);
1665 }
1666 kmem_cache_destroy(conf->slab_cache);
1667
1668 /* Step 3.
1669 * At this point, we are holding all the stripes so the array
1670 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001671 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001672 */
1673 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1674 if (ndisks) {
1675 for (i=0; i<conf->raid_disks; i++)
1676 ndisks[i] = conf->disks[i];
1677 kfree(conf->disks);
1678 conf->disks = ndisks;
1679 } else
1680 err = -ENOMEM;
1681
Dan Williamsd6f38f32009-07-14 11:50:52 -07001682 get_online_cpus();
1683 conf->scribble_len = scribble_len(newsize);
1684 for_each_present_cpu(cpu) {
1685 struct raid5_percpu *percpu;
1686 void *scribble;
1687
1688 percpu = per_cpu_ptr(conf->percpu, cpu);
1689 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1690
1691 if (scribble) {
1692 kfree(percpu->scribble);
1693 percpu->scribble = scribble;
1694 } else {
1695 err = -ENOMEM;
1696 break;
1697 }
1698 }
1699 put_online_cpus();
1700
NeilBrownad01c9e2006-03-27 01:18:07 -08001701 /* Step 4, return new stripes to service */
1702 while(!list_empty(&newstripes)) {
1703 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1704 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001705
NeilBrownad01c9e2006-03-27 01:18:07 -08001706 for (i=conf->raid_disks; i < newsize; i++)
1707 if (nsh->dev[i].page == NULL) {
1708 struct page *p = alloc_page(GFP_NOIO);
1709 nsh->dev[i].page = p;
1710 if (!p)
1711 err = -ENOMEM;
1712 }
1713 release_stripe(nsh);
1714 }
1715 /* critical section pass, GFP_NOIO no longer needed */
1716
1717 conf->slab_cache = sc;
1718 conf->active_name = 1-conf->active_name;
1719 conf->pool_size = newsize;
1720 return err;
1721}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
NeilBrownd1688a62011-10-11 16:49:52 +11001723static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724{
1725 struct stripe_head *sh;
1726
NeilBrown3f294f42005-11-08 21:39:25 -08001727 spin_lock_irq(&conf->device_lock);
1728 sh = get_free_stripe(conf);
1729 spin_unlock_irq(&conf->device_lock);
1730 if (!sh)
1731 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001732 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001733 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001734 kmem_cache_free(conf->slab_cache, sh);
1735 atomic_dec(&conf->active_stripes);
1736 return 1;
1737}
1738
NeilBrownd1688a62011-10-11 16:49:52 +11001739static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001740{
1741 while (drop_one_stripe(conf))
1742 ;
1743
NeilBrown29fc7e32006-02-03 03:03:41 -08001744 if (conf->slab_cache)
1745 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 conf->slab_cache = NULL;
1747}
1748
NeilBrown6712ecf2007-09-27 12:47:43 +02001749static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
NeilBrown99c0fb52009-03-31 14:39:38 +11001751 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001752 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001753 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001755 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11001756 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10001757 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
1759 for (i=0 ; i<disks; i++)
1760 if (bi == &sh->dev[i].req)
1761 break;
1762
Dan Williams45b42332007-07-09 11:56:43 -07001763 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1764 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 uptodate);
1766 if (i == disks) {
1767 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001768 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 }
NeilBrown14a75d32011-12-23 10:17:52 +11001770 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11001771 /* If replacement finished while this request was outstanding,
1772 * 'replacement' might be NULL already.
1773 * In that case it moved down to 'rdev'.
1774 * rdev is not removed until all requests are finished.
1775 */
NeilBrown14a75d32011-12-23 10:17:52 +11001776 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001777 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11001778 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
NeilBrown05616be2012-05-21 09:27:00 +10001780 if (use_new_offset(conf, sh))
1781 s = sh->sector + rdev->new_data_offset;
1782 else
1783 s = sh->sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001786 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11001787 /* Note that this cannot happen on a
1788 * replacement device. We just fail those on
1789 * any error
1790 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001791 printk_ratelimited(
1792 KERN_INFO
1793 "md/raid:%s: read error corrected"
1794 " (%lu sectors at %llu on %s)\n",
1795 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10001796 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001797 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001798 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001799 clear_bit(R5_ReadError, &sh->dev[i].flags);
1800 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng3f9e7c12012-07-31 10:04:21 +10001801 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
1802 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1803
NeilBrown14a75d32011-12-23 10:17:52 +11001804 if (atomic_read(&rdev->read_errors))
1805 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11001807 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001808 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10001809 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001812 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11001813 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1814 printk_ratelimited(
1815 KERN_WARNING
1816 "md/raid:%s: read error on replacement device "
1817 "(sector %llu on %s).\n",
1818 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001819 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11001820 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001821 else if (conf->mddev->degraded >= conf->max_degraded) {
1822 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001823 printk_ratelimited(
1824 KERN_WARNING
1825 "md/raid:%s: read error not correctable "
1826 "(sector %llu on %s).\n",
1827 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001828 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001829 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001830 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08001831 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10001832 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001833 printk_ratelimited(
1834 KERN_WARNING
1835 "md/raid:%s: read error NOT corrected!! "
1836 "(sector %llu on %s).\n",
1837 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001838 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001839 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001840 } else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001841 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001842 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001843 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001844 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001845 else
1846 retry = 1;
1847 if (retry)
majianpeng3f9e7c12012-07-31 10:04:21 +10001848 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
1849 set_bit(R5_ReadError, &sh->dev[i].flags);
1850 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1851 } else
1852 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -08001853 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001854 clear_bit(R5_ReadError, &sh->dev[i].flags);
1855 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10001856 if (!(set_bad
1857 && test_bit(In_sync, &rdev->flags)
1858 && rdev_set_badblocks(
1859 rdev, sh->sector, STRIPE_SECTORS, 0)))
1860 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 }
NeilBrown14a75d32011-12-23 10:17:52 +11001863 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1865 set_bit(STRIPE_HANDLE, &sh->state);
1866 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867}
1868
NeilBrownd710e132008-10-13 11:55:12 +11001869static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870{
NeilBrown99c0fb52009-03-31 14:39:38 +11001871 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001872 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001873 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11001874 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001876 sector_t first_bad;
1877 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11001878 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
NeilBrown977df362011-12-23 10:17:53 +11001880 for (i = 0 ; i < disks; i++) {
1881 if (bi == &sh->dev[i].req) {
1882 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 break;
NeilBrown977df362011-12-23 10:17:53 +11001884 }
1885 if (bi == &sh->dev[i].rreq) {
1886 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001887 if (rdev)
1888 replacement = 1;
1889 else
1890 /* rdev was removed and 'replacement'
1891 * replaced it. rdev is not removed
1892 * until all requests are finished.
1893 */
1894 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11001895 break;
1896 }
1897 }
Dan Williams45b42332007-07-09 11:56:43 -07001898 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1900 uptodate);
1901 if (i == disks) {
1902 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001903 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 }
1905
NeilBrown977df362011-12-23 10:17:53 +11001906 if (replacement) {
1907 if (!uptodate)
1908 md_error(conf->mddev, rdev);
1909 else if (is_badblock(rdev, sh->sector,
1910 STRIPE_SECTORS,
1911 &first_bad, &bad_sectors))
1912 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1913 } else {
1914 if (!uptodate) {
1915 set_bit(WriteErrorSeen, &rdev->flags);
1916 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11001917 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1918 set_bit(MD_RECOVERY_NEEDED,
1919 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11001920 } else if (is_badblock(rdev, sh->sector,
1921 STRIPE_SECTORS,
1922 &first_bad, &bad_sectors))
1923 set_bit(R5_MadeGood, &sh->dev[i].flags);
1924 }
1925 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
NeilBrown977df362011-12-23 10:17:53 +11001927 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1928 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001930 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931}
1932
NeilBrown784052e2009-03-31 15:19:07 +11001933static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
NeilBrown784052e2009-03-31 15:19:07 +11001935static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
1937 struct r5dev *dev = &sh->dev[i];
1938
1939 bio_init(&dev->req);
1940 dev->req.bi_io_vec = &dev->vec;
1941 dev->req.bi_vcnt++;
1942 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11001944 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
NeilBrown977df362011-12-23 10:17:53 +11001946 bio_init(&dev->rreq);
1947 dev->rreq.bi_io_vec = &dev->rvec;
1948 dev->rreq.bi_vcnt++;
1949 dev->rreq.bi_max_vecs++;
1950 dev->rreq.bi_private = sh;
1951 dev->rvec.bv_page = dev->page;
1952
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001954 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955}
1956
NeilBrownfd01b882011-10-11 16:47:53 +11001957static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958{
1959 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11001960 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11001961 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10001962 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
NeilBrown908f4fb2011-12-23 10:17:50 +11001964 spin_lock_irqsave(&conf->device_lock, flags);
1965 clear_bit(In_sync, &rdev->flags);
1966 mddev->degraded = calc_degraded(conf);
1967 spin_unlock_irqrestore(&conf->device_lock, flags);
1968 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1969
NeilBrownde393cd2011-07-28 11:31:48 +10001970 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001971 set_bit(Faulty, &rdev->flags);
1972 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1973 printk(KERN_ALERT
1974 "md/raid:%s: Disk failure on %s, disabling device.\n"
1975 "md/raid:%s: Operation continuing on %d devices.\n",
1976 mdname(mddev),
1977 bdevname(rdev->bdev, b),
1978 mdname(mddev),
1979 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001980}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
1982/*
1983 * Input: a 'big' sector number,
1984 * Output: index of the data and parity disk, and the sector # in them.
1985 */
NeilBrownd1688a62011-10-11 16:49:52 +11001986static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001987 int previous, int *dd_idx,
1988 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001990 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001991 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001993 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001994 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001996 int algorithm = previous ? conf->prev_algo
1997 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001998 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1999 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11002000 int raid_disks = previous ? conf->previous_raid_disks
2001 : conf->raid_disks;
2002 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
2004 /* First compute the information on this sector */
2005
2006 /*
2007 * Compute the chunk number and the sector offset inside the chunk
2008 */
2009 chunk_offset = sector_div(r_sector, sectors_per_chunk);
2010 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
2012 /*
2013 * Compute the stripe number
2014 */
NeilBrown35f2a592010-04-20 14:13:34 +10002015 stripe = chunk_number;
2016 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10002017 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 /*
2019 * Select the parity disk based on the user selected algorithm.
2020 */
NeilBrown84789552011-07-27 11:00:36 +10002021 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07002022 switch(conf->level) {
2023 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11002024 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002025 break;
2026 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002027 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002029 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002030 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 (*dd_idx)++;
2032 break;
2033 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002034 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002035 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 (*dd_idx)++;
2037 break;
2038 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002039 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002040 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 break;
2042 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002043 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002044 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002046 case ALGORITHM_PARITY_0:
2047 pd_idx = 0;
2048 (*dd_idx)++;
2049 break;
2050 case ALGORITHM_PARITY_N:
2051 pd_idx = data_disks;
2052 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002054 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002055 }
2056 break;
2057 case 6:
2058
NeilBrowne183eae2009-03-31 15:20:22 +11002059 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002060 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002061 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002062 qd_idx = pd_idx + 1;
2063 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002064 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002065 qd_idx = 0;
2066 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002067 (*dd_idx) += 2; /* D D P Q D */
2068 break;
2069 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002070 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002071 qd_idx = pd_idx + 1;
2072 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002073 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002074 qd_idx = 0;
2075 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002076 (*dd_idx) += 2; /* D D P Q D */
2077 break;
2078 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002079 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002080 qd_idx = (pd_idx + 1) % raid_disks;
2081 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002082 break;
2083 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002084 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002085 qd_idx = (pd_idx + 1) % raid_disks;
2086 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002087 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002088
2089 case ALGORITHM_PARITY_0:
2090 pd_idx = 0;
2091 qd_idx = 1;
2092 (*dd_idx) += 2;
2093 break;
2094 case ALGORITHM_PARITY_N:
2095 pd_idx = data_disks;
2096 qd_idx = data_disks + 1;
2097 break;
2098
2099 case ALGORITHM_ROTATING_ZERO_RESTART:
2100 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2101 * of blocks for computing Q is different.
2102 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002103 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002104 qd_idx = pd_idx + 1;
2105 if (pd_idx == raid_disks-1) {
2106 (*dd_idx)++; /* Q D D D P */
2107 qd_idx = 0;
2108 } else if (*dd_idx >= pd_idx)
2109 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002110 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002111 break;
2112
2113 case ALGORITHM_ROTATING_N_RESTART:
2114 /* Same a left_asymmetric, by first stripe is
2115 * D D D P Q rather than
2116 * Q D D D P
2117 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002118 stripe2 += 1;
2119 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002120 qd_idx = pd_idx + 1;
2121 if (pd_idx == raid_disks-1) {
2122 (*dd_idx)++; /* Q D D D P */
2123 qd_idx = 0;
2124 } else if (*dd_idx >= pd_idx)
2125 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002126 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002127 break;
2128
2129 case ALGORITHM_ROTATING_N_CONTINUE:
2130 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002131 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002132 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2133 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002134 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002135 break;
2136
2137 case ALGORITHM_LEFT_ASYMMETRIC_6:
2138 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002139 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002140 if (*dd_idx >= pd_idx)
2141 (*dd_idx)++;
2142 qd_idx = raid_disks - 1;
2143 break;
2144
2145 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002146 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002147 if (*dd_idx >= pd_idx)
2148 (*dd_idx)++;
2149 qd_idx = raid_disks - 1;
2150 break;
2151
2152 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002153 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002154 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2155 qd_idx = raid_disks - 1;
2156 break;
2157
2158 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002159 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002160 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2161 qd_idx = raid_disks - 1;
2162 break;
2163
2164 case ALGORITHM_PARITY_0_6:
2165 pd_idx = 0;
2166 (*dd_idx)++;
2167 qd_idx = raid_disks - 1;
2168 break;
2169
NeilBrown16a53ec2006-06-26 00:27:38 -07002170 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002171 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002172 }
2173 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 }
2175
NeilBrown911d4ee2009-03-31 14:39:38 +11002176 if (sh) {
2177 sh->pd_idx = pd_idx;
2178 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002179 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 /*
2182 * Finally, compute the new sector number
2183 */
2184 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2185 return new_sector;
2186}
2187
2188
NeilBrown784052e2009-03-31 15:19:07 +11002189static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190{
NeilBrownd1688a62011-10-11 16:49:52 +11002191 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002192 int raid_disks = sh->disks;
2193 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002195 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2196 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002197 int algorithm = previous ? conf->prev_algo
2198 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 sector_t stripe;
2200 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002201 sector_t chunk_number;
2202 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002204 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
NeilBrown16a53ec2006-06-26 00:27:38 -07002206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2208 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
NeilBrown16a53ec2006-06-26 00:27:38 -07002210 if (i == sh->pd_idx)
2211 return 0;
2212 switch(conf->level) {
2213 case 4: break;
2214 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002215 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 case ALGORITHM_LEFT_ASYMMETRIC:
2217 case ALGORITHM_RIGHT_ASYMMETRIC:
2218 if (i > sh->pd_idx)
2219 i--;
2220 break;
2221 case ALGORITHM_LEFT_SYMMETRIC:
2222 case ALGORITHM_RIGHT_SYMMETRIC:
2223 if (i < sh->pd_idx)
2224 i += raid_disks;
2225 i -= (sh->pd_idx + 1);
2226 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002227 case ALGORITHM_PARITY_0:
2228 i -= 1;
2229 break;
2230 case ALGORITHM_PARITY_N:
2231 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002233 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002234 }
2235 break;
2236 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002237 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002238 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002239 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002240 case ALGORITHM_LEFT_ASYMMETRIC:
2241 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002242 case ALGORITHM_ROTATING_ZERO_RESTART:
2243 case ALGORITHM_ROTATING_N_RESTART:
2244 if (sh->pd_idx == raid_disks-1)
2245 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002246 else if (i > sh->pd_idx)
2247 i -= 2; /* D D P Q D */
2248 break;
2249 case ALGORITHM_LEFT_SYMMETRIC:
2250 case ALGORITHM_RIGHT_SYMMETRIC:
2251 if (sh->pd_idx == raid_disks-1)
2252 i--; /* Q D D D P */
2253 else {
2254 /* D D P Q D */
2255 if (i < sh->pd_idx)
2256 i += raid_disks;
2257 i -= (sh->pd_idx + 2);
2258 }
2259 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002260 case ALGORITHM_PARITY_0:
2261 i -= 2;
2262 break;
2263 case ALGORITHM_PARITY_N:
2264 break;
2265 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002266 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002267 if (sh->pd_idx == 0)
2268 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002269 else {
2270 /* D D Q P D */
2271 if (i < sh->pd_idx)
2272 i += raid_disks;
2273 i -= (sh->pd_idx + 1);
2274 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002275 break;
2276 case ALGORITHM_LEFT_ASYMMETRIC_6:
2277 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2278 if (i > sh->pd_idx)
2279 i--;
2280 break;
2281 case ALGORITHM_LEFT_SYMMETRIC_6:
2282 case ALGORITHM_RIGHT_SYMMETRIC_6:
2283 if (i < sh->pd_idx)
2284 i += data_disks + 1;
2285 i -= (sh->pd_idx + 1);
2286 break;
2287 case ALGORITHM_PARITY_0_6:
2288 i -= 1;
2289 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002290 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002291 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002292 }
2293 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 }
2295
2296 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002297 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
NeilBrown112bf892009-03-31 14:39:38 +11002299 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002300 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002301 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2302 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002303 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2304 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 return 0;
2306 }
2307 return r_sector;
2308}
2309
2310
Dan Williams600aa102008-06-28 08:32:05 +10002311static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002312schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002313 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002314{
2315 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002316 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002317 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002318
Dan Williamse33129d2007-01-02 13:52:30 -07002319 if (rcw) {
2320 /* if we are not expanding this is a proper write request, and
2321 * there will be bios with new data to be drained into the
2322 * stripe cache
2323 */
2324 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10002325 sh->reconstruct_state = reconstruct_state_drain_run;
2326 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2327 } else
2328 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07002329
Dan Williamsac6b53b2009-07-14 13:40:19 -07002330 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002331
2332 for (i = disks; i--; ) {
2333 struct r5dev *dev = &sh->dev[i];
2334
2335 if (dev->towrite) {
2336 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002337 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002338 if (!expand)
2339 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002340 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002341 }
2342 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002343 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002344 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002345 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002346 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002347 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002348 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2349 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2350
Dan Williamsd8ee0722008-06-28 08:32:06 +10002351 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002352 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2353 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002354 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002355
2356 for (i = disks; i--; ) {
2357 struct r5dev *dev = &sh->dev[i];
2358 if (i == pd_idx)
2359 continue;
2360
Dan Williamse33129d2007-01-02 13:52:30 -07002361 if (dev->towrite &&
2362 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002363 test_bit(R5_Wantcompute, &dev->flags))) {
2364 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002365 set_bit(R5_LOCKED, &dev->flags);
2366 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002367 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002368 }
2369 }
2370 }
2371
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002372 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002373 * are in flight
2374 */
2375 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2376 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002377 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002378
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002379 if (level == 6) {
2380 int qd_idx = sh->qd_idx;
2381 struct r5dev *dev = &sh->dev[qd_idx];
2382
2383 set_bit(R5_LOCKED, &dev->flags);
2384 clear_bit(R5_UPTODATE, &dev->flags);
2385 s->locked++;
2386 }
2387
Dan Williams600aa102008-06-28 08:32:05 +10002388 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002389 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002390 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002391}
NeilBrown16a53ec2006-06-26 00:27:38 -07002392
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393/*
2394 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002395 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 * The bi_next chain must be in order.
2397 */
2398static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2399{
2400 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002401 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002402 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
NeilBrowncbe47ec2011-07-26 11:20:35 +10002404 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 (unsigned long long)bi->bi_sector,
2406 (unsigned long long)sh->sector);
2407
Shaohua Lib17459c2012-07-19 16:01:31 +10002408 /*
2409 * If several bio share a stripe. The bio bi_phys_segments acts as a
2410 * reference count to avoid race. The reference count should already be
2411 * increased before this function is called (for example, in
2412 * make_request()), so other bio sharing this stripe will not free the
2413 * stripe. If a stripe is owned by one stripe, the stripe lock will
2414 * protect it.
2415 */
2416 spin_lock_irq(&sh->stripe_lock);
NeilBrown72626682005-09-09 16:23:54 -07002417 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002419 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07002420 firstwrite = 1;
2421 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 bip = &sh->dev[dd_idx].toread;
2423 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2424 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2425 goto overlap;
2426 bip = & (*bip)->bi_next;
2427 }
2428 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2429 goto overlap;
2430
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002431 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 if (*bip)
2433 bi->bi_next = *bip;
2434 *bip = bi;
Shaohua Lie7836bd62012-07-19 16:01:31 +10002435 raid5_inc_bi_active_stripes(bi);
NeilBrown72626682005-09-09 16:23:54 -07002436
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 if (forwrite) {
2438 /* check if page is covered */
2439 sector_t sector = sh->dev[dd_idx].sector;
2440 for (bi=sh->dev[dd_idx].towrite;
2441 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2442 bi && bi->bi_sector <= sector;
2443 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2444 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2445 sector = bi->bi_sector + (bi->bi_size>>9);
2446 }
2447 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2448 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2449 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002450
2451 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2452 (unsigned long long)(*bip)->bi_sector,
2453 (unsigned long long)sh->sector, dd_idx);
NeilBrownb97390a2012-10-11 13:50:12 +11002454 spin_unlock_irq(&sh->stripe_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002455
2456 if (conf->mddev->bitmap && firstwrite) {
2457 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2458 STRIPE_SECTORS, 0);
2459 sh->bm_seq = conf->seq_flush+1;
2460 set_bit(STRIPE_BIT_DELAY, &sh->state);
2461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 return 1;
2463
2464 overlap:
2465 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10002466 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 return 0;
2468}
2469
NeilBrownd1688a62011-10-11 16:49:52 +11002470static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002471
NeilBrownd1688a62011-10-11 16:49:52 +11002472static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002473 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002474{
NeilBrown784052e2009-03-31 15:19:07 +11002475 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002476 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002477 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002478 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002479 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002480
NeilBrown112bf892009-03-31 14:39:38 +11002481 raid5_compute_sector(conf,
2482 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002483 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002484 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002485 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002486}
2487
Dan Williamsa4456852007-07-09 11:56:43 -07002488static void
NeilBrownd1688a62011-10-11 16:49:52 +11002489handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002490 struct stripe_head_state *s, int disks,
2491 struct bio **return_bi)
2492{
2493 int i;
2494 for (i = disks; i--; ) {
2495 struct bio *bi;
2496 int bitmap_end = 0;
2497
2498 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002499 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002500 rcu_read_lock();
2501 rdev = rcu_dereference(conf->disks[i].rdev);
2502 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002503 atomic_inc(&rdev->nr_pending);
2504 else
2505 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002506 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002507 if (rdev) {
2508 if (!rdev_set_badblocks(
2509 rdev,
2510 sh->sector,
2511 STRIPE_SECTORS, 0))
2512 md_error(conf->mddev, rdev);
2513 rdev_dec_pending(rdev, conf->mddev);
2514 }
Dan Williamsa4456852007-07-09 11:56:43 -07002515 }
Shaohua Lib17459c2012-07-19 16:01:31 +10002516 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002517 /* fail all writes first */
2518 bi = sh->dev[i].towrite;
2519 sh->dev[i].towrite = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10002520 spin_unlock_irq(&sh->stripe_lock);
NeilBrown1ed850f2012-10-11 13:50:13 +11002521 if (bi)
Dan Williamsa4456852007-07-09 11:56:43 -07002522 bitmap_end = 1;
Dan Williamsa4456852007-07-09 11:56:43 -07002523
2524 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2525 wake_up(&conf->wait_for_overlap);
2526
2527 while (bi && bi->bi_sector <
2528 sh->dev[i].sector + STRIPE_SECTORS) {
2529 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2530 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002531 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002532 md_write_end(conf->mddev);
2533 bi->bi_next = *return_bi;
2534 *return_bi = bi;
2535 }
2536 bi = nextbi;
2537 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002538 if (bitmap_end)
2539 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2540 STRIPE_SECTORS, 0, 0);
2541 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002542 /* and fail all 'written' */
2543 bi = sh->dev[i].written;
2544 sh->dev[i].written = NULL;
2545 if (bi) bitmap_end = 1;
2546 while (bi && bi->bi_sector <
2547 sh->dev[i].sector + STRIPE_SECTORS) {
2548 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2549 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002550 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002551 md_write_end(conf->mddev);
2552 bi->bi_next = *return_bi;
2553 *return_bi = bi;
2554 }
2555 bi = bi2;
2556 }
2557
Dan Williamsb5e98d62007-01-02 13:52:31 -07002558 /* fail any reads if this device is non-operational and
2559 * the data has not reached the cache yet.
2560 */
2561 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2562 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2563 test_bit(R5_ReadError, &sh->dev[i].flags))) {
NeilBrown143c4d02012-10-11 13:50:12 +11002564 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002565 bi = sh->dev[i].toread;
2566 sh->dev[i].toread = NULL;
NeilBrown143c4d02012-10-11 13:50:12 +11002567 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002568 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2569 wake_up(&conf->wait_for_overlap);
Dan Williamsa4456852007-07-09 11:56:43 -07002570 while (bi && bi->bi_sector <
2571 sh->dev[i].sector + STRIPE_SECTORS) {
2572 struct bio *nextbi =
2573 r5_next_bio(bi, sh->dev[i].sector);
2574 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002575 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002576 bi->bi_next = *return_bi;
2577 *return_bi = bi;
2578 }
2579 bi = nextbi;
2580 }
2581 }
Dan Williamsa4456852007-07-09 11:56:43 -07002582 if (bitmap_end)
2583 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2584 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002585 /* If we were in the middle of a write the parity block might
2586 * still be locked - so just clear all R5_LOCKED flags
2587 */
2588 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002589 }
2590
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002591 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2592 if (atomic_dec_and_test(&conf->pending_full_writes))
2593 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002594}
2595
NeilBrown7f0da592011-07-28 11:39:22 +10002596static void
NeilBrownd1688a62011-10-11 16:49:52 +11002597handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002598 struct stripe_head_state *s)
2599{
2600 int abort = 0;
2601 int i;
2602
NeilBrown7f0da592011-07-28 11:39:22 +10002603 clear_bit(STRIPE_SYNCING, &sh->state);
2604 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002605 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002606 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10002607 * Don't even need to abort as that is handled elsewhere
2608 * if needed, and not always wanted e.g. if there is a known
2609 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11002610 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002611 * non-sync devices, or abort the recovery
2612 */
NeilBrown18b98372012-04-01 23:48:38 +10002613 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2614 /* During recovery devices cannot be removed, so
2615 * locking and refcounting of rdevs is not needed
2616 */
2617 for (i = 0; i < conf->raid_disks; i++) {
2618 struct md_rdev *rdev = conf->disks[i].rdev;
2619 if (rdev
2620 && !test_bit(Faulty, &rdev->flags)
2621 && !test_bit(In_sync, &rdev->flags)
2622 && !rdev_set_badblocks(rdev, sh->sector,
2623 STRIPE_SECTORS, 0))
2624 abort = 1;
2625 rdev = conf->disks[i].replacement;
2626 if (rdev
2627 && !test_bit(Faulty, &rdev->flags)
2628 && !test_bit(In_sync, &rdev->flags)
2629 && !rdev_set_badblocks(rdev, sh->sector,
2630 STRIPE_SECTORS, 0))
2631 abort = 1;
2632 }
2633 if (abort)
2634 conf->recovery_disabled =
2635 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10002636 }
NeilBrown18b98372012-04-01 23:48:38 +10002637 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10002638}
2639
NeilBrown9a3e1102011-12-23 10:17:53 +11002640static int want_replace(struct stripe_head *sh, int disk_idx)
2641{
2642 struct md_rdev *rdev;
2643 int rv = 0;
2644 /* Doing recovery so rcu locking not required */
2645 rdev = sh->raid_conf->disks[disk_idx].replacement;
2646 if (rdev
2647 && !test_bit(Faulty, &rdev->flags)
2648 && !test_bit(In_sync, &rdev->flags)
2649 && (rdev->recovery_offset <= sh->sector
2650 || rdev->mddev->recovery_cp <= sh->sector))
2651 rv = 1;
2652
2653 return rv;
2654}
2655
NeilBrown93b3dbc2011-07-27 11:00:36 +10002656/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002657 * to be read or computed to satisfy a request.
2658 *
2659 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002660 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002661 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002662static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2663 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002664{
2665 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002666 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2667 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002668
Dan Williamsf38e1212007-01-02 13:52:30 -07002669 /* is the data in this block needed, and can we get it? */
2670 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002671 !test_bit(R5_UPTODATE, &dev->flags) &&
2672 (dev->toread ||
2673 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2674 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002675 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002676 (s->failed >= 1 && fdev[0]->toread) ||
2677 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002678 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2679 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2680 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002681 /* we would like to get this block, possibly by computing it,
2682 * otherwise read it if the backing disk is insync
2683 */
2684 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2685 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2686 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002687 (s->failed && (disk_idx == s->failed_num[0] ||
2688 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002689 /* have disk failed, and we're requested to fetch it;
2690 * do compute it
2691 */
2692 pr_debug("Computing stripe %llu block %d\n",
2693 (unsigned long long)sh->sector, disk_idx);
2694 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2695 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2696 set_bit(R5_Wantcompute, &dev->flags);
2697 sh->ops.target = disk_idx;
2698 sh->ops.target2 = -1; /* no 2nd target */
2699 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002700 /* Careful: from this point on 'uptodate' is in the eye
2701 * of raid_run_ops which services 'compute' operations
2702 * before writes. R5_Wantcompute flags a block that will
2703 * be R5_UPTODATE by the time it is needed for a
2704 * subsequent operation.
2705 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002706 s->uptodate++;
2707 return 1;
2708 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2709 /* Computing 2-failure is *very* expensive; only
2710 * do it if failed >= 2
2711 */
2712 int other;
2713 for (other = disks; other--; ) {
2714 if (other == disk_idx)
2715 continue;
2716 if (!test_bit(R5_UPTODATE,
2717 &sh->dev[other].flags))
2718 break;
2719 }
2720 BUG_ON(other < 0);
2721 pr_debug("Computing stripe %llu blocks %d,%d\n",
2722 (unsigned long long)sh->sector,
2723 disk_idx, other);
2724 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2725 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2726 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2727 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2728 sh->ops.target = disk_idx;
2729 sh->ops.target2 = other;
2730 s->uptodate += 2;
2731 s->req_compute = 1;
2732 return 1;
2733 } else if (test_bit(R5_Insync, &dev->flags)) {
2734 set_bit(R5_LOCKED, &dev->flags);
2735 set_bit(R5_Wantread, &dev->flags);
2736 s->locked++;
2737 pr_debug("Reading block %d (sync=%d)\n",
2738 disk_idx, s->syncing);
2739 }
2740 }
2741
2742 return 0;
2743}
2744
2745/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002746 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002747 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002748static void handle_stripe_fill(struct stripe_head *sh,
2749 struct stripe_head_state *s,
2750 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002751{
2752 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002753
2754 /* look for blocks to read/compute, skip this if a compute
2755 * is already in flight, or if the stripe contents are in the
2756 * midst of changing due to a write
2757 */
2758 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2759 !sh->reconstruct_state)
2760 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002761 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002762 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002763 set_bit(STRIPE_HANDLE, &sh->state);
2764}
2765
2766
Dan Williams1fe797e2008-06-28 09:16:30 +10002767/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002768 * any written block on an uptodate or failed drive can be returned.
2769 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2770 * never LOCKED, so we don't need to test 'failed' directly.
2771 */
NeilBrownd1688a62011-10-11 16:49:52 +11002772static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002773 struct stripe_head *sh, int disks, struct bio **return_bi)
2774{
2775 int i;
2776 struct r5dev *dev;
2777
2778 for (i = disks; i--; )
2779 if (sh->dev[i].written) {
2780 dev = &sh->dev[i];
2781 if (!test_bit(R5_LOCKED, &dev->flags) &&
Shaohua Li9e4447682012-10-11 13:49:49 +11002782 (test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownca64cae2012-11-21 16:33:40 +11002783 test_bit(R5_Discard, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002784 /* We can return any write requests */
2785 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07002786 pr_debug("Return write for disc %d\n", i);
NeilBrownca64cae2012-11-21 16:33:40 +11002787 if (test_and_clear_bit(R5_Discard, &dev->flags))
2788 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002789 wbi = dev->written;
2790 dev->written = NULL;
2791 while (wbi && wbi->bi_sector <
2792 dev->sector + STRIPE_SECTORS) {
2793 wbi2 = r5_next_bio(wbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002794 if (!raid5_dec_bi_active_stripes(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002795 md_write_end(conf->mddev);
2796 wbi->bi_next = *return_bi;
2797 *return_bi = wbi;
2798 }
2799 wbi = wbi2;
2800 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002801 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2802 STRIPE_SECTORS,
Dan Williamsa4456852007-07-09 11:56:43 -07002803 !test_bit(STRIPE_DEGRADED, &sh->state),
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002804 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002805 }
NeilBrownca64cae2012-11-21 16:33:40 +11002806 } else if (test_bit(R5_Discard, &sh->dev[i].flags))
2807 clear_bit(R5_Discard, &sh->dev[i].flags);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002808
2809 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2810 if (atomic_dec_and_test(&conf->pending_full_writes))
2811 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002812}
2813
NeilBrownd1688a62011-10-11 16:49:52 +11002814static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002815 struct stripe_head *sh,
2816 struct stripe_head_state *s,
2817 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002818{
2819 int rmw = 0, rcw = 0, i;
Alexander Lyakasa7854482012-10-11 13:50:12 +11002820 sector_t recovery_cp = conf->mddev->recovery_cp;
2821
2822 /* RAID6 requires 'rcw' in current implementation.
2823 * Otherwise, check whether resync is now happening or should start.
2824 * If yes, then the array is dirty (after unclean shutdown or
2825 * initial creation), so parity in some stripes might be inconsistent.
2826 * In this case, we need to always do reconstruct-write, to ensure
2827 * that in case of drive failure or read-error correction, we
2828 * generate correct data from the parity.
2829 */
2830 if (conf->max_degraded == 2 ||
2831 (recovery_cp < MaxSector && sh->sector >= recovery_cp)) {
2832 /* Calculate the real rcw later - for now make it
NeilBrownc8ac1802011-07-27 11:00:36 +10002833 * look like rcw is cheaper
2834 */
2835 rcw = 1; rmw = 2;
Alexander Lyakasa7854482012-10-11 13:50:12 +11002836 pr_debug("force RCW max_degraded=%u, recovery_cp=%llu sh->sector=%llu\n",
2837 conf->max_degraded, (unsigned long long)recovery_cp,
2838 (unsigned long long)sh->sector);
NeilBrownc8ac1802011-07-27 11:00:36 +10002839 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002840 /* would I have to read this buffer for read_modify_write */
2841 struct r5dev *dev = &sh->dev[i];
2842 if ((dev->towrite || i == sh->pd_idx) &&
2843 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002844 !(test_bit(R5_UPTODATE, &dev->flags) ||
2845 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002846 if (test_bit(R5_Insync, &dev->flags))
2847 rmw++;
2848 else
2849 rmw += 2*disks; /* cannot read it */
2850 }
2851 /* Would I have to read this buffer for reconstruct_write */
2852 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2853 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002854 !(test_bit(R5_UPTODATE, &dev->flags) ||
2855 test_bit(R5_Wantcompute, &dev->flags))) {
2856 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002857 else
2858 rcw += 2*disks;
2859 }
2860 }
Dan Williams45b42332007-07-09 11:56:43 -07002861 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002862 (unsigned long long)sh->sector, rmw, rcw);
2863 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrowna9add5d2012-10-31 11:59:09 +11002864 if (rmw < rcw && rmw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002865 /* prefer read-modify-write, but need to get some data */
NeilBrowna9add5d2012-10-31 11:59:09 +11002866 blk_add_trace_msg(conf->mddev->queue, "raid5 rmw %llu %d",
2867 (unsigned long long)sh->sector, rmw);
Dan Williamsa4456852007-07-09 11:56:43 -07002868 for (i = disks; i--; ) {
2869 struct r5dev *dev = &sh->dev[i];
2870 if ((dev->towrite || i == sh->pd_idx) &&
2871 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002872 !(test_bit(R5_UPTODATE, &dev->flags) ||
2873 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002874 test_bit(R5_Insync, &dev->flags)) {
2875 if (
2876 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002877 pr_debug("Read_old block "
NeilBrowna9add5d2012-10-31 11:59:09 +11002878 "%d for r-m-w\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002879 set_bit(R5_LOCKED, &dev->flags);
2880 set_bit(R5_Wantread, &dev->flags);
2881 s->locked++;
2882 } else {
2883 set_bit(STRIPE_DELAYED, &sh->state);
2884 set_bit(STRIPE_HANDLE, &sh->state);
2885 }
2886 }
2887 }
NeilBrowna9add5d2012-10-31 11:59:09 +11002888 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002889 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002890 /* want reconstruct write, but need to get some data */
NeilBrowna9add5d2012-10-31 11:59:09 +11002891 int qread =0;
NeilBrownc8ac1802011-07-27 11:00:36 +10002892 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002893 for (i = disks; i--; ) {
2894 struct r5dev *dev = &sh->dev[i];
2895 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002896 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002897 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002898 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002899 test_bit(R5_Wantcompute, &dev->flags))) {
2900 rcw++;
2901 if (!test_bit(R5_Insync, &dev->flags))
2902 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002903 if (
2904 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002905 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002906 "%d for Reconstruct\n", i);
2907 set_bit(R5_LOCKED, &dev->flags);
2908 set_bit(R5_Wantread, &dev->flags);
2909 s->locked++;
NeilBrowna9add5d2012-10-31 11:59:09 +11002910 qread++;
Dan Williamsa4456852007-07-09 11:56:43 -07002911 } else {
2912 set_bit(STRIPE_DELAYED, &sh->state);
2913 set_bit(STRIPE_HANDLE, &sh->state);
2914 }
2915 }
2916 }
NeilBrowna9add5d2012-10-31 11:59:09 +11002917 if (rcw)
2918 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
2919 (unsigned long long)sh->sector,
2920 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
NeilBrownc8ac1802011-07-27 11:00:36 +10002921 }
Dan Williamsa4456852007-07-09 11:56:43 -07002922 /* now if nothing is locked, and if we have enough data,
2923 * we can start a write request
2924 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002925 /* since handle_stripe can be called at any time we need to handle the
2926 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002927 * subsequent call wants to start a write request. raid_run_ops only
2928 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002929 * simultaneously. If this is not the case then new writes need to be
2930 * held off until the compute completes.
2931 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002932 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2933 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2934 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002935 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002936}
2937
NeilBrownd1688a62011-10-11 16:49:52 +11002938static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002939 struct stripe_head_state *s, int disks)
2940{
Dan Williamsecc65c92008-06-28 08:31:57 +10002941 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002942
Dan Williamsbd2ab672008-04-10 21:29:27 -07002943 set_bit(STRIPE_HANDLE, &sh->state);
2944
Dan Williamsecc65c92008-06-28 08:31:57 +10002945 switch (sh->check_state) {
2946 case check_state_idle:
2947 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002948 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002949 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002950 sh->check_state = check_state_run;
2951 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002952 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002953 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002954 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002955 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002956 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002957 /* fall through */
2958 case check_state_compute_result:
2959 sh->check_state = check_state_idle;
2960 if (!dev)
2961 dev = &sh->dev[sh->pd_idx];
2962
2963 /* check that a write has not made the stripe insync */
2964 if (test_bit(STRIPE_INSYNC, &sh->state))
2965 break;
2966
2967 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002968 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2969 BUG_ON(s->uptodate != disks);
2970
2971 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002972 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002973 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002974
Dan Williamsa4456852007-07-09 11:56:43 -07002975 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002976 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002977 break;
2978 case check_state_run:
2979 break; /* we will be called again upon completion */
2980 case check_state_check_result:
2981 sh->check_state = check_state_idle;
2982
2983 /* if a failure occurred during the check operation, leave
2984 * STRIPE_INSYNC not set and let the stripe be handled again
2985 */
2986 if (s->failed)
2987 break;
2988
2989 /* handle a successful check operation, if parity is correct
2990 * we are done. Otherwise update the mismatch count and repair
2991 * parity if !MD_RECOVERY_CHECK
2992 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002993 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002994 /* parity is correct (on disc,
2995 * not in buffer any more)
2996 */
2997 set_bit(STRIPE_INSYNC, &sh->state);
2998 else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11002999 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsecc65c92008-06-28 08:31:57 +10003000 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3001 /* don't try to repair!! */
3002 set_bit(STRIPE_INSYNC, &sh->state);
3003 else {
3004 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10003005 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003006 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3007 set_bit(R5_Wantcompute,
3008 &sh->dev[sh->pd_idx].flags);
3009 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07003010 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10003011 s->uptodate++;
3012 }
3013 }
3014 break;
3015 case check_state_compute_run:
3016 break;
3017 default:
3018 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3019 __func__, sh->check_state,
3020 (unsigned long long) sh->sector);
3021 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003022 }
3023}
3024
3025
NeilBrownd1688a62011-10-11 16:49:52 +11003026static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07003027 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10003028 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003029{
Dan Williamsa4456852007-07-09 11:56:43 -07003030 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11003031 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07003032 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07003033
3034 set_bit(STRIPE_HANDLE, &sh->state);
3035
3036 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003037
Dan Williamsa4456852007-07-09 11:56:43 -07003038 /* Want to check and possibly repair P and Q.
3039 * However there could be one 'failed' device, in which
3040 * case we can only check one of them, possibly using the
3041 * other to generate missing data
3042 */
3043
Dan Williamsd82dfee2009-07-14 13:40:57 -07003044 switch (sh->check_state) {
3045 case check_state_idle:
3046 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10003047 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003048 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07003049 * makes sense to check P (If anything else were failed,
3050 * we would have used P to recreate it).
3051 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003052 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07003053 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003054 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003055 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07003056 * anything, so it makes sense to check it
3057 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003058 if (sh->check_state == check_state_run)
3059 sh->check_state = check_state_run_pq;
3060 else
3061 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07003062 }
Dan Williams36d1c642009-07-14 11:48:22 -07003063
Dan Williamsd82dfee2009-07-14 13:40:57 -07003064 /* discard potentially stale zero_sum_result */
3065 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07003066
Dan Williamsd82dfee2009-07-14 13:40:57 -07003067 if (sh->check_state == check_state_run) {
3068 /* async_xor_zero_sum destroys the contents of P */
3069 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
3070 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07003071 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003072 if (sh->check_state >= check_state_run &&
3073 sh->check_state <= check_state_run_pq) {
3074 /* async_syndrome_zero_sum preserves P and Q, so
3075 * no need to mark them !uptodate here
3076 */
3077 set_bit(STRIPE_OP_CHECK, &s->ops_request);
3078 break;
3079 }
Dan Williams36d1c642009-07-14 11:48:22 -07003080
Dan Williamsd82dfee2009-07-14 13:40:57 -07003081 /* we have 2-disk failure */
3082 BUG_ON(s->failed != 2);
3083 /* fall through */
3084 case check_state_compute_result:
3085 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07003086
Dan Williamsd82dfee2009-07-14 13:40:57 -07003087 /* check that a write has not made the stripe insync */
3088 if (test_bit(STRIPE_INSYNC, &sh->state))
3089 break;
Dan Williamsa4456852007-07-09 11:56:43 -07003090
3091 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07003092 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07003093 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003094 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07003095 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003096 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07003097 s->locked++;
3098 set_bit(R5_LOCKED, &dev->flags);
3099 set_bit(R5_Wantwrite, &dev->flags);
3100 }
3101 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003102 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07003103 s->locked++;
3104 set_bit(R5_LOCKED, &dev->flags);
3105 set_bit(R5_Wantwrite, &dev->flags);
3106 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003107 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003108 dev = &sh->dev[pd_idx];
3109 s->locked++;
3110 set_bit(R5_LOCKED, &dev->flags);
3111 set_bit(R5_Wantwrite, &dev->flags);
3112 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003113 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003114 dev = &sh->dev[qd_idx];
3115 s->locked++;
3116 set_bit(R5_LOCKED, &dev->flags);
3117 set_bit(R5_Wantwrite, &dev->flags);
3118 }
3119 clear_bit(STRIPE_DEGRADED, &sh->state);
3120
3121 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003122 break;
3123 case check_state_run:
3124 case check_state_run_q:
3125 case check_state_run_pq:
3126 break; /* we will be called again upon completion */
3127 case check_state_check_result:
3128 sh->check_state = check_state_idle;
3129
3130 /* handle a successful check operation, if parity is correct
3131 * we are done. Otherwise update the mismatch count and repair
3132 * parity if !MD_RECOVERY_CHECK
3133 */
3134 if (sh->ops.zero_sum_result == 0) {
3135 /* both parities are correct */
3136 if (!s->failed)
3137 set_bit(STRIPE_INSYNC, &sh->state);
3138 else {
3139 /* in contrast to the raid5 case we can validate
3140 * parity, but still have a failure to write
3141 * back
3142 */
3143 sh->check_state = check_state_compute_result;
3144 /* Returning at this point means that we may go
3145 * off and bring p and/or q uptodate again so
3146 * we make sure to check zero_sum_result again
3147 * to verify if p or q need writeback
3148 */
3149 }
3150 } else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003151 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003152 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3153 /* don't try to repair!! */
3154 set_bit(STRIPE_INSYNC, &sh->state);
3155 else {
3156 int *target = &sh->ops.target;
3157
3158 sh->ops.target = -1;
3159 sh->ops.target2 = -1;
3160 sh->check_state = check_state_compute_run;
3161 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3162 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3163 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3164 set_bit(R5_Wantcompute,
3165 &sh->dev[pd_idx].flags);
3166 *target = pd_idx;
3167 target = &sh->ops.target2;
3168 s->uptodate++;
3169 }
3170 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3171 set_bit(R5_Wantcompute,
3172 &sh->dev[qd_idx].flags);
3173 *target = qd_idx;
3174 s->uptodate++;
3175 }
3176 }
3177 }
3178 break;
3179 case check_state_compute_run:
3180 break;
3181 default:
3182 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3183 __func__, sh->check_state,
3184 (unsigned long long) sh->sector);
3185 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003186 }
3187}
3188
NeilBrownd1688a62011-10-11 16:49:52 +11003189static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003190{
3191 int i;
3192
3193 /* We have read all the blocks in this stripe and now we need to
3194 * copy some of them into a target stripe for expand.
3195 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003196 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003197 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3198 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003199 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003200 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003201 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003202 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003203
NeilBrown784052e2009-03-31 15:19:07 +11003204 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003205 sector_t s = raid5_compute_sector(conf, bn, 0,
3206 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003207 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003208 if (sh2 == NULL)
3209 /* so far only the early blocks of this stripe
3210 * have been requested. When later blocks
3211 * get requested, we will try again
3212 */
3213 continue;
3214 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3215 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3216 /* must have already done this block */
3217 release_stripe(sh2);
3218 continue;
3219 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003220
3221 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003222 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003223 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003224 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003225 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003226
Dan Williamsa4456852007-07-09 11:56:43 -07003227 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3228 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3229 for (j = 0; j < conf->raid_disks; j++)
3230 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003231 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003232 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3233 break;
3234 if (j == conf->raid_disks) {
3235 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3236 set_bit(STRIPE_HANDLE, &sh2->state);
3237 }
3238 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003239
Dan Williamsa4456852007-07-09 11:56:43 -07003240 }
NeilBrowna2e08552007-09-11 15:23:36 -07003241 /* done submitting copies, wait for them to complete */
NeilBrown749586b2012-11-20 14:11:15 +11003242 async_tx_quiesce(&tx);
Dan Williamsa4456852007-07-09 11:56:43 -07003243}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
3245/*
3246 * handle_stripe - do things to a stripe.
3247 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003248 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3249 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003251 * return some read requests which now have data
3252 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 * schedule a read on some buffers
3254 * schedule a write of some buffers
3255 * return confirmation of parity correctness
3256 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 */
Dan Williamsa4456852007-07-09 11:56:43 -07003258
NeilBrownacfe7262011-07-27 11:00:36 +10003259static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003260{
NeilBrownd1688a62011-10-11 16:49:52 +11003261 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003262 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003263 struct r5dev *dev;
3264 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003265 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003266
NeilBrownacfe7262011-07-27 11:00:36 +10003267 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003268
NeilBrownacfe7262011-07-27 11:00:36 +10003269 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3270 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3271 s->failed_num[0] = -1;
3272 s->failed_num[1] = -1;
3273
3274 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003275 rcu_read_lock();
3276 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003277 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003278 sector_t first_bad;
3279 int bad_sectors;
3280 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003281
NeilBrown16a53ec2006-06-26 00:27:38 -07003282 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003283
Dan Williams45b42332007-07-09 11:56:43 -07003284 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003285 i, dev->flags,
3286 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003287 /* maybe we can reply to a read
3288 *
3289 * new wantfill requests are only permitted while
3290 * ops_complete_biofill is guaranteed to be inactive
3291 */
3292 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3293 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3294 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003295
3296 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003297 if (test_bit(R5_LOCKED, &dev->flags))
3298 s->locked++;
3299 if (test_bit(R5_UPTODATE, &dev->flags))
3300 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003301 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003302 s->compute++;
3303 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003304 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003305
NeilBrownacfe7262011-07-27 11:00:36 +10003306 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003307 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003308 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003309 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003310 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003311 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003312 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003313 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003314 }
Dan Williamsa4456852007-07-09 11:56:43 -07003315 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003316 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003317 /* Prefer to use the replacement for reads, but only
3318 * if it is recovered enough and has no bad blocks.
3319 */
3320 rdev = rcu_dereference(conf->disks[i].replacement);
3321 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3322 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3323 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3324 &first_bad, &bad_sectors))
3325 set_bit(R5_ReadRepl, &dev->flags);
3326 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003327 if (rdev)
3328 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003329 rdev = rcu_dereference(conf->disks[i].rdev);
3330 clear_bit(R5_ReadRepl, &dev->flags);
3331 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003332 if (rdev && test_bit(Faulty, &rdev->flags))
3333 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003334 if (rdev) {
3335 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3336 &first_bad, &bad_sectors);
3337 if (s->blocked_rdev == NULL
3338 && (test_bit(Blocked, &rdev->flags)
3339 || is_bad < 0)) {
3340 if (is_bad < 0)
3341 set_bit(BlockedBadBlocks,
3342 &rdev->flags);
3343 s->blocked_rdev = rdev;
3344 atomic_inc(&rdev->nr_pending);
3345 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003346 }
NeilBrown415e72d2010-06-17 17:25:21 +10003347 clear_bit(R5_Insync, &dev->flags);
3348 if (!rdev)
3349 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003350 else if (is_bad) {
3351 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10003352 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3353 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003354 /* treat as in-sync, but with a read error
3355 * which we can now try to correct
3356 */
3357 set_bit(R5_Insync, &dev->flags);
3358 set_bit(R5_ReadError, &dev->flags);
3359 }
3360 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003361 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003362 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003363 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003364 set_bit(R5_Insync, &dev->flags);
3365 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3366 test_bit(R5_Expanded, &dev->flags))
3367 /* If we've reshaped into here, we assume it is Insync.
3368 * We will shortly update recovery_offset to make
3369 * it official.
3370 */
3371 set_bit(R5_Insync, &dev->flags);
3372
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003373 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003374 /* This flag does not apply to '.replacement'
3375 * only to .rdev, so make sure to check that*/
3376 struct md_rdev *rdev2 = rcu_dereference(
3377 conf->disks[i].rdev);
3378 if (rdev2 == rdev)
3379 clear_bit(R5_Insync, &dev->flags);
3380 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003381 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003382 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003383 } else
3384 clear_bit(R5_WriteError, &dev->flags);
3385 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003386 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003387 /* This flag does not apply to '.replacement'
3388 * only to .rdev, so make sure to check that*/
3389 struct md_rdev *rdev2 = rcu_dereference(
3390 conf->disks[i].rdev);
3391 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003392 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003393 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003394 } else
3395 clear_bit(R5_MadeGood, &dev->flags);
3396 }
NeilBrown977df362011-12-23 10:17:53 +11003397 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3398 struct md_rdev *rdev2 = rcu_dereference(
3399 conf->disks[i].replacement);
3400 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3401 s->handle_bad_blocks = 1;
3402 atomic_inc(&rdev2->nr_pending);
3403 } else
3404 clear_bit(R5_MadeGoodRepl, &dev->flags);
3405 }
NeilBrown415e72d2010-06-17 17:25:21 +10003406 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003407 /* The ReadError flag will just be confusing now */
3408 clear_bit(R5_ReadError, &dev->flags);
3409 clear_bit(R5_ReWrite, &dev->flags);
3410 }
NeilBrown415e72d2010-06-17 17:25:21 +10003411 if (test_bit(R5_ReadError, &dev->flags))
3412 clear_bit(R5_Insync, &dev->flags);
3413 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003414 if (s->failed < 2)
3415 s->failed_num[s->failed] = i;
3416 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003417 if (rdev && !test_bit(Faulty, &rdev->flags))
3418 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003419 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003420 }
NeilBrown9a3e1102011-12-23 10:17:53 +11003421 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3422 /* If there is a failed device being replaced,
3423 * we must be recovering.
3424 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10003425 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11003426 * else we can only be replacing
3427 * sync and recovery both need to read all devices, and so
3428 * use the same flag.
3429 */
3430 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10003431 sh->sector >= conf->mddev->recovery_cp ||
3432 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11003433 s->syncing = 1;
3434 else
3435 s->replacing = 1;
3436 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003437 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003438}
NeilBrownf4168852007-02-28 20:11:53 -08003439
NeilBrowncc940152011-07-26 11:35:35 +10003440static void handle_stripe(struct stripe_head *sh)
3441{
3442 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003443 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003444 int i;
NeilBrown84789552011-07-27 11:00:36 +10003445 int prexor;
3446 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003447 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003448
3449 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003450 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003451 /* already being handled, ensure it gets handled
3452 * again when current action finishes */
3453 set_bit(STRIPE_HANDLE, &sh->state);
3454 return;
3455 }
3456
3457 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3458 set_bit(STRIPE_SYNCING, &sh->state);
3459 clear_bit(STRIPE_INSYNC, &sh->state);
3460 }
3461 clear_bit(STRIPE_DELAYED, &sh->state);
3462
3463 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3464 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3465 (unsigned long long)sh->sector, sh->state,
3466 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3467 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003468
NeilBrownacfe7262011-07-27 11:00:36 +10003469 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003470
NeilBrownbc2607f2011-07-28 11:39:22 +10003471 if (s.handle_bad_blocks) {
3472 set_bit(STRIPE_HANDLE, &sh->state);
3473 goto finish;
3474 }
3475
NeilBrown474af965fe2011-07-27 11:00:36 +10003476 if (unlikely(s.blocked_rdev)) {
3477 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003478 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003479 set_bit(STRIPE_HANDLE, &sh->state);
3480 goto finish;
3481 }
3482 /* There is nothing for the blocked_rdev to block */
3483 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3484 s.blocked_rdev = NULL;
3485 }
3486
3487 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3488 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3489 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3490 }
3491
3492 pr_debug("locked=%d uptodate=%d to_read=%d"
3493 " to_write=%d failed=%d failed_num=%d,%d\n",
3494 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3495 s.failed_num[0], s.failed_num[1]);
3496 /* check if the array has lost more than max_degraded devices and,
3497 * if so, some requests might need to be failed.
3498 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003499 if (s.failed > conf->max_degraded) {
3500 sh->check_state = 0;
3501 sh->reconstruct_state = 0;
3502 if (s.to_read+s.to_write+s.written)
3503 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003504 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003505 handle_failed_sync(conf, sh, &s);
3506 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003507
NeilBrown84789552011-07-27 11:00:36 +10003508 /* Now we check to see if any write operations have recently
3509 * completed
3510 */
3511 prexor = 0;
3512 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3513 prexor = 1;
3514 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3515 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3516 sh->reconstruct_state = reconstruct_state_idle;
3517
3518 /* All the 'written' buffers and the parity block are ready to
3519 * be written back to disk
3520 */
Shaohua Li9e4447682012-10-11 13:49:49 +11003521 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
3522 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003523 BUG_ON(sh->qd_idx >= 0 &&
Shaohua Li9e4447682012-10-11 13:49:49 +11003524 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
3525 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003526 for (i = disks; i--; ) {
3527 struct r5dev *dev = &sh->dev[i];
3528 if (test_bit(R5_LOCKED, &dev->flags) &&
3529 (i == sh->pd_idx || i == sh->qd_idx ||
3530 dev->written)) {
3531 pr_debug("Writing block %d\n", i);
3532 set_bit(R5_Wantwrite, &dev->flags);
3533 if (prexor)
3534 continue;
3535 if (!test_bit(R5_Insync, &dev->flags) ||
3536 ((i == sh->pd_idx || i == sh->qd_idx) &&
3537 s.failed == 0))
3538 set_bit(STRIPE_INSYNC, &sh->state);
3539 }
3540 }
3541 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3542 s.dec_preread_active = 1;
3543 }
3544
NeilBrownef5b7c62012-11-22 09:13:36 +11003545 /*
3546 * might be able to return some write requests if the parity blocks
3547 * are safe, or on a failed drive
3548 */
3549 pdev = &sh->dev[sh->pd_idx];
3550 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3551 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3552 qdev = &sh->dev[sh->qd_idx];
3553 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3554 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3555 || conf->level < 6;
3556
3557 if (s.written &&
3558 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3559 && !test_bit(R5_LOCKED, &pdev->flags)
3560 && (test_bit(R5_UPTODATE, &pdev->flags) ||
3561 test_bit(R5_Discard, &pdev->flags))))) &&
3562 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3563 && !test_bit(R5_LOCKED, &qdev->flags)
3564 && (test_bit(R5_UPTODATE, &qdev->flags) ||
3565 test_bit(R5_Discard, &qdev->flags))))))
3566 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3567
3568 /* Now we might consider reading some blocks, either to check/generate
3569 * parity, or to satisfy requests
3570 * or to load a block that is being partially written.
3571 */
3572 if (s.to_read || s.non_overwrite
3573 || (conf->level == 6 && s.to_write && s.failed)
3574 || (s.syncing && (s.uptodate + s.compute < disks))
3575 || s.replacing
3576 || s.expanding)
3577 handle_stripe_fill(sh, &s, disks);
3578
NeilBrown84789552011-07-27 11:00:36 +10003579 /* Now to consider new write requests and what else, if anything
3580 * should be read. We do not handle new writes when:
3581 * 1/ A 'write' operation (copy+xor) is already in flight.
3582 * 2/ A 'check' operation is in flight, as it may clobber the parity
3583 * block.
3584 */
3585 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3586 handle_stripe_dirtying(conf, sh, &s, disks);
3587
3588 /* maybe we need to check and possibly fix the parity for this stripe
3589 * Any reads will already have been scheduled, so we just see if enough
3590 * data is available. The parity check is held off while parity
3591 * dependent operations are in flight.
3592 */
3593 if (sh->check_state ||
3594 (s.syncing && s.locked == 0 &&
3595 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3596 !test_bit(STRIPE_INSYNC, &sh->state))) {
3597 if (conf->level == 6)
3598 handle_parity_checks6(conf, sh, &s, disks);
3599 else
3600 handle_parity_checks5(conf, sh, &s, disks);
3601 }
NeilBrownc5a31002011-07-27 11:00:36 +10003602
NeilBrown9a3e1102011-12-23 10:17:53 +11003603 if (s.replacing && s.locked == 0
3604 && !test_bit(STRIPE_INSYNC, &sh->state)) {
3605 /* Write out to replacement devices where possible */
3606 for (i = 0; i < conf->raid_disks; i++)
3607 if (test_bit(R5_UPTODATE, &sh->dev[i].flags) &&
3608 test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3609 set_bit(R5_WantReplace, &sh->dev[i].flags);
3610 set_bit(R5_LOCKED, &sh->dev[i].flags);
3611 s.locked++;
3612 }
3613 set_bit(STRIPE_INSYNC, &sh->state);
3614 }
3615 if ((s.syncing || s.replacing) && s.locked == 0 &&
3616 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003617 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3618 clear_bit(STRIPE_SYNCING, &sh->state);
3619 }
3620
3621 /* If the failed drives are just a ReadError, then we might need
3622 * to progress the repair/check process
3623 */
3624 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3625 for (i = 0; i < s.failed; i++) {
3626 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3627 if (test_bit(R5_ReadError, &dev->flags)
3628 && !test_bit(R5_LOCKED, &dev->flags)
3629 && test_bit(R5_UPTODATE, &dev->flags)
3630 ) {
3631 if (!test_bit(R5_ReWrite, &dev->flags)) {
3632 set_bit(R5_Wantwrite, &dev->flags);
3633 set_bit(R5_ReWrite, &dev->flags);
3634 set_bit(R5_LOCKED, &dev->flags);
3635 s.locked++;
3636 } else {
3637 /* let's read it back */
3638 set_bit(R5_Wantread, &dev->flags);
3639 set_bit(R5_LOCKED, &dev->flags);
3640 s.locked++;
3641 }
3642 }
3643 }
3644
3645
NeilBrown3687c062011-07-27 11:00:36 +10003646 /* Finish reconstruct operations initiated by the expansion process */
3647 if (sh->reconstruct_state == reconstruct_state_result) {
3648 struct stripe_head *sh_src
3649 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3650 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3651 /* sh cannot be written until sh_src has been read.
3652 * so arrange for sh to be delayed a little
3653 */
3654 set_bit(STRIPE_DELAYED, &sh->state);
3655 set_bit(STRIPE_HANDLE, &sh->state);
3656 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3657 &sh_src->state))
3658 atomic_inc(&conf->preread_active_stripes);
3659 release_stripe(sh_src);
3660 goto finish;
3661 }
3662 if (sh_src)
3663 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003664
NeilBrown3687c062011-07-27 11:00:36 +10003665 sh->reconstruct_state = reconstruct_state_idle;
3666 clear_bit(STRIPE_EXPANDING, &sh->state);
3667 for (i = conf->raid_disks; i--; ) {
3668 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3669 set_bit(R5_LOCKED, &sh->dev[i].flags);
3670 s.locked++;
3671 }
3672 }
3673
3674 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3675 !sh->reconstruct_state) {
3676 /* Need to write out all blocks after computing parity */
3677 sh->disks = conf->raid_disks;
3678 stripe_set_idx(sh->sector, conf, 0, sh);
3679 schedule_reconstruction(sh, &s, 1, 1);
3680 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3681 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3682 atomic_dec(&conf->reshape_stripes);
3683 wake_up(&conf->wait_for_overlap);
3684 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3685 }
3686
3687 if (s.expanding && s.locked == 0 &&
3688 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3689 handle_stripe_expansion(conf, sh);
3690
3691finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003692 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10003693 if (unlikely(s.blocked_rdev)) {
3694 if (conf->mddev->external)
3695 md_wait_for_blocked_rdev(s.blocked_rdev,
3696 conf->mddev);
3697 else
3698 /* Internal metadata will immediately
3699 * be written by raid5d, so we don't
3700 * need to wait here.
3701 */
3702 rdev_dec_pending(s.blocked_rdev,
3703 conf->mddev);
3704 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003705
NeilBrownbc2607f2011-07-28 11:39:22 +10003706 if (s.handle_bad_blocks)
3707 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003708 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003709 struct r5dev *dev = &sh->dev[i];
3710 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3711 /* We own a safe reference to the rdev */
3712 rdev = conf->disks[i].rdev;
3713 if (!rdev_set_badblocks(rdev, sh->sector,
3714 STRIPE_SECTORS, 0))
3715 md_error(conf->mddev, rdev);
3716 rdev_dec_pending(rdev, conf->mddev);
3717 }
NeilBrownb84db562011-07-28 11:39:23 +10003718 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3719 rdev = conf->disks[i].rdev;
3720 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003721 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10003722 rdev_dec_pending(rdev, conf->mddev);
3723 }
NeilBrown977df362011-12-23 10:17:53 +11003724 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3725 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11003726 if (!rdev)
3727 /* rdev have been moved down */
3728 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11003729 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003730 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11003731 rdev_dec_pending(rdev, conf->mddev);
3732 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003733 }
3734
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003735 if (s.ops_request)
3736 raid_run_ops(sh, s.ops_request);
3737
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003738 ops_run_io(sh, &s);
3739
NeilBrownc5709ef2011-07-26 11:35:20 +10003740 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003741 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003742 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003743 * have actually been submitted.
3744 */
3745 atomic_dec(&conf->preread_active_stripes);
3746 if (atomic_read(&conf->preread_active_stripes) <
3747 IO_THRESHOLD)
3748 md_wakeup_thread(conf->mddev->thread);
3749 }
3750
NeilBrownc5709ef2011-07-26 11:35:20 +10003751 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003752
Dan Williams257a4b42011-11-08 16:22:06 +11003753 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003754}
3755
NeilBrownd1688a62011-10-11 16:49:52 +11003756static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757{
3758 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3759 while (!list_empty(&conf->delayed_list)) {
3760 struct list_head *l = conf->delayed_list.next;
3761 struct stripe_head *sh;
3762 sh = list_entry(l, struct stripe_head, lru);
3763 list_del_init(l);
3764 clear_bit(STRIPE_DELAYED, &sh->state);
3765 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3766 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003767 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 }
NeilBrown482c0832011-04-18 18:25:42 +10003769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770}
3771
NeilBrownd1688a62011-10-11 16:49:52 +11003772static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003773{
3774 /* device_lock is held */
3775 struct list_head head;
3776 list_add(&head, &conf->bitmap_list);
3777 list_del_init(&conf->bitmap_list);
3778 while (!list_empty(&head)) {
3779 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3780 list_del_init(&sh->lru);
3781 atomic_inc(&sh->count);
3782 __release_stripe(conf, sh);
3783 }
3784}
3785
NeilBrownfd01b882011-10-11 16:47:53 +11003786int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003787{
NeilBrownd1688a62011-10-11 16:49:52 +11003788 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003789
3790 /* No difference between reads and writes. Just check
3791 * how busy the stripe_cache is
3792 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003793
NeilBrownf022b2f2006-10-03 01:15:56 -07003794 if (conf->inactive_blocked)
3795 return 1;
3796 if (conf->quiesce)
3797 return 1;
3798 if (list_empty_careful(&conf->inactive_list))
3799 return 1;
3800
3801 return 0;
3802}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003803EXPORT_SYMBOL_GPL(md_raid5_congested);
3804
3805static int raid5_congested(void *data, int bits)
3806{
NeilBrownfd01b882011-10-11 16:47:53 +11003807 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003808
3809 return mddev_congested(mddev, bits) ||
3810 md_raid5_congested(mddev, bits);
3811}
NeilBrownf022b2f2006-10-03 01:15:56 -07003812
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003813/* We want read requests to align with chunks where possible,
3814 * but write requests don't need to.
3815 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003816static int raid5_mergeable_bvec(struct request_queue *q,
3817 struct bvec_merge_data *bvm,
3818 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003819{
NeilBrownfd01b882011-10-11 16:47:53 +11003820 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003821 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003822 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003823 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003824 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003825
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003826 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003827 return biovec->bv_len; /* always allow writes to be mergeable */
3828
Andre Noll664e7c42009-06-18 08:45:27 +10003829 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3830 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003831 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3832 if (max < 0) max = 0;
3833 if (max <= biovec->bv_len && bio_sectors == 0)
3834 return biovec->bv_len;
3835 else
3836 return max;
3837}
3838
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003839
NeilBrownfd01b882011-10-11 16:47:53 +11003840static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003841{
3842 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003843 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003844 unsigned int bio_sectors = bio->bi_size >> 9;
3845
Andre Noll664e7c42009-06-18 08:45:27 +10003846 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3847 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003848 return chunk_sectors >=
3849 ((sector & (chunk_sectors - 1)) + bio_sectors);
3850}
3851
3852/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003853 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3854 * later sampled by raid5d.
3855 */
NeilBrownd1688a62011-10-11 16:49:52 +11003856static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003857{
3858 unsigned long flags;
3859
3860 spin_lock_irqsave(&conf->device_lock, flags);
3861
3862 bi->bi_next = conf->retry_read_aligned_list;
3863 conf->retry_read_aligned_list = bi;
3864
3865 spin_unlock_irqrestore(&conf->device_lock, flags);
3866 md_wakeup_thread(conf->mddev->thread);
3867}
3868
3869
NeilBrownd1688a62011-10-11 16:49:52 +11003870static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003871{
3872 struct bio *bi;
3873
3874 bi = conf->retry_read_aligned;
3875 if (bi) {
3876 conf->retry_read_aligned = NULL;
3877 return bi;
3878 }
3879 bi = conf->retry_read_aligned_list;
3880 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003881 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003882 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003883 /*
3884 * this sets the active strip count to 1 and the processed
3885 * strip count to zero (upper 8 bits)
3886 */
Shaohua Lie7836bd62012-07-19 16:01:31 +10003887 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003888 }
3889
3890 return bi;
3891}
3892
3893
3894/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003895 * The "raid5_align_endio" should check if the read succeeded and if it
3896 * did, call bio_endio on the original bio (having bio_put the new bio
3897 * first).
3898 * If the read failed..
3899 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003900static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003901{
3902 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003903 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003904 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003905 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003906 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003907
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003908 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003909
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003910 rdev = (void*)raid_bi->bi_next;
3911 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003912 mddev = rdev->mddev;
3913 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003914
3915 rdev_dec_pending(rdev, conf->mddev);
3916
3917 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003918 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003919 if (atomic_dec_and_test(&conf->active_aligned_reads))
3920 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003921 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003922 }
3923
3924
Dan Williams45b42332007-07-09 11:56:43 -07003925 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003926
3927 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003928}
3929
Neil Brown387bb172007-02-08 14:20:29 -08003930static int bio_fits_rdev(struct bio *bi)
3931{
Jens Axboe165125e2007-07-24 09:28:11 +02003932 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003933
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003934 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003935 return 0;
3936 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003937 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003938 return 0;
3939
3940 if (q->merge_bvec_fn)
3941 /* it's too hard to apply the merge_bvec_fn at this stage,
3942 * just just give up
3943 */
3944 return 0;
3945
3946 return 1;
3947}
3948
3949
NeilBrownfd01b882011-10-11 16:47:53 +11003950static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003951{
NeilBrownd1688a62011-10-11 16:49:52 +11003952 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11003953 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003954 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11003955 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11003956 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003957
3958 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003959 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003960 return 0;
3961 }
3962 /*
NeilBrowna167f662010-10-26 18:31:13 +11003963 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003964 */
NeilBrowna167f662010-10-26 18:31:13 +11003965 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003966 if (!align_bi)
3967 return 0;
3968 /*
3969 * set bi_end_io to a new function, and set bi_private to the
3970 * original bio.
3971 */
3972 align_bi->bi_end_io = raid5_align_endio;
3973 align_bi->bi_private = raid_bio;
3974 /*
3975 * compute position
3976 */
NeilBrown112bf892009-03-31 14:39:38 +11003977 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3978 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003979 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003980
NeilBrown671488c2011-12-23 10:17:52 +11003981 end_sector = align_bi->bi_sector + (align_bi->bi_size >> 9);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003982 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11003983 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
3984 if (!rdev || test_bit(Faulty, &rdev->flags) ||
3985 rdev->recovery_offset < end_sector) {
3986 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3987 if (rdev &&
3988 (test_bit(Faulty, &rdev->flags) ||
3989 !(test_bit(In_sync, &rdev->flags) ||
3990 rdev->recovery_offset >= end_sector)))
3991 rdev = NULL;
3992 }
3993 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10003994 sector_t first_bad;
3995 int bad_sectors;
3996
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003997 atomic_inc(&rdev->nr_pending);
3998 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003999 raid_bio->bi_next = (void*)rdev;
4000 align_bi->bi_bdev = rdev->bdev;
4001 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004002
NeilBrown31c176e2011-07-28 11:39:22 +10004003 if (!bio_fits_rdev(align_bi) ||
4004 is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
4005 &first_bad, &bad_sectors)) {
4006 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08004007 bio_put(align_bi);
4008 rdev_dec_pending(rdev, mddev);
4009 return 0;
4010 }
4011
majianpeng6c0544e2012-06-12 08:31:10 +08004012 /* No reshape active, so we can trust rdev->data_offset */
4013 align_bi->bi_sector += rdev->data_offset;
4014
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004015 spin_lock_irq(&conf->device_lock);
4016 wait_event_lock_irq(conf->wait_for_stripe,
4017 conf->quiesce == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01004018 conf->device_lock);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004019 atomic_inc(&conf->active_aligned_reads);
4020 spin_unlock_irq(&conf->device_lock);
4021
NeilBrowna9add5d2012-10-31 11:59:09 +11004022 trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev),
4023 align_bi, disk_devt(mddev->gendisk),
4024 raid_bio->bi_sector);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004025 generic_make_request(align_bi);
4026 return 1;
4027 } else {
4028 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004029 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004030 return 0;
4031 }
4032}
4033
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004034/* __get_priority_stripe - get the next stripe to process
4035 *
4036 * Full stripe writes are allowed to pass preread active stripes up until
4037 * the bypass_threshold is exceeded. In general the bypass_count
4038 * increments when the handle_list is handled before the hold_list; however, it
4039 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
4040 * stripe with in flight i/o. The bypass_count will be reset when the
4041 * head of the hold_list has changed, i.e. the head was promoted to the
4042 * handle_list.
4043 */
NeilBrownd1688a62011-10-11 16:49:52 +11004044static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004045{
4046 struct stripe_head *sh;
4047
4048 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
4049 __func__,
4050 list_empty(&conf->handle_list) ? "empty" : "busy",
4051 list_empty(&conf->hold_list) ? "empty" : "busy",
4052 atomic_read(&conf->pending_full_writes), conf->bypass_count);
4053
4054 if (!list_empty(&conf->handle_list)) {
4055 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
4056
4057 if (list_empty(&conf->hold_list))
4058 conf->bypass_count = 0;
4059 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
4060 if (conf->hold_list.next == conf->last_hold)
4061 conf->bypass_count++;
4062 else {
4063 conf->last_hold = conf->hold_list.next;
4064 conf->bypass_count -= conf->bypass_threshold;
4065 if (conf->bypass_count < 0)
4066 conf->bypass_count = 0;
4067 }
4068 }
4069 } else if (!list_empty(&conf->hold_list) &&
4070 ((conf->bypass_threshold &&
4071 conf->bypass_count > conf->bypass_threshold) ||
4072 atomic_read(&conf->pending_full_writes) == 0)) {
4073 sh = list_entry(conf->hold_list.next,
4074 typeof(*sh), lru);
4075 conf->bypass_count -= conf->bypass_threshold;
4076 if (conf->bypass_count < 0)
4077 conf->bypass_count = 0;
4078 } else
4079 return NULL;
4080
4081 list_del_init(&sh->lru);
4082 atomic_inc(&sh->count);
4083 BUG_ON(atomic_read(&sh->count) != 1);
4084 return sh;
4085}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004086
Shaohua Li8811b592012-08-02 08:33:00 +10004087struct raid5_plug_cb {
4088 struct blk_plug_cb cb;
4089 struct list_head list;
4090};
4091
4092static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
4093{
4094 struct raid5_plug_cb *cb = container_of(
4095 blk_cb, struct raid5_plug_cb, cb);
4096 struct stripe_head *sh;
4097 struct mddev *mddev = cb->cb.data;
4098 struct r5conf *conf = mddev->private;
NeilBrowna9add5d2012-10-31 11:59:09 +11004099 int cnt = 0;
Shaohua Li8811b592012-08-02 08:33:00 +10004100
4101 if (cb->list.next && !list_empty(&cb->list)) {
4102 spin_lock_irq(&conf->device_lock);
4103 while (!list_empty(&cb->list)) {
4104 sh = list_first_entry(&cb->list, struct stripe_head, lru);
4105 list_del_init(&sh->lru);
4106 /*
4107 * avoid race release_stripe_plug() sees
4108 * STRIPE_ON_UNPLUG_LIST clear but the stripe
4109 * is still in our list
4110 */
4111 smp_mb__before_clear_bit();
4112 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
4113 __release_stripe(conf, sh);
NeilBrowna9add5d2012-10-31 11:59:09 +11004114 cnt++;
Shaohua Li8811b592012-08-02 08:33:00 +10004115 }
4116 spin_unlock_irq(&conf->device_lock);
4117 }
NeilBrowna9add5d2012-10-31 11:59:09 +11004118 trace_block_unplug(mddev->queue, cnt, !from_schedule);
Shaohua Li8811b592012-08-02 08:33:00 +10004119 kfree(cb);
4120}
4121
4122static void release_stripe_plug(struct mddev *mddev,
4123 struct stripe_head *sh)
4124{
4125 struct blk_plug_cb *blk_cb = blk_check_plugged(
4126 raid5_unplug, mddev,
4127 sizeof(struct raid5_plug_cb));
4128 struct raid5_plug_cb *cb;
4129
4130 if (!blk_cb) {
4131 release_stripe(sh);
4132 return;
4133 }
4134
4135 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
4136
4137 if (cb->list.next == NULL)
4138 INIT_LIST_HEAD(&cb->list);
4139
4140 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
4141 list_add_tail(&sh->lru, &cb->list);
4142 else
4143 release_stripe(sh);
4144}
4145
Shaohua Li620125f2012-10-11 13:49:05 +11004146static void make_discard_request(struct mddev *mddev, struct bio *bi)
4147{
4148 struct r5conf *conf = mddev->private;
4149 sector_t logical_sector, last_sector;
4150 struct stripe_head *sh;
4151 int remaining;
4152 int stripe_sectors;
4153
4154 if (mddev->reshape_position != MaxSector)
4155 /* Skip discard while reshape is happening */
4156 return;
4157
4158 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4159 last_sector = bi->bi_sector + (bi->bi_size>>9);
4160
4161 bi->bi_next = NULL;
4162 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
4163
4164 stripe_sectors = conf->chunk_sectors *
4165 (conf->raid_disks - conf->max_degraded);
4166 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
4167 stripe_sectors);
4168 sector_div(last_sector, stripe_sectors);
4169
4170 logical_sector *= conf->chunk_sectors;
4171 last_sector *= conf->chunk_sectors;
4172
4173 for (; logical_sector < last_sector;
4174 logical_sector += STRIPE_SECTORS) {
4175 DEFINE_WAIT(w);
4176 int d;
4177 again:
4178 sh = get_active_stripe(conf, logical_sector, 0, 0, 0);
4179 prepare_to_wait(&conf->wait_for_overlap, &w,
4180 TASK_UNINTERRUPTIBLE);
4181 spin_lock_irq(&sh->stripe_lock);
4182 for (d = 0; d < conf->raid_disks; d++) {
4183 if (d == sh->pd_idx || d == sh->qd_idx)
4184 continue;
4185 if (sh->dev[d].towrite || sh->dev[d].toread) {
4186 set_bit(R5_Overlap, &sh->dev[d].flags);
4187 spin_unlock_irq(&sh->stripe_lock);
4188 release_stripe(sh);
4189 schedule();
4190 goto again;
4191 }
4192 }
4193 finish_wait(&conf->wait_for_overlap, &w);
4194 for (d = 0; d < conf->raid_disks; d++) {
4195 if (d == sh->pd_idx || d == sh->qd_idx)
4196 continue;
4197 sh->dev[d].towrite = bi;
4198 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
4199 raid5_inc_bi_active_stripes(bi);
4200 }
4201 spin_unlock_irq(&sh->stripe_lock);
4202 if (conf->mddev->bitmap) {
4203 for (d = 0;
4204 d < conf->raid_disks - conf->max_degraded;
4205 d++)
4206 bitmap_startwrite(mddev->bitmap,
4207 sh->sector,
4208 STRIPE_SECTORS,
4209 0);
4210 sh->bm_seq = conf->seq_flush + 1;
4211 set_bit(STRIPE_BIT_DELAY, &sh->state);
4212 }
4213
4214 set_bit(STRIPE_HANDLE, &sh->state);
4215 clear_bit(STRIPE_DELAYED, &sh->state);
4216 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4217 atomic_inc(&conf->preread_active_stripes);
4218 release_stripe_plug(mddev, sh);
4219 }
4220
4221 remaining = raid5_dec_bi_active_stripes(bi);
4222 if (remaining == 0) {
4223 md_write_end(mddev);
4224 bio_endio(bi, 0);
4225 }
4226}
4227
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07004228static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229{
NeilBrownd1688a62011-10-11 16:49:52 +11004230 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11004231 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 sector_t new_sector;
4233 sector_t logical_sector, last_sector;
4234 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01004235 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11004236 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237
Tejun Heoe9c74692010-09-03 11:56:18 +02004238 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
4239 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004240 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07004241 }
4242
NeilBrown3d310eb2005-06-21 17:17:26 -07004243 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07004244
NeilBrown802ba062006-12-13 00:34:13 -08004245 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004246 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11004247 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004248 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004249
Shaohua Li620125f2012-10-11 13:49:05 +11004250 if (unlikely(bi->bi_rw & REQ_DISCARD)) {
4251 make_discard_request(mddev, bi);
4252 return;
4253 }
4254
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4256 last_sector = bi->bi_sector + (bi->bi_size>>9);
4257 bi->bi_next = NULL;
4258 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07004259
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
4261 DEFINE_WAIT(w);
NeilBrownb5663ba2009-03-31 14:39:38 +11004262 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08004263
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004264 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11004265 previous = 0;
NeilBrownb578d552006-03-27 01:18:12 -08004266 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004267 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11004268 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f762006-03-27 01:18:15 -08004269 * 64bit on a 32bit platform, and so it might be
4270 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02004271 * Of course reshape_progress could change after
NeilBrowndf8e7f762006-03-27 01:18:15 -08004272 * the lock is dropped, so once we get a reference
4273 * to the stripe that we think it is, we will have
4274 * to check again.
4275 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004276 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004277 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004278 ? logical_sector < conf->reshape_progress
4279 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004280 previous = 1;
4281 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10004282 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004283 ? logical_sector < conf->reshape_safe
4284 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08004285 spin_unlock_irq(&conf->device_lock);
4286 schedule();
4287 goto retry;
4288 }
4289 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004290 spin_unlock_irq(&conf->device_lock);
4291 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004292
NeilBrown112bf892009-03-31 14:39:38 +11004293 new_sector = raid5_compute_sector(conf, logical_sector,
4294 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11004295 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10004296 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 (unsigned long long)new_sector,
4298 (unsigned long long)logical_sector);
4299
NeilBrownb5663ba2009-03-31 14:39:38 +11004300 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10004301 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004303 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004304 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f762006-03-27 01:18:15 -08004305 * stripe, so we must do the range check again.
4306 * Expansion could still move past after this
4307 * test, but as we are holding a reference to
4308 * 'sh', we know that if that happens,
4309 * STRIPE_EXPANDING will get set and the expansion
4310 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004311 */
4312 int must_retry = 0;
4313 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004314 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11004315 ? logical_sector >= conf->reshape_progress
4316 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004317 /* mismatch, need to try again */
4318 must_retry = 1;
4319 spin_unlock_irq(&conf->device_lock);
4320 if (must_retry) {
4321 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004322 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004323 goto retry;
4324 }
4325 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004326
Namhyung Kimffd96e32011-07-18 17:38:51 +10004327 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004328 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004329 logical_sector < mddev->suspend_hi) {
4330 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004331 /* As the suspend_* range is controlled by
4332 * userspace, we want an interruptible
4333 * wait.
4334 */
4335 flush_signals(current);
4336 prepare_to_wait(&conf->wait_for_overlap,
4337 &w, TASK_INTERRUPTIBLE);
4338 if (logical_sector >= mddev->suspend_lo &&
4339 logical_sector < mddev->suspend_hi)
4340 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004341 goto retry;
4342 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004343
4344 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004345 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004346 /* Stripe is busy expanding or
4347 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 * and wait a while
4349 */
NeilBrown482c0832011-04-18 18:25:42 +10004350 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 release_stripe(sh);
4352 schedule();
4353 goto retry;
4354 }
4355 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004356 set_bit(STRIPE_HANDLE, &sh->state);
4357 clear_bit(STRIPE_DELAYED, &sh->state);
NeilBrowna852d7b2012-09-19 12:48:30 +10004358 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004359 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4360 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10004361 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 } else {
4363 /* cannot get stripe for read-ahead, just give-up */
4364 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4365 finish_wait(&conf->wait_for_overlap, &w);
4366 break;
4367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004369
Shaohua Lie7836bd62012-07-19 16:01:31 +10004370 remaining = raid5_dec_bi_active_stripes(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004371 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372
NeilBrown16a53ec2006-06-26 00:27:38 -07004373 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004375
Neil Brown0e13fe232008-06-28 08:31:20 +10004376 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378}
4379
NeilBrownfd01b882011-10-11 16:47:53 +11004380static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004381
NeilBrownfd01b882011-10-11 16:47:53 +11004382static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383{
NeilBrown52c03292006-06-26 00:27:43 -07004384 /* reshaping is quite different to recovery/resync so it is
4385 * handled quite separately ... here.
4386 *
4387 * On each call to sync_request, we gather one chunk worth of
4388 * destination stripes and flag them as expanding.
4389 * Then we find all the source stripes and request reads.
4390 * As the reads complete, handle_stripe will copy the data
4391 * into the destination stripe and release that stripe.
4392 */
NeilBrownd1688a62011-10-11 16:49:52 +11004393 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004395 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004396 int raid_disks = conf->previous_raid_disks;
4397 int data_disks = raid_disks - conf->max_degraded;
4398 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004399 int i;
4400 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004401 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004402 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004403 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004404 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004405
NeilBrownfef9c612009-03-31 15:16:46 +11004406 if (sector_nr == 0) {
4407 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10004408 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004409 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4410 sector_nr = raid5_size(mddev, 0, 0)
4411 - conf->reshape_progress;
NeilBrown2c810cd2012-05-21 09:27:00 +10004412 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004413 conf->reshape_progress > 0)
4414 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004415 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004416 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004417 mddev->curr_resync_completed = sector_nr;
4418 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004419 *skipped = 1;
4420 return sector_nr;
4421 }
NeilBrown52c03292006-06-26 00:27:43 -07004422 }
4423
NeilBrown7a661382009-03-31 15:21:40 +11004424 /* We need to process a full chunk at a time.
4425 * If old and new chunk sizes differ, we need to process the
4426 * largest of these
4427 */
Andre Noll664e7c42009-06-18 08:45:27 +10004428 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4429 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004430 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004431 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004432
NeilBrownb5254dd2012-05-21 09:27:01 +10004433 /* We update the metadata at least every 10 seconds, or when
4434 * the data about to be copied would over-write the source of
4435 * the data at the front of the range. i.e. one new_stripe
4436 * along from reshape_progress new_maps to after where
4437 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004438 */
NeilBrownfef9c612009-03-31 15:16:46 +11004439 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004440 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004441 readpos = conf->reshape_progress;
4442 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004443 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004444 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10004445 if (mddev->reshape_backwards) {
NeilBrowned37d832009-05-27 21:39:05 +10004446 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004447 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004448 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004449 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004450 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004451 readpos -= min_t(sector_t, reshape_sectors, readpos);
4452 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004453 }
NeilBrown52c03292006-06-26 00:27:43 -07004454
NeilBrownb5254dd2012-05-21 09:27:01 +10004455 /* Having calculated the 'writepos' possibly use it
4456 * to set 'stripe_addr' which is where we will write to.
4457 */
4458 if (mddev->reshape_backwards) {
4459 BUG_ON(conf->reshape_progress == 0);
4460 stripe_addr = writepos;
4461 BUG_ON((mddev->dev_sectors &
4462 ~((sector_t)reshape_sectors - 1))
4463 - reshape_sectors - stripe_addr
4464 != sector_nr);
4465 } else {
4466 BUG_ON(writepos != sector_nr + reshape_sectors);
4467 stripe_addr = sector_nr;
4468 }
4469
NeilBrownc8f517c2009-03-31 15:28:40 +11004470 /* 'writepos' is the most advanced device address we might write.
4471 * 'readpos' is the least advanced device address we might read.
4472 * 'safepos' is the least address recorded in the metadata as having
4473 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10004474 * If there is a min_offset_diff, these are adjusted either by
4475 * increasing the safepos/readpos if diff is negative, or
4476 * increasing writepos if diff is positive.
4477 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11004478 * ensure safety in the face of a crash - that must be done by userspace
4479 * making a backup of the data. So in that case there is no particular
4480 * rush to update metadata.
4481 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4482 * update the metadata to advance 'safepos' to match 'readpos' so that
4483 * we can be safe in the event of a crash.
4484 * So we insist on updating metadata if safepos is behind writepos and
4485 * readpos is beyond writepos.
4486 * In any case, update the metadata every 10 seconds.
4487 * Maybe that number should be configurable, but I'm not sure it is
4488 * worth it.... maybe it could be a multiple of safemode_delay???
4489 */
NeilBrownb5254dd2012-05-21 09:27:01 +10004490 if (conf->min_offset_diff < 0) {
4491 safepos += -conf->min_offset_diff;
4492 readpos += -conf->min_offset_diff;
4493 } else
4494 writepos += conf->min_offset_diff;
4495
NeilBrown2c810cd2012-05-21 09:27:00 +10004496 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11004497 ? (safepos > writepos && readpos < writepos)
4498 : (safepos < writepos && readpos > writepos)) ||
4499 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004500 /* Cannot proceed until we've updated the superblock... */
4501 wait_event(conf->wait_for_overlap,
4502 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004503 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004504 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004505 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07004506 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004507 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07004508 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004509 kthread_should_stop());
4510 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004511 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004512 spin_unlock_irq(&conf->device_lock);
4513 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004514 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004515 }
4516
NeilBrownab69ae12009-03-31 15:26:47 +11004517 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004518 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004519 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004520 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004521 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004522 set_bit(STRIPE_EXPANDING, &sh->state);
4523 atomic_inc(&conf->reshape_stripes);
4524 /* If any of this stripe is beyond the end of the old
4525 * array, then we need to zero those blocks
4526 */
4527 for (j=sh->disks; j--;) {
4528 sector_t s;
4529 if (j == sh->pd_idx)
4530 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004531 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004532 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004533 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004534 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004535 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004536 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004537 continue;
4538 }
4539 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4540 set_bit(R5_Expanded, &sh->dev[j].flags);
4541 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4542 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004543 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004544 set_bit(STRIPE_EXPAND_READY, &sh->state);
4545 set_bit(STRIPE_HANDLE, &sh->state);
4546 }
NeilBrownab69ae12009-03-31 15:26:47 +11004547 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004548 }
4549 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004550 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11004551 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004552 else
NeilBrown7a661382009-03-31 15:21:40 +11004553 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004554 spin_unlock_irq(&conf->device_lock);
4555 /* Ok, those stripe are ready. We can start scheduling
4556 * reads on the source stripes.
4557 * The source stripes are determined by mapping the first and last
4558 * block on the destination stripes.
4559 */
NeilBrown52c03292006-06-26 00:27:43 -07004560 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004561 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004562 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004563 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004564 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004565 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004566 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004567 if (last_sector >= mddev->dev_sectors)
4568 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004569 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004570 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004571 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4572 set_bit(STRIPE_HANDLE, &sh->state);
4573 release_stripe(sh);
4574 first_sector += STRIPE_SECTORS;
4575 }
NeilBrownab69ae12009-03-31 15:26:47 +11004576 /* Now that the sources are clearly marked, we can release
4577 * the destination stripes
4578 */
4579 while (!list_empty(&stripes)) {
4580 sh = list_entry(stripes.next, struct stripe_head, lru);
4581 list_del_init(&sh->lru);
4582 release_stripe(sh);
4583 }
NeilBrownc6207272008-02-06 01:39:52 -08004584 /* If this takes us to the resync_max point where we have to pause,
4585 * then we need to write out the superblock.
4586 */
NeilBrown7a661382009-03-31 15:21:40 +11004587 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004588 if ((sector_nr - mddev->curr_resync_completed) * 2
4589 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004590 /* Cannot proceed until we've updated the superblock... */
4591 wait_event(conf->wait_for_overlap,
4592 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004593 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004594 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004595 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004596 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4597 md_wakeup_thread(mddev->thread);
4598 wait_event(mddev->sb_wait,
4599 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4600 || kthread_should_stop());
4601 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004602 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004603 spin_unlock_irq(&conf->device_lock);
4604 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004605 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004606 }
NeilBrown7a661382009-03-31 15:21:40 +11004607 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004608}
4609
4610/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004611static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004612{
NeilBrownd1688a62011-10-11 16:49:52 +11004613 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004614 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004615 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004616 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004617 int still_degraded = 0;
4618 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619
NeilBrown72626682005-09-09 16:23:54 -07004620 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004622
NeilBrown29269552006-03-27 01:18:10 -08004623 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4624 end_reshape(conf);
4625 return 0;
4626 }
NeilBrown72626682005-09-09 16:23:54 -07004627
4628 if (mddev->curr_resync < max_sector) /* aborted */
4629 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4630 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004631 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004632 conf->fullsync = 0;
4633 bitmap_close_sync(mddev->bitmap);
4634
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 return 0;
4636 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004637
NeilBrown64bd6602009-08-03 10:59:58 +10004638 /* Allow raid5_quiesce to complete */
4639 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4640
NeilBrown52c03292006-06-26 00:27:43 -07004641 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4642 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004643
NeilBrownc6207272008-02-06 01:39:52 -08004644 /* No need to check resync_max as we never do more than one
4645 * stripe, and as resync_max will always be on a chunk boundary,
4646 * if the check in md_do_sync didn't fire, there is no chance
4647 * of overstepping resync_max here
4648 */
4649
NeilBrown16a53ec2006-06-26 00:27:38 -07004650 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 * to resync, then assert that we are finished, because there is
4652 * nothing we can do.
4653 */
NeilBrown3285edf2006-06-26 00:27:55 -07004654 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004655 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004656 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004657 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 return rv;
4659 }
NeilBrown72626682005-09-09 16:23:54 -07004660 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004661 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004662 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4663 /* we can skip this block, and probably more */
4664 sync_blocks /= STRIPE_SECTORS;
4665 *skipped = 1;
4666 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668
NeilBrownb47490c2008-02-06 01:39:50 -08004669 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4670
NeilBrowna8c906c2009-06-09 14:39:59 +10004671 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004673 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004675 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004677 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004679 /* Need to check if array will still be degraded after recovery/resync
4680 * We don't need to check the 'failed' flag as when that gets set,
4681 * recovery aborts.
4682 */
NeilBrownf001a702009-06-09 14:30:31 +10004683 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004684 if (conf->disks[i].rdev == NULL)
4685 still_degraded = 1;
4686
4687 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4688
NeilBrown83206d62011-07-26 11:19:49 +10004689 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690
NeilBrown14425772009-10-16 15:55:25 +11004691 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 release_stripe(sh);
4693
4694 return STRIPE_SECTORS;
4695}
4696
NeilBrownd1688a62011-10-11 16:49:52 +11004697static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004698{
4699 /* We may not be able to submit a whole bio at once as there
4700 * may not be enough stripe_heads available.
4701 * We cannot pre-allocate enough stripe_heads as we may need
4702 * more than exist in the cache (if we allow ever large chunks).
4703 * So we do one stripe head at a time and record in
4704 * ->bi_hw_segments how many have been done.
4705 *
4706 * We *know* that this entire raid_bio is in one chunk, so
4707 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4708 */
4709 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004710 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004711 sector_t sector, logical_sector, last_sector;
4712 int scnt = 0;
4713 int remaining;
4714 int handled = 0;
4715
4716 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004717 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004718 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004719 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4720
4721 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004722 logical_sector += STRIPE_SECTORS,
4723 sector += STRIPE_SECTORS,
4724 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004725
Shaohua Lie7836bd62012-07-19 16:01:31 +10004726 if (scnt < raid5_bi_processed_stripes(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004727 /* already done this stripe */
4728 continue;
4729
NeilBrowna8c906c2009-06-09 14:39:59 +10004730 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004731
4732 if (!sh) {
4733 /* failed to get a stripe - must wait */
Shaohua Lie7836bd62012-07-19 16:01:31 +10004734 raid5_set_bi_processed_stripes(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004735 conf->retry_read_aligned = raid_bio;
4736 return handled;
4737 }
4738
Neil Brown387bb172007-02-08 14:20:29 -08004739 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4740 release_stripe(sh);
Shaohua Lie7836bd62012-07-19 16:01:31 +10004741 raid5_set_bi_processed_stripes(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004742 conf->retry_read_aligned = raid_bio;
4743 return handled;
4744 }
4745
majianpeng3f9e7c12012-07-31 10:04:21 +10004746 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07004747 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004748 release_stripe(sh);
4749 handled++;
4750 }
Shaohua Lie7836bd62012-07-19 16:01:31 +10004751 remaining = raid5_dec_bi_active_stripes(raid_bio);
Tejun Heo3a366e62013-01-11 13:06:33 -08004752 if (remaining == 0)
Neil Brown0e13fe232008-06-28 08:31:20 +10004753 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004754 if (atomic_dec_and_test(&conf->active_aligned_reads))
4755 wake_up(&conf->wait_for_stripe);
4756 return handled;
4757}
4758
Shaohua Li46a06402012-08-02 08:33:15 +10004759#define MAX_STRIPE_BATCH 8
4760static int handle_active_stripes(struct r5conf *conf)
4761{
4762 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
4763 int i, batch_size = 0;
4764
4765 while (batch_size < MAX_STRIPE_BATCH &&
4766 (sh = __get_priority_stripe(conf)) != NULL)
4767 batch[batch_size++] = sh;
4768
4769 if (batch_size == 0)
4770 return batch_size;
4771 spin_unlock_irq(&conf->device_lock);
4772
4773 for (i = 0; i < batch_size; i++)
4774 handle_stripe(batch[i]);
4775
4776 cond_resched();
4777
4778 spin_lock_irq(&conf->device_lock);
4779 for (i = 0; i < batch_size; i++)
4780 __release_stripe(conf, batch[i]);
4781 return batch_size;
4782}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004783
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784/*
4785 * This is our raid5 kernel thread.
4786 *
4787 * We scan the hash table for stripes which can be handled now.
4788 * During the scan, completed stripes are saved for us by the interrupt
4789 * handler, so that they will not have to wait for our next wakeup.
4790 */
Shaohua Li4ed87312012-10-11 13:34:00 +11004791static void raid5d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792{
Shaohua Li4ed87312012-10-11 13:34:00 +11004793 struct mddev *mddev = thread->mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11004794 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004796 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797
Dan Williams45b42332007-07-09 11:56:43 -07004798 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799
4800 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004802 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 handled = 0;
4804 spin_lock_irq(&conf->device_lock);
4805 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004806 struct bio *bio;
Shaohua Li46a06402012-08-02 08:33:15 +10004807 int batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808
NeilBrown0021b7b2012-07-31 09:08:14 +02004809 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10004810 !list_empty(&conf->bitmap_list)) {
4811 /* Now is a good time to flush some bitmap updates */
4812 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004813 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004814 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004815 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004816 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004817 activate_bit_delay(conf);
4818 }
NeilBrown0021b7b2012-07-31 09:08:14 +02004819 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004820
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004821 while ((bio = remove_bio_from_retry(conf))) {
4822 int ok;
4823 spin_unlock_irq(&conf->device_lock);
4824 ok = retry_aligned_read(conf, bio);
4825 spin_lock_irq(&conf->device_lock);
4826 if (!ok)
4827 break;
4828 handled++;
4829 }
4830
Shaohua Li46a06402012-08-02 08:33:15 +10004831 batch_size = handle_active_stripes(conf);
4832 if (!batch_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 break;
Shaohua Li46a06402012-08-02 08:33:15 +10004834 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835
Shaohua Li46a06402012-08-02 08:33:15 +10004836 if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) {
4837 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10004838 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10004839 spin_lock_irq(&conf->device_lock);
4840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 }
Dan Williams45b42332007-07-09 11:56:43 -07004842 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843
4844 spin_unlock_irq(&conf->device_lock);
4845
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004846 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004847 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848
Dan Williams45b42332007-07-09 11:56:43 -07004849 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850}
4851
NeilBrown3f294f42005-11-08 21:39:25 -08004852static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004853raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004854{
NeilBrownd1688a62011-10-11 16:49:52 +11004855 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004856 if (conf)
4857 return sprintf(page, "%d\n", conf->max_nr_stripes);
4858 else
4859 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004860}
4861
NeilBrownc41d4ac2010-06-01 19:37:24 +10004862int
NeilBrownfd01b882011-10-11 16:47:53 +11004863raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004864{
NeilBrownd1688a62011-10-11 16:49:52 +11004865 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004866 int err;
4867
4868 if (size <= 16 || size > 32768)
4869 return -EINVAL;
4870 while (size < conf->max_nr_stripes) {
4871 if (drop_one_stripe(conf))
4872 conf->max_nr_stripes--;
4873 else
4874 break;
4875 }
4876 err = md_allow_write(mddev);
4877 if (err)
4878 return err;
4879 while (size > conf->max_nr_stripes) {
4880 if (grow_one_stripe(conf))
4881 conf->max_nr_stripes++;
4882 else break;
4883 }
4884 return 0;
4885}
4886EXPORT_SYMBOL(raid5_set_cache_size);
4887
NeilBrown3f294f42005-11-08 21:39:25 -08004888static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004889raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004890{
NeilBrownd1688a62011-10-11 16:49:52 +11004891 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004892 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004893 int err;
4894
NeilBrown3f294f42005-11-08 21:39:25 -08004895 if (len >= PAGE_SIZE)
4896 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004897 if (!conf)
4898 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004899
Dan Williams4ef197d82008-04-28 02:15:54 -07004900 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004901 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004902 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004903 if (err)
4904 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004905 return len;
4906}
NeilBrown007583c2005-11-08 21:39:30 -08004907
NeilBrown96de1e62005-11-08 21:39:39 -08004908static struct md_sysfs_entry
4909raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4910 raid5_show_stripe_cache_size,
4911 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004912
4913static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004914raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004915{
NeilBrownd1688a62011-10-11 16:49:52 +11004916 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004917 if (conf)
4918 return sprintf(page, "%d\n", conf->bypass_threshold);
4919 else
4920 return 0;
4921}
4922
4923static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004924raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004925{
NeilBrownd1688a62011-10-11 16:49:52 +11004926 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004927 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004928 if (len >= PAGE_SIZE)
4929 return -EINVAL;
4930 if (!conf)
4931 return -ENODEV;
4932
Dan Williams4ef197d82008-04-28 02:15:54 -07004933 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004934 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004935 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004936 return -EINVAL;
4937 conf->bypass_threshold = new;
4938 return len;
4939}
4940
4941static struct md_sysfs_entry
4942raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4943 S_IRUGO | S_IWUSR,
4944 raid5_show_preread_threshold,
4945 raid5_store_preread_threshold);
4946
4947static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004948stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004949{
NeilBrownd1688a62011-10-11 16:49:52 +11004950 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004951 if (conf)
4952 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4953 else
4954 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004955}
4956
NeilBrown96de1e62005-11-08 21:39:39 -08004957static struct md_sysfs_entry
4958raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004959
NeilBrown007583c2005-11-08 21:39:30 -08004960static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004961 &raid5_stripecache_size.attr,
4962 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004963 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004964 NULL,
4965};
NeilBrown007583c2005-11-08 21:39:30 -08004966static struct attribute_group raid5_attrs_group = {
4967 .name = NULL,
4968 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004969};
4970
Dan Williams80c3a6c2009-03-17 18:10:40 -07004971static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11004972raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07004973{
NeilBrownd1688a62011-10-11 16:49:52 +11004974 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004975
4976 if (!sectors)
4977 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004978 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11004979 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11004980 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004981
Andre Noll9d8f0362009-06-18 08:45:01 +10004982 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004983 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004984 return sectors * (raid_disks - conf->max_degraded);
4985}
4986
NeilBrownd1688a62011-10-11 16:49:52 +11004987static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004988{
4989 struct raid5_percpu *percpu;
4990 unsigned long cpu;
4991
4992 if (!conf->percpu)
4993 return;
4994
4995 get_online_cpus();
4996 for_each_possible_cpu(cpu) {
4997 percpu = per_cpu_ptr(conf->percpu, cpu);
4998 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004999 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005000 }
5001#ifdef CONFIG_HOTPLUG_CPU
5002 unregister_cpu_notifier(&conf->cpu_notify);
5003#endif
5004 put_online_cpus();
5005
5006 free_percpu(conf->percpu);
5007}
5008
NeilBrownd1688a62011-10-11 16:49:52 +11005009static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10005010{
5011 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07005012 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10005013 kfree(conf->disks);
5014 kfree(conf->stripe_hashtbl);
5015 kfree(conf);
5016}
5017
Dan Williams36d1c642009-07-14 11:48:22 -07005018#ifdef CONFIG_HOTPLUG_CPU
5019static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
5020 void *hcpu)
5021{
NeilBrownd1688a62011-10-11 16:49:52 +11005022 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07005023 long cpu = (long)hcpu;
5024 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
5025
5026 switch (action) {
5027 case CPU_UP_PREPARE:
5028 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07005029 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07005030 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005031 if (!percpu->scribble)
5032 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
5033
5034 if (!percpu->scribble ||
5035 (conf->level == 6 && !percpu->spare_page)) {
5036 safe_put_page(percpu->spare_page);
5037 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005038 pr_err("%s: failed memory allocation for cpu%ld\n",
5039 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07005040 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07005041 }
5042 break;
5043 case CPU_DEAD:
5044 case CPU_DEAD_FROZEN:
5045 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005046 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005047 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07005048 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07005049 break;
5050 default:
5051 break;
5052 }
5053 return NOTIFY_OK;
5054}
5055#endif
5056
NeilBrownd1688a62011-10-11 16:49:52 +11005057static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07005058{
5059 unsigned long cpu;
5060 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09005061 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07005062 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07005063 int err;
5064
Dan Williams36d1c642009-07-14 11:48:22 -07005065 allcpus = alloc_percpu(struct raid5_percpu);
5066 if (!allcpus)
5067 return -ENOMEM;
5068 conf->percpu = allcpus;
5069
5070 get_online_cpus();
5071 err = 0;
5072 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07005073 if (conf->level == 6) {
5074 spare_page = alloc_page(GFP_KERNEL);
5075 if (!spare_page) {
5076 err = -ENOMEM;
5077 break;
5078 }
5079 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
5080 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11005081 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005082 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07005083 err = -ENOMEM;
5084 break;
5085 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07005086 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07005087 }
5088#ifdef CONFIG_HOTPLUG_CPU
5089 conf->cpu_notify.notifier_call = raid456_cpu_notify;
5090 conf->cpu_notify.priority = 0;
5091 if (err == 0)
5092 err = register_cpu_notifier(&conf->cpu_notify);
5093#endif
5094 put_online_cpus();
5095
5096 return err;
5097}
5098
NeilBrownd1688a62011-10-11 16:49:52 +11005099static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100{
NeilBrownd1688a62011-10-11 16:49:52 +11005101 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005102 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11005103 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10005105 char pers_name[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106
NeilBrown91adb562009-03-31 14:39:39 +11005107 if (mddev->new_level != 5
5108 && mddev->new_level != 4
5109 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10005110 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005111 mdname(mddev), mddev->new_level);
5112 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 }
NeilBrown91adb562009-03-31 14:39:39 +11005114 if ((mddev->new_level == 5
5115 && !algorithm_valid_raid5(mddev->new_layout)) ||
5116 (mddev->new_level == 6
5117 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005118 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11005119 mdname(mddev), mddev->new_layout);
5120 return ERR_PTR(-EIO);
5121 }
5122 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10005123 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005124 mdname(mddev), mddev->raid_disks);
5125 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11005126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127
Andre Noll664e7c42009-06-18 08:45:27 +10005128 if (!mddev->new_chunk_sectors ||
5129 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
5130 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005131 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
5132 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11005133 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11005134 }
5135
NeilBrownd1688a62011-10-11 16:49:52 +11005136 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11005137 if (conf == NULL)
5138 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11005139 spin_lock_init(&conf->device_lock);
5140 init_waitqueue_head(&conf->wait_for_stripe);
5141 init_waitqueue_head(&conf->wait_for_overlap);
5142 INIT_LIST_HEAD(&conf->handle_list);
5143 INIT_LIST_HEAD(&conf->hold_list);
5144 INIT_LIST_HEAD(&conf->delayed_list);
5145 INIT_LIST_HEAD(&conf->bitmap_list);
5146 INIT_LIST_HEAD(&conf->inactive_list);
5147 atomic_set(&conf->active_stripes, 0);
5148 atomic_set(&conf->preread_active_stripes, 0);
5149 atomic_set(&conf->active_aligned_reads, 0);
5150 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11005151 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11005152
5153 conf->raid_disks = mddev->raid_disks;
5154 if (mddev->reshape_position == MaxSector)
5155 conf->previous_raid_disks = mddev->raid_disks;
5156 else
5157 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005158 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
5159 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11005160
NeilBrown5e5e3e72009-10-16 16:35:30 +11005161 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11005162 GFP_KERNEL);
5163 if (!conf->disks)
5164 goto abort;
5165
5166 conf->mddev = mddev;
5167
5168 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
5169 goto abort;
5170
Dan Williams36d1c642009-07-14 11:48:22 -07005171 conf->level = mddev->new_level;
5172 if (raid5_alloc_percpu(conf) != 0)
5173 goto abort;
5174
NeilBrown0c55e022010-05-03 14:09:02 +10005175 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11005176
NeilBrowndafb20f2012-03-19 12:46:39 +11005177 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11005178 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005179 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11005180 || raid_disk < 0)
5181 continue;
5182 disk = conf->disks + raid_disk;
5183
NeilBrown17045f52011-12-23 10:17:53 +11005184 if (test_bit(Replacement, &rdev->flags)) {
5185 if (disk->replacement)
5186 goto abort;
5187 disk->replacement = rdev;
5188 } else {
5189 if (disk->rdev)
5190 goto abort;
5191 disk->rdev = rdev;
5192 }
NeilBrown91adb562009-03-31 14:39:39 +11005193
5194 if (test_bit(In_sync, &rdev->flags)) {
5195 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10005196 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
5197 " disk %d\n",
5198 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05005199 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11005200 /* Cannot rely on bitmap to complete recovery */
5201 conf->fullsync = 1;
5202 }
5203
Andre Noll09c9e5f2009-06-18 08:45:55 +10005204 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11005205 conf->level = mddev->new_level;
5206 if (conf->level == 6)
5207 conf->max_degraded = 2;
5208 else
5209 conf->max_degraded = 1;
5210 conf->algorithm = mddev->new_layout;
5211 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11005212 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11005213 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10005214 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11005215 conf->prev_algo = mddev->layout;
5216 }
NeilBrown91adb562009-03-31 14:39:39 +11005217
5218 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11005219 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11005220 if (grow_stripes(conf, conf->max_nr_stripes)) {
5221 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005222 "md/raid:%s: couldn't allocate %dkB for buffers\n",
5223 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005224 goto abort;
5225 } else
NeilBrown0c55e022010-05-03 14:09:02 +10005226 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
5227 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005228
NeilBrown02326052012-07-03 15:56:52 +10005229 sprintf(pers_name, "raid%d", mddev->new_level);
5230 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11005231 if (!conf->thread) {
5232 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005233 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11005234 mdname(mddev));
5235 goto abort;
5236 }
5237
5238 return conf;
5239
5240 abort:
5241 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10005242 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11005243 return ERR_PTR(-EIO);
5244 } else
5245 return ERR_PTR(-ENOMEM);
5246}
5247
NeilBrownc148ffd2009-11-13 17:47:00 +11005248
5249static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
5250{
5251 switch (algo) {
5252 case ALGORITHM_PARITY_0:
5253 if (raid_disk < max_degraded)
5254 return 1;
5255 break;
5256 case ALGORITHM_PARITY_N:
5257 if (raid_disk >= raid_disks - max_degraded)
5258 return 1;
5259 break;
5260 case ALGORITHM_PARITY_0_6:
5261 if (raid_disk == 0 ||
5262 raid_disk == raid_disks - 1)
5263 return 1;
5264 break;
5265 case ALGORITHM_LEFT_ASYMMETRIC_6:
5266 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5267 case ALGORITHM_LEFT_SYMMETRIC_6:
5268 case ALGORITHM_RIGHT_SYMMETRIC_6:
5269 if (raid_disk == raid_disks - 1)
5270 return 1;
5271 }
5272 return 0;
5273}
5274
NeilBrownfd01b882011-10-11 16:47:53 +11005275static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11005276{
NeilBrownd1688a62011-10-11 16:49:52 +11005277 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10005278 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11005279 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11005280 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11005281 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11005282 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10005283 long long min_offset_diff = 0;
5284 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11005285
Andre Noll8c6ac862009-06-18 08:48:06 +10005286 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10005287 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10005288 " -- starting background reconstruction\n",
5289 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10005290
5291 rdev_for_each(rdev, mddev) {
5292 long long diff;
5293 if (rdev->raid_disk < 0)
5294 continue;
5295 diff = (rdev->new_data_offset - rdev->data_offset);
5296 if (first) {
5297 min_offset_diff = diff;
5298 first = 0;
5299 } else if (mddev->reshape_backwards &&
5300 diff < min_offset_diff)
5301 min_offset_diff = diff;
5302 else if (!mddev->reshape_backwards &&
5303 diff > min_offset_diff)
5304 min_offset_diff = diff;
5305 }
5306
NeilBrownf6705572006-03-27 01:18:11 -08005307 if (mddev->reshape_position != MaxSector) {
5308 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10005309 * Difficulties arise if the stripe we would write to
5310 * next is at or after the stripe we would read from next.
5311 * For a reshape that changes the number of devices, this
5312 * is only possible for a very short time, and mdadm makes
5313 * sure that time appears to have past before assembling
5314 * the array. So we fail if that time hasn't passed.
5315 * For a reshape that keeps the number of devices the same
5316 * mdadm must be monitoring the reshape can keeping the
5317 * critical areas read-only and backed up. It will start
5318 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08005319 */
5320 sector_t here_new, here_old;
5321 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11005322 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08005323
NeilBrown88ce4932009-03-31 15:24:23 +11005324 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10005325 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08005326 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08005327 mdname(mddev));
5328 return -EINVAL;
5329 }
NeilBrownf6705572006-03-27 01:18:11 -08005330 old_disks = mddev->raid_disks - mddev->delta_disks;
5331 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08005332 * further up in new geometry must map after here in old
5333 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08005334 */
5335 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10005336 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005337 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005338 printk(KERN_ERR "md/raid:%s: reshape_position not "
5339 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005340 return -EINVAL;
5341 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005342 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005343 /* here_new is the stripe we will write to */
5344 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10005345 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005346 (old_disks-max_degraded));
5347 /* here_old is the first stripe that we might need to read
5348 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10005349 if (mddev->delta_disks == 0) {
NeilBrownb5254dd2012-05-21 09:27:01 +10005350 if ((here_new * mddev->new_chunk_sectors !=
5351 here_old * mddev->chunk_sectors)) {
5352 printk(KERN_ERR "md/raid:%s: reshape position is"
5353 " confused - aborting\n", mdname(mddev));
5354 return -EINVAL;
5355 }
NeilBrown67ac6012009-08-13 10:06:24 +10005356 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10005357 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10005358 * and taking constant backups.
5359 * mdadm always starts a situation like this in
5360 * readonly mode so it can take control before
5361 * allowing any writes. So just check for that.
5362 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005363 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
5364 abs(min_offset_diff) >= mddev->new_chunk_sectors)
5365 /* not really in-place - so OK */;
5366 else if (mddev->ro == 0) {
5367 printk(KERN_ERR "md/raid:%s: in-place reshape "
5368 "must be started in read-only mode "
5369 "- aborting\n",
NeilBrown0c55e022010-05-03 14:09:02 +10005370 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005371 return -EINVAL;
5372 }
NeilBrown2c810cd2012-05-21 09:27:00 +10005373 } else if (mddev->reshape_backwards
NeilBrownb5254dd2012-05-21 09:27:01 +10005374 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
NeilBrown67ac6012009-08-13 10:06:24 +10005375 here_old * mddev->chunk_sectors)
5376 : (here_new * mddev->new_chunk_sectors >=
NeilBrownb5254dd2012-05-21 09:27:01 +10005377 here_old * mddev->chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08005378 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005379 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5380 "auto-recovery - aborting.\n",
5381 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005382 return -EINVAL;
5383 }
NeilBrown0c55e022010-05-03 14:09:02 +10005384 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5385 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005386 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005387 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005388 BUG_ON(mddev->level != mddev->new_level);
5389 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005390 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005391 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005392 }
5393
NeilBrown245f46c2009-03-31 14:39:39 +11005394 if (mddev->private == NULL)
5395 conf = setup_conf(mddev);
5396 else
5397 conf = mddev->private;
5398
NeilBrown91adb562009-03-31 14:39:39 +11005399 if (IS_ERR(conf))
5400 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005401
NeilBrownb5254dd2012-05-21 09:27:01 +10005402 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11005403 mddev->thread = conf->thread;
5404 conf->thread = NULL;
5405 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406
NeilBrown17045f52011-12-23 10:17:53 +11005407 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5408 i++) {
5409 rdev = conf->disks[i].rdev;
5410 if (!rdev && conf->disks[i].replacement) {
5411 /* The replacement is all we have yet */
5412 rdev = conf->disks[i].replacement;
5413 conf->disks[i].replacement = NULL;
5414 clear_bit(Replacement, &rdev->flags);
5415 conf->disks[i].rdev = rdev;
5416 }
5417 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11005418 continue;
NeilBrown17045f52011-12-23 10:17:53 +11005419 if (conf->disks[i].replacement &&
5420 conf->reshape_progress != MaxSector) {
5421 /* replacements and reshape simply do not mix. */
5422 printk(KERN_ERR "md: cannot handle concurrent "
5423 "replacement and reshape.\n");
5424 goto abort;
5425 }
NeilBrown2f115882010-06-17 17:41:03 +10005426 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005427 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005428 continue;
5429 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005430 /* This disc is not fully in-sync. However if it
5431 * just stored parity (beyond the recovery_offset),
5432 * when we don't need to be concerned about the
5433 * array being dirty.
5434 * When reshape goes 'backwards', we never have
5435 * partially completed devices, so we only need
5436 * to worry about reshape going forwards.
5437 */
5438 /* Hack because v0.91 doesn't store recovery_offset properly. */
5439 if (mddev->major_version == 0 &&
5440 mddev->minor_version > 90)
5441 rdev->recovery_offset = reshape_offset;
5442
NeilBrownc148ffd2009-11-13 17:47:00 +11005443 if (rdev->recovery_offset < reshape_offset) {
5444 /* We need to check old and new layout */
5445 if (!only_parity(rdev->raid_disk,
5446 conf->algorithm,
5447 conf->raid_disks,
5448 conf->max_degraded))
5449 continue;
5450 }
5451 if (!only_parity(rdev->raid_disk,
5452 conf->prev_algo,
5453 conf->previous_raid_disks,
5454 conf->max_degraded))
5455 continue;
5456 dirty_parity_disks++;
5457 }
NeilBrown91adb562009-03-31 14:39:39 +11005458
NeilBrown17045f52011-12-23 10:17:53 +11005459 /*
5460 * 0 for a fully functional array, 1 or 2 for a degraded array.
5461 */
NeilBrown908f4fb2011-12-23 10:17:50 +11005462 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463
NeilBrown674806d2010-06-16 17:17:53 +10005464 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005465 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07005467 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 goto abort;
5469 }
5470
NeilBrown91adb562009-03-31 14:39:39 +11005471 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10005472 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11005473 mddev->resync_max_sectors = mddev->dev_sectors;
5474
NeilBrownc148ffd2009-11-13 17:47:00 +11005475 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005477 if (mddev->ok_start_degraded)
5478 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10005479 "md/raid:%s: starting dirty degraded array"
5480 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005481 mdname(mddev));
5482 else {
5483 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005484 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005485 mdname(mddev));
5486 goto abort;
5487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 }
5489
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10005491 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5492 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11005493 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5494 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 else
NeilBrown0c55e022010-05-03 14:09:02 +10005496 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5497 " out of %d devices, algorithm %d\n",
5498 mdname(mddev), conf->level,
5499 mddev->raid_disks - mddev->degraded,
5500 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501
5502 print_raid5_conf(conf);
5503
NeilBrownfef9c612009-03-31 15:16:46 +11005504 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11005505 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005506 atomic_set(&conf->reshape_stripes, 0);
5507 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5508 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5509 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5510 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5511 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005512 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005513 }
5514
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515
5516 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005517 if (mddev->to_remove == &raid5_attrs_group)
5518 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005519 else if (mddev->kobj.sd &&
5520 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005521 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10005522 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08005523 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10005524 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5525
5526 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005527 int chunk_size;
Shaohua Li620125f2012-10-11 13:49:05 +11005528 bool discard_supported = true;
NeilBrown4a5add42010-06-01 19:37:28 +10005529 /* read-ahead size must cover two whole stripes, which
5530 * is 2 * (datadisks) * chunksize where 'n' is the
5531 * number of raid devices
5532 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5534 int stripe = data_disks *
5535 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5536 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5537 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10005538
5539 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005540
5541 mddev->queue->backing_dev_info.congested_data = mddev;
5542 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10005543
5544 chunk_size = mddev->chunk_sectors << 9;
5545 blk_queue_io_min(mddev->queue, chunk_size);
5546 blk_queue_io_opt(mddev->queue, chunk_size *
5547 (conf->raid_disks - conf->max_degraded));
Shaohua Li620125f2012-10-11 13:49:05 +11005548 /*
5549 * We can only discard a whole stripe. It doesn't make sense to
5550 * discard data disk but write parity disk
5551 */
5552 stripe = stripe * PAGE_SIZE;
NeilBrown4ac68752012-11-19 13:11:26 +11005553 /* Round up to power of 2, as discard handling
5554 * currently assumes that */
5555 while ((stripe-1) & stripe)
5556 stripe = (stripe | (stripe-1)) + 1;
Shaohua Li620125f2012-10-11 13:49:05 +11005557 mddev->queue->limits.discard_alignment = stripe;
5558 mddev->queue->limits.discard_granularity = stripe;
5559 /*
5560 * unaligned part of discard request will be ignored, so can't
5561 * guarantee discard_zerors_data
5562 */
5563 mddev->queue->limits.discard_zeroes_data = 0;
NeilBrown9f7c2222010-07-26 12:04:13 +10005564
NeilBrown05616be2012-05-21 09:27:00 +10005565 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005566 disk_stack_limits(mddev->gendisk, rdev->bdev,
5567 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10005568 disk_stack_limits(mddev->gendisk, rdev->bdev,
5569 rdev->new_data_offset << 9);
Shaohua Li620125f2012-10-11 13:49:05 +11005570 /*
5571 * discard_zeroes_data is required, otherwise data
5572 * could be lost. Consider a scenario: discard a stripe
5573 * (the stripe could be inconsistent if
5574 * discard_zeroes_data is 0); write one disk of the
5575 * stripe (the stripe could be inconsistent again
5576 * depending on which disks are used to calculate
5577 * parity); the disk is broken; The stripe data of this
5578 * disk is lost.
5579 */
5580 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
5581 !bdev_get_queue(rdev->bdev)->
5582 limits.discard_zeroes_data)
5583 discard_supported = false;
NeilBrown05616be2012-05-21 09:27:00 +10005584 }
Shaohua Li620125f2012-10-11 13:49:05 +11005585
5586 if (discard_supported &&
5587 mddev->queue->limits.max_discard_sectors >= stripe &&
5588 mddev->queue->limits.discard_granularity >= stripe)
5589 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
5590 mddev->queue);
5591 else
5592 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
5593 mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 }
5595
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596 return 0;
5597abort:
NeilBrown01f96c02011-09-21 15:30:20 +10005598 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11005599 print_raid5_conf(conf);
5600 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005602 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 return -EIO;
5604}
5605
NeilBrownfd01b882011-10-11 16:47:53 +11005606static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607{
NeilBrownd1688a62011-10-11 16:49:52 +11005608 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609
NeilBrown01f96c02011-09-21 15:30:20 +10005610 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005611 if (mddev->queue)
5612 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10005613 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005614 mddev->private = NULL;
5615 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 return 0;
5617}
5618
NeilBrownfd01b882011-10-11 16:47:53 +11005619static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620{
NeilBrownd1688a62011-10-11 16:49:52 +11005621 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622 int i;
5623
Andre Noll9d8f0362009-06-18 08:45:01 +10005624 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5625 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005626 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 for (i = 0; i < conf->raid_disks; i++)
5628 seq_printf (seq, "%s",
5629 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005630 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632}
5633
NeilBrownd1688a62011-10-11 16:49:52 +11005634static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635{
5636 int i;
5637 struct disk_info *tmp;
5638
NeilBrown0c55e022010-05-03 14:09:02 +10005639 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 if (!conf) {
5641 printk("(conf==NULL)\n");
5642 return;
5643 }
NeilBrown0c55e022010-05-03 14:09:02 +10005644 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5645 conf->raid_disks,
5646 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647
5648 for (i = 0; i < conf->raid_disks; i++) {
5649 char b[BDEVNAME_SIZE];
5650 tmp = conf->disks + i;
5651 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005652 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5653 i, !test_bit(Faulty, &tmp->rdev->flags),
5654 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 }
5656}
5657
NeilBrownfd01b882011-10-11 16:47:53 +11005658static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659{
5660 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005661 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005663 int count = 0;
5664 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665
5666 for (i = 0; i < conf->raid_disks; i++) {
5667 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11005668 if (tmp->replacement
5669 && tmp->replacement->recovery_offset == MaxSector
5670 && !test_bit(Faulty, &tmp->replacement->flags)
5671 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
5672 /* Replacement has just become active. */
5673 if (!tmp->rdev
5674 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
5675 count++;
5676 if (tmp->rdev) {
5677 /* Replaced device not technically faulty,
5678 * but we need to be sure it gets removed
5679 * and never re-added.
5680 */
5681 set_bit(Faulty, &tmp->rdev->flags);
5682 sysfs_notify_dirent_safe(
5683 tmp->rdev->sysfs_state);
5684 }
5685 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
5686 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005687 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005688 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005689 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005690 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005691 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 }
5693 }
NeilBrown6b965622010-08-18 11:56:59 +10005694 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005695 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005696 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005698 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699}
5700
NeilBrownb8321b62011-12-23 10:17:51 +11005701static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702{
NeilBrownd1688a62011-10-11 16:49:52 +11005703 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11005705 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11005706 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707 struct disk_info *p = conf->disks + number;
5708
5709 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11005710 if (rdev == p->rdev)
5711 rdevp = &p->rdev;
5712 else if (rdev == p->replacement)
5713 rdevp = &p->replacement;
5714 else
5715 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11005716
NeilBrown657e3e42011-12-23 10:17:52 +11005717 if (number >= conf->raid_disks &&
5718 conf->reshape_progress == MaxSector)
5719 clear_bit(In_sync, &rdev->flags);
5720
5721 if (test_bit(In_sync, &rdev->flags) ||
5722 atomic_read(&rdev->nr_pending)) {
5723 err = -EBUSY;
5724 goto abort;
5725 }
5726 /* Only remove non-faulty devices if recovery
5727 * isn't possible.
5728 */
5729 if (!test_bit(Faulty, &rdev->flags) &&
5730 mddev->recovery_disabled != conf->recovery_disabled &&
5731 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11005732 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11005733 number < conf->raid_disks) {
5734 err = -EBUSY;
5735 goto abort;
5736 }
5737 *rdevp = NULL;
5738 synchronize_rcu();
5739 if (atomic_read(&rdev->nr_pending)) {
5740 /* lost the race, try later */
5741 err = -EBUSY;
5742 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11005743 } else if (p->replacement) {
5744 /* We must have just cleared 'rdev' */
5745 p->rdev = p->replacement;
5746 clear_bit(Replacement, &p->replacement->flags);
5747 smp_mb(); /* Make sure other CPUs may see both as identical
5748 * but will never see neither - if they are careful
5749 */
5750 p->replacement = NULL;
5751 clear_bit(WantReplacement, &rdev->flags);
5752 } else
5753 /* We might have just removed the Replacement as faulty-
5754 * clear the bit just in case
5755 */
5756 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757abort:
5758
5759 print_raid5_conf(conf);
5760 return err;
5761}
5762
NeilBrownfd01b882011-10-11 16:47:53 +11005763static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764{
NeilBrownd1688a62011-10-11 16:49:52 +11005765 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005766 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 int disk;
5768 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005769 int first = 0;
5770 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771
NeilBrown7f0da592011-07-28 11:39:22 +10005772 if (mddev->recovery_disabled == conf->recovery_disabled)
5773 return -EBUSY;
5774
NeilBrowndc10c642012-03-19 12:46:37 +11005775 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005777 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778
Neil Brown6c2fce22008-06-28 08:31:31 +10005779 if (rdev->raid_disk >= 0)
5780 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781
5782 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005783 * find the disk ... but prefer rdev->saved_raid_disk
5784 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005786 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005787 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005788 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10005789 first = rdev->saved_raid_disk;
5790
5791 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11005792 p = conf->disks + disk;
5793 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005794 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005796 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005797 if (rdev->saved_raid_disk != disk)
5798 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005799 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10005800 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005802 }
5803 for (disk = first; disk <= last; disk++) {
5804 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11005805 if (test_bit(WantReplacement, &p->rdev->flags) &&
5806 p->replacement == NULL) {
5807 clear_bit(In_sync, &rdev->flags);
5808 set_bit(Replacement, &rdev->flags);
5809 rdev->raid_disk = disk;
5810 err = 0;
5811 conf->fullsync = 1;
5812 rcu_assign_pointer(p->replacement, rdev);
5813 break;
5814 }
5815 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005816out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005818 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819}
5820
NeilBrownfd01b882011-10-11 16:47:53 +11005821static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822{
5823 /* no resync is happening, and there is enough space
5824 * on all devices, so we can resize.
5825 * We need to make sure resync covers any new space.
5826 * If the array is shrinking we should possibly wait until
5827 * any io in the removed space completes, but it hardly seems
5828 * worth it.
5829 */
NeilBrowna4a61252012-05-22 13:55:27 +10005830 sector_t newsize;
Andre Noll9d8f0362009-06-18 08:45:01 +10005831 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10005832 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
5833 if (mddev->external_size &&
5834 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11005835 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10005836 if (mddev->bitmap) {
5837 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
5838 if (ret)
5839 return ret;
5840 }
5841 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10005842 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005843 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005844 if (sectors > mddev->dev_sectors &&
5845 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005846 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5848 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005849 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005850 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 return 0;
5852}
5853
NeilBrownfd01b882011-10-11 16:47:53 +11005854static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005855{
5856 /* Can only proceed if there are plenty of stripe_heads.
5857 * We need a minimum of one full stripe,, and for sensible progress
5858 * it is best to have about 4 times that.
5859 * If we require 4 times, then the default 256 4K stripe_heads will
5860 * allow for chunk sizes up to 256K, which is probably OK.
5861 * If the chunk size is greater, user-space should request more
5862 * stripe_heads first.
5863 */
NeilBrownd1688a62011-10-11 16:49:52 +11005864 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005865 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5866 > conf->max_nr_stripes ||
5867 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5868 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005869 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5870 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005871 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5872 / STRIPE_SIZE)*4);
5873 return 0;
5874 }
5875 return 1;
5876}
5877
NeilBrownfd01b882011-10-11 16:47:53 +11005878static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005879{
NeilBrownd1688a62011-10-11 16:49:52 +11005880 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005881
NeilBrown88ce4932009-03-31 15:24:23 +11005882 if (mddev->delta_disks == 0 &&
5883 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005884 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005885 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10005886 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005887 return -EINVAL;
5888 if (mddev->delta_disks < 0) {
5889 /* We might be able to shrink, but the devices must
5890 * be made bigger first.
5891 * For raid6, 4 is the minimum size.
5892 * Otherwise 2 is the minimum
5893 */
5894 int min = 2;
5895 if (mddev->level == 6)
5896 min = 4;
5897 if (mddev->raid_disks + mddev->delta_disks < min)
5898 return -EINVAL;
5899 }
NeilBrown29269552006-03-27 01:18:10 -08005900
NeilBrown01ee22b2009-06-18 08:47:20 +10005901 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005902 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005903
NeilBrowne56108d62012-10-11 14:24:13 +11005904 return resize_stripes(conf, (conf->previous_raid_disks
5905 + mddev->delta_disks));
NeilBrown63c70c42006-03-27 01:18:13 -08005906}
5907
NeilBrownfd01b882011-10-11 16:47:53 +11005908static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005909{
NeilBrownd1688a62011-10-11 16:49:52 +11005910 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005911 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005912 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005913 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005914
NeilBrownf4168852007-02-28 20:11:53 -08005915 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005916 return -EBUSY;
5917
NeilBrown01ee22b2009-06-18 08:47:20 +10005918 if (!check_stripe_cache(mddev))
5919 return -ENOSPC;
5920
NeilBrown30b67642012-05-22 13:55:28 +10005921 if (has_failed(conf))
5922 return -EINVAL;
5923
NeilBrownc6563a82012-05-21 09:27:00 +10005924 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11005925 if (!test_bit(In_sync, &rdev->flags)
5926 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005927 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10005928 }
NeilBrown63c70c42006-03-27 01:18:13 -08005929
NeilBrownf4168852007-02-28 20:11:53 -08005930 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005931 /* Not enough devices even to make a degraded array
5932 * of that size
5933 */
5934 return -EINVAL;
5935
NeilBrownec32a2b2009-03-31 15:17:38 +11005936 /* Refuse to reduce size of the array. Any reductions in
5937 * array size must be through explicit setting of array_size
5938 * attribute.
5939 */
5940 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5941 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10005942 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11005943 "before number of disks\n", mdname(mddev));
5944 return -EINVAL;
5945 }
5946
NeilBrownf6705572006-03-27 01:18:11 -08005947 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005948 spin_lock_irq(&conf->device_lock);
5949 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005950 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005951 conf->prev_chunk_sectors = conf->chunk_sectors;
5952 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005953 conf->prev_algo = conf->algorithm;
5954 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10005955 conf->generation++;
5956 /* Code that selects data_offset needs to see the generation update
5957 * if reshape_progress has been set - so a memory barrier needed.
5958 */
5959 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10005960 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11005961 conf->reshape_progress = raid5_size(mddev, 0, 0);
5962 else
5963 conf->reshape_progress = 0;
5964 conf->reshape_safe = conf->reshape_progress;
NeilBrown29269552006-03-27 01:18:10 -08005965 spin_unlock_irq(&conf->device_lock);
5966
5967 /* Add some new drives, as many as will fit.
5968 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10005969 * Don't add devices if we are reducing the number of
5970 * devices in the array. This is because it is not possible
5971 * to correctly record the "partially reconstructed" state of
5972 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08005973 */
NeilBrown87a8dec2011-01-31 11:57:43 +11005974 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11005975 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11005976 if (rdev->raid_disk < 0 &&
5977 !test_bit(Faulty, &rdev->flags)) {
5978 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11005979 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11005980 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11005981 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11005982 else
NeilBrown87a8dec2011-01-31 11:57:43 +11005983 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10005984
5985 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11005986 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11005987 }
NeilBrown87a8dec2011-01-31 11:57:43 +11005988 } else if (rdev->raid_disk >= conf->previous_raid_disks
5989 && !test_bit(Faulty, &rdev->flags)) {
5990 /* This is a spare that was manually added */
5991 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11005992 }
NeilBrown29269552006-03-27 01:18:10 -08005993
NeilBrown87a8dec2011-01-31 11:57:43 +11005994 /* When a reshape changes the number of devices,
5995 * ->degraded is measured against the larger of the
5996 * pre and post number of devices.
5997 */
NeilBrownec32a2b2009-03-31 15:17:38 +11005998 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005999 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11006000 spin_unlock_irqrestore(&conf->device_lock, flags);
6001 }
NeilBrown63c70c42006-03-27 01:18:13 -08006002 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10006003 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07006004 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08006005
NeilBrown29269552006-03-27 01:18:10 -08006006 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6007 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6008 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6009 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6010 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10006011 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08006012 if (!mddev->sync_thread) {
6013 mddev->recovery = 0;
6014 spin_lock_irq(&conf->device_lock);
6015 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10006016 rdev_for_each(rdev, mddev)
6017 rdev->new_data_offset = rdev->data_offset;
6018 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11006019 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11006020 mddev->reshape_position = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08006021 spin_unlock_irq(&conf->device_lock);
6022 return -EAGAIN;
6023 }
NeilBrownc8f517c2009-03-31 15:28:40 +11006024 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08006025 md_wakeup_thread(mddev->sync_thread);
6026 md_new_event(mddev);
6027 return 0;
6028}
NeilBrown29269552006-03-27 01:18:10 -08006029
NeilBrownec32a2b2009-03-31 15:17:38 +11006030/* This is called from the reshape thread and should make any
6031 * changes needed in 'conf'
6032 */
NeilBrownd1688a62011-10-11 16:49:52 +11006033static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08006034{
NeilBrown29269552006-03-27 01:18:10 -08006035
NeilBrownf6705572006-03-27 01:18:11 -08006036 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10006037 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07006038
NeilBrownf6705572006-03-27 01:18:11 -08006039 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11006040 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10006041 rdev_for_each(rdev, conf->mddev)
6042 rdev->data_offset = rdev->new_data_offset;
6043 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11006044 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08006045 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11006046 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07006047
6048 /* read-ahead size must cover two whole stripes, which is
6049 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
6050 */
NeilBrown4a5add42010-06-01 19:37:28 +10006051 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11006052 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006053 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11006054 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07006055 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
6056 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
6057 }
NeilBrown29269552006-03-27 01:18:10 -08006058 }
NeilBrown29269552006-03-27 01:18:10 -08006059}
6060
NeilBrownec32a2b2009-03-31 15:17:38 +11006061/* This is called from the raid5d thread with mddev_lock held.
6062 * It makes config changes to the device.
6063 */
NeilBrownfd01b882011-10-11 16:47:53 +11006064static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11006065{
NeilBrownd1688a62011-10-11 16:49:52 +11006066 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11006067
6068 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6069
NeilBrownec32a2b2009-03-31 15:17:38 +11006070 if (mddev->delta_disks > 0) {
6071 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
6072 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006073 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11006074 } else {
6075 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11006076 spin_lock_irq(&conf->device_lock);
6077 mddev->degraded = calc_degraded(conf);
6078 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11006079 for (d = conf->raid_disks ;
6080 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10006081 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11006082 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10006083 if (rdev)
6084 clear_bit(In_sync, &rdev->flags);
6085 rdev = conf->disks[d].replacement;
6086 if (rdev)
6087 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10006088 }
NeilBrowncea9c222009-03-31 15:15:05 +11006089 }
NeilBrown88ce4932009-03-31 15:24:23 +11006090 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006091 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11006092 mddev->reshape_position = MaxSector;
6093 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006094 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11006095 }
6096}
6097
NeilBrownfd01b882011-10-11 16:47:53 +11006098static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07006099{
NeilBrownd1688a62011-10-11 16:49:52 +11006100 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07006101
6102 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08006103 case 2: /* resume for a suspend */
6104 wake_up(&conf->wait_for_overlap);
6105 break;
6106
NeilBrown72626682005-09-09 16:23:54 -07006107 case 1: /* stop all writes */
6108 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006109 /* '2' tells resync/reshape to pause so that all
6110 * active stripes can drain
6111 */
6112 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07006113 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006114 atomic_read(&conf->active_stripes) == 0 &&
6115 atomic_read(&conf->active_aligned_reads) == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01006116 conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006117 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07006118 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006119 /* allow reshape to continue */
6120 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006121 break;
6122
6123 case 0: /* re-enable writes */
6124 spin_lock_irq(&conf->device_lock);
6125 conf->quiesce = 0;
6126 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08006127 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006128 spin_unlock_irq(&conf->device_lock);
6129 break;
6130 }
NeilBrown72626682005-09-09 16:23:54 -07006131}
NeilBrownb15c2e52006-01-06 00:20:16 -08006132
NeilBrownd562b0c2009-03-31 14:39:39 +11006133
NeilBrownfd01b882011-10-11 16:47:53 +11006134static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11006135{
NeilBrowne373ab12011-10-11 16:48:59 +11006136 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07006137 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11006138
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006139 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11006140 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10006141 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
6142 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006143 return ERR_PTR(-EINVAL);
6144 }
6145
NeilBrowne373ab12011-10-11 16:48:59 +11006146 sectors = raid0_conf->strip_zone[0].zone_end;
6147 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10006148 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006149 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11006150 mddev->new_layout = ALGORITHM_PARITY_N;
6151 mddev->new_chunk_sectors = mddev->chunk_sectors;
6152 mddev->raid_disks += 1;
6153 mddev->delta_disks = 1;
6154 /* make sure it will be not marked as dirty */
6155 mddev->recovery_cp = MaxSector;
6156
6157 return setup_conf(mddev);
6158}
6159
6160
NeilBrownfd01b882011-10-11 16:47:53 +11006161static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006162{
6163 int chunksect;
6164
6165 if (mddev->raid_disks != 2 ||
6166 mddev->degraded > 1)
6167 return ERR_PTR(-EINVAL);
6168
6169 /* Should check if there are write-behind devices? */
6170
6171 chunksect = 64*2; /* 64K by default */
6172
6173 /* The array must be an exact multiple of chunksize */
6174 while (chunksect && (mddev->array_sectors & (chunksect-1)))
6175 chunksect >>= 1;
6176
6177 if ((chunksect<<9) < STRIPE_SIZE)
6178 /* array size does not allow a suitable chunk size */
6179 return ERR_PTR(-EINVAL);
6180
6181 mddev->new_level = 5;
6182 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10006183 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11006184
6185 return setup_conf(mddev);
6186}
6187
NeilBrownfd01b882011-10-11 16:47:53 +11006188static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11006189{
6190 int new_layout;
6191
6192 switch (mddev->layout) {
6193 case ALGORITHM_LEFT_ASYMMETRIC_6:
6194 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
6195 break;
6196 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6197 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
6198 break;
6199 case ALGORITHM_LEFT_SYMMETRIC_6:
6200 new_layout = ALGORITHM_LEFT_SYMMETRIC;
6201 break;
6202 case ALGORITHM_RIGHT_SYMMETRIC_6:
6203 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
6204 break;
6205 case ALGORITHM_PARITY_0_6:
6206 new_layout = ALGORITHM_PARITY_0;
6207 break;
6208 case ALGORITHM_PARITY_N:
6209 new_layout = ALGORITHM_PARITY_N;
6210 break;
6211 default:
6212 return ERR_PTR(-EINVAL);
6213 }
6214 mddev->new_level = 5;
6215 mddev->new_layout = new_layout;
6216 mddev->delta_disks = -1;
6217 mddev->raid_disks -= 1;
6218 return setup_conf(mddev);
6219}
6220
NeilBrownd562b0c2009-03-31 14:39:39 +11006221
NeilBrownfd01b882011-10-11 16:47:53 +11006222static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11006223{
NeilBrown88ce4932009-03-31 15:24:23 +11006224 /* For a 2-drive array, the layout and chunk size can be changed
6225 * immediately as not restriping is needed.
6226 * For larger arrays we record the new value - after validation
6227 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11006228 */
NeilBrownd1688a62011-10-11 16:49:52 +11006229 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10006230 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11006231
NeilBrown597a7112009-06-18 08:47:42 +10006232 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11006233 return -EINVAL;
6234 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006235 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11006236 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006237 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11006238 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006239 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11006240 /* not factor of array size */
6241 return -EINVAL;
6242 }
6243
6244 /* They look valid */
6245
NeilBrown88ce4932009-03-31 15:24:23 +11006246 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10006247 /* can make the change immediately */
6248 if (mddev->new_layout >= 0) {
6249 conf->algorithm = mddev->new_layout;
6250 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11006251 }
6252 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10006253 conf->chunk_sectors = new_chunk ;
6254 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11006255 }
6256 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6257 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11006258 }
NeilBrown50ac1682009-06-18 08:47:55 +10006259 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11006260}
6261
NeilBrownfd01b882011-10-11 16:47:53 +11006262static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11006263{
NeilBrown597a7112009-06-18 08:47:42 +10006264 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10006265
NeilBrown597a7112009-06-18 08:47:42 +10006266 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11006267 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006268 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006269 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11006270 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006271 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11006272 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006273 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11006274 /* not factor of array size */
6275 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006276 }
NeilBrown88ce4932009-03-31 15:24:23 +11006277
6278 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10006279 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11006280}
6281
NeilBrownfd01b882011-10-11 16:47:53 +11006282static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006283{
6284 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006285 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006286 * raid1 - if there are two drives. We need to know the chunk size
6287 * raid4 - trivial - just use a raid4 layout.
6288 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006289 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006290 if (mddev->level == 0)
6291 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11006292 if (mddev->level == 1)
6293 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11006294 if (mddev->level == 4) {
6295 mddev->new_layout = ALGORITHM_PARITY_N;
6296 mddev->new_level = 5;
6297 return setup_conf(mddev);
6298 }
NeilBrownfc9739c2009-03-31 14:57:20 +11006299 if (mddev->level == 6)
6300 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11006301
6302 return ERR_PTR(-EINVAL);
6303}
6304
NeilBrownfd01b882011-10-11 16:47:53 +11006305static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11006306{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006307 /* raid4 can take over:
6308 * raid0 - if there is only one strip zone
6309 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11006310 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006311 if (mddev->level == 0)
6312 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11006313 if (mddev->level == 5 &&
6314 mddev->layout == ALGORITHM_PARITY_N) {
6315 mddev->new_layout = 0;
6316 mddev->new_level = 4;
6317 return setup_conf(mddev);
6318 }
6319 return ERR_PTR(-EINVAL);
6320}
NeilBrownd562b0c2009-03-31 14:39:39 +11006321
NeilBrown84fc4b52011-10-11 16:49:58 +11006322static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11006323
NeilBrownfd01b882011-10-11 16:47:53 +11006324static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11006325{
6326 /* Currently can only take over a raid5. We map the
6327 * personality to an equivalent raid6 personality
6328 * with the Q block at the end.
6329 */
6330 int new_layout;
6331
6332 if (mddev->pers != &raid5_personality)
6333 return ERR_PTR(-EINVAL);
6334 if (mddev->degraded > 1)
6335 return ERR_PTR(-EINVAL);
6336 if (mddev->raid_disks > 253)
6337 return ERR_PTR(-EINVAL);
6338 if (mddev->raid_disks < 3)
6339 return ERR_PTR(-EINVAL);
6340
6341 switch (mddev->layout) {
6342 case ALGORITHM_LEFT_ASYMMETRIC:
6343 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
6344 break;
6345 case ALGORITHM_RIGHT_ASYMMETRIC:
6346 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
6347 break;
6348 case ALGORITHM_LEFT_SYMMETRIC:
6349 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
6350 break;
6351 case ALGORITHM_RIGHT_SYMMETRIC:
6352 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
6353 break;
6354 case ALGORITHM_PARITY_0:
6355 new_layout = ALGORITHM_PARITY_0_6;
6356 break;
6357 case ALGORITHM_PARITY_N:
6358 new_layout = ALGORITHM_PARITY_N;
6359 break;
6360 default:
6361 return ERR_PTR(-EINVAL);
6362 }
6363 mddev->new_level = 6;
6364 mddev->new_layout = new_layout;
6365 mddev->delta_disks = 1;
6366 mddev->raid_disks += 1;
6367 return setup_conf(mddev);
6368}
6369
6370
NeilBrown84fc4b52011-10-11 16:49:58 +11006371static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07006372{
6373 .name = "raid6",
6374 .level = 6,
6375 .owner = THIS_MODULE,
6376 .make_request = make_request,
6377 .run = run,
6378 .stop = stop,
6379 .status = status,
6380 .error_handler = error,
6381 .hot_add_disk = raid5_add_disk,
6382 .hot_remove_disk= raid5_remove_disk,
6383 .spare_active = raid5_spare_active,
6384 .sync_request = sync_request,
6385 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006386 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10006387 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08006388 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006389 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07006390 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11006391 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07006392};
NeilBrown84fc4b52011-10-11 16:49:58 +11006393static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394{
6395 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08006396 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006397 .owner = THIS_MODULE,
6398 .make_request = make_request,
6399 .run = run,
6400 .stop = stop,
6401 .status = status,
6402 .error_handler = error,
6403 .hot_add_disk = raid5_add_disk,
6404 .hot_remove_disk= raid5_remove_disk,
6405 .spare_active = raid5_spare_active,
6406 .sync_request = sync_request,
6407 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006408 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08006409 .check_reshape = raid5_check_reshape,
6410 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006411 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07006412 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11006413 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006414};
6415
NeilBrown84fc4b52011-10-11 16:49:58 +11006416static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417{
NeilBrown2604b702006-01-06 00:20:36 -08006418 .name = "raid4",
6419 .level = 4,
6420 .owner = THIS_MODULE,
6421 .make_request = make_request,
6422 .run = run,
6423 .stop = stop,
6424 .status = status,
6425 .error_handler = error,
6426 .hot_add_disk = raid5_add_disk,
6427 .hot_remove_disk= raid5_remove_disk,
6428 .spare_active = raid5_spare_active,
6429 .sync_request = sync_request,
6430 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006431 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08006432 .check_reshape = raid5_check_reshape,
6433 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006434 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08006435 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11006436 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08006437};
6438
6439static int __init raid5_init(void)
6440{
NeilBrown16a53ec2006-06-26 00:27:38 -07006441 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006442 register_md_personality(&raid5_personality);
6443 register_md_personality(&raid4_personality);
6444 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006445}
6446
NeilBrown2604b702006-01-06 00:20:36 -08006447static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006448{
NeilBrown16a53ec2006-06-26 00:27:38 -07006449 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006450 unregister_md_personality(&raid5_personality);
6451 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452}
6453
6454module_init(raid5_init);
6455module_exit(raid5_exit);
6456MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11006457MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006458MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08006459MODULE_ALIAS("md-raid5");
6460MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08006461MODULE_ALIAS("md-level-5");
6462MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07006463MODULE_ALIAS("md-personality-8"); /* RAID6 */
6464MODULE_ALIAS("md-raid6");
6465MODULE_ALIAS("md-level-6");
6466
6467/* This used to be two separate modules, they were: */
6468MODULE_ALIAS("raid5");
6469MODULE_ALIAS("raid6");