blob: 287cc3b30043a61e45af3635a437893dff9e3ccb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * raid5.c : Multiple Devices driver for Linux
3 * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
4 * Copyright (C) 1999, 2000 Ingo Molnar
NeilBrown16a53ec2006-06-26 00:27:38 -07005 * Copyright (C) 2002, 2003 H. Peter Anvin
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
NeilBrown16a53ec2006-06-26 00:27:38 -07007 * RAID-4/5/6 management functions.
8 * Thanks to Penguin Computing for making the RAID-6 development possible
9 * by donating a test server!
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
NeilBrownae3c20c2006-07-10 04:44:17 -070021/*
22 * BITMAP UNPLUGGING:
23 *
24 * The sequencing for updating the bitmap reliably is a little
25 * subtle (and I got it wrong the first time) so it deserves some
26 * explanation.
27 *
28 * We group bitmap updates into batches. Each batch has a number.
29 * We may write out several batches at once, but that isn't very important.
NeilBrown7c13edc2011-04-18 18:25:43 +100030 * conf->seq_write is the number of the last batch successfully written.
31 * conf->seq_flush is the number of the last batch that was closed to
NeilBrownae3c20c2006-07-10 04:44:17 -070032 * new additions.
33 * When we discover that we will need to write to any block in a stripe
34 * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq
NeilBrown7c13edc2011-04-18 18:25:43 +100035 * the number of the batch it will be in. This is seq_flush+1.
NeilBrownae3c20c2006-07-10 04:44:17 -070036 * When we are ready to do a write, if that batch hasn't been written yet,
37 * we plug the array and queue the stripe for later.
38 * When an unplug happens, we increment bm_flush, thus closing the current
39 * batch.
40 * When we notice that bm_flush > bm_write, we write out all pending updates
41 * to the bitmap, and advance bm_write to where bm_flush was.
42 * This may occasionally write a bit out twice, but is sure never to
43 * miss any bits.
44 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
NeilBrownbff61972009-03-31 14:33:13 +110046#include <linux/blkdev.h>
NeilBrownf6705572006-03-27 01:18:11 -080047#include <linux/kthread.h>
Dan Williamsf701d582009-03-31 15:09:39 +110048#include <linux/raid/pq.h>
Dan Williams91c00922007-01-02 13:52:30 -070049#include <linux/async_tx.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040050#include <linux/module.h>
Dan Williams07a3b412009-08-29 19:13:13 -070051#include <linux/async.h>
NeilBrownbff61972009-03-31 14:33:13 +110052#include <linux/seq_file.h>
Dan Williams36d1c642009-07-14 11:48:22 -070053#include <linux/cpu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090054#include <linux/slab.h>
Christian Dietrich8bda4702011-07-27 11:00:36 +100055#include <linux/ratelimit.h>
NeilBrowna9add5d2012-10-31 11:59:09 +110056#include <trace/events/block.h>
57
NeilBrown43b2e5d2009-03-31 14:33:13 +110058#include "md.h"
NeilBrownbff61972009-03-31 14:33:13 +110059#include "raid5.h"
Trela Maciej54071b32010-03-08 16:02:42 +110060#include "raid0.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110061#include "bitmap.h"
NeilBrown72626682005-09-09 16:23:54 -070062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/*
64 * Stripe cache
65 */
66
67#define NR_STRIPES 256
68#define STRIPE_SIZE PAGE_SIZE
69#define STRIPE_SHIFT (PAGE_SHIFT - 9)
70#define STRIPE_SECTORS (STRIPE_SIZE>>9)
71#define IO_THRESHOLD 1
Dan Williams8b3e6cd2008-04-28 02:15:53 -070072#define BYPASS_THRESHOLD 1
NeilBrownfccddba2006-01-06 00:20:33 -080073#define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define HASH_MASK (NR_HASH - 1)
75
NeilBrownd1688a62011-10-11 16:49:52 +110076static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
NeilBrowndb298e12011-10-07 14:23:00 +110077{
78 int hash = (sect >> STRIPE_SHIFT) & HASH_MASK;
79 return &conf->stripe_hashtbl[hash];
80}
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82/* bio's attached to a stripe+device for I/O are linked together in bi_sector
83 * order without overlap. There may be several bio's per stripe+device, and
84 * a bio could span several devices.
85 * When walking this list for a particular stripe+device, we must never proceed
86 * beyond a bio that extends past this device, as the next bio might no longer
87 * be valid.
NeilBrowndb298e12011-10-07 14:23:00 +110088 * This function is used to determine the 'next' bio in the list, given the sector
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 * of the current stripe+device
90 */
NeilBrowndb298e12011-10-07 14:23:00 +110091static inline struct bio *r5_next_bio(struct bio *bio, sector_t sector)
92{
Kent Overstreetaa8b57a2013-02-05 15:19:29 -080093 int sectors = bio_sectors(bio);
NeilBrowndb298e12011-10-07 14:23:00 +110094 if (bio->bi_sector + sectors < sector + STRIPE_SECTORS)
95 return bio->bi_next;
96 else
97 return NULL;
98}
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Jens Axboe960e7392008-08-15 10:41:18 +0200100/*
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200101 * We maintain a biased count of active stripes in the bottom 16 bits of
102 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
Jens Axboe960e7392008-08-15 10:41:18 +0200103 */
Shaohua Lie7836bd62012-07-19 16:01:31 +1000104static inline int raid5_bi_processed_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200105{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000106 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
107 return (atomic_read(segments) >> 16) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200108}
109
Shaohua Lie7836bd62012-07-19 16:01:31 +1000110static inline int raid5_dec_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200111{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000112 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
113 return atomic_sub_return(1, segments) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200114}
115
Shaohua Lie7836bd62012-07-19 16:01:31 +1000116static inline void raid5_inc_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200117{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000118 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
119 atomic_inc(segments);
Jens Axboe960e7392008-08-15 10:41:18 +0200120}
121
Shaohua Lie7836bd62012-07-19 16:01:31 +1000122static inline void raid5_set_bi_processed_stripes(struct bio *bio,
123 unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200124{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000125 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
126 int old, new;
Jens Axboe960e7392008-08-15 10:41:18 +0200127
Shaohua Lie7836bd62012-07-19 16:01:31 +1000128 do {
129 old = atomic_read(segments);
130 new = (old & 0xffff) | (cnt << 16);
131 } while (atomic_cmpxchg(segments, old, new) != old);
Jens Axboe960e7392008-08-15 10:41:18 +0200132}
133
Shaohua Lie7836bd62012-07-19 16:01:31 +1000134static inline void raid5_set_bi_stripes(struct bio *bio, unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200135{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000136 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
137 atomic_set(segments, cnt);
Jens Axboe960e7392008-08-15 10:41:18 +0200138}
139
NeilBrownd0dabf72009-03-31 14:39:38 +1100140/* Find first data disk in a raid6 stripe */
141static inline int raid6_d0(struct stripe_head *sh)
142{
NeilBrown67cc2b82009-03-31 14:39:38 +1100143 if (sh->ddf_layout)
144 /* ddf always start from first device */
145 return 0;
146 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100147 if (sh->qd_idx == sh->disks - 1)
148 return 0;
149 else
150 return sh->qd_idx + 1;
151}
NeilBrown16a53ec2006-06-26 00:27:38 -0700152static inline int raid6_next_disk(int disk, int raid_disks)
153{
154 disk++;
155 return (disk < raid_disks) ? disk : 0;
156}
Dan Williamsa4456852007-07-09 11:56:43 -0700157
NeilBrownd0dabf72009-03-31 14:39:38 +1100158/* When walking through the disks in a raid5, starting at raid6_d0,
159 * We need to map each disk to a 'slot', where the data disks are slot
160 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
161 * is raid_disks-1. This help does that mapping.
162 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100163static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
164 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100165{
Dan Williams66295422009-10-19 18:09:32 -0700166 int slot = *count;
NeilBrown67cc2b82009-03-31 14:39:38 +1100167
NeilBrowne4424fe2009-10-16 16:27:34 +1100168 if (sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700169 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100170 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100171 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100172 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100173 return syndrome_disks + 1;
NeilBrowne4424fe2009-10-16 16:27:34 +1100174 if (!sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700175 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100176 return slot;
177}
178
Dan Williamsa4456852007-07-09 11:56:43 -0700179static void return_io(struct bio *return_bi)
180{
181 struct bio *bi = return_bi;
182 while (bi) {
Dan Williamsa4456852007-07-09 11:56:43 -0700183
184 return_bi = bi->bi_next;
185 bi->bi_next = NULL;
186 bi->bi_size = 0;
Linus Torvalds0a82a8d2013-04-18 09:00:26 -0700187 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
188 bi, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +1000189 bio_endio(bi, 0);
Dan Williamsa4456852007-07-09 11:56:43 -0700190 bi = return_bi;
191 }
192}
193
NeilBrownd1688a62011-10-11 16:49:52 +1100194static void print_raid5_conf (struct r5conf *conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Dan Williams600aa102008-06-28 08:32:05 +1000196static int stripe_operations_active(struct stripe_head *sh)
197{
198 return sh->check_state || sh->reconstruct_state ||
199 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
200 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
201}
202
Shaohua Li4eb788d2012-07-19 16:01:31 +1000203static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Shaohua Li4eb788d2012-07-19 16:01:31 +1000205 BUG_ON(!list_empty(&sh->lru));
206 BUG_ON(atomic_read(&conf->active_stripes)==0);
207 if (test_bit(STRIPE_HANDLE, &sh->state)) {
208 if (test_bit(STRIPE_DELAYED, &sh->state) &&
209 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
210 list_add_tail(&sh->lru, &conf->delayed_list);
211 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
212 sh->bm_seq - conf->seq_write > 0)
213 list_add_tail(&sh->lru, &conf->bitmap_list);
214 else {
215 clear_bit(STRIPE_DELAYED, &sh->state);
216 clear_bit(STRIPE_BIT_DELAY, &sh->state);
217 list_add_tail(&sh->lru, &conf->handle_list);
218 }
219 md_wakeup_thread(conf->mddev->thread);
220 } else {
221 BUG_ON(stripe_operations_active(sh));
222 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
223 if (atomic_dec_return(&conf->preread_active_stripes)
224 < IO_THRESHOLD)
225 md_wakeup_thread(conf->mddev->thread);
226 atomic_dec(&conf->active_stripes);
227 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
228 list_add_tail(&sh->lru, &conf->inactive_list);
229 wake_up(&conf->wait_for_stripe);
230 if (conf->retry_read_aligned)
231 md_wakeup_thread(conf->mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233 }
234}
NeilBrownd0dabf72009-03-31 14:39:38 +1100235
Shaohua Li4eb788d2012-07-19 16:01:31 +1000236static void __release_stripe(struct r5conf *conf, struct stripe_head *sh)
237{
238 if (atomic_dec_and_test(&sh->count))
239 do_release_stripe(conf, sh);
240}
241
Shaohua Li773ca822013-08-27 17:50:39 +0800242/* should hold conf->device_lock already */
243static int release_stripe_list(struct r5conf *conf)
244{
245 struct stripe_head *sh;
246 int count = 0;
247 struct llist_node *head;
248
249 head = llist_del_all(&conf->released_stripes);
250 while (head) {
251 sh = llist_entry(head, struct stripe_head, release_list);
252 head = llist_next(head);
253 /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */
254 smp_mb();
255 clear_bit(STRIPE_ON_RELEASE_LIST, &sh->state);
256 /*
257 * Don't worry the bit is set here, because if the bit is set
258 * again, the count is always > 1. This is true for
259 * STRIPE_ON_UNPLUG_LIST bit too.
260 */
261 __release_stripe(conf, sh);
262 count++;
263 }
264
265 return count;
266}
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268static void release_stripe(struct stripe_head *sh)
269{
NeilBrownd1688a62011-10-11 16:49:52 +1100270 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 unsigned long flags;
Shaohua Li773ca822013-08-27 17:50:39 +0800272 bool wakeup;
NeilBrown16a53ec2006-06-26 00:27:38 -0700273
Shaohua Li773ca822013-08-27 17:50:39 +0800274 if (test_and_set_bit(STRIPE_ON_RELEASE_LIST, &sh->state))
275 goto slow_path;
276 wakeup = llist_add(&sh->release_list, &conf->released_stripes);
277 if (wakeup)
278 md_wakeup_thread(conf->mddev->thread);
279 return;
280slow_path:
Shaohua Li4eb788d2012-07-19 16:01:31 +1000281 local_irq_save(flags);
Shaohua Li773ca822013-08-27 17:50:39 +0800282 /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */
Shaohua Li4eb788d2012-07-19 16:01:31 +1000283 if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) {
284 do_release_stripe(conf, sh);
285 spin_unlock(&conf->device_lock);
286 }
287 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
NeilBrownfccddba2006-01-06 00:20:33 -0800290static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Dan Williams45b42332007-07-09 11:56:43 -0700292 pr_debug("remove_hash(), stripe %llu\n",
293 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
NeilBrownfccddba2006-01-06 00:20:33 -0800295 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
NeilBrownd1688a62011-10-11 16:49:52 +1100298static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
NeilBrownfccddba2006-01-06 00:20:33 -0800300 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Dan Williams45b42332007-07-09 11:56:43 -0700302 pr_debug("insert_hash(), stripe %llu\n",
303 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
NeilBrownfccddba2006-01-06 00:20:33 -0800305 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
308
309/* find an idle stripe, make sure it is unhashed, and return it. */
NeilBrownd1688a62011-10-11 16:49:52 +1100310static struct stripe_head *get_free_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 struct stripe_head *sh = NULL;
313 struct list_head *first;
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if (list_empty(&conf->inactive_list))
316 goto out;
317 first = conf->inactive_list.next;
318 sh = list_entry(first, struct stripe_head, lru);
319 list_del_init(first);
320 remove_hash(sh);
321 atomic_inc(&conf->active_stripes);
322out:
323 return sh;
324}
325
NeilBrowne4e11e32010-06-16 16:45:16 +1000326static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
328 struct page *p;
329 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000330 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
NeilBrowne4e11e32010-06-16 16:45:16 +1000332 for (i = 0; i < num ; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 p = sh->dev[i].page;
334 if (!p)
335 continue;
336 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800337 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339}
340
NeilBrowne4e11e32010-06-16 16:45:16 +1000341static int grow_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
343 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000344 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
NeilBrowne4e11e32010-06-16 16:45:16 +1000346 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 struct page *page;
348
349 if (!(page = alloc_page(GFP_KERNEL))) {
350 return 1;
351 }
352 sh->dev[i].page = page;
353 }
354 return 0;
355}
356
NeilBrown784052e2009-03-31 15:19:07 +1100357static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrownd1688a62011-10-11 16:49:52 +1100358static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100359 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
NeilBrownb5663ba2009-03-31 14:39:38 +1100361static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
NeilBrownd1688a62011-10-11 16:49:52 +1100363 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800364 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200366 BUG_ON(atomic_read(&sh->count) != 0);
367 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000368 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700369
Dan Williams45b42332007-07-09 11:56:43 -0700370 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 (unsigned long long)sh->sector);
372
373 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700374
NeilBrown86b42c72009-03-31 15:19:03 +1100375 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100376 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100378 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 sh->state = 0;
380
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800381
382 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 struct r5dev *dev = &sh->dev[i];
384
Dan Williamsd84e0f12007-01-02 13:52:30 -0700385 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700387 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700389 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000391 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100394 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396 insert_hash(conf, sh);
397}
398
NeilBrownd1688a62011-10-11 16:49:52 +1100399static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100400 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
402 struct stripe_head *sh;
403
Dan Williams45b42332007-07-09 11:56:43 -0700404 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800405 hlist_for_each_entry(sh, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100406 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700408 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return NULL;
410}
411
NeilBrown674806d2010-06-16 17:17:53 +1000412/*
413 * Need to check if array has failed when deciding whether to:
414 * - start an array
415 * - remove non-faulty devices
416 * - add a spare
417 * - allow a reshape
418 * This determination is simple when no reshape is happening.
419 * However if there is a reshape, we need to carefully check
420 * both the before and after sections.
421 * This is because some failed devices may only affect one
422 * of the two sections, and some non-in_sync devices may
423 * be insync in the section most affected by failed devices.
424 */
NeilBrown908f4fb2011-12-23 10:17:50 +1100425static int calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000426{
NeilBrown908f4fb2011-12-23 10:17:50 +1100427 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000428 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000429
430 rcu_read_lock();
431 degraded = 0;
432 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100433 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000434 if (rdev && test_bit(Faulty, &rdev->flags))
435 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000436 if (!rdev || test_bit(Faulty, &rdev->flags))
437 degraded++;
438 else if (test_bit(In_sync, &rdev->flags))
439 ;
440 else
441 /* not in-sync or faulty.
442 * If the reshape increases the number of devices,
443 * this is being recovered by the reshape, so
444 * this 'previous' section is not in_sync.
445 * If the number of devices is being reduced however,
446 * the device can only be part of the array if
447 * we are reverting a reshape, so this section will
448 * be in-sync.
449 */
450 if (conf->raid_disks >= conf->previous_raid_disks)
451 degraded++;
452 }
453 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100454 if (conf->raid_disks == conf->previous_raid_disks)
455 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000456 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100457 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000458 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100459 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000460 if (rdev && test_bit(Faulty, &rdev->flags))
461 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000462 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100463 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000464 else if (test_bit(In_sync, &rdev->flags))
465 ;
466 else
467 /* not in-sync or faulty.
468 * If reshape increases the number of devices, this
469 * section has already been recovered, else it
470 * almost certainly hasn't.
471 */
472 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100473 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000474 }
475 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100476 if (degraded2 > degraded)
477 return degraded2;
478 return degraded;
479}
480
481static int has_failed(struct r5conf *conf)
482{
483 int degraded;
484
485 if (conf->mddev->reshape_position == MaxSector)
486 return conf->mddev->degraded > conf->max_degraded;
487
488 degraded = calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000489 if (degraded > conf->max_degraded)
490 return 1;
491 return 0;
492}
493
NeilBrownb5663ba2009-03-31 14:39:38 +1100494static struct stripe_head *
NeilBrownd1688a62011-10-11 16:49:52 +1100495get_active_stripe(struct r5conf *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000496 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 struct stripe_head *sh;
499
Dan Williams45b42332007-07-09 11:56:43 -0700500 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 spin_lock_irq(&conf->device_lock);
503
504 do {
NeilBrown72626682005-09-09 16:23:54 -0700505 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000506 conf->quiesce == 0 || noquiesce,
Lukas Czernereed8c022012-11-30 11:42:40 +0100507 conf->device_lock);
NeilBrown86b42c72009-03-31 15:19:03 +1100508 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (!sh) {
510 if (!conf->inactive_blocked)
511 sh = get_free_stripe(conf);
512 if (noblock && sh == NULL)
513 break;
514 if (!sh) {
515 conf->inactive_blocked = 1;
516 wait_event_lock_irq(conf->wait_for_stripe,
517 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800518 (atomic_read(&conf->active_stripes)
519 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 || !conf->inactive_blocked),
Lukas Czernereed8c022012-11-30 11:42:40 +0100521 conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 conf->inactive_blocked = 0;
523 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100524 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 } else {
526 if (atomic_read(&sh->count)) {
NeilBrownab69ae12009-03-31 15:26:47 +1100527 BUG_ON(!list_empty(&sh->lru)
Shaohua Li8811b592012-08-02 08:33:00 +1000528 && !test_bit(STRIPE_EXPANDING, &sh->state)
Shaohua Li773ca822013-08-27 17:50:39 +0800529 && !test_bit(STRIPE_ON_UNPLUG_LIST, &sh->state)
530 && !test_bit(STRIPE_ON_RELEASE_LIST, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 } else {
532 if (!test_bit(STRIPE_HANDLE, &sh->state))
533 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700534 if (list_empty(&sh->lru) &&
535 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700536 BUG();
537 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
539 }
540 } while (sh == NULL);
541
542 if (sh)
543 atomic_inc(&sh->count);
544
545 spin_unlock_irq(&conf->device_lock);
546 return sh;
547}
548
NeilBrown05616be2012-05-21 09:27:00 +1000549/* Determine if 'data_offset' or 'new_data_offset' should be used
550 * in this stripe_head.
551 */
552static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
553{
554 sector_t progress = conf->reshape_progress;
555 /* Need a memory barrier to make sure we see the value
556 * of conf->generation, or ->data_offset that was set before
557 * reshape_progress was updated.
558 */
559 smp_rmb();
560 if (progress == MaxSector)
561 return 0;
562 if (sh->generation == conf->generation - 1)
563 return 0;
564 /* We are in a reshape, and this is a new-generation stripe,
565 * so use new_data_offset.
566 */
567 return 1;
568}
569
NeilBrown6712ecf2007-09-27 12:47:43 +0200570static void
571raid5_end_read_request(struct bio *bi, int error);
572static void
573raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700574
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000575static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700576{
NeilBrownd1688a62011-10-11 16:49:52 +1100577 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700578 int i, disks = sh->disks;
579
580 might_sleep();
581
582 for (i = disks; i--; ) {
583 int rw;
NeilBrown9a3e1102011-12-23 10:17:53 +1100584 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100585 struct bio *bi, *rbi;
586 struct md_rdev *rdev, *rrdev = NULL;
Tejun Heoe9c74692010-09-03 11:56:18 +0200587 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
588 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
589 rw = WRITE_FUA;
590 else
591 rw = WRITE;
Shaohua Li9e4447682012-10-11 13:49:49 +1100592 if (test_bit(R5_Discard, &sh->dev[i].flags))
Shaohua Li620125f2012-10-11 13:49:05 +1100593 rw |= REQ_DISCARD;
Tejun Heoe9c74692010-09-03 11:56:18 +0200594 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700595 rw = READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100596 else if (test_and_clear_bit(R5_WantReplace,
597 &sh->dev[i].flags)) {
598 rw = WRITE;
599 replace_only = 1;
600 } else
Dan Williams91c00922007-01-02 13:52:30 -0700601 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +1000602 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
603 rw |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -0700604
605 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100606 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700607
Dan Williams91c00922007-01-02 13:52:30 -0700608 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100609 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100610 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
611 rdev = rcu_dereference(conf->disks[i].rdev);
612 if (!rdev) {
613 rdev = rrdev;
614 rrdev = NULL;
615 }
NeilBrown9a3e1102011-12-23 10:17:53 +1100616 if (rw & WRITE) {
617 if (replace_only)
618 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100619 if (rdev == rrdev)
620 /* We raced and saw duplicates */
621 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100622 } else {
NeilBrowndd054fc2011-12-23 10:17:53 +1100623 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100624 rdev = rrdev;
625 rrdev = NULL;
626 }
NeilBrown977df362011-12-23 10:17:53 +1100627
Dan Williams91c00922007-01-02 13:52:30 -0700628 if (rdev && test_bit(Faulty, &rdev->flags))
629 rdev = NULL;
630 if (rdev)
631 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100632 if (rrdev && test_bit(Faulty, &rrdev->flags))
633 rrdev = NULL;
634 if (rrdev)
635 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700636 rcu_read_unlock();
637
NeilBrown73e92e52011-07-28 11:39:22 +1000638 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100639 * need to check for writes. We never accept write errors
640 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000641 */
642 while ((rw & WRITE) && rdev &&
643 test_bit(WriteErrorSeen, &rdev->flags)) {
644 sector_t first_bad;
645 int bad_sectors;
646 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
647 &first_bad, &bad_sectors);
648 if (!bad)
649 break;
650
651 if (bad < 0) {
652 set_bit(BlockedBadBlocks, &rdev->flags);
653 if (!conf->mddev->external &&
654 conf->mddev->flags) {
655 /* It is very unlikely, but we might
656 * still need to write out the
657 * bad block log - better give it
658 * a chance*/
659 md_check_recovery(conf->mddev);
660 }
majianpeng18507532012-07-03 12:11:54 +1000661 /*
662 * Because md_wait_for_blocked_rdev
663 * will dec nr_pending, we must
664 * increment it first.
665 */
666 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +1000667 md_wait_for_blocked_rdev(rdev, conf->mddev);
668 } else {
669 /* Acknowledged bad block - skip the write */
670 rdev_dec_pending(rdev, conf->mddev);
671 rdev = NULL;
672 }
673 }
674
Dan Williams91c00922007-01-02 13:52:30 -0700675 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100676 if (s->syncing || s->expanding || s->expanded
677 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700678 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
679
Dan Williams2b7497f2008-06-28 08:31:52 +1000680 set_bit(STRIPE_IO_STARTED, &sh->state);
681
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700682 bio_reset(bi);
Dan Williams91c00922007-01-02 13:52:30 -0700683 bi->bi_bdev = rdev->bdev;
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700684 bi->bi_rw = rw;
685 bi->bi_end_io = (rw & WRITE)
686 ? raid5_end_write_request
687 : raid5_end_read_request;
688 bi->bi_private = sh;
689
Dan Williams91c00922007-01-02 13:52:30 -0700690 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700691 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700692 bi->bi_rw, i);
693 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000694 if (use_new_offset(conf, sh))
695 bi->bi_sector = (sh->sector
696 + rdev->new_data_offset);
697 else
698 bi->bi_sector = (sh->sector
699 + rdev->data_offset);
majianpeng3f9e7c12012-07-31 10:04:21 +1000700 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
701 bi->bi_rw |= REQ_FLUSH;
702
Kent Overstreet4997b722013-05-30 08:44:39 +0200703 bi->bi_vcnt = 1;
Dan Williams91c00922007-01-02 13:52:30 -0700704 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
705 bi->bi_io_vec[0].bv_offset = 0;
706 bi->bi_size = STRIPE_SIZE;
NeilBrown977df362011-12-23 10:17:53 +1100707 if (rrdev)
708 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Jonathan Brassowe3620a32013-03-07 16:22:01 -0600709
710 if (conf->mddev->gendisk)
711 trace_block_bio_remap(bdev_get_queue(bi->bi_bdev),
712 bi, disk_devt(conf->mddev->gendisk),
713 sh->dev[i].sector);
Dan Williams91c00922007-01-02 13:52:30 -0700714 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100715 }
716 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100717 if (s->syncing || s->expanding || s->expanded
718 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +1100719 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
720
721 set_bit(STRIPE_IO_STARTED, &sh->state);
722
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700723 bio_reset(rbi);
NeilBrown977df362011-12-23 10:17:53 +1100724 rbi->bi_bdev = rrdev->bdev;
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700725 rbi->bi_rw = rw;
726 BUG_ON(!(rw & WRITE));
727 rbi->bi_end_io = raid5_end_write_request;
728 rbi->bi_private = sh;
729
NeilBrown977df362011-12-23 10:17:53 +1100730 pr_debug("%s: for %llu schedule op %ld on "
731 "replacement disc %d\n",
732 __func__, (unsigned long long)sh->sector,
733 rbi->bi_rw, i);
734 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000735 if (use_new_offset(conf, sh))
736 rbi->bi_sector = (sh->sector
737 + rrdev->new_data_offset);
738 else
739 rbi->bi_sector = (sh->sector
740 + rrdev->data_offset);
Kent Overstreet4997b722013-05-30 08:44:39 +0200741 rbi->bi_vcnt = 1;
NeilBrown977df362011-12-23 10:17:53 +1100742 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
743 rbi->bi_io_vec[0].bv_offset = 0;
744 rbi->bi_size = STRIPE_SIZE;
Jonathan Brassowe3620a32013-03-07 16:22:01 -0600745 if (conf->mddev->gendisk)
746 trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev),
747 rbi, disk_devt(conf->mddev->gendisk),
748 sh->dev[i].sector);
NeilBrown977df362011-12-23 10:17:53 +1100749 generic_make_request(rbi);
750 }
751 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000752 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700753 set_bit(STRIPE_DEGRADED, &sh->state);
754 pr_debug("skip op %ld on disc %d for sector %llu\n",
755 bi->bi_rw, i, (unsigned long long)sh->sector);
756 clear_bit(R5_LOCKED, &sh->dev[i].flags);
757 set_bit(STRIPE_HANDLE, &sh->state);
758 }
759 }
760}
761
762static struct dma_async_tx_descriptor *
763async_copy_data(int frombio, struct bio *bio, struct page *page,
764 sector_t sector, struct dma_async_tx_descriptor *tx)
765{
766 struct bio_vec *bvl;
767 struct page *bio_page;
768 int i;
769 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700770 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700771 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700772
773 if (bio->bi_sector >= sector)
774 page_offset = (signed)(bio->bi_sector - sector) * 512;
775 else
776 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700777
Dan Williams0403e382009-09-08 17:42:50 -0700778 if (frombio)
779 flags |= ASYNC_TX_FENCE;
780 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
781
Dan Williams91c00922007-01-02 13:52:30 -0700782 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000783 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700784 int clen;
785 int b_offset = 0;
786
787 if (page_offset < 0) {
788 b_offset = -page_offset;
789 page_offset += b_offset;
790 len -= b_offset;
791 }
792
793 if (len > 0 && page_offset + len > STRIPE_SIZE)
794 clen = STRIPE_SIZE - page_offset;
795 else
796 clen = len;
797
798 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000799 b_offset += bvl->bv_offset;
800 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700801 if (frombio)
802 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700803 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700804 else
805 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700806 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700807 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700808 /* chain the operations */
809 submit.depend_tx = tx;
810
Dan Williams91c00922007-01-02 13:52:30 -0700811 if (clen < len) /* hit end of page */
812 break;
813 page_offset += len;
814 }
815
816 return tx;
817}
818
819static void ops_complete_biofill(void *stripe_head_ref)
820{
821 struct stripe_head *sh = stripe_head_ref;
822 struct bio *return_bi = NULL;
Dan Williamse4d84902007-09-24 10:06:13 -0700823 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700824
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700825 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700826 (unsigned long long)sh->sector);
827
828 /* clear completed biofills */
829 for (i = sh->disks; i--; ) {
830 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700831
832 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700833 /* and check if we need to reply to a read request,
834 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000835 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700836 */
837 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700838 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700839
Dan Williams91c00922007-01-02 13:52:30 -0700840 BUG_ON(!dev->read);
841 rbi = dev->read;
842 dev->read = NULL;
843 while (rbi && rbi->bi_sector <
844 dev->sector + STRIPE_SECTORS) {
845 rbi2 = r5_next_bio(rbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +1000846 if (!raid5_dec_bi_active_stripes(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700847 rbi->bi_next = return_bi;
848 return_bi = rbi;
849 }
Dan Williams91c00922007-01-02 13:52:30 -0700850 rbi = rbi2;
851 }
852 }
853 }
Dan Williams83de75c2008-06-28 08:31:58 +1000854 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700855
856 return_io(return_bi);
857
Dan Williamse4d84902007-09-24 10:06:13 -0700858 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700859 release_stripe(sh);
860}
861
862static void ops_run_biofill(struct stripe_head *sh)
863{
864 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa08abd82009-06-03 11:43:59 -0700865 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700866 int i;
867
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700868 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700869 (unsigned long long)sh->sector);
870
871 for (i = sh->disks; i--; ) {
872 struct r5dev *dev = &sh->dev[i];
873 if (test_bit(R5_Wantfill, &dev->flags)) {
874 struct bio *rbi;
Shaohua Lib17459c2012-07-19 16:01:31 +1000875 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700876 dev->read = rbi = dev->toread;
877 dev->toread = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +1000878 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700879 while (rbi && rbi->bi_sector <
880 dev->sector + STRIPE_SECTORS) {
881 tx = async_copy_data(0, rbi, dev->page,
882 dev->sector, tx);
883 rbi = r5_next_bio(rbi, dev->sector);
884 }
885 }
886 }
887
888 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700889 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
890 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700891}
892
Dan Williams4e7d2c02009-08-29 19:13:11 -0700893static void mark_target_uptodate(struct stripe_head *sh, int target)
894{
895 struct r5dev *tgt;
896
897 if (target < 0)
898 return;
899
900 tgt = &sh->dev[target];
901 set_bit(R5_UPTODATE, &tgt->flags);
902 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
903 clear_bit(R5_Wantcompute, &tgt->flags);
904}
905
Dan Williamsac6b53b2009-07-14 13:40:19 -0700906static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700907{
908 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700909
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700910 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700911 (unsigned long long)sh->sector);
912
Dan Williamsac6b53b2009-07-14 13:40:19 -0700913 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700914 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700915 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700916
Dan Williamsecc65c92008-06-28 08:31:57 +1000917 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
918 if (sh->check_state == check_state_compute_run)
919 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700920 set_bit(STRIPE_HANDLE, &sh->state);
921 release_stripe(sh);
922}
923
Dan Williamsd6f38f32009-07-14 11:50:52 -0700924/* return a pointer to the address conversion region of the scribble buffer */
925static addr_conv_t *to_addr_conv(struct stripe_head *sh,
926 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700927{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700928 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
929}
930
931static struct dma_async_tx_descriptor *
932ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
933{
Dan Williams91c00922007-01-02 13:52:30 -0700934 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700935 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700936 int target = sh->ops.target;
937 struct r5dev *tgt = &sh->dev[target];
938 struct page *xor_dest = tgt->page;
939 int count = 0;
940 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700941 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700942 int i;
943
944 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700945 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700946 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
947
948 for (i = disks; i--; )
949 if (i != target)
950 xor_srcs[count++] = sh->dev[i].page;
951
952 atomic_inc(&sh->count);
953
Dan Williams0403e382009-09-08 17:42:50 -0700954 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700955 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700956 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700957 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700958 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700959 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700960
Dan Williams91c00922007-01-02 13:52:30 -0700961 return tx;
962}
963
Dan Williamsac6b53b2009-07-14 13:40:19 -0700964/* set_syndrome_sources - populate source buffers for gen_syndrome
965 * @srcs - (struct page *) array of size sh->disks
966 * @sh - stripe_head to parse
967 *
968 * Populates srcs in proper layout order for the stripe and returns the
969 * 'count' of sources to be used in a call to async_gen_syndrome. The P
970 * destination buffer is recorded in srcs[count] and the Q destination
971 * is recorded in srcs[count+1]].
972 */
973static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
974{
975 int disks = sh->disks;
976 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
977 int d0_idx = raid6_d0(sh);
978 int count;
979 int i;
980
981 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100982 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700983
984 count = 0;
985 i = d0_idx;
986 do {
987 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
988
989 srcs[slot] = sh->dev[i].page;
990 i = raid6_next_disk(i, disks);
991 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700992
NeilBrowne4424fe2009-10-16 16:27:34 +1100993 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700994}
995
996static struct dma_async_tx_descriptor *
997ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
998{
999 int disks = sh->disks;
1000 struct page **blocks = percpu->scribble;
1001 int target;
1002 int qd_idx = sh->qd_idx;
1003 struct dma_async_tx_descriptor *tx;
1004 struct async_submit_ctl submit;
1005 struct r5dev *tgt;
1006 struct page *dest;
1007 int i;
1008 int count;
1009
1010 if (sh->ops.target < 0)
1011 target = sh->ops.target2;
1012 else if (sh->ops.target2 < 0)
1013 target = sh->ops.target;
1014 else
1015 /* we should only have one valid target */
1016 BUG();
1017 BUG_ON(target < 0);
1018 pr_debug("%s: stripe %llu block: %d\n",
1019 __func__, (unsigned long long)sh->sector, target);
1020
1021 tgt = &sh->dev[target];
1022 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1023 dest = tgt->page;
1024
1025 atomic_inc(&sh->count);
1026
1027 if (target == qd_idx) {
1028 count = set_syndrome_sources(blocks, sh);
1029 blocks[count] = NULL; /* regenerating p is not necessary */
1030 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -07001031 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1032 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001033 to_addr_conv(sh, percpu));
1034 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1035 } else {
1036 /* Compute any data- or p-drive using XOR */
1037 count = 0;
1038 for (i = disks; i-- ; ) {
1039 if (i == target || i == qd_idx)
1040 continue;
1041 blocks[count++] = sh->dev[i].page;
1042 }
1043
Dan Williams0403e382009-09-08 17:42:50 -07001044 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1045 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001046 to_addr_conv(sh, percpu));
1047 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
1048 }
1049
1050 return tx;
1051}
1052
1053static struct dma_async_tx_descriptor *
1054ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1055{
1056 int i, count, disks = sh->disks;
1057 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1058 int d0_idx = raid6_d0(sh);
1059 int faila = -1, failb = -1;
1060 int target = sh->ops.target;
1061 int target2 = sh->ops.target2;
1062 struct r5dev *tgt = &sh->dev[target];
1063 struct r5dev *tgt2 = &sh->dev[target2];
1064 struct dma_async_tx_descriptor *tx;
1065 struct page **blocks = percpu->scribble;
1066 struct async_submit_ctl submit;
1067
1068 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1069 __func__, (unsigned long long)sh->sector, target, target2);
1070 BUG_ON(target < 0 || target2 < 0);
1071 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1072 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1073
Dan Williams6c910a72009-09-16 12:24:54 -07001074 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001075 * slot number conversion for 'faila' and 'failb'
1076 */
1077 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001078 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001079 count = 0;
1080 i = d0_idx;
1081 do {
1082 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1083
1084 blocks[slot] = sh->dev[i].page;
1085
1086 if (i == target)
1087 faila = slot;
1088 if (i == target2)
1089 failb = slot;
1090 i = raid6_next_disk(i, disks);
1091 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001092
1093 BUG_ON(faila == failb);
1094 if (failb < faila)
1095 swap(faila, failb);
1096 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1097 __func__, (unsigned long long)sh->sector, faila, failb);
1098
1099 atomic_inc(&sh->count);
1100
1101 if (failb == syndrome_disks+1) {
1102 /* Q disk is one of the missing disks */
1103 if (faila == syndrome_disks) {
1104 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001105 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1106 ops_complete_compute, sh,
1107 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +11001108 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001109 STRIPE_SIZE, &submit);
1110 } else {
1111 struct page *dest;
1112 int data_target;
1113 int qd_idx = sh->qd_idx;
1114
1115 /* Missing D+Q: recompute D from P, then recompute Q */
1116 if (target == qd_idx)
1117 data_target = target2;
1118 else
1119 data_target = target;
1120
1121 count = 0;
1122 for (i = disks; i-- ; ) {
1123 if (i == data_target || i == qd_idx)
1124 continue;
1125 blocks[count++] = sh->dev[i].page;
1126 }
1127 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001128 init_async_submit(&submit,
1129 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1130 NULL, NULL, NULL,
1131 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001132 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1133 &submit);
1134
1135 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001136 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1137 ops_complete_compute, sh,
1138 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001139 return async_gen_syndrome(blocks, 0, count+2,
1140 STRIPE_SIZE, &submit);
1141 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001142 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001143 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1144 ops_complete_compute, sh,
1145 to_addr_conv(sh, percpu));
1146 if (failb == syndrome_disks) {
1147 /* We're missing D+P. */
1148 return async_raid6_datap_recov(syndrome_disks+2,
1149 STRIPE_SIZE, faila,
1150 blocks, &submit);
1151 } else {
1152 /* We're missing D+D. */
1153 return async_raid6_2data_recov(syndrome_disks+2,
1154 STRIPE_SIZE, faila, failb,
1155 blocks, &submit);
1156 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001157 }
1158}
1159
1160
Dan Williams91c00922007-01-02 13:52:30 -07001161static void ops_complete_prexor(void *stripe_head_ref)
1162{
1163 struct stripe_head *sh = stripe_head_ref;
1164
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001165 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001166 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001167}
1168
1169static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001170ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1171 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001172{
Dan Williams91c00922007-01-02 13:52:30 -07001173 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001174 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001175 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001176 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001177
1178 /* existing parity data subtracted */
1179 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1180
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001181 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001182 (unsigned long long)sh->sector);
1183
1184 for (i = disks; i--; ) {
1185 struct r5dev *dev = &sh->dev[i];
1186 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001187 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001188 xor_srcs[count++] = dev->page;
1189 }
1190
Dan Williams0403e382009-09-08 17:42:50 -07001191 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001192 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001193 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001194
1195 return tx;
1196}
1197
1198static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001199ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001200{
1201 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001202 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001203
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001204 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001205 (unsigned long long)sh->sector);
1206
1207 for (i = disks; i--; ) {
1208 struct r5dev *dev = &sh->dev[i];
1209 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001210
Dan Williamsd8ee0722008-06-28 08:32:06 +10001211 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001212 struct bio *wbi;
1213
Shaohua Lib17459c2012-07-19 16:01:31 +10001214 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001215 chosen = dev->towrite;
1216 dev->towrite = NULL;
1217 BUG_ON(dev->written);
1218 wbi = dev->written = chosen;
Shaohua Lib17459c2012-07-19 16:01:31 +10001219 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001220
1221 while (wbi && wbi->bi_sector <
1222 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001223 if (wbi->bi_rw & REQ_FUA)
1224 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001225 if (wbi->bi_rw & REQ_SYNC)
1226 set_bit(R5_SyncIO, &dev->flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001227 if (wbi->bi_rw & REQ_DISCARD)
Shaohua Li620125f2012-10-11 13:49:05 +11001228 set_bit(R5_Discard, &dev->flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001229 else
Shaohua Li620125f2012-10-11 13:49:05 +11001230 tx = async_copy_data(1, wbi, dev->page,
1231 dev->sector, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001232 wbi = r5_next_bio(wbi, dev->sector);
1233 }
1234 }
1235 }
1236
1237 return tx;
1238}
1239
Dan Williamsac6b53b2009-07-14 13:40:19 -07001240static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001241{
1242 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001243 int disks = sh->disks;
1244 int pd_idx = sh->pd_idx;
1245 int qd_idx = sh->qd_idx;
1246 int i;
Shaohua Li9e4447682012-10-11 13:49:49 +11001247 bool fua = false, sync = false, discard = false;
Dan Williams91c00922007-01-02 13:52:30 -07001248
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001249 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001250 (unsigned long long)sh->sector);
1251
Shaohua Libc0934f2012-05-22 13:55:05 +10001252 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001253 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001254 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001255 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001256 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001257
Dan Williams91c00922007-01-02 13:52:30 -07001258 for (i = disks; i--; ) {
1259 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001260
Tejun Heoe9c74692010-09-03 11:56:18 +02001261 if (dev->written || i == pd_idx || i == qd_idx) {
Shaohua Li9e4447682012-10-11 13:49:49 +11001262 if (!discard)
1263 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001264 if (fua)
1265 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001266 if (sync)
1267 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001268 }
Dan Williams91c00922007-01-02 13:52:30 -07001269 }
1270
Dan Williamsd8ee0722008-06-28 08:32:06 +10001271 if (sh->reconstruct_state == reconstruct_state_drain_run)
1272 sh->reconstruct_state = reconstruct_state_drain_result;
1273 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1274 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1275 else {
1276 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1277 sh->reconstruct_state = reconstruct_state_result;
1278 }
Dan Williams91c00922007-01-02 13:52:30 -07001279
1280 set_bit(STRIPE_HANDLE, &sh->state);
1281 release_stripe(sh);
1282}
1283
1284static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001285ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1286 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001287{
Dan Williams91c00922007-01-02 13:52:30 -07001288 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001289 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001290 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001291 int count = 0, pd_idx = sh->pd_idx, i;
1292 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001293 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001294 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001295
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001296 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001297 (unsigned long long)sh->sector);
1298
Shaohua Li620125f2012-10-11 13:49:05 +11001299 for (i = 0; i < sh->disks; i++) {
1300 if (pd_idx == i)
1301 continue;
1302 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1303 break;
1304 }
1305 if (i >= sh->disks) {
1306 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001307 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1308 ops_complete_reconstruct(sh);
1309 return;
1310 }
Dan Williams91c00922007-01-02 13:52:30 -07001311 /* check if prexor is active which means only process blocks
1312 * that are part of a read-modify-write (written)
1313 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001314 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1315 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001316 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1317 for (i = disks; i--; ) {
1318 struct r5dev *dev = &sh->dev[i];
1319 if (dev->written)
1320 xor_srcs[count++] = dev->page;
1321 }
1322 } else {
1323 xor_dest = sh->dev[pd_idx].page;
1324 for (i = disks; i--; ) {
1325 struct r5dev *dev = &sh->dev[i];
1326 if (i != pd_idx)
1327 xor_srcs[count++] = dev->page;
1328 }
1329 }
1330
Dan Williams91c00922007-01-02 13:52:30 -07001331 /* 1/ if we prexor'd then the dest is reused as a source
1332 * 2/ if we did not prexor then we are redoing the parity
1333 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1334 * for the synchronous xor case
1335 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001336 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001337 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1338
1339 atomic_inc(&sh->count);
1340
Dan Williamsac6b53b2009-07-14 13:40:19 -07001341 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001342 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001343 if (unlikely(count == 1))
1344 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1345 else
1346 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001347}
1348
Dan Williamsac6b53b2009-07-14 13:40:19 -07001349static void
1350ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1351 struct dma_async_tx_descriptor *tx)
1352{
1353 struct async_submit_ctl submit;
1354 struct page **blocks = percpu->scribble;
Shaohua Li620125f2012-10-11 13:49:05 +11001355 int count, i;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001356
1357 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1358
Shaohua Li620125f2012-10-11 13:49:05 +11001359 for (i = 0; i < sh->disks; i++) {
1360 if (sh->pd_idx == i || sh->qd_idx == i)
1361 continue;
1362 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1363 break;
1364 }
1365 if (i >= sh->disks) {
1366 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001367 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1368 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1369 ops_complete_reconstruct(sh);
1370 return;
1371 }
1372
Dan Williamsac6b53b2009-07-14 13:40:19 -07001373 count = set_syndrome_sources(blocks, sh);
1374
1375 atomic_inc(&sh->count);
1376
1377 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1378 sh, to_addr_conv(sh, percpu));
1379 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001380}
1381
1382static void ops_complete_check(void *stripe_head_ref)
1383{
1384 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001385
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001386 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001387 (unsigned long long)sh->sector);
1388
Dan Williamsecc65c92008-06-28 08:31:57 +10001389 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001390 set_bit(STRIPE_HANDLE, &sh->state);
1391 release_stripe(sh);
1392}
1393
Dan Williamsac6b53b2009-07-14 13:40:19 -07001394static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001395{
Dan Williams91c00922007-01-02 13:52:30 -07001396 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001397 int pd_idx = sh->pd_idx;
1398 int qd_idx = sh->qd_idx;
1399 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001400 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001401 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001402 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001403 int count;
1404 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001405
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001406 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001407 (unsigned long long)sh->sector);
1408
Dan Williamsac6b53b2009-07-14 13:40:19 -07001409 count = 0;
1410 xor_dest = sh->dev[pd_idx].page;
1411 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001412 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001413 if (i == pd_idx || i == qd_idx)
1414 continue;
1415 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001416 }
1417
Dan Williamsd6f38f32009-07-14 11:50:52 -07001418 init_async_submit(&submit, 0, NULL, NULL, NULL,
1419 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001420 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001421 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001422
Dan Williams91c00922007-01-02 13:52:30 -07001423 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001424 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1425 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001426}
1427
Dan Williamsac6b53b2009-07-14 13:40:19 -07001428static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1429{
1430 struct page **srcs = percpu->scribble;
1431 struct async_submit_ctl submit;
1432 int count;
1433
1434 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1435 (unsigned long long)sh->sector, checkp);
1436
1437 count = set_syndrome_sources(srcs, sh);
1438 if (!checkp)
1439 srcs[count] = NULL;
1440
1441 atomic_inc(&sh->count);
1442 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1443 sh, to_addr_conv(sh, percpu));
1444 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1445 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1446}
1447
NeilBrown51acbce2013-02-28 09:08:34 +11001448static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001449{
1450 int overlap_clear = 0, i, disks = sh->disks;
1451 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001452 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001453 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001454 struct raid5_percpu *percpu;
1455 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001456
Dan Williamsd6f38f32009-07-14 11:50:52 -07001457 cpu = get_cpu();
1458 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001459 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001460 ops_run_biofill(sh);
1461 overlap_clear++;
1462 }
1463
Dan Williams7b3a8712008-06-28 08:32:09 +10001464 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001465 if (level < 6)
1466 tx = ops_run_compute5(sh, percpu);
1467 else {
1468 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1469 tx = ops_run_compute6_1(sh, percpu);
1470 else
1471 tx = ops_run_compute6_2(sh, percpu);
1472 }
1473 /* terminate the chain if reconstruct is not set to be run */
1474 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001475 async_tx_ack(tx);
1476 }
Dan Williams91c00922007-01-02 13:52:30 -07001477
Dan Williams600aa102008-06-28 08:32:05 +10001478 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001479 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001480
Dan Williams600aa102008-06-28 08:32:05 +10001481 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001482 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001483 overlap_clear++;
1484 }
1485
Dan Williamsac6b53b2009-07-14 13:40:19 -07001486 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1487 if (level < 6)
1488 ops_run_reconstruct5(sh, percpu, tx);
1489 else
1490 ops_run_reconstruct6(sh, percpu, tx);
1491 }
Dan Williams91c00922007-01-02 13:52:30 -07001492
Dan Williamsac6b53b2009-07-14 13:40:19 -07001493 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1494 if (sh->check_state == check_state_run)
1495 ops_run_check_p(sh, percpu);
1496 else if (sh->check_state == check_state_run_q)
1497 ops_run_check_pq(sh, percpu, 0);
1498 else if (sh->check_state == check_state_run_pq)
1499 ops_run_check_pq(sh, percpu, 1);
1500 else
1501 BUG();
1502 }
Dan Williams91c00922007-01-02 13:52:30 -07001503
Dan Williams91c00922007-01-02 13:52:30 -07001504 if (overlap_clear)
1505 for (i = disks; i--; ) {
1506 struct r5dev *dev = &sh->dev[i];
1507 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1508 wake_up(&sh->raid_conf->wait_for_overlap);
1509 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001510 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001511}
1512
NeilBrownd1688a62011-10-11 16:49:52 +11001513static int grow_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
1515 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001516 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001517 if (!sh)
1518 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001519
NeilBrown3f294f42005-11-08 21:39:25 -08001520 sh->raid_conf = conf;
NeilBrown3f294f42005-11-08 21:39:25 -08001521
Shaohua Lib17459c2012-07-19 16:01:31 +10001522 spin_lock_init(&sh->stripe_lock);
1523
NeilBrowne4e11e32010-06-16 16:45:16 +10001524 if (grow_buffers(sh)) {
1525 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001526 kmem_cache_free(conf->slab_cache, sh);
1527 return 0;
1528 }
1529 /* we just created an active stripe so... */
1530 atomic_set(&sh->count, 1);
1531 atomic_inc(&conf->active_stripes);
1532 INIT_LIST_HEAD(&sh->lru);
1533 release_stripe(sh);
1534 return 1;
1535}
1536
NeilBrownd1688a62011-10-11 16:49:52 +11001537static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001538{
Christoph Lametere18b8902006-12-06 20:33:20 -08001539 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001540 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
NeilBrownf4be6b42010-06-01 19:37:25 +10001542 if (conf->mddev->gendisk)
1543 sprintf(conf->cache_name[0],
1544 "raid%d-%s", conf->level, mdname(conf->mddev));
1545 else
1546 sprintf(conf->cache_name[0],
1547 "raid%d-%p", conf->level, conf->mddev);
1548 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1549
NeilBrownad01c9e2006-03-27 01:18:07 -08001550 conf->active_name = 0;
1551 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001553 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 if (!sc)
1555 return 1;
1556 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001557 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001558 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001559 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 return 0;
1562}
NeilBrown29269552006-03-27 01:18:10 -08001563
Dan Williamsd6f38f32009-07-14 11:50:52 -07001564/**
1565 * scribble_len - return the required size of the scribble region
1566 * @num - total number of disks in the array
1567 *
1568 * The size must be enough to contain:
1569 * 1/ a struct page pointer for each device in the array +2
1570 * 2/ room to convert each entry in (1) to its corresponding dma
1571 * (dma_map_page()) or page (page_address()) address.
1572 *
1573 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1574 * calculate over all devices (not just the data blocks), using zeros in place
1575 * of the P and Q blocks.
1576 */
1577static size_t scribble_len(int num)
1578{
1579 size_t len;
1580
1581 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1582
1583 return len;
1584}
1585
NeilBrownd1688a62011-10-11 16:49:52 +11001586static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001587{
1588 /* Make all the stripes able to hold 'newsize' devices.
1589 * New slots in each stripe get 'page' set to a new page.
1590 *
1591 * This happens in stages:
1592 * 1/ create a new kmem_cache and allocate the required number of
1593 * stripe_heads.
Masanari Iida83f0d772012-10-30 00:18:08 +09001594 * 2/ gather all the old stripe_heads and transfer the pages across
NeilBrownad01c9e2006-03-27 01:18:07 -08001595 * to the new stripe_heads. This will have the side effect of
1596 * freezing the array as once all stripe_heads have been collected,
1597 * no IO will be possible. Old stripe heads are freed once their
1598 * pages have been transferred over, and the old kmem_cache is
1599 * freed when all stripes are done.
1600 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1601 * we simple return a failre status - no need to clean anything up.
1602 * 4/ allocate new pages for the new slots in the new stripe_heads.
1603 * If this fails, we don't bother trying the shrink the
1604 * stripe_heads down again, we just leave them as they are.
1605 * As each stripe_head is processed the new one is released into
1606 * active service.
1607 *
1608 * Once step2 is started, we cannot afford to wait for a write,
1609 * so we use GFP_NOIO allocations.
1610 */
1611 struct stripe_head *osh, *nsh;
1612 LIST_HEAD(newstripes);
1613 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001614 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001615 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001616 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001617 int i;
1618
1619 if (newsize <= conf->pool_size)
1620 return 0; /* never bother to shrink */
1621
Dan Williamsb5470dc2008-06-27 21:44:04 -07001622 err = md_allow_write(conf->mddev);
1623 if (err)
1624 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001625
NeilBrownad01c9e2006-03-27 01:18:07 -08001626 /* Step 1 */
1627 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1628 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001629 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001630 if (!sc)
1631 return -ENOMEM;
1632
1633 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001634 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001635 if (!nsh)
1636 break;
1637
NeilBrownad01c9e2006-03-27 01:18:07 -08001638 nsh->raid_conf = conf;
NeilBrowncb13ff62012-09-24 16:27:20 +10001639 spin_lock_init(&nsh->stripe_lock);
NeilBrownad01c9e2006-03-27 01:18:07 -08001640
1641 list_add(&nsh->lru, &newstripes);
1642 }
1643 if (i) {
1644 /* didn't get enough, give up */
1645 while (!list_empty(&newstripes)) {
1646 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1647 list_del(&nsh->lru);
1648 kmem_cache_free(sc, nsh);
1649 }
1650 kmem_cache_destroy(sc);
1651 return -ENOMEM;
1652 }
1653 /* Step 2 - Must use GFP_NOIO now.
1654 * OK, we have enough stripes, start collecting inactive
1655 * stripes and copying them over
1656 */
1657 list_for_each_entry(nsh, &newstripes, lru) {
1658 spin_lock_irq(&conf->device_lock);
1659 wait_event_lock_irq(conf->wait_for_stripe,
1660 !list_empty(&conf->inactive_list),
Lukas Czernereed8c022012-11-30 11:42:40 +01001661 conf->device_lock);
NeilBrownad01c9e2006-03-27 01:18:07 -08001662 osh = get_free_stripe(conf);
1663 spin_unlock_irq(&conf->device_lock);
1664 atomic_set(&nsh->count, 1);
1665 for(i=0; i<conf->pool_size; i++)
1666 nsh->dev[i].page = osh->dev[i].page;
1667 for( ; i<newsize; i++)
1668 nsh->dev[i].page = NULL;
1669 kmem_cache_free(conf->slab_cache, osh);
1670 }
1671 kmem_cache_destroy(conf->slab_cache);
1672
1673 /* Step 3.
1674 * At this point, we are holding all the stripes so the array
1675 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001676 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001677 */
1678 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1679 if (ndisks) {
1680 for (i=0; i<conf->raid_disks; i++)
1681 ndisks[i] = conf->disks[i];
1682 kfree(conf->disks);
1683 conf->disks = ndisks;
1684 } else
1685 err = -ENOMEM;
1686
Dan Williamsd6f38f32009-07-14 11:50:52 -07001687 get_online_cpus();
1688 conf->scribble_len = scribble_len(newsize);
1689 for_each_present_cpu(cpu) {
1690 struct raid5_percpu *percpu;
1691 void *scribble;
1692
1693 percpu = per_cpu_ptr(conf->percpu, cpu);
1694 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1695
1696 if (scribble) {
1697 kfree(percpu->scribble);
1698 percpu->scribble = scribble;
1699 } else {
1700 err = -ENOMEM;
1701 break;
1702 }
1703 }
1704 put_online_cpus();
1705
NeilBrownad01c9e2006-03-27 01:18:07 -08001706 /* Step 4, return new stripes to service */
1707 while(!list_empty(&newstripes)) {
1708 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1709 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001710
NeilBrownad01c9e2006-03-27 01:18:07 -08001711 for (i=conf->raid_disks; i < newsize; i++)
1712 if (nsh->dev[i].page == NULL) {
1713 struct page *p = alloc_page(GFP_NOIO);
1714 nsh->dev[i].page = p;
1715 if (!p)
1716 err = -ENOMEM;
1717 }
1718 release_stripe(nsh);
1719 }
1720 /* critical section pass, GFP_NOIO no longer needed */
1721
1722 conf->slab_cache = sc;
1723 conf->active_name = 1-conf->active_name;
1724 conf->pool_size = newsize;
1725 return err;
1726}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
NeilBrownd1688a62011-10-11 16:49:52 +11001728static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729{
1730 struct stripe_head *sh;
1731
NeilBrown3f294f42005-11-08 21:39:25 -08001732 spin_lock_irq(&conf->device_lock);
1733 sh = get_free_stripe(conf);
1734 spin_unlock_irq(&conf->device_lock);
1735 if (!sh)
1736 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001737 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001738 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001739 kmem_cache_free(conf->slab_cache, sh);
1740 atomic_dec(&conf->active_stripes);
1741 return 1;
1742}
1743
NeilBrownd1688a62011-10-11 16:49:52 +11001744static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001745{
1746 while (drop_one_stripe(conf))
1747 ;
1748
NeilBrown29fc7e32006-02-03 03:03:41 -08001749 if (conf->slab_cache)
1750 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 conf->slab_cache = NULL;
1752}
1753
NeilBrown6712ecf2007-09-27 12:47:43 +02001754static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
NeilBrown99c0fb52009-03-31 14:39:38 +11001756 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001757 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001758 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001760 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11001761 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10001762 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
1764 for (i=0 ; i<disks; i++)
1765 if (bi == &sh->dev[i].req)
1766 break;
1767
Dan Williams45b42332007-07-09 11:56:43 -07001768 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1769 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 uptodate);
1771 if (i == disks) {
1772 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001773 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 }
NeilBrown14a75d32011-12-23 10:17:52 +11001775 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11001776 /* If replacement finished while this request was outstanding,
1777 * 'replacement' might be NULL already.
1778 * In that case it moved down to 'rdev'.
1779 * rdev is not removed until all requests are finished.
1780 */
NeilBrown14a75d32011-12-23 10:17:52 +11001781 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001782 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11001783 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
NeilBrown05616be2012-05-21 09:27:00 +10001785 if (use_new_offset(conf, sh))
1786 s = sh->sector + rdev->new_data_offset;
1787 else
1788 s = sh->sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001791 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11001792 /* Note that this cannot happen on a
1793 * replacement device. We just fail those on
1794 * any error
1795 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001796 printk_ratelimited(
1797 KERN_INFO
1798 "md/raid:%s: read error corrected"
1799 " (%lu sectors at %llu on %s)\n",
1800 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10001801 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001802 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001803 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001804 clear_bit(R5_ReadError, &sh->dev[i].flags);
1805 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng3f9e7c12012-07-31 10:04:21 +10001806 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
1807 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1808
NeilBrown14a75d32011-12-23 10:17:52 +11001809 if (atomic_read(&rdev->read_errors))
1810 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11001812 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001813 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10001814 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001817 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11001818 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1819 printk_ratelimited(
1820 KERN_WARNING
1821 "md/raid:%s: read error on replacement device "
1822 "(sector %llu on %s).\n",
1823 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001824 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11001825 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001826 else if (conf->mddev->degraded >= conf->max_degraded) {
1827 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001828 printk_ratelimited(
1829 KERN_WARNING
1830 "md/raid:%s: read error not correctable "
1831 "(sector %llu on %s).\n",
1832 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001833 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001834 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001835 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08001836 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10001837 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001838 printk_ratelimited(
1839 KERN_WARNING
1840 "md/raid:%s: read error NOT corrected!! "
1841 "(sector %llu on %s).\n",
1842 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001843 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001844 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001845 } else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001846 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001847 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001848 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001849 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001850 else
1851 retry = 1;
1852 if (retry)
majianpeng3f9e7c12012-07-31 10:04:21 +10001853 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
1854 set_bit(R5_ReadError, &sh->dev[i].flags);
1855 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1856 } else
1857 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -08001858 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001859 clear_bit(R5_ReadError, &sh->dev[i].flags);
1860 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10001861 if (!(set_bad
1862 && test_bit(In_sync, &rdev->flags)
1863 && rdev_set_badblocks(
1864 rdev, sh->sector, STRIPE_SECTORS, 0)))
1865 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 }
NeilBrown14a75d32011-12-23 10:17:52 +11001868 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1870 set_bit(STRIPE_HANDLE, &sh->state);
1871 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872}
1873
NeilBrownd710e132008-10-13 11:55:12 +11001874static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875{
NeilBrown99c0fb52009-03-31 14:39:38 +11001876 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001877 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001878 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11001879 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001881 sector_t first_bad;
1882 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11001883 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
NeilBrown977df362011-12-23 10:17:53 +11001885 for (i = 0 ; i < disks; i++) {
1886 if (bi == &sh->dev[i].req) {
1887 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 break;
NeilBrown977df362011-12-23 10:17:53 +11001889 }
1890 if (bi == &sh->dev[i].rreq) {
1891 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001892 if (rdev)
1893 replacement = 1;
1894 else
1895 /* rdev was removed and 'replacement'
1896 * replaced it. rdev is not removed
1897 * until all requests are finished.
1898 */
1899 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11001900 break;
1901 }
1902 }
Dan Williams45b42332007-07-09 11:56:43 -07001903 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1905 uptodate);
1906 if (i == disks) {
1907 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001908 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 }
1910
NeilBrown977df362011-12-23 10:17:53 +11001911 if (replacement) {
1912 if (!uptodate)
1913 md_error(conf->mddev, rdev);
1914 else if (is_badblock(rdev, sh->sector,
1915 STRIPE_SECTORS,
1916 &first_bad, &bad_sectors))
1917 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1918 } else {
1919 if (!uptodate) {
1920 set_bit(WriteErrorSeen, &rdev->flags);
1921 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11001922 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1923 set_bit(MD_RECOVERY_NEEDED,
1924 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11001925 } else if (is_badblock(rdev, sh->sector,
1926 STRIPE_SECTORS,
NeilBrownc0b32972013-04-24 11:42:42 +10001927 &first_bad, &bad_sectors)) {
NeilBrown977df362011-12-23 10:17:53 +11001928 set_bit(R5_MadeGood, &sh->dev[i].flags);
NeilBrownc0b32972013-04-24 11:42:42 +10001929 if (test_bit(R5_ReadError, &sh->dev[i].flags))
1930 /* That was a successful write so make
1931 * sure it looks like we already did
1932 * a re-write.
1933 */
1934 set_bit(R5_ReWrite, &sh->dev[i].flags);
1935 }
NeilBrown977df362011-12-23 10:17:53 +11001936 }
1937 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
NeilBrown977df362011-12-23 10:17:53 +11001939 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1940 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001942 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943}
1944
NeilBrown784052e2009-03-31 15:19:07 +11001945static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
NeilBrown784052e2009-03-31 15:19:07 +11001947static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948{
1949 struct r5dev *dev = &sh->dev[i];
1950
1951 bio_init(&dev->req);
1952 dev->req.bi_io_vec = &dev->vec;
1953 dev->req.bi_vcnt++;
1954 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11001956 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
NeilBrown977df362011-12-23 10:17:53 +11001958 bio_init(&dev->rreq);
1959 dev->rreq.bi_io_vec = &dev->rvec;
1960 dev->rreq.bi_vcnt++;
1961 dev->rreq.bi_max_vecs++;
1962 dev->rreq.bi_private = sh;
1963 dev->rvec.bv_page = dev->page;
1964
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001966 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967}
1968
NeilBrownfd01b882011-10-11 16:47:53 +11001969static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
1971 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11001972 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11001973 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10001974 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
NeilBrown908f4fb2011-12-23 10:17:50 +11001976 spin_lock_irqsave(&conf->device_lock, flags);
1977 clear_bit(In_sync, &rdev->flags);
1978 mddev->degraded = calc_degraded(conf);
1979 spin_unlock_irqrestore(&conf->device_lock, flags);
1980 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1981
NeilBrownde393cd2011-07-28 11:31:48 +10001982 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001983 set_bit(Faulty, &rdev->flags);
1984 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1985 printk(KERN_ALERT
1986 "md/raid:%s: Disk failure on %s, disabling device.\n"
1987 "md/raid:%s: Operation continuing on %d devices.\n",
1988 mdname(mddev),
1989 bdevname(rdev->bdev, b),
1990 mdname(mddev),
1991 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001992}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
1994/*
1995 * Input: a 'big' sector number,
1996 * Output: index of the data and parity disk, and the sector # in them.
1997 */
NeilBrownd1688a62011-10-11 16:49:52 +11001998static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001999 int previous, int *dd_idx,
2000 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001{
NeilBrown6e3b96e2010-04-23 07:08:28 +10002002 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10002003 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11002005 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002006 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11002008 int algorithm = previous ? conf->prev_algo
2009 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002010 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2011 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11002012 int raid_disks = previous ? conf->previous_raid_disks
2013 : conf->raid_disks;
2014 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016 /* First compute the information on this sector */
2017
2018 /*
2019 * Compute the chunk number and the sector offset inside the chunk
2020 */
2021 chunk_offset = sector_div(r_sector, sectors_per_chunk);
2022 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
2024 /*
2025 * Compute the stripe number
2026 */
NeilBrown35f2a592010-04-20 14:13:34 +10002027 stripe = chunk_number;
2028 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10002029 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 /*
2031 * Select the parity disk based on the user selected algorithm.
2032 */
NeilBrown84789552011-07-27 11:00:36 +10002033 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07002034 switch(conf->level) {
2035 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11002036 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002037 break;
2038 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002039 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002041 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002042 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 (*dd_idx)++;
2044 break;
2045 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002046 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002047 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 (*dd_idx)++;
2049 break;
2050 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002051 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002052 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 break;
2054 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002055 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002056 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002058 case ALGORITHM_PARITY_0:
2059 pd_idx = 0;
2060 (*dd_idx)++;
2061 break;
2062 case ALGORITHM_PARITY_N:
2063 pd_idx = data_disks;
2064 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002066 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002067 }
2068 break;
2069 case 6:
2070
NeilBrowne183eae2009-03-31 15:20:22 +11002071 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002072 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002073 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002074 qd_idx = pd_idx + 1;
2075 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002076 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002077 qd_idx = 0;
2078 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002079 (*dd_idx) += 2; /* D D P Q D */
2080 break;
2081 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002082 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002083 qd_idx = pd_idx + 1;
2084 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002085 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002086 qd_idx = 0;
2087 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002088 (*dd_idx) += 2; /* D D P Q D */
2089 break;
2090 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002091 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002092 qd_idx = (pd_idx + 1) % raid_disks;
2093 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002094 break;
2095 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002096 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002097 qd_idx = (pd_idx + 1) % raid_disks;
2098 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002099 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002100
2101 case ALGORITHM_PARITY_0:
2102 pd_idx = 0;
2103 qd_idx = 1;
2104 (*dd_idx) += 2;
2105 break;
2106 case ALGORITHM_PARITY_N:
2107 pd_idx = data_disks;
2108 qd_idx = data_disks + 1;
2109 break;
2110
2111 case ALGORITHM_ROTATING_ZERO_RESTART:
2112 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2113 * of blocks for computing Q is different.
2114 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002115 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002116 qd_idx = pd_idx + 1;
2117 if (pd_idx == raid_disks-1) {
2118 (*dd_idx)++; /* Q D D D P */
2119 qd_idx = 0;
2120 } else if (*dd_idx >= pd_idx)
2121 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002122 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002123 break;
2124
2125 case ALGORITHM_ROTATING_N_RESTART:
2126 /* Same a left_asymmetric, by first stripe is
2127 * D D D P Q rather than
2128 * Q D D D P
2129 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002130 stripe2 += 1;
2131 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002132 qd_idx = pd_idx + 1;
2133 if (pd_idx == raid_disks-1) {
2134 (*dd_idx)++; /* Q D D D P */
2135 qd_idx = 0;
2136 } else if (*dd_idx >= pd_idx)
2137 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002138 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002139 break;
2140
2141 case ALGORITHM_ROTATING_N_CONTINUE:
2142 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002143 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002144 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2145 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002146 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002147 break;
2148
2149 case ALGORITHM_LEFT_ASYMMETRIC_6:
2150 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002151 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002152 if (*dd_idx >= pd_idx)
2153 (*dd_idx)++;
2154 qd_idx = raid_disks - 1;
2155 break;
2156
2157 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002158 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002159 if (*dd_idx >= pd_idx)
2160 (*dd_idx)++;
2161 qd_idx = raid_disks - 1;
2162 break;
2163
2164 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002165 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002166 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2167 qd_idx = raid_disks - 1;
2168 break;
2169
2170 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002171 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002172 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2173 qd_idx = raid_disks - 1;
2174 break;
2175
2176 case ALGORITHM_PARITY_0_6:
2177 pd_idx = 0;
2178 (*dd_idx)++;
2179 qd_idx = raid_disks - 1;
2180 break;
2181
NeilBrown16a53ec2006-06-26 00:27:38 -07002182 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002183 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002184 }
2185 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 }
2187
NeilBrown911d4ee2009-03-31 14:39:38 +11002188 if (sh) {
2189 sh->pd_idx = pd_idx;
2190 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002191 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 /*
2194 * Finally, compute the new sector number
2195 */
2196 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2197 return new_sector;
2198}
2199
2200
NeilBrown784052e2009-03-31 15:19:07 +11002201static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202{
NeilBrownd1688a62011-10-11 16:49:52 +11002203 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002204 int raid_disks = sh->disks;
2205 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002207 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2208 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002209 int algorithm = previous ? conf->prev_algo
2210 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 sector_t stripe;
2212 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002213 sector_t chunk_number;
2214 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002216 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
NeilBrown16a53ec2006-06-26 00:27:38 -07002218
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2220 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
NeilBrown16a53ec2006-06-26 00:27:38 -07002222 if (i == sh->pd_idx)
2223 return 0;
2224 switch(conf->level) {
2225 case 4: break;
2226 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002227 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 case ALGORITHM_LEFT_ASYMMETRIC:
2229 case ALGORITHM_RIGHT_ASYMMETRIC:
2230 if (i > sh->pd_idx)
2231 i--;
2232 break;
2233 case ALGORITHM_LEFT_SYMMETRIC:
2234 case ALGORITHM_RIGHT_SYMMETRIC:
2235 if (i < sh->pd_idx)
2236 i += raid_disks;
2237 i -= (sh->pd_idx + 1);
2238 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002239 case ALGORITHM_PARITY_0:
2240 i -= 1;
2241 break;
2242 case ALGORITHM_PARITY_N:
2243 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002245 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002246 }
2247 break;
2248 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002249 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002250 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002251 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002252 case ALGORITHM_LEFT_ASYMMETRIC:
2253 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002254 case ALGORITHM_ROTATING_ZERO_RESTART:
2255 case ALGORITHM_ROTATING_N_RESTART:
2256 if (sh->pd_idx == raid_disks-1)
2257 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002258 else if (i > sh->pd_idx)
2259 i -= 2; /* D D P Q D */
2260 break;
2261 case ALGORITHM_LEFT_SYMMETRIC:
2262 case ALGORITHM_RIGHT_SYMMETRIC:
2263 if (sh->pd_idx == raid_disks-1)
2264 i--; /* Q D D D P */
2265 else {
2266 /* D D P Q D */
2267 if (i < sh->pd_idx)
2268 i += raid_disks;
2269 i -= (sh->pd_idx + 2);
2270 }
2271 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002272 case ALGORITHM_PARITY_0:
2273 i -= 2;
2274 break;
2275 case ALGORITHM_PARITY_N:
2276 break;
2277 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002278 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002279 if (sh->pd_idx == 0)
2280 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002281 else {
2282 /* D D Q P D */
2283 if (i < sh->pd_idx)
2284 i += raid_disks;
2285 i -= (sh->pd_idx + 1);
2286 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002287 break;
2288 case ALGORITHM_LEFT_ASYMMETRIC_6:
2289 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2290 if (i > sh->pd_idx)
2291 i--;
2292 break;
2293 case ALGORITHM_LEFT_SYMMETRIC_6:
2294 case ALGORITHM_RIGHT_SYMMETRIC_6:
2295 if (i < sh->pd_idx)
2296 i += data_disks + 1;
2297 i -= (sh->pd_idx + 1);
2298 break;
2299 case ALGORITHM_PARITY_0_6:
2300 i -= 1;
2301 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002302 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002303 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002304 }
2305 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 }
2307
2308 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002309 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
NeilBrown112bf892009-03-31 14:39:38 +11002311 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002312 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002313 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2314 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002315 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2316 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 return 0;
2318 }
2319 return r_sector;
2320}
2321
2322
Dan Williams600aa102008-06-28 08:32:05 +10002323static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002324schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002325 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002326{
2327 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002328 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002329 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002330
Dan Williamse33129d2007-01-02 13:52:30 -07002331 if (rcw) {
Dan Williamse33129d2007-01-02 13:52:30 -07002332
2333 for (i = disks; i--; ) {
2334 struct r5dev *dev = &sh->dev[i];
2335
2336 if (dev->towrite) {
2337 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002338 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002339 if (!expand)
2340 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002341 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002342 }
2343 }
NeilBrownce7d3632013-03-04 12:37:14 +11002344 /* if we are not expanding this is a proper write request, and
2345 * there will be bios with new data to be drained into the
2346 * stripe cache
2347 */
2348 if (!expand) {
2349 if (!s->locked)
2350 /* False alarm, nothing to do */
2351 return;
2352 sh->reconstruct_state = reconstruct_state_drain_run;
2353 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2354 } else
2355 sh->reconstruct_state = reconstruct_state_run;
2356
2357 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
2358
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002359 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002360 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002361 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002362 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002363 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002364 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2365 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2366
Dan Williamse33129d2007-01-02 13:52:30 -07002367 for (i = disks; i--; ) {
2368 struct r5dev *dev = &sh->dev[i];
2369 if (i == pd_idx)
2370 continue;
2371
Dan Williamse33129d2007-01-02 13:52:30 -07002372 if (dev->towrite &&
2373 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002374 test_bit(R5_Wantcompute, &dev->flags))) {
2375 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002376 set_bit(R5_LOCKED, &dev->flags);
2377 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002378 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002379 }
2380 }
NeilBrownce7d3632013-03-04 12:37:14 +11002381 if (!s->locked)
2382 /* False alarm - nothing to do */
2383 return;
2384 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
2385 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2386 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2387 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002388 }
2389
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002390 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002391 * are in flight
2392 */
2393 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2394 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002395 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002396
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002397 if (level == 6) {
2398 int qd_idx = sh->qd_idx;
2399 struct r5dev *dev = &sh->dev[qd_idx];
2400
2401 set_bit(R5_LOCKED, &dev->flags);
2402 clear_bit(R5_UPTODATE, &dev->flags);
2403 s->locked++;
2404 }
2405
Dan Williams600aa102008-06-28 08:32:05 +10002406 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07002407 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002408 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002409}
NeilBrown16a53ec2006-06-26 00:27:38 -07002410
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411/*
2412 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002413 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 * The bi_next chain must be in order.
2415 */
2416static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2417{
2418 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002419 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002420 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
NeilBrowncbe47ec2011-07-26 11:20:35 +10002422 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 (unsigned long long)bi->bi_sector,
2424 (unsigned long long)sh->sector);
2425
Shaohua Lib17459c2012-07-19 16:01:31 +10002426 /*
2427 * If several bio share a stripe. The bio bi_phys_segments acts as a
2428 * reference count to avoid race. The reference count should already be
2429 * increased before this function is called (for example, in
2430 * make_request()), so other bio sharing this stripe will not free the
2431 * stripe. If a stripe is owned by one stripe, the stripe lock will
2432 * protect it.
2433 */
2434 spin_lock_irq(&sh->stripe_lock);
NeilBrown72626682005-09-09 16:23:54 -07002435 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002437 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07002438 firstwrite = 1;
2439 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 bip = &sh->dev[dd_idx].toread;
2441 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002442 if (bio_end_sector(*bip) > bi->bi_sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 goto overlap;
2444 bip = & (*bip)->bi_next;
2445 }
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002446 if (*bip && (*bip)->bi_sector < bio_end_sector(bi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 goto overlap;
2448
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002449 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 if (*bip)
2451 bi->bi_next = *bip;
2452 *bip = bi;
Shaohua Lie7836bd62012-07-19 16:01:31 +10002453 raid5_inc_bi_active_stripes(bi);
NeilBrown72626682005-09-09 16:23:54 -07002454
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 if (forwrite) {
2456 /* check if page is covered */
2457 sector_t sector = sh->dev[dd_idx].sector;
2458 for (bi=sh->dev[dd_idx].towrite;
2459 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2460 bi && bi->bi_sector <= sector;
2461 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002462 if (bio_end_sector(bi) >= sector)
2463 sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 }
2465 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2466 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2467 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002468
2469 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2470 (unsigned long long)(*bip)->bi_sector,
2471 (unsigned long long)sh->sector, dd_idx);
NeilBrownb97390a2012-10-11 13:50:12 +11002472 spin_unlock_irq(&sh->stripe_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002473
2474 if (conf->mddev->bitmap && firstwrite) {
2475 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2476 STRIPE_SECTORS, 0);
2477 sh->bm_seq = conf->seq_flush+1;
2478 set_bit(STRIPE_BIT_DELAY, &sh->state);
2479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 return 1;
2481
2482 overlap:
2483 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10002484 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 return 0;
2486}
2487
NeilBrownd1688a62011-10-11 16:49:52 +11002488static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002489
NeilBrownd1688a62011-10-11 16:49:52 +11002490static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002491 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002492{
NeilBrown784052e2009-03-31 15:19:07 +11002493 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002494 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002495 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002496 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002497 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002498
NeilBrown112bf892009-03-31 14:39:38 +11002499 raid5_compute_sector(conf,
2500 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002501 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002502 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002503 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002504}
2505
Dan Williamsa4456852007-07-09 11:56:43 -07002506static void
NeilBrownd1688a62011-10-11 16:49:52 +11002507handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002508 struct stripe_head_state *s, int disks,
2509 struct bio **return_bi)
2510{
2511 int i;
2512 for (i = disks; i--; ) {
2513 struct bio *bi;
2514 int bitmap_end = 0;
2515
2516 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002517 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002518 rcu_read_lock();
2519 rdev = rcu_dereference(conf->disks[i].rdev);
2520 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002521 atomic_inc(&rdev->nr_pending);
2522 else
2523 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002524 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002525 if (rdev) {
2526 if (!rdev_set_badblocks(
2527 rdev,
2528 sh->sector,
2529 STRIPE_SECTORS, 0))
2530 md_error(conf->mddev, rdev);
2531 rdev_dec_pending(rdev, conf->mddev);
2532 }
Dan Williamsa4456852007-07-09 11:56:43 -07002533 }
Shaohua Lib17459c2012-07-19 16:01:31 +10002534 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002535 /* fail all writes first */
2536 bi = sh->dev[i].towrite;
2537 sh->dev[i].towrite = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10002538 spin_unlock_irq(&sh->stripe_lock);
NeilBrown1ed850f2012-10-11 13:50:13 +11002539 if (bi)
Dan Williamsa4456852007-07-09 11:56:43 -07002540 bitmap_end = 1;
Dan Williamsa4456852007-07-09 11:56:43 -07002541
2542 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2543 wake_up(&conf->wait_for_overlap);
2544
2545 while (bi && bi->bi_sector <
2546 sh->dev[i].sector + STRIPE_SECTORS) {
2547 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2548 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002549 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002550 md_write_end(conf->mddev);
2551 bi->bi_next = *return_bi;
2552 *return_bi = bi;
2553 }
2554 bi = nextbi;
2555 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002556 if (bitmap_end)
2557 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2558 STRIPE_SECTORS, 0, 0);
2559 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002560 /* and fail all 'written' */
2561 bi = sh->dev[i].written;
2562 sh->dev[i].written = NULL;
2563 if (bi) bitmap_end = 1;
2564 while (bi && bi->bi_sector <
2565 sh->dev[i].sector + STRIPE_SECTORS) {
2566 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2567 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002568 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002569 md_write_end(conf->mddev);
2570 bi->bi_next = *return_bi;
2571 *return_bi = bi;
2572 }
2573 bi = bi2;
2574 }
2575
Dan Williamsb5e98d62007-01-02 13:52:31 -07002576 /* fail any reads if this device is non-operational and
2577 * the data has not reached the cache yet.
2578 */
2579 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2580 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2581 test_bit(R5_ReadError, &sh->dev[i].flags))) {
NeilBrown143c4d02012-10-11 13:50:12 +11002582 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002583 bi = sh->dev[i].toread;
2584 sh->dev[i].toread = NULL;
NeilBrown143c4d02012-10-11 13:50:12 +11002585 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002586 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2587 wake_up(&conf->wait_for_overlap);
Dan Williamsa4456852007-07-09 11:56:43 -07002588 while (bi && bi->bi_sector <
2589 sh->dev[i].sector + STRIPE_SECTORS) {
2590 struct bio *nextbi =
2591 r5_next_bio(bi, sh->dev[i].sector);
2592 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002593 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002594 bi->bi_next = *return_bi;
2595 *return_bi = bi;
2596 }
2597 bi = nextbi;
2598 }
2599 }
Dan Williamsa4456852007-07-09 11:56:43 -07002600 if (bitmap_end)
2601 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2602 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002603 /* If we were in the middle of a write the parity block might
2604 * still be locked - so just clear all R5_LOCKED flags
2605 */
2606 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002607 }
2608
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002609 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2610 if (atomic_dec_and_test(&conf->pending_full_writes))
2611 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002612}
2613
NeilBrown7f0da592011-07-28 11:39:22 +10002614static void
NeilBrownd1688a62011-10-11 16:49:52 +11002615handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002616 struct stripe_head_state *s)
2617{
2618 int abort = 0;
2619 int i;
2620
NeilBrown7f0da592011-07-28 11:39:22 +10002621 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11002622 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
2623 wake_up(&conf->wait_for_overlap);
NeilBrown7f0da592011-07-28 11:39:22 +10002624 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002625 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002626 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10002627 * Don't even need to abort as that is handled elsewhere
2628 * if needed, and not always wanted e.g. if there is a known
2629 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11002630 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002631 * non-sync devices, or abort the recovery
2632 */
NeilBrown18b98372012-04-01 23:48:38 +10002633 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2634 /* During recovery devices cannot be removed, so
2635 * locking and refcounting of rdevs is not needed
2636 */
2637 for (i = 0; i < conf->raid_disks; i++) {
2638 struct md_rdev *rdev = conf->disks[i].rdev;
2639 if (rdev
2640 && !test_bit(Faulty, &rdev->flags)
2641 && !test_bit(In_sync, &rdev->flags)
2642 && !rdev_set_badblocks(rdev, sh->sector,
2643 STRIPE_SECTORS, 0))
2644 abort = 1;
2645 rdev = conf->disks[i].replacement;
2646 if (rdev
2647 && !test_bit(Faulty, &rdev->flags)
2648 && !test_bit(In_sync, &rdev->flags)
2649 && !rdev_set_badblocks(rdev, sh->sector,
2650 STRIPE_SECTORS, 0))
2651 abort = 1;
2652 }
2653 if (abort)
2654 conf->recovery_disabled =
2655 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10002656 }
NeilBrown18b98372012-04-01 23:48:38 +10002657 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10002658}
2659
NeilBrown9a3e1102011-12-23 10:17:53 +11002660static int want_replace(struct stripe_head *sh, int disk_idx)
2661{
2662 struct md_rdev *rdev;
2663 int rv = 0;
2664 /* Doing recovery so rcu locking not required */
2665 rdev = sh->raid_conf->disks[disk_idx].replacement;
2666 if (rdev
2667 && !test_bit(Faulty, &rdev->flags)
2668 && !test_bit(In_sync, &rdev->flags)
2669 && (rdev->recovery_offset <= sh->sector
2670 || rdev->mddev->recovery_cp <= sh->sector))
2671 rv = 1;
2672
2673 return rv;
2674}
2675
NeilBrown93b3dbc2011-07-27 11:00:36 +10002676/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002677 * to be read or computed to satisfy a request.
2678 *
2679 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002680 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002681 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002682static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2683 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002684{
2685 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002686 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2687 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002688
Dan Williamsf38e1212007-01-02 13:52:30 -07002689 /* is the data in this block needed, and can we get it? */
2690 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002691 !test_bit(R5_UPTODATE, &dev->flags) &&
2692 (dev->toread ||
2693 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2694 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002695 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002696 (s->failed >= 1 && fdev[0]->toread) ||
2697 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002698 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2699 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2700 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002701 /* we would like to get this block, possibly by computing it,
2702 * otherwise read it if the backing disk is insync
2703 */
2704 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2705 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2706 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002707 (s->failed && (disk_idx == s->failed_num[0] ||
2708 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002709 /* have disk failed, and we're requested to fetch it;
2710 * do compute it
2711 */
2712 pr_debug("Computing stripe %llu block %d\n",
2713 (unsigned long long)sh->sector, disk_idx);
2714 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2715 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2716 set_bit(R5_Wantcompute, &dev->flags);
2717 sh->ops.target = disk_idx;
2718 sh->ops.target2 = -1; /* no 2nd target */
2719 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002720 /* Careful: from this point on 'uptodate' is in the eye
2721 * of raid_run_ops which services 'compute' operations
2722 * before writes. R5_Wantcompute flags a block that will
2723 * be R5_UPTODATE by the time it is needed for a
2724 * subsequent operation.
2725 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002726 s->uptodate++;
2727 return 1;
2728 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2729 /* Computing 2-failure is *very* expensive; only
2730 * do it if failed >= 2
2731 */
2732 int other;
2733 for (other = disks; other--; ) {
2734 if (other == disk_idx)
2735 continue;
2736 if (!test_bit(R5_UPTODATE,
2737 &sh->dev[other].flags))
2738 break;
2739 }
2740 BUG_ON(other < 0);
2741 pr_debug("Computing stripe %llu blocks %d,%d\n",
2742 (unsigned long long)sh->sector,
2743 disk_idx, other);
2744 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2745 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2746 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2747 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2748 sh->ops.target = disk_idx;
2749 sh->ops.target2 = other;
2750 s->uptodate += 2;
2751 s->req_compute = 1;
2752 return 1;
2753 } else if (test_bit(R5_Insync, &dev->flags)) {
2754 set_bit(R5_LOCKED, &dev->flags);
2755 set_bit(R5_Wantread, &dev->flags);
2756 s->locked++;
2757 pr_debug("Reading block %d (sync=%d)\n",
2758 disk_idx, s->syncing);
2759 }
2760 }
2761
2762 return 0;
2763}
2764
2765/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002766 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002767 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002768static void handle_stripe_fill(struct stripe_head *sh,
2769 struct stripe_head_state *s,
2770 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002771{
2772 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002773
2774 /* look for blocks to read/compute, skip this if a compute
2775 * is already in flight, or if the stripe contents are in the
2776 * midst of changing due to a write
2777 */
2778 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2779 !sh->reconstruct_state)
2780 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002781 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002782 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002783 set_bit(STRIPE_HANDLE, &sh->state);
2784}
2785
2786
Dan Williams1fe797e2008-06-28 09:16:30 +10002787/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002788 * any written block on an uptodate or failed drive can be returned.
2789 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2790 * never LOCKED, so we don't need to test 'failed' directly.
2791 */
NeilBrownd1688a62011-10-11 16:49:52 +11002792static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002793 struct stripe_head *sh, int disks, struct bio **return_bi)
2794{
2795 int i;
2796 struct r5dev *dev;
NeilBrownf8dfcff2013-03-12 12:18:06 +11002797 int discard_pending = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002798
2799 for (i = disks; i--; )
2800 if (sh->dev[i].written) {
2801 dev = &sh->dev[i];
2802 if (!test_bit(R5_LOCKED, &dev->flags) &&
Shaohua Li9e4447682012-10-11 13:49:49 +11002803 (test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownca64cae2012-11-21 16:33:40 +11002804 test_bit(R5_Discard, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002805 /* We can return any write requests */
2806 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07002807 pr_debug("Return write for disc %d\n", i);
NeilBrownca64cae2012-11-21 16:33:40 +11002808 if (test_and_clear_bit(R5_Discard, &dev->flags))
2809 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002810 wbi = dev->written;
2811 dev->written = NULL;
2812 while (wbi && wbi->bi_sector <
2813 dev->sector + STRIPE_SECTORS) {
2814 wbi2 = r5_next_bio(wbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002815 if (!raid5_dec_bi_active_stripes(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002816 md_write_end(conf->mddev);
2817 wbi->bi_next = *return_bi;
2818 *return_bi = wbi;
2819 }
2820 wbi = wbi2;
2821 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002822 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2823 STRIPE_SECTORS,
Dan Williamsa4456852007-07-09 11:56:43 -07002824 !test_bit(STRIPE_DEGRADED, &sh->state),
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002825 0);
NeilBrownf8dfcff2013-03-12 12:18:06 +11002826 } else if (test_bit(R5_Discard, &dev->flags))
2827 discard_pending = 1;
2828 }
2829 if (!discard_pending &&
2830 test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) {
2831 clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
2832 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
2833 if (sh->qd_idx >= 0) {
2834 clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
2835 clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags);
2836 }
2837 /* now that discard is done we can proceed with any sync */
2838 clear_bit(STRIPE_DISCARD, &sh->state);
2839 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state))
2840 set_bit(STRIPE_HANDLE, &sh->state);
2841
2842 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002843
2844 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2845 if (atomic_dec_and_test(&conf->pending_full_writes))
2846 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002847}
2848
NeilBrownd1688a62011-10-11 16:49:52 +11002849static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002850 struct stripe_head *sh,
2851 struct stripe_head_state *s,
2852 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002853{
2854 int rmw = 0, rcw = 0, i;
Alexander Lyakasa7854482012-10-11 13:50:12 +11002855 sector_t recovery_cp = conf->mddev->recovery_cp;
2856
2857 /* RAID6 requires 'rcw' in current implementation.
2858 * Otherwise, check whether resync is now happening or should start.
2859 * If yes, then the array is dirty (after unclean shutdown or
2860 * initial creation), so parity in some stripes might be inconsistent.
2861 * In this case, we need to always do reconstruct-write, to ensure
2862 * that in case of drive failure or read-error correction, we
2863 * generate correct data from the parity.
2864 */
2865 if (conf->max_degraded == 2 ||
2866 (recovery_cp < MaxSector && sh->sector >= recovery_cp)) {
2867 /* Calculate the real rcw later - for now make it
NeilBrownc8ac1802011-07-27 11:00:36 +10002868 * look like rcw is cheaper
2869 */
2870 rcw = 1; rmw = 2;
Alexander Lyakasa7854482012-10-11 13:50:12 +11002871 pr_debug("force RCW max_degraded=%u, recovery_cp=%llu sh->sector=%llu\n",
2872 conf->max_degraded, (unsigned long long)recovery_cp,
2873 (unsigned long long)sh->sector);
NeilBrownc8ac1802011-07-27 11:00:36 +10002874 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002875 /* would I have to read this buffer for read_modify_write */
2876 struct r5dev *dev = &sh->dev[i];
2877 if ((dev->towrite || i == sh->pd_idx) &&
2878 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002879 !(test_bit(R5_UPTODATE, &dev->flags) ||
2880 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002881 if (test_bit(R5_Insync, &dev->flags))
2882 rmw++;
2883 else
2884 rmw += 2*disks; /* cannot read it */
2885 }
2886 /* Would I have to read this buffer for reconstruct_write */
2887 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2888 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002889 !(test_bit(R5_UPTODATE, &dev->flags) ||
2890 test_bit(R5_Wantcompute, &dev->flags))) {
2891 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002892 else
2893 rcw += 2*disks;
2894 }
2895 }
Dan Williams45b42332007-07-09 11:56:43 -07002896 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002897 (unsigned long long)sh->sector, rmw, rcw);
2898 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrowna9add5d2012-10-31 11:59:09 +11002899 if (rmw < rcw && rmw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002900 /* prefer read-modify-write, but need to get some data */
Jonathan Brassowe3620a32013-03-07 16:22:01 -06002901 if (conf->mddev->queue)
2902 blk_add_trace_msg(conf->mddev->queue,
2903 "raid5 rmw %llu %d",
2904 (unsigned long long)sh->sector, rmw);
Dan Williamsa4456852007-07-09 11:56:43 -07002905 for (i = disks; i--; ) {
2906 struct r5dev *dev = &sh->dev[i];
2907 if ((dev->towrite || i == sh->pd_idx) &&
2908 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002909 !(test_bit(R5_UPTODATE, &dev->flags) ||
2910 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002911 test_bit(R5_Insync, &dev->flags)) {
2912 if (
2913 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002914 pr_debug("Read_old block "
NeilBrowna9add5d2012-10-31 11:59:09 +11002915 "%d for r-m-w\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002916 set_bit(R5_LOCKED, &dev->flags);
2917 set_bit(R5_Wantread, &dev->flags);
2918 s->locked++;
2919 } else {
2920 set_bit(STRIPE_DELAYED, &sh->state);
2921 set_bit(STRIPE_HANDLE, &sh->state);
2922 }
2923 }
2924 }
NeilBrowna9add5d2012-10-31 11:59:09 +11002925 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002926 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002927 /* want reconstruct write, but need to get some data */
NeilBrowna9add5d2012-10-31 11:59:09 +11002928 int qread =0;
NeilBrownc8ac1802011-07-27 11:00:36 +10002929 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002930 for (i = disks; i--; ) {
2931 struct r5dev *dev = &sh->dev[i];
2932 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002933 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002934 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002935 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002936 test_bit(R5_Wantcompute, &dev->flags))) {
2937 rcw++;
2938 if (!test_bit(R5_Insync, &dev->flags))
2939 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002940 if (
2941 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002942 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002943 "%d for Reconstruct\n", i);
2944 set_bit(R5_LOCKED, &dev->flags);
2945 set_bit(R5_Wantread, &dev->flags);
2946 s->locked++;
NeilBrowna9add5d2012-10-31 11:59:09 +11002947 qread++;
Dan Williamsa4456852007-07-09 11:56:43 -07002948 } else {
2949 set_bit(STRIPE_DELAYED, &sh->state);
2950 set_bit(STRIPE_HANDLE, &sh->state);
2951 }
2952 }
2953 }
Jonathan Brassowe3620a32013-03-07 16:22:01 -06002954 if (rcw && conf->mddev->queue)
NeilBrowna9add5d2012-10-31 11:59:09 +11002955 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
2956 (unsigned long long)sh->sector,
2957 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
NeilBrownc8ac1802011-07-27 11:00:36 +10002958 }
Dan Williamsa4456852007-07-09 11:56:43 -07002959 /* now if nothing is locked, and if we have enough data,
2960 * we can start a write request
2961 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002962 /* since handle_stripe can be called at any time we need to handle the
2963 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002964 * subsequent call wants to start a write request. raid_run_ops only
2965 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002966 * simultaneously. If this is not the case then new writes need to be
2967 * held off until the compute completes.
2968 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002969 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2970 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2971 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002972 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002973}
2974
NeilBrownd1688a62011-10-11 16:49:52 +11002975static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002976 struct stripe_head_state *s, int disks)
2977{
Dan Williamsecc65c92008-06-28 08:31:57 +10002978 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002979
Dan Williamsbd2ab672008-04-10 21:29:27 -07002980 set_bit(STRIPE_HANDLE, &sh->state);
2981
Dan Williamsecc65c92008-06-28 08:31:57 +10002982 switch (sh->check_state) {
2983 case check_state_idle:
2984 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002985 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002986 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002987 sh->check_state = check_state_run;
2988 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002989 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002990 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002991 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002992 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002993 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002994 /* fall through */
2995 case check_state_compute_result:
2996 sh->check_state = check_state_idle;
2997 if (!dev)
2998 dev = &sh->dev[sh->pd_idx];
2999
3000 /* check that a write has not made the stripe insync */
3001 if (test_bit(STRIPE_INSYNC, &sh->state))
3002 break;
3003
3004 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07003005 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
3006 BUG_ON(s->uptodate != disks);
3007
3008 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10003009 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07003010 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07003011
Dan Williamsa4456852007-07-09 11:56:43 -07003012 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003013 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003014 break;
3015 case check_state_run:
3016 break; /* we will be called again upon completion */
3017 case check_state_check_result:
3018 sh->check_state = check_state_idle;
3019
3020 /* if a failure occurred during the check operation, leave
3021 * STRIPE_INSYNC not set and let the stripe be handled again
3022 */
3023 if (s->failed)
3024 break;
3025
3026 /* handle a successful check operation, if parity is correct
3027 * we are done. Otherwise update the mismatch count and repair
3028 * parity if !MD_RECOVERY_CHECK
3029 */
Dan Williamsad283ea2009-08-29 19:09:26 -07003030 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10003031 /* parity is correct (on disc,
3032 * not in buffer any more)
3033 */
3034 set_bit(STRIPE_INSYNC, &sh->state);
3035 else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003036 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsecc65c92008-06-28 08:31:57 +10003037 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3038 /* don't try to repair!! */
3039 set_bit(STRIPE_INSYNC, &sh->state);
3040 else {
3041 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10003042 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003043 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3044 set_bit(R5_Wantcompute,
3045 &sh->dev[sh->pd_idx].flags);
3046 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07003047 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10003048 s->uptodate++;
3049 }
3050 }
3051 break;
3052 case check_state_compute_run:
3053 break;
3054 default:
3055 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3056 __func__, sh->check_state,
3057 (unsigned long long) sh->sector);
3058 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003059 }
3060}
3061
3062
NeilBrownd1688a62011-10-11 16:49:52 +11003063static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07003064 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10003065 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003066{
Dan Williamsa4456852007-07-09 11:56:43 -07003067 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11003068 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07003069 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07003070
3071 set_bit(STRIPE_HANDLE, &sh->state);
3072
3073 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003074
Dan Williamsa4456852007-07-09 11:56:43 -07003075 /* Want to check and possibly repair P and Q.
3076 * However there could be one 'failed' device, in which
3077 * case we can only check one of them, possibly using the
3078 * other to generate missing data
3079 */
3080
Dan Williamsd82dfee2009-07-14 13:40:57 -07003081 switch (sh->check_state) {
3082 case check_state_idle:
3083 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10003084 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003085 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07003086 * makes sense to check P (If anything else were failed,
3087 * we would have used P to recreate it).
3088 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003089 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07003090 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003091 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003092 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07003093 * anything, so it makes sense to check it
3094 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003095 if (sh->check_state == check_state_run)
3096 sh->check_state = check_state_run_pq;
3097 else
3098 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07003099 }
Dan Williams36d1c642009-07-14 11:48:22 -07003100
Dan Williamsd82dfee2009-07-14 13:40:57 -07003101 /* discard potentially stale zero_sum_result */
3102 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07003103
Dan Williamsd82dfee2009-07-14 13:40:57 -07003104 if (sh->check_state == check_state_run) {
3105 /* async_xor_zero_sum destroys the contents of P */
3106 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
3107 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07003108 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003109 if (sh->check_state >= check_state_run &&
3110 sh->check_state <= check_state_run_pq) {
3111 /* async_syndrome_zero_sum preserves P and Q, so
3112 * no need to mark them !uptodate here
3113 */
3114 set_bit(STRIPE_OP_CHECK, &s->ops_request);
3115 break;
3116 }
Dan Williams36d1c642009-07-14 11:48:22 -07003117
Dan Williamsd82dfee2009-07-14 13:40:57 -07003118 /* we have 2-disk failure */
3119 BUG_ON(s->failed != 2);
3120 /* fall through */
3121 case check_state_compute_result:
3122 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07003123
Dan Williamsd82dfee2009-07-14 13:40:57 -07003124 /* check that a write has not made the stripe insync */
3125 if (test_bit(STRIPE_INSYNC, &sh->state))
3126 break;
Dan Williamsa4456852007-07-09 11:56:43 -07003127
3128 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07003129 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07003130 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003131 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07003132 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003133 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07003134 s->locked++;
3135 set_bit(R5_LOCKED, &dev->flags);
3136 set_bit(R5_Wantwrite, &dev->flags);
3137 }
3138 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003139 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07003140 s->locked++;
3141 set_bit(R5_LOCKED, &dev->flags);
3142 set_bit(R5_Wantwrite, &dev->flags);
3143 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003144 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003145 dev = &sh->dev[pd_idx];
3146 s->locked++;
3147 set_bit(R5_LOCKED, &dev->flags);
3148 set_bit(R5_Wantwrite, &dev->flags);
3149 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003150 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003151 dev = &sh->dev[qd_idx];
3152 s->locked++;
3153 set_bit(R5_LOCKED, &dev->flags);
3154 set_bit(R5_Wantwrite, &dev->flags);
3155 }
3156 clear_bit(STRIPE_DEGRADED, &sh->state);
3157
3158 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003159 break;
3160 case check_state_run:
3161 case check_state_run_q:
3162 case check_state_run_pq:
3163 break; /* we will be called again upon completion */
3164 case check_state_check_result:
3165 sh->check_state = check_state_idle;
3166
3167 /* handle a successful check operation, if parity is correct
3168 * we are done. Otherwise update the mismatch count and repair
3169 * parity if !MD_RECOVERY_CHECK
3170 */
3171 if (sh->ops.zero_sum_result == 0) {
3172 /* both parities are correct */
3173 if (!s->failed)
3174 set_bit(STRIPE_INSYNC, &sh->state);
3175 else {
3176 /* in contrast to the raid5 case we can validate
3177 * parity, but still have a failure to write
3178 * back
3179 */
3180 sh->check_state = check_state_compute_result;
3181 /* Returning at this point means that we may go
3182 * off and bring p and/or q uptodate again so
3183 * we make sure to check zero_sum_result again
3184 * to verify if p or q need writeback
3185 */
3186 }
3187 } else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003188 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003189 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3190 /* don't try to repair!! */
3191 set_bit(STRIPE_INSYNC, &sh->state);
3192 else {
3193 int *target = &sh->ops.target;
3194
3195 sh->ops.target = -1;
3196 sh->ops.target2 = -1;
3197 sh->check_state = check_state_compute_run;
3198 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3199 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3200 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3201 set_bit(R5_Wantcompute,
3202 &sh->dev[pd_idx].flags);
3203 *target = pd_idx;
3204 target = &sh->ops.target2;
3205 s->uptodate++;
3206 }
3207 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3208 set_bit(R5_Wantcompute,
3209 &sh->dev[qd_idx].flags);
3210 *target = qd_idx;
3211 s->uptodate++;
3212 }
3213 }
3214 }
3215 break;
3216 case check_state_compute_run:
3217 break;
3218 default:
3219 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3220 __func__, sh->check_state,
3221 (unsigned long long) sh->sector);
3222 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003223 }
3224}
3225
NeilBrownd1688a62011-10-11 16:49:52 +11003226static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003227{
3228 int i;
3229
3230 /* We have read all the blocks in this stripe and now we need to
3231 * copy some of them into a target stripe for expand.
3232 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003233 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003234 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3235 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003236 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003237 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003238 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003239 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003240
NeilBrown784052e2009-03-31 15:19:07 +11003241 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003242 sector_t s = raid5_compute_sector(conf, bn, 0,
3243 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003244 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003245 if (sh2 == NULL)
3246 /* so far only the early blocks of this stripe
3247 * have been requested. When later blocks
3248 * get requested, we will try again
3249 */
3250 continue;
3251 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3252 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3253 /* must have already done this block */
3254 release_stripe(sh2);
3255 continue;
3256 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003257
3258 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003259 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003260 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003261 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003262 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003263
Dan Williamsa4456852007-07-09 11:56:43 -07003264 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3265 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3266 for (j = 0; j < conf->raid_disks; j++)
3267 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003268 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003269 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3270 break;
3271 if (j == conf->raid_disks) {
3272 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3273 set_bit(STRIPE_HANDLE, &sh2->state);
3274 }
3275 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003276
Dan Williamsa4456852007-07-09 11:56:43 -07003277 }
NeilBrowna2e08552007-09-11 15:23:36 -07003278 /* done submitting copies, wait for them to complete */
NeilBrown749586b2012-11-20 14:11:15 +11003279 async_tx_quiesce(&tx);
Dan Williamsa4456852007-07-09 11:56:43 -07003280}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281
3282/*
3283 * handle_stripe - do things to a stripe.
3284 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003285 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3286 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003288 * return some read requests which now have data
3289 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 * schedule a read on some buffers
3291 * schedule a write of some buffers
3292 * return confirmation of parity correctness
3293 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 */
Dan Williamsa4456852007-07-09 11:56:43 -07003295
NeilBrownacfe7262011-07-27 11:00:36 +10003296static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003297{
NeilBrownd1688a62011-10-11 16:49:52 +11003298 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003299 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003300 struct r5dev *dev;
3301 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003302 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003303
NeilBrownacfe7262011-07-27 11:00:36 +10003304 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003305
NeilBrownacfe7262011-07-27 11:00:36 +10003306 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3307 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3308 s->failed_num[0] = -1;
3309 s->failed_num[1] = -1;
3310
3311 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003312 rcu_read_lock();
3313 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003314 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003315 sector_t first_bad;
3316 int bad_sectors;
3317 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003318
NeilBrown16a53ec2006-06-26 00:27:38 -07003319 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003320
Dan Williams45b42332007-07-09 11:56:43 -07003321 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003322 i, dev->flags,
3323 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003324 /* maybe we can reply to a read
3325 *
3326 * new wantfill requests are only permitted while
3327 * ops_complete_biofill is guaranteed to be inactive
3328 */
3329 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3330 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3331 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003332
3333 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003334 if (test_bit(R5_LOCKED, &dev->flags))
3335 s->locked++;
3336 if (test_bit(R5_UPTODATE, &dev->flags))
3337 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003338 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003339 s->compute++;
3340 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003341 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003342
NeilBrownacfe7262011-07-27 11:00:36 +10003343 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003344 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003345 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003346 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003347 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003348 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003349 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003350 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003351 }
Dan Williamsa4456852007-07-09 11:56:43 -07003352 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003353 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003354 /* Prefer to use the replacement for reads, but only
3355 * if it is recovered enough and has no bad blocks.
3356 */
3357 rdev = rcu_dereference(conf->disks[i].replacement);
3358 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3359 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3360 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3361 &first_bad, &bad_sectors))
3362 set_bit(R5_ReadRepl, &dev->flags);
3363 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003364 if (rdev)
3365 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003366 rdev = rcu_dereference(conf->disks[i].rdev);
3367 clear_bit(R5_ReadRepl, &dev->flags);
3368 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003369 if (rdev && test_bit(Faulty, &rdev->flags))
3370 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003371 if (rdev) {
3372 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3373 &first_bad, &bad_sectors);
3374 if (s->blocked_rdev == NULL
3375 && (test_bit(Blocked, &rdev->flags)
3376 || is_bad < 0)) {
3377 if (is_bad < 0)
3378 set_bit(BlockedBadBlocks,
3379 &rdev->flags);
3380 s->blocked_rdev = rdev;
3381 atomic_inc(&rdev->nr_pending);
3382 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003383 }
NeilBrown415e72d2010-06-17 17:25:21 +10003384 clear_bit(R5_Insync, &dev->flags);
3385 if (!rdev)
3386 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003387 else if (is_bad) {
3388 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10003389 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3390 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003391 /* treat as in-sync, but with a read error
3392 * which we can now try to correct
3393 */
3394 set_bit(R5_Insync, &dev->flags);
3395 set_bit(R5_ReadError, &dev->flags);
3396 }
3397 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003398 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003399 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003400 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003401 set_bit(R5_Insync, &dev->flags);
3402 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3403 test_bit(R5_Expanded, &dev->flags))
3404 /* If we've reshaped into here, we assume it is Insync.
3405 * We will shortly update recovery_offset to make
3406 * it official.
3407 */
3408 set_bit(R5_Insync, &dev->flags);
3409
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003410 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003411 /* This flag does not apply to '.replacement'
3412 * only to .rdev, so make sure to check that*/
3413 struct md_rdev *rdev2 = rcu_dereference(
3414 conf->disks[i].rdev);
3415 if (rdev2 == rdev)
3416 clear_bit(R5_Insync, &dev->flags);
3417 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003418 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003419 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003420 } else
3421 clear_bit(R5_WriteError, &dev->flags);
3422 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003423 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003424 /* This flag does not apply to '.replacement'
3425 * only to .rdev, so make sure to check that*/
3426 struct md_rdev *rdev2 = rcu_dereference(
3427 conf->disks[i].rdev);
3428 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003429 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003430 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003431 } else
3432 clear_bit(R5_MadeGood, &dev->flags);
3433 }
NeilBrown977df362011-12-23 10:17:53 +11003434 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3435 struct md_rdev *rdev2 = rcu_dereference(
3436 conf->disks[i].replacement);
3437 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3438 s->handle_bad_blocks = 1;
3439 atomic_inc(&rdev2->nr_pending);
3440 } else
3441 clear_bit(R5_MadeGoodRepl, &dev->flags);
3442 }
NeilBrown415e72d2010-06-17 17:25:21 +10003443 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003444 /* The ReadError flag will just be confusing now */
3445 clear_bit(R5_ReadError, &dev->flags);
3446 clear_bit(R5_ReWrite, &dev->flags);
3447 }
NeilBrown415e72d2010-06-17 17:25:21 +10003448 if (test_bit(R5_ReadError, &dev->flags))
3449 clear_bit(R5_Insync, &dev->flags);
3450 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003451 if (s->failed < 2)
3452 s->failed_num[s->failed] = i;
3453 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003454 if (rdev && !test_bit(Faulty, &rdev->flags))
3455 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003456 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003457 }
NeilBrown9a3e1102011-12-23 10:17:53 +11003458 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3459 /* If there is a failed device being replaced,
3460 * we must be recovering.
3461 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10003462 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11003463 * else we can only be replacing
3464 * sync and recovery both need to read all devices, and so
3465 * use the same flag.
3466 */
3467 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10003468 sh->sector >= conf->mddev->recovery_cp ||
3469 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11003470 s->syncing = 1;
3471 else
3472 s->replacing = 1;
3473 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003474 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003475}
NeilBrownf4168852007-02-28 20:11:53 -08003476
NeilBrowncc940152011-07-26 11:35:35 +10003477static void handle_stripe(struct stripe_head *sh)
3478{
3479 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003480 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003481 int i;
NeilBrown84789552011-07-27 11:00:36 +10003482 int prexor;
3483 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003484 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003485
3486 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003487 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003488 /* already being handled, ensure it gets handled
3489 * again when current action finishes */
3490 set_bit(STRIPE_HANDLE, &sh->state);
3491 return;
3492 }
3493
NeilBrownf8dfcff2013-03-12 12:18:06 +11003494 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3495 spin_lock(&sh->stripe_lock);
3496 /* Cannot process 'sync' concurrently with 'discard' */
3497 if (!test_bit(STRIPE_DISCARD, &sh->state) &&
3498 test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3499 set_bit(STRIPE_SYNCING, &sh->state);
3500 clear_bit(STRIPE_INSYNC, &sh->state);
NeilBrownf94c0b62013-07-22 12:57:21 +10003501 clear_bit(STRIPE_REPLACED, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003502 }
3503 spin_unlock(&sh->stripe_lock);
NeilBrowncc940152011-07-26 11:35:35 +10003504 }
3505 clear_bit(STRIPE_DELAYED, &sh->state);
3506
3507 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3508 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3509 (unsigned long long)sh->sector, sh->state,
3510 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3511 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003512
NeilBrownacfe7262011-07-27 11:00:36 +10003513 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003514
NeilBrownbc2607f2011-07-28 11:39:22 +10003515 if (s.handle_bad_blocks) {
3516 set_bit(STRIPE_HANDLE, &sh->state);
3517 goto finish;
3518 }
3519
NeilBrown474af965fe2011-07-27 11:00:36 +10003520 if (unlikely(s.blocked_rdev)) {
3521 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003522 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003523 set_bit(STRIPE_HANDLE, &sh->state);
3524 goto finish;
3525 }
3526 /* There is nothing for the blocked_rdev to block */
3527 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3528 s.blocked_rdev = NULL;
3529 }
3530
3531 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3532 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3533 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3534 }
3535
3536 pr_debug("locked=%d uptodate=%d to_read=%d"
3537 " to_write=%d failed=%d failed_num=%d,%d\n",
3538 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3539 s.failed_num[0], s.failed_num[1]);
3540 /* check if the array has lost more than max_degraded devices and,
3541 * if so, some requests might need to be failed.
3542 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003543 if (s.failed > conf->max_degraded) {
3544 sh->check_state = 0;
3545 sh->reconstruct_state = 0;
3546 if (s.to_read+s.to_write+s.written)
3547 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003548 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003549 handle_failed_sync(conf, sh, &s);
3550 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003551
NeilBrown84789552011-07-27 11:00:36 +10003552 /* Now we check to see if any write operations have recently
3553 * completed
3554 */
3555 prexor = 0;
3556 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3557 prexor = 1;
3558 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3559 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3560 sh->reconstruct_state = reconstruct_state_idle;
3561
3562 /* All the 'written' buffers and the parity block are ready to
3563 * be written back to disk
3564 */
Shaohua Li9e4447682012-10-11 13:49:49 +11003565 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
3566 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003567 BUG_ON(sh->qd_idx >= 0 &&
Shaohua Li9e4447682012-10-11 13:49:49 +11003568 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
3569 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003570 for (i = disks; i--; ) {
3571 struct r5dev *dev = &sh->dev[i];
3572 if (test_bit(R5_LOCKED, &dev->flags) &&
3573 (i == sh->pd_idx || i == sh->qd_idx ||
3574 dev->written)) {
3575 pr_debug("Writing block %d\n", i);
3576 set_bit(R5_Wantwrite, &dev->flags);
3577 if (prexor)
3578 continue;
3579 if (!test_bit(R5_Insync, &dev->flags) ||
3580 ((i == sh->pd_idx || i == sh->qd_idx) &&
3581 s.failed == 0))
3582 set_bit(STRIPE_INSYNC, &sh->state);
3583 }
3584 }
3585 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3586 s.dec_preread_active = 1;
3587 }
3588
NeilBrownef5b7c62012-11-22 09:13:36 +11003589 /*
3590 * might be able to return some write requests if the parity blocks
3591 * are safe, or on a failed drive
3592 */
3593 pdev = &sh->dev[sh->pd_idx];
3594 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3595 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3596 qdev = &sh->dev[sh->qd_idx];
3597 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3598 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3599 || conf->level < 6;
3600
3601 if (s.written &&
3602 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3603 && !test_bit(R5_LOCKED, &pdev->flags)
3604 && (test_bit(R5_UPTODATE, &pdev->flags) ||
3605 test_bit(R5_Discard, &pdev->flags))))) &&
3606 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3607 && !test_bit(R5_LOCKED, &qdev->flags)
3608 && (test_bit(R5_UPTODATE, &qdev->flags) ||
3609 test_bit(R5_Discard, &qdev->flags))))))
3610 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3611
3612 /* Now we might consider reading some blocks, either to check/generate
3613 * parity, or to satisfy requests
3614 * or to load a block that is being partially written.
3615 */
3616 if (s.to_read || s.non_overwrite
3617 || (conf->level == 6 && s.to_write && s.failed)
3618 || (s.syncing && (s.uptodate + s.compute < disks))
3619 || s.replacing
3620 || s.expanding)
3621 handle_stripe_fill(sh, &s, disks);
3622
NeilBrown84789552011-07-27 11:00:36 +10003623 /* Now to consider new write requests and what else, if anything
3624 * should be read. We do not handle new writes when:
3625 * 1/ A 'write' operation (copy+xor) is already in flight.
3626 * 2/ A 'check' operation is in flight, as it may clobber the parity
3627 * block.
3628 */
3629 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3630 handle_stripe_dirtying(conf, sh, &s, disks);
3631
3632 /* maybe we need to check and possibly fix the parity for this stripe
3633 * Any reads will already have been scheduled, so we just see if enough
3634 * data is available. The parity check is held off while parity
3635 * dependent operations are in flight.
3636 */
3637 if (sh->check_state ||
3638 (s.syncing && s.locked == 0 &&
3639 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3640 !test_bit(STRIPE_INSYNC, &sh->state))) {
3641 if (conf->level == 6)
3642 handle_parity_checks6(conf, sh, &s, disks);
3643 else
3644 handle_parity_checks5(conf, sh, &s, disks);
3645 }
NeilBrownc5a31002011-07-27 11:00:36 +10003646
NeilBrownf94c0b62013-07-22 12:57:21 +10003647 if ((s.replacing || s.syncing) && s.locked == 0
3648 && !test_bit(STRIPE_COMPUTE_RUN, &sh->state)
3649 && !test_bit(STRIPE_REPLACED, &sh->state)) {
NeilBrown9a3e1102011-12-23 10:17:53 +11003650 /* Write out to replacement devices where possible */
3651 for (i = 0; i < conf->raid_disks; i++)
NeilBrownf94c0b62013-07-22 12:57:21 +10003652 if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3653 WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags));
NeilBrown9a3e1102011-12-23 10:17:53 +11003654 set_bit(R5_WantReplace, &sh->dev[i].flags);
3655 set_bit(R5_LOCKED, &sh->dev[i].flags);
3656 s.locked++;
3657 }
NeilBrownf94c0b62013-07-22 12:57:21 +10003658 if (s.replacing)
3659 set_bit(STRIPE_INSYNC, &sh->state);
3660 set_bit(STRIPE_REPLACED, &sh->state);
NeilBrown9a3e1102011-12-23 10:17:53 +11003661 }
3662 if ((s.syncing || s.replacing) && s.locked == 0 &&
NeilBrownf94c0b62013-07-22 12:57:21 +10003663 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
NeilBrown9a3e1102011-12-23 10:17:53 +11003664 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003665 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3666 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003667 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
3668 wake_up(&conf->wait_for_overlap);
NeilBrownc5a31002011-07-27 11:00:36 +10003669 }
3670
3671 /* If the failed drives are just a ReadError, then we might need
3672 * to progress the repair/check process
3673 */
3674 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3675 for (i = 0; i < s.failed; i++) {
3676 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3677 if (test_bit(R5_ReadError, &dev->flags)
3678 && !test_bit(R5_LOCKED, &dev->flags)
3679 && test_bit(R5_UPTODATE, &dev->flags)
3680 ) {
3681 if (!test_bit(R5_ReWrite, &dev->flags)) {
3682 set_bit(R5_Wantwrite, &dev->flags);
3683 set_bit(R5_ReWrite, &dev->flags);
3684 set_bit(R5_LOCKED, &dev->flags);
3685 s.locked++;
3686 } else {
3687 /* let's read it back */
3688 set_bit(R5_Wantread, &dev->flags);
3689 set_bit(R5_LOCKED, &dev->flags);
3690 s.locked++;
3691 }
3692 }
3693 }
3694
3695
NeilBrown3687c062011-07-27 11:00:36 +10003696 /* Finish reconstruct operations initiated by the expansion process */
3697 if (sh->reconstruct_state == reconstruct_state_result) {
3698 struct stripe_head *sh_src
3699 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3700 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3701 /* sh cannot be written until sh_src has been read.
3702 * so arrange for sh to be delayed a little
3703 */
3704 set_bit(STRIPE_DELAYED, &sh->state);
3705 set_bit(STRIPE_HANDLE, &sh->state);
3706 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3707 &sh_src->state))
3708 atomic_inc(&conf->preread_active_stripes);
3709 release_stripe(sh_src);
3710 goto finish;
3711 }
3712 if (sh_src)
3713 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003714
NeilBrown3687c062011-07-27 11:00:36 +10003715 sh->reconstruct_state = reconstruct_state_idle;
3716 clear_bit(STRIPE_EXPANDING, &sh->state);
3717 for (i = conf->raid_disks; i--; ) {
3718 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3719 set_bit(R5_LOCKED, &sh->dev[i].flags);
3720 s.locked++;
3721 }
3722 }
3723
3724 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3725 !sh->reconstruct_state) {
3726 /* Need to write out all blocks after computing parity */
3727 sh->disks = conf->raid_disks;
3728 stripe_set_idx(sh->sector, conf, 0, sh);
3729 schedule_reconstruction(sh, &s, 1, 1);
3730 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3731 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3732 atomic_dec(&conf->reshape_stripes);
3733 wake_up(&conf->wait_for_overlap);
3734 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3735 }
3736
3737 if (s.expanding && s.locked == 0 &&
3738 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3739 handle_stripe_expansion(conf, sh);
3740
3741finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003742 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10003743 if (unlikely(s.blocked_rdev)) {
3744 if (conf->mddev->external)
3745 md_wait_for_blocked_rdev(s.blocked_rdev,
3746 conf->mddev);
3747 else
3748 /* Internal metadata will immediately
3749 * be written by raid5d, so we don't
3750 * need to wait here.
3751 */
3752 rdev_dec_pending(s.blocked_rdev,
3753 conf->mddev);
3754 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003755
NeilBrownbc2607f2011-07-28 11:39:22 +10003756 if (s.handle_bad_blocks)
3757 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003758 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003759 struct r5dev *dev = &sh->dev[i];
3760 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3761 /* We own a safe reference to the rdev */
3762 rdev = conf->disks[i].rdev;
3763 if (!rdev_set_badblocks(rdev, sh->sector,
3764 STRIPE_SECTORS, 0))
3765 md_error(conf->mddev, rdev);
3766 rdev_dec_pending(rdev, conf->mddev);
3767 }
NeilBrownb84db562011-07-28 11:39:23 +10003768 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3769 rdev = conf->disks[i].rdev;
3770 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003771 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10003772 rdev_dec_pending(rdev, conf->mddev);
3773 }
NeilBrown977df362011-12-23 10:17:53 +11003774 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3775 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11003776 if (!rdev)
3777 /* rdev have been moved down */
3778 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11003779 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003780 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11003781 rdev_dec_pending(rdev, conf->mddev);
3782 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003783 }
3784
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003785 if (s.ops_request)
3786 raid_run_ops(sh, s.ops_request);
3787
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003788 ops_run_io(sh, &s);
3789
NeilBrownc5709ef2011-07-26 11:35:20 +10003790 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003791 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003792 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003793 * have actually been submitted.
3794 */
3795 atomic_dec(&conf->preread_active_stripes);
3796 if (atomic_read(&conf->preread_active_stripes) <
3797 IO_THRESHOLD)
3798 md_wakeup_thread(conf->mddev->thread);
3799 }
3800
NeilBrownc5709ef2011-07-26 11:35:20 +10003801 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003802
Dan Williams257a4b42011-11-08 16:22:06 +11003803 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003804}
3805
NeilBrownd1688a62011-10-11 16:49:52 +11003806static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807{
3808 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3809 while (!list_empty(&conf->delayed_list)) {
3810 struct list_head *l = conf->delayed_list.next;
3811 struct stripe_head *sh;
3812 sh = list_entry(l, struct stripe_head, lru);
3813 list_del_init(l);
3814 clear_bit(STRIPE_DELAYED, &sh->state);
3815 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3816 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003817 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 }
NeilBrown482c0832011-04-18 18:25:42 +10003819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820}
3821
NeilBrownd1688a62011-10-11 16:49:52 +11003822static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003823{
3824 /* device_lock is held */
3825 struct list_head head;
3826 list_add(&head, &conf->bitmap_list);
3827 list_del_init(&conf->bitmap_list);
3828 while (!list_empty(&head)) {
3829 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3830 list_del_init(&sh->lru);
3831 atomic_inc(&sh->count);
3832 __release_stripe(conf, sh);
3833 }
3834}
3835
NeilBrownfd01b882011-10-11 16:47:53 +11003836int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003837{
NeilBrownd1688a62011-10-11 16:49:52 +11003838 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003839
3840 /* No difference between reads and writes. Just check
3841 * how busy the stripe_cache is
3842 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003843
NeilBrownf022b2f2006-10-03 01:15:56 -07003844 if (conf->inactive_blocked)
3845 return 1;
3846 if (conf->quiesce)
3847 return 1;
3848 if (list_empty_careful(&conf->inactive_list))
3849 return 1;
3850
3851 return 0;
3852}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003853EXPORT_SYMBOL_GPL(md_raid5_congested);
3854
3855static int raid5_congested(void *data, int bits)
3856{
NeilBrownfd01b882011-10-11 16:47:53 +11003857 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003858
3859 return mddev_congested(mddev, bits) ||
3860 md_raid5_congested(mddev, bits);
3861}
NeilBrownf022b2f2006-10-03 01:15:56 -07003862
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003863/* We want read requests to align with chunks where possible,
3864 * but write requests don't need to.
3865 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003866static int raid5_mergeable_bvec(struct request_queue *q,
3867 struct bvec_merge_data *bvm,
3868 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003869{
NeilBrownfd01b882011-10-11 16:47:53 +11003870 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003871 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003872 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003873 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003874 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003875
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003876 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003877 return biovec->bv_len; /* always allow writes to be mergeable */
3878
Andre Noll664e7c42009-06-18 08:45:27 +10003879 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3880 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003881 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3882 if (max < 0) max = 0;
3883 if (max <= biovec->bv_len && bio_sectors == 0)
3884 return biovec->bv_len;
3885 else
3886 return max;
3887}
3888
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003889
NeilBrownfd01b882011-10-11 16:47:53 +11003890static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003891{
3892 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003893 unsigned int chunk_sectors = mddev->chunk_sectors;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08003894 unsigned int bio_sectors = bio_sectors(bio);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003895
Andre Noll664e7c42009-06-18 08:45:27 +10003896 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3897 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003898 return chunk_sectors >=
3899 ((sector & (chunk_sectors - 1)) + bio_sectors);
3900}
3901
3902/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003903 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3904 * later sampled by raid5d.
3905 */
NeilBrownd1688a62011-10-11 16:49:52 +11003906static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003907{
3908 unsigned long flags;
3909
3910 spin_lock_irqsave(&conf->device_lock, flags);
3911
3912 bi->bi_next = conf->retry_read_aligned_list;
3913 conf->retry_read_aligned_list = bi;
3914
3915 spin_unlock_irqrestore(&conf->device_lock, flags);
3916 md_wakeup_thread(conf->mddev->thread);
3917}
3918
3919
NeilBrownd1688a62011-10-11 16:49:52 +11003920static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003921{
3922 struct bio *bi;
3923
3924 bi = conf->retry_read_aligned;
3925 if (bi) {
3926 conf->retry_read_aligned = NULL;
3927 return bi;
3928 }
3929 bi = conf->retry_read_aligned_list;
3930 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003931 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003932 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003933 /*
3934 * this sets the active strip count to 1 and the processed
3935 * strip count to zero (upper 8 bits)
3936 */
Shaohua Lie7836bd62012-07-19 16:01:31 +10003937 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003938 }
3939
3940 return bi;
3941}
3942
3943
3944/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003945 * The "raid5_align_endio" should check if the read succeeded and if it
3946 * did, call bio_endio on the original bio (having bio_put the new bio
3947 * first).
3948 * If the read failed..
3949 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003950static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003951{
3952 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003953 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003954 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003955 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003956 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003957
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003958 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003959
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003960 rdev = (void*)raid_bi->bi_next;
3961 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003962 mddev = rdev->mddev;
3963 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003964
3965 rdev_dec_pending(rdev, conf->mddev);
3966
3967 if (!error && uptodate) {
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07003968 trace_block_bio_complete(bdev_get_queue(raid_bi->bi_bdev),
3969 raid_bi, 0);
NeilBrown6712ecf2007-09-27 12:47:43 +02003970 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003971 if (atomic_dec_and_test(&conf->active_aligned_reads))
3972 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003973 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003974 }
3975
3976
Dan Williams45b42332007-07-09 11:56:43 -07003977 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003978
3979 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003980}
3981
Neil Brown387bb172007-02-08 14:20:29 -08003982static int bio_fits_rdev(struct bio *bi)
3983{
Jens Axboe165125e2007-07-24 09:28:11 +02003984 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003985
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08003986 if (bio_sectors(bi) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003987 return 0;
3988 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003989 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003990 return 0;
3991
3992 if (q->merge_bvec_fn)
3993 /* it's too hard to apply the merge_bvec_fn at this stage,
3994 * just just give up
3995 */
3996 return 0;
3997
3998 return 1;
3999}
4000
4001
NeilBrownfd01b882011-10-11 16:47:53 +11004002static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004003{
NeilBrownd1688a62011-10-11 16:49:52 +11004004 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11004005 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004006 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11004007 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11004008 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004009
4010 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07004011 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004012 return 0;
4013 }
4014 /*
NeilBrowna167f662010-10-26 18:31:13 +11004015 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004016 */
NeilBrowna167f662010-10-26 18:31:13 +11004017 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004018 if (!align_bi)
4019 return 0;
4020 /*
4021 * set bi_end_io to a new function, and set bi_private to the
4022 * original bio.
4023 */
4024 align_bi->bi_end_io = raid5_align_endio;
4025 align_bi->bi_private = raid_bio;
4026 /*
4027 * compute position
4028 */
NeilBrown112bf892009-03-31 14:39:38 +11004029 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
4030 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11004031 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004032
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004033 end_sector = bio_end_sector(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004034 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11004035 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
4036 if (!rdev || test_bit(Faulty, &rdev->flags) ||
4037 rdev->recovery_offset < end_sector) {
4038 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
4039 if (rdev &&
4040 (test_bit(Faulty, &rdev->flags) ||
4041 !(test_bit(In_sync, &rdev->flags) ||
4042 rdev->recovery_offset >= end_sector)))
4043 rdev = NULL;
4044 }
4045 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10004046 sector_t first_bad;
4047 int bad_sectors;
4048
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004049 atomic_inc(&rdev->nr_pending);
4050 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004051 raid_bio->bi_next = (void*)rdev;
4052 align_bi->bi_bdev = rdev->bdev;
4053 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004054
NeilBrown31c176e2011-07-28 11:39:22 +10004055 if (!bio_fits_rdev(align_bi) ||
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08004056 is_badblock(rdev, align_bi->bi_sector, bio_sectors(align_bi),
NeilBrown31c176e2011-07-28 11:39:22 +10004057 &first_bad, &bad_sectors)) {
4058 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08004059 bio_put(align_bi);
4060 rdev_dec_pending(rdev, mddev);
4061 return 0;
4062 }
4063
majianpeng6c0544e2012-06-12 08:31:10 +08004064 /* No reshape active, so we can trust rdev->data_offset */
4065 align_bi->bi_sector += rdev->data_offset;
4066
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004067 spin_lock_irq(&conf->device_lock);
4068 wait_event_lock_irq(conf->wait_for_stripe,
4069 conf->quiesce == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01004070 conf->device_lock);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004071 atomic_inc(&conf->active_aligned_reads);
4072 spin_unlock_irq(&conf->device_lock);
4073
Jonathan Brassowe3620a32013-03-07 16:22:01 -06004074 if (mddev->gendisk)
4075 trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev),
4076 align_bi, disk_devt(mddev->gendisk),
4077 raid_bio->bi_sector);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004078 generic_make_request(align_bi);
4079 return 1;
4080 } else {
4081 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004082 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004083 return 0;
4084 }
4085}
4086
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004087/* __get_priority_stripe - get the next stripe to process
4088 *
4089 * Full stripe writes are allowed to pass preread active stripes up until
4090 * the bypass_threshold is exceeded. In general the bypass_count
4091 * increments when the handle_list is handled before the hold_list; however, it
4092 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
4093 * stripe with in flight i/o. The bypass_count will be reset when the
4094 * head of the hold_list has changed, i.e. the head was promoted to the
4095 * handle_list.
4096 */
NeilBrownd1688a62011-10-11 16:49:52 +11004097static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004098{
4099 struct stripe_head *sh;
4100
4101 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
4102 __func__,
4103 list_empty(&conf->handle_list) ? "empty" : "busy",
4104 list_empty(&conf->hold_list) ? "empty" : "busy",
4105 atomic_read(&conf->pending_full_writes), conf->bypass_count);
4106
4107 if (!list_empty(&conf->handle_list)) {
4108 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
4109
4110 if (list_empty(&conf->hold_list))
4111 conf->bypass_count = 0;
4112 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
4113 if (conf->hold_list.next == conf->last_hold)
4114 conf->bypass_count++;
4115 else {
4116 conf->last_hold = conf->hold_list.next;
4117 conf->bypass_count -= conf->bypass_threshold;
4118 if (conf->bypass_count < 0)
4119 conf->bypass_count = 0;
4120 }
4121 }
4122 } else if (!list_empty(&conf->hold_list) &&
4123 ((conf->bypass_threshold &&
4124 conf->bypass_count > conf->bypass_threshold) ||
4125 atomic_read(&conf->pending_full_writes) == 0)) {
4126 sh = list_entry(conf->hold_list.next,
4127 typeof(*sh), lru);
4128 conf->bypass_count -= conf->bypass_threshold;
4129 if (conf->bypass_count < 0)
4130 conf->bypass_count = 0;
4131 } else
4132 return NULL;
4133
4134 list_del_init(&sh->lru);
4135 atomic_inc(&sh->count);
4136 BUG_ON(atomic_read(&sh->count) != 1);
4137 return sh;
4138}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004139
Shaohua Li8811b592012-08-02 08:33:00 +10004140struct raid5_plug_cb {
4141 struct blk_plug_cb cb;
4142 struct list_head list;
4143};
4144
4145static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
4146{
4147 struct raid5_plug_cb *cb = container_of(
4148 blk_cb, struct raid5_plug_cb, cb);
4149 struct stripe_head *sh;
4150 struct mddev *mddev = cb->cb.data;
4151 struct r5conf *conf = mddev->private;
NeilBrowna9add5d2012-10-31 11:59:09 +11004152 int cnt = 0;
Shaohua Li8811b592012-08-02 08:33:00 +10004153
4154 if (cb->list.next && !list_empty(&cb->list)) {
4155 spin_lock_irq(&conf->device_lock);
4156 while (!list_empty(&cb->list)) {
4157 sh = list_first_entry(&cb->list, struct stripe_head, lru);
4158 list_del_init(&sh->lru);
4159 /*
4160 * avoid race release_stripe_plug() sees
4161 * STRIPE_ON_UNPLUG_LIST clear but the stripe
4162 * is still in our list
4163 */
4164 smp_mb__before_clear_bit();
4165 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
Shaohua Li773ca822013-08-27 17:50:39 +08004166 /*
4167 * STRIPE_ON_RELEASE_LIST could be set here. In that
4168 * case, the count is always > 1 here
4169 */
Shaohua Li8811b592012-08-02 08:33:00 +10004170 __release_stripe(conf, sh);
NeilBrowna9add5d2012-10-31 11:59:09 +11004171 cnt++;
Shaohua Li8811b592012-08-02 08:33:00 +10004172 }
4173 spin_unlock_irq(&conf->device_lock);
4174 }
Jonathan Brassowe3620a32013-03-07 16:22:01 -06004175 if (mddev->queue)
4176 trace_block_unplug(mddev->queue, cnt, !from_schedule);
Shaohua Li8811b592012-08-02 08:33:00 +10004177 kfree(cb);
4178}
4179
4180static void release_stripe_plug(struct mddev *mddev,
4181 struct stripe_head *sh)
4182{
4183 struct blk_plug_cb *blk_cb = blk_check_plugged(
4184 raid5_unplug, mddev,
4185 sizeof(struct raid5_plug_cb));
4186 struct raid5_plug_cb *cb;
4187
4188 if (!blk_cb) {
4189 release_stripe(sh);
4190 return;
4191 }
4192
4193 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
4194
4195 if (cb->list.next == NULL)
4196 INIT_LIST_HEAD(&cb->list);
4197
4198 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
4199 list_add_tail(&sh->lru, &cb->list);
4200 else
4201 release_stripe(sh);
4202}
4203
Shaohua Li620125f2012-10-11 13:49:05 +11004204static void make_discard_request(struct mddev *mddev, struct bio *bi)
4205{
4206 struct r5conf *conf = mddev->private;
4207 sector_t logical_sector, last_sector;
4208 struct stripe_head *sh;
4209 int remaining;
4210 int stripe_sectors;
4211
4212 if (mddev->reshape_position != MaxSector)
4213 /* Skip discard while reshape is happening */
4214 return;
4215
4216 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4217 last_sector = bi->bi_sector + (bi->bi_size>>9);
4218
4219 bi->bi_next = NULL;
4220 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
4221
4222 stripe_sectors = conf->chunk_sectors *
4223 (conf->raid_disks - conf->max_degraded);
4224 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
4225 stripe_sectors);
4226 sector_div(last_sector, stripe_sectors);
4227
4228 logical_sector *= conf->chunk_sectors;
4229 last_sector *= conf->chunk_sectors;
4230
4231 for (; logical_sector < last_sector;
4232 logical_sector += STRIPE_SECTORS) {
4233 DEFINE_WAIT(w);
4234 int d;
4235 again:
4236 sh = get_active_stripe(conf, logical_sector, 0, 0, 0);
4237 prepare_to_wait(&conf->wait_for_overlap, &w,
4238 TASK_UNINTERRUPTIBLE);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004239 set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
4240 if (test_bit(STRIPE_SYNCING, &sh->state)) {
4241 release_stripe(sh);
4242 schedule();
4243 goto again;
4244 }
4245 clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
Shaohua Li620125f2012-10-11 13:49:05 +11004246 spin_lock_irq(&sh->stripe_lock);
4247 for (d = 0; d < conf->raid_disks; d++) {
4248 if (d == sh->pd_idx || d == sh->qd_idx)
4249 continue;
4250 if (sh->dev[d].towrite || sh->dev[d].toread) {
4251 set_bit(R5_Overlap, &sh->dev[d].flags);
4252 spin_unlock_irq(&sh->stripe_lock);
4253 release_stripe(sh);
4254 schedule();
4255 goto again;
4256 }
4257 }
NeilBrownf8dfcff2013-03-12 12:18:06 +11004258 set_bit(STRIPE_DISCARD, &sh->state);
Shaohua Li620125f2012-10-11 13:49:05 +11004259 finish_wait(&conf->wait_for_overlap, &w);
4260 for (d = 0; d < conf->raid_disks; d++) {
4261 if (d == sh->pd_idx || d == sh->qd_idx)
4262 continue;
4263 sh->dev[d].towrite = bi;
4264 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
4265 raid5_inc_bi_active_stripes(bi);
4266 }
4267 spin_unlock_irq(&sh->stripe_lock);
4268 if (conf->mddev->bitmap) {
4269 for (d = 0;
4270 d < conf->raid_disks - conf->max_degraded;
4271 d++)
4272 bitmap_startwrite(mddev->bitmap,
4273 sh->sector,
4274 STRIPE_SECTORS,
4275 0);
4276 sh->bm_seq = conf->seq_flush + 1;
4277 set_bit(STRIPE_BIT_DELAY, &sh->state);
4278 }
4279
4280 set_bit(STRIPE_HANDLE, &sh->state);
4281 clear_bit(STRIPE_DELAYED, &sh->state);
4282 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4283 atomic_inc(&conf->preread_active_stripes);
4284 release_stripe_plug(mddev, sh);
4285 }
4286
4287 remaining = raid5_dec_bi_active_stripes(bi);
4288 if (remaining == 0) {
4289 md_write_end(mddev);
4290 bio_endio(bi, 0);
4291 }
4292}
4293
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07004294static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295{
NeilBrownd1688a62011-10-11 16:49:52 +11004296 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11004297 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 sector_t new_sector;
4299 sector_t logical_sector, last_sector;
4300 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01004301 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11004302 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303
Tejun Heoe9c74692010-09-03 11:56:18 +02004304 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
4305 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004306 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07004307 }
4308
NeilBrown3d310eb2005-06-21 17:17:26 -07004309 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07004310
NeilBrown802ba062006-12-13 00:34:13 -08004311 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004312 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11004313 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004314 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004315
Shaohua Li620125f2012-10-11 13:49:05 +11004316 if (unlikely(bi->bi_rw & REQ_DISCARD)) {
4317 make_discard_request(mddev, bi);
4318 return;
4319 }
4320
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004322 last_sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 bi->bi_next = NULL;
4324 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07004325
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
4327 DEFINE_WAIT(w);
NeilBrownb5663ba2009-03-31 14:39:38 +11004328 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08004329
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004330 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11004331 previous = 0;
NeilBrownb578d552006-03-27 01:18:12 -08004332 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004333 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11004334 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f762006-03-27 01:18:15 -08004335 * 64bit on a 32bit platform, and so it might be
4336 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02004337 * Of course reshape_progress could change after
NeilBrowndf8e7f762006-03-27 01:18:15 -08004338 * the lock is dropped, so once we get a reference
4339 * to the stripe that we think it is, we will have
4340 * to check again.
4341 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004342 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004343 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004344 ? logical_sector < conf->reshape_progress
4345 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004346 previous = 1;
4347 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10004348 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004349 ? logical_sector < conf->reshape_safe
4350 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08004351 spin_unlock_irq(&conf->device_lock);
4352 schedule();
4353 goto retry;
4354 }
4355 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004356 spin_unlock_irq(&conf->device_lock);
4357 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004358
NeilBrown112bf892009-03-31 14:39:38 +11004359 new_sector = raid5_compute_sector(conf, logical_sector,
4360 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11004361 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10004362 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 (unsigned long long)new_sector,
4364 (unsigned long long)logical_sector);
4365
NeilBrownb5663ba2009-03-31 14:39:38 +11004366 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10004367 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004369 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004370 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f762006-03-27 01:18:15 -08004371 * stripe, so we must do the range check again.
4372 * Expansion could still move past after this
4373 * test, but as we are holding a reference to
4374 * 'sh', we know that if that happens,
4375 * STRIPE_EXPANDING will get set and the expansion
4376 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004377 */
4378 int must_retry = 0;
4379 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004380 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11004381 ? logical_sector >= conf->reshape_progress
4382 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004383 /* mismatch, need to try again */
4384 must_retry = 1;
4385 spin_unlock_irq(&conf->device_lock);
4386 if (must_retry) {
4387 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004388 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004389 goto retry;
4390 }
4391 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004392
Namhyung Kimffd96e32011-07-18 17:38:51 +10004393 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004394 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004395 logical_sector < mddev->suspend_hi) {
4396 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004397 /* As the suspend_* range is controlled by
4398 * userspace, we want an interruptible
4399 * wait.
4400 */
4401 flush_signals(current);
4402 prepare_to_wait(&conf->wait_for_overlap,
4403 &w, TASK_INTERRUPTIBLE);
4404 if (logical_sector >= mddev->suspend_lo &&
4405 logical_sector < mddev->suspend_hi)
4406 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004407 goto retry;
4408 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004409
4410 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004411 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004412 /* Stripe is busy expanding or
4413 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 * and wait a while
4415 */
NeilBrown482c0832011-04-18 18:25:42 +10004416 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 release_stripe(sh);
4418 schedule();
4419 goto retry;
4420 }
4421 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004422 set_bit(STRIPE_HANDLE, &sh->state);
4423 clear_bit(STRIPE_DELAYED, &sh->state);
NeilBrowna852d7b2012-09-19 12:48:30 +10004424 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004425 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4426 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10004427 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 } else {
4429 /* cannot get stripe for read-ahead, just give-up */
4430 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4431 finish_wait(&conf->wait_for_overlap, &w);
4432 break;
4433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004435
Shaohua Lie7836bd62012-07-19 16:01:31 +10004436 remaining = raid5_dec_bi_active_stripes(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004437 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438
NeilBrown16a53ec2006-06-26 00:27:38 -07004439 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004441
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07004442 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
4443 bi, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +10004444 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446}
4447
NeilBrownfd01b882011-10-11 16:47:53 +11004448static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004449
NeilBrownfd01b882011-10-11 16:47:53 +11004450static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451{
NeilBrown52c03292006-06-26 00:27:43 -07004452 /* reshaping is quite different to recovery/resync so it is
4453 * handled quite separately ... here.
4454 *
4455 * On each call to sync_request, we gather one chunk worth of
4456 * destination stripes and flag them as expanding.
4457 * Then we find all the source stripes and request reads.
4458 * As the reads complete, handle_stripe will copy the data
4459 * into the destination stripe and release that stripe.
4460 */
NeilBrownd1688a62011-10-11 16:49:52 +11004461 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004463 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004464 int raid_disks = conf->previous_raid_disks;
4465 int data_disks = raid_disks - conf->max_degraded;
4466 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004467 int i;
4468 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004469 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004470 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004471 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004472 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004473
NeilBrownfef9c612009-03-31 15:16:46 +11004474 if (sector_nr == 0) {
4475 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10004476 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004477 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4478 sector_nr = raid5_size(mddev, 0, 0)
4479 - conf->reshape_progress;
NeilBrown2c810cd2012-05-21 09:27:00 +10004480 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004481 conf->reshape_progress > 0)
4482 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004483 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004484 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004485 mddev->curr_resync_completed = sector_nr;
4486 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004487 *skipped = 1;
4488 return sector_nr;
4489 }
NeilBrown52c03292006-06-26 00:27:43 -07004490 }
4491
NeilBrown7a661382009-03-31 15:21:40 +11004492 /* We need to process a full chunk at a time.
4493 * If old and new chunk sizes differ, we need to process the
4494 * largest of these
4495 */
Andre Noll664e7c42009-06-18 08:45:27 +10004496 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4497 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004498 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004499 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004500
NeilBrownb5254dd2012-05-21 09:27:01 +10004501 /* We update the metadata at least every 10 seconds, or when
4502 * the data about to be copied would over-write the source of
4503 * the data at the front of the range. i.e. one new_stripe
4504 * along from reshape_progress new_maps to after where
4505 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004506 */
NeilBrownfef9c612009-03-31 15:16:46 +11004507 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004508 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004509 readpos = conf->reshape_progress;
4510 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004511 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004512 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10004513 if (mddev->reshape_backwards) {
NeilBrowned37d832009-05-27 21:39:05 +10004514 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004515 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004516 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004517 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004518 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004519 readpos -= min_t(sector_t, reshape_sectors, readpos);
4520 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004521 }
NeilBrown52c03292006-06-26 00:27:43 -07004522
NeilBrownb5254dd2012-05-21 09:27:01 +10004523 /* Having calculated the 'writepos' possibly use it
4524 * to set 'stripe_addr' which is where we will write to.
4525 */
4526 if (mddev->reshape_backwards) {
4527 BUG_ON(conf->reshape_progress == 0);
4528 stripe_addr = writepos;
4529 BUG_ON((mddev->dev_sectors &
4530 ~((sector_t)reshape_sectors - 1))
4531 - reshape_sectors - stripe_addr
4532 != sector_nr);
4533 } else {
4534 BUG_ON(writepos != sector_nr + reshape_sectors);
4535 stripe_addr = sector_nr;
4536 }
4537
NeilBrownc8f517c2009-03-31 15:28:40 +11004538 /* 'writepos' is the most advanced device address we might write.
4539 * 'readpos' is the least advanced device address we might read.
4540 * 'safepos' is the least address recorded in the metadata as having
4541 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10004542 * If there is a min_offset_diff, these are adjusted either by
4543 * increasing the safepos/readpos if diff is negative, or
4544 * increasing writepos if diff is positive.
4545 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11004546 * ensure safety in the face of a crash - that must be done by userspace
4547 * making a backup of the data. So in that case there is no particular
4548 * rush to update metadata.
4549 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4550 * update the metadata to advance 'safepos' to match 'readpos' so that
4551 * we can be safe in the event of a crash.
4552 * So we insist on updating metadata if safepos is behind writepos and
4553 * readpos is beyond writepos.
4554 * In any case, update the metadata every 10 seconds.
4555 * Maybe that number should be configurable, but I'm not sure it is
4556 * worth it.... maybe it could be a multiple of safemode_delay???
4557 */
NeilBrownb5254dd2012-05-21 09:27:01 +10004558 if (conf->min_offset_diff < 0) {
4559 safepos += -conf->min_offset_diff;
4560 readpos += -conf->min_offset_diff;
4561 } else
4562 writepos += conf->min_offset_diff;
4563
NeilBrown2c810cd2012-05-21 09:27:00 +10004564 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11004565 ? (safepos > writepos && readpos < writepos)
4566 : (safepos < writepos && readpos > writepos)) ||
4567 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004568 /* Cannot proceed until we've updated the superblock... */
4569 wait_event(conf->wait_for_overlap,
4570 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004571 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004572 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004573 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b422006-10-03 01:15:46 -07004574 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004575 md_wakeup_thread(mddev->thread);
NeilBrown850b2b422006-10-03 01:15:46 -07004576 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004577 kthread_should_stop());
4578 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004579 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004580 spin_unlock_irq(&conf->device_lock);
4581 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004582 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004583 }
4584
NeilBrownab69ae12009-03-31 15:26:47 +11004585 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004586 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004587 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004588 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004589 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004590 set_bit(STRIPE_EXPANDING, &sh->state);
4591 atomic_inc(&conf->reshape_stripes);
4592 /* If any of this stripe is beyond the end of the old
4593 * array, then we need to zero those blocks
4594 */
4595 for (j=sh->disks; j--;) {
4596 sector_t s;
4597 if (j == sh->pd_idx)
4598 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004599 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004600 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004601 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004602 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004603 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004604 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004605 continue;
4606 }
4607 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4608 set_bit(R5_Expanded, &sh->dev[j].flags);
4609 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4610 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004611 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004612 set_bit(STRIPE_EXPAND_READY, &sh->state);
4613 set_bit(STRIPE_HANDLE, &sh->state);
4614 }
NeilBrownab69ae12009-03-31 15:26:47 +11004615 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004616 }
4617 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004618 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11004619 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004620 else
NeilBrown7a661382009-03-31 15:21:40 +11004621 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004622 spin_unlock_irq(&conf->device_lock);
4623 /* Ok, those stripe are ready. We can start scheduling
4624 * reads on the source stripes.
4625 * The source stripes are determined by mapping the first and last
4626 * block on the destination stripes.
4627 */
NeilBrown52c03292006-06-26 00:27:43 -07004628 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004629 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004630 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004631 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004632 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004633 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004634 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004635 if (last_sector >= mddev->dev_sectors)
4636 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004637 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004638 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004639 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4640 set_bit(STRIPE_HANDLE, &sh->state);
4641 release_stripe(sh);
4642 first_sector += STRIPE_SECTORS;
4643 }
NeilBrownab69ae12009-03-31 15:26:47 +11004644 /* Now that the sources are clearly marked, we can release
4645 * the destination stripes
4646 */
4647 while (!list_empty(&stripes)) {
4648 sh = list_entry(stripes.next, struct stripe_head, lru);
4649 list_del_init(&sh->lru);
4650 release_stripe(sh);
4651 }
NeilBrownc6207272008-02-06 01:39:52 -08004652 /* If this takes us to the resync_max point where we have to pause,
4653 * then we need to write out the superblock.
4654 */
NeilBrown7a661382009-03-31 15:21:40 +11004655 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004656 if ((sector_nr - mddev->curr_resync_completed) * 2
4657 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004658 /* Cannot proceed until we've updated the superblock... */
4659 wait_event(conf->wait_for_overlap,
4660 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004661 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004662 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004663 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004664 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4665 md_wakeup_thread(mddev->thread);
4666 wait_event(mddev->sb_wait,
4667 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4668 || kthread_should_stop());
4669 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004670 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004671 spin_unlock_irq(&conf->device_lock);
4672 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004673 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004674 }
NeilBrown7a661382009-03-31 15:21:40 +11004675 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004676}
4677
4678/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004679static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004680{
NeilBrownd1688a62011-10-11 16:49:52 +11004681 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004682 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004683 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004684 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004685 int still_degraded = 0;
4686 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687
NeilBrown72626682005-09-09 16:23:54 -07004688 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004690
NeilBrown29269552006-03-27 01:18:10 -08004691 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4692 end_reshape(conf);
4693 return 0;
4694 }
NeilBrown72626682005-09-09 16:23:54 -07004695
4696 if (mddev->curr_resync < max_sector) /* aborted */
4697 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4698 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004699 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004700 conf->fullsync = 0;
4701 bitmap_close_sync(mddev->bitmap);
4702
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 return 0;
4704 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004705
NeilBrown64bd6602009-08-03 10:59:58 +10004706 /* Allow raid5_quiesce to complete */
4707 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4708
NeilBrown52c03292006-06-26 00:27:43 -07004709 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4710 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004711
NeilBrownc6207272008-02-06 01:39:52 -08004712 /* No need to check resync_max as we never do more than one
4713 * stripe, and as resync_max will always be on a chunk boundary,
4714 * if the check in md_do_sync didn't fire, there is no chance
4715 * of overstepping resync_max here
4716 */
4717
NeilBrown16a53ec2006-06-26 00:27:38 -07004718 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 * to resync, then assert that we are finished, because there is
4720 * nothing we can do.
4721 */
NeilBrown3285edf2006-06-26 00:27:55 -07004722 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004723 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004724 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004725 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 return rv;
4727 }
majianpeng6f608042013-04-24 11:42:41 +10004728 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
4729 !conf->fullsync &&
4730 !bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
4731 sync_blocks >= STRIPE_SECTORS) {
NeilBrown72626682005-09-09 16:23:54 -07004732 /* we can skip this block, and probably more */
4733 sync_blocks /= STRIPE_SECTORS;
4734 *skipped = 1;
4735 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737
NeilBrownb47490c2008-02-06 01:39:50 -08004738 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4739
NeilBrowna8c906c2009-06-09 14:39:59 +10004740 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004742 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004744 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004746 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004748 /* Need to check if array will still be degraded after recovery/resync
4749 * We don't need to check the 'failed' flag as when that gets set,
4750 * recovery aborts.
4751 */
NeilBrownf001a702009-06-09 14:30:31 +10004752 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004753 if (conf->disks[i].rdev == NULL)
4754 still_degraded = 1;
4755
4756 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4757
NeilBrown83206d62011-07-26 11:19:49 +10004758 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759
NeilBrown14425772009-10-16 15:55:25 +11004760 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 release_stripe(sh);
4762
4763 return STRIPE_SECTORS;
4764}
4765
NeilBrownd1688a62011-10-11 16:49:52 +11004766static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004767{
4768 /* We may not be able to submit a whole bio at once as there
4769 * may not be enough stripe_heads available.
4770 * We cannot pre-allocate enough stripe_heads as we may need
4771 * more than exist in the cache (if we allow ever large chunks).
4772 * So we do one stripe head at a time and record in
4773 * ->bi_hw_segments how many have been done.
4774 *
4775 * We *know* that this entire raid_bio is in one chunk, so
4776 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4777 */
4778 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004779 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004780 sector_t sector, logical_sector, last_sector;
4781 int scnt = 0;
4782 int remaining;
4783 int handled = 0;
4784
4785 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004786 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004787 0, &dd_idx, NULL);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004788 last_sector = bio_end_sector(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004789
4790 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004791 logical_sector += STRIPE_SECTORS,
4792 sector += STRIPE_SECTORS,
4793 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004794
Shaohua Lie7836bd62012-07-19 16:01:31 +10004795 if (scnt < raid5_bi_processed_stripes(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004796 /* already done this stripe */
4797 continue;
4798
NeilBrowna8c906c2009-06-09 14:39:59 +10004799 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004800
4801 if (!sh) {
4802 /* failed to get a stripe - must wait */
Shaohua Lie7836bd62012-07-19 16:01:31 +10004803 raid5_set_bi_processed_stripes(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004804 conf->retry_read_aligned = raid_bio;
4805 return handled;
4806 }
4807
Neil Brown387bb172007-02-08 14:20:29 -08004808 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4809 release_stripe(sh);
Shaohua Lie7836bd62012-07-19 16:01:31 +10004810 raid5_set_bi_processed_stripes(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004811 conf->retry_read_aligned = raid_bio;
4812 return handled;
4813 }
4814
majianpeng3f9e7c12012-07-31 10:04:21 +10004815 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07004816 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004817 release_stripe(sh);
4818 handled++;
4819 }
Shaohua Lie7836bd62012-07-19 16:01:31 +10004820 remaining = raid5_dec_bi_active_stripes(raid_bio);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07004821 if (remaining == 0) {
4822 trace_block_bio_complete(bdev_get_queue(raid_bio->bi_bdev),
4823 raid_bio, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +10004824 bio_endio(raid_bio, 0);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07004825 }
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004826 if (atomic_dec_and_test(&conf->active_aligned_reads))
4827 wake_up(&conf->wait_for_stripe);
4828 return handled;
4829}
4830
Shaohua Li46a06402012-08-02 08:33:15 +10004831#define MAX_STRIPE_BATCH 8
4832static int handle_active_stripes(struct r5conf *conf)
4833{
4834 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
4835 int i, batch_size = 0;
4836
4837 while (batch_size < MAX_STRIPE_BATCH &&
4838 (sh = __get_priority_stripe(conf)) != NULL)
4839 batch[batch_size++] = sh;
4840
4841 if (batch_size == 0)
4842 return batch_size;
4843 spin_unlock_irq(&conf->device_lock);
4844
4845 for (i = 0; i < batch_size; i++)
4846 handle_stripe(batch[i]);
4847
4848 cond_resched();
4849
4850 spin_lock_irq(&conf->device_lock);
4851 for (i = 0; i < batch_size; i++)
4852 __release_stripe(conf, batch[i]);
4853 return batch_size;
4854}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004855
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856/*
4857 * This is our raid5 kernel thread.
4858 *
4859 * We scan the hash table for stripes which can be handled now.
4860 * During the scan, completed stripes are saved for us by the interrupt
4861 * handler, so that they will not have to wait for our next wakeup.
4862 */
Shaohua Li4ed87312012-10-11 13:34:00 +11004863static void raid5d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864{
Shaohua Li4ed87312012-10-11 13:34:00 +11004865 struct mddev *mddev = thread->mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11004866 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004868 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869
Dan Williams45b42332007-07-09 11:56:43 -07004870 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871
4872 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004874 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875 handled = 0;
4876 spin_lock_irq(&conf->device_lock);
4877 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004878 struct bio *bio;
Shaohua Li773ca822013-08-27 17:50:39 +08004879 int batch_size, released;
4880
4881 released = release_stripe_list(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882
NeilBrown0021b7b2012-07-31 09:08:14 +02004883 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10004884 !list_empty(&conf->bitmap_list)) {
4885 /* Now is a good time to flush some bitmap updates */
4886 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004887 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004888 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004889 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004890 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004891 activate_bit_delay(conf);
4892 }
NeilBrown0021b7b2012-07-31 09:08:14 +02004893 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004894
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004895 while ((bio = remove_bio_from_retry(conf))) {
4896 int ok;
4897 spin_unlock_irq(&conf->device_lock);
4898 ok = retry_aligned_read(conf, bio);
4899 spin_lock_irq(&conf->device_lock);
4900 if (!ok)
4901 break;
4902 handled++;
4903 }
4904
Shaohua Li46a06402012-08-02 08:33:15 +10004905 batch_size = handle_active_stripes(conf);
Shaohua Li773ca822013-08-27 17:50:39 +08004906 if (!batch_size && !released)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 break;
Shaohua Li46a06402012-08-02 08:33:15 +10004908 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909
Shaohua Li46a06402012-08-02 08:33:15 +10004910 if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) {
4911 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10004912 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10004913 spin_lock_irq(&conf->device_lock);
4914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 }
Dan Williams45b42332007-07-09 11:56:43 -07004916 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917
4918 spin_unlock_irq(&conf->device_lock);
4919
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004920 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004921 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922
Dan Williams45b42332007-07-09 11:56:43 -07004923 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924}
4925
NeilBrown3f294f42005-11-08 21:39:25 -08004926static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004927raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004928{
NeilBrownd1688a62011-10-11 16:49:52 +11004929 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004930 if (conf)
4931 return sprintf(page, "%d\n", conf->max_nr_stripes);
4932 else
4933 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004934}
4935
NeilBrownc41d4ac2010-06-01 19:37:24 +10004936int
NeilBrownfd01b882011-10-11 16:47:53 +11004937raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004938{
NeilBrownd1688a62011-10-11 16:49:52 +11004939 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004940 int err;
4941
4942 if (size <= 16 || size > 32768)
4943 return -EINVAL;
4944 while (size < conf->max_nr_stripes) {
4945 if (drop_one_stripe(conf))
4946 conf->max_nr_stripes--;
4947 else
4948 break;
4949 }
4950 err = md_allow_write(mddev);
4951 if (err)
4952 return err;
4953 while (size > conf->max_nr_stripes) {
4954 if (grow_one_stripe(conf))
4955 conf->max_nr_stripes++;
4956 else break;
4957 }
4958 return 0;
4959}
4960EXPORT_SYMBOL(raid5_set_cache_size);
4961
NeilBrown3f294f42005-11-08 21:39:25 -08004962static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004963raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004964{
NeilBrownd1688a62011-10-11 16:49:52 +11004965 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004966 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004967 int err;
4968
NeilBrown3f294f42005-11-08 21:39:25 -08004969 if (len >= PAGE_SIZE)
4970 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004971 if (!conf)
4972 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004973
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004974 if (kstrtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004975 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004976 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004977 if (err)
4978 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004979 return len;
4980}
NeilBrown007583c2005-11-08 21:39:30 -08004981
NeilBrown96de1e62005-11-08 21:39:39 -08004982static struct md_sysfs_entry
4983raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4984 raid5_show_stripe_cache_size,
4985 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004986
4987static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004988raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004989{
NeilBrownd1688a62011-10-11 16:49:52 +11004990 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004991 if (conf)
4992 return sprintf(page, "%d\n", conf->bypass_threshold);
4993 else
4994 return 0;
4995}
4996
4997static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004998raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004999{
NeilBrownd1688a62011-10-11 16:49:52 +11005000 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07005001 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005002 if (len >= PAGE_SIZE)
5003 return -EINVAL;
5004 if (!conf)
5005 return -ENODEV;
5006
Jingoo Hanb29bebd2013-06-01 16:15:16 +09005007 if (kstrtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005008 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07005009 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005010 return -EINVAL;
5011 conf->bypass_threshold = new;
5012 return len;
5013}
5014
5015static struct md_sysfs_entry
5016raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
5017 S_IRUGO | S_IWUSR,
5018 raid5_show_preread_threshold,
5019 raid5_store_preread_threshold);
5020
5021static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005022stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08005023{
NeilBrownd1688a62011-10-11 16:49:52 +11005024 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08005025 if (conf)
5026 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
5027 else
5028 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08005029}
5030
NeilBrown96de1e62005-11-08 21:39:39 -08005031static struct md_sysfs_entry
5032raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08005033
NeilBrown007583c2005-11-08 21:39:30 -08005034static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08005035 &raid5_stripecache_size.attr,
5036 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005037 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08005038 NULL,
5039};
NeilBrown007583c2005-11-08 21:39:30 -08005040static struct attribute_group raid5_attrs_group = {
5041 .name = NULL,
5042 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08005043};
5044
Dan Williams80c3a6c2009-03-17 18:10:40 -07005045static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11005046raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07005047{
NeilBrownd1688a62011-10-11 16:49:52 +11005048 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07005049
5050 if (!sectors)
5051 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005052 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11005053 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11005054 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07005055
Andre Noll9d8f0362009-06-18 08:45:01 +10005056 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10005057 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07005058 return sectors * (raid_disks - conf->max_degraded);
5059}
5060
NeilBrownd1688a62011-10-11 16:49:52 +11005061static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07005062{
5063 struct raid5_percpu *percpu;
5064 unsigned long cpu;
5065
5066 if (!conf->percpu)
5067 return;
5068
5069 get_online_cpus();
5070 for_each_possible_cpu(cpu) {
5071 percpu = per_cpu_ptr(conf->percpu, cpu);
5072 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005073 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005074 }
5075#ifdef CONFIG_HOTPLUG_CPU
5076 unregister_cpu_notifier(&conf->cpu_notify);
5077#endif
5078 put_online_cpus();
5079
5080 free_percpu(conf->percpu);
5081}
5082
NeilBrownd1688a62011-10-11 16:49:52 +11005083static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10005084{
5085 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07005086 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10005087 kfree(conf->disks);
5088 kfree(conf->stripe_hashtbl);
5089 kfree(conf);
5090}
5091
Dan Williams36d1c642009-07-14 11:48:22 -07005092#ifdef CONFIG_HOTPLUG_CPU
5093static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
5094 void *hcpu)
5095{
NeilBrownd1688a62011-10-11 16:49:52 +11005096 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07005097 long cpu = (long)hcpu;
5098 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
5099
5100 switch (action) {
5101 case CPU_UP_PREPARE:
5102 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07005103 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07005104 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005105 if (!percpu->scribble)
5106 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
5107
5108 if (!percpu->scribble ||
5109 (conf->level == 6 && !percpu->spare_page)) {
5110 safe_put_page(percpu->spare_page);
5111 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005112 pr_err("%s: failed memory allocation for cpu%ld\n",
5113 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07005114 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07005115 }
5116 break;
5117 case CPU_DEAD:
5118 case CPU_DEAD_FROZEN:
5119 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005120 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005121 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07005122 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07005123 break;
5124 default:
5125 break;
5126 }
5127 return NOTIFY_OK;
5128}
5129#endif
5130
NeilBrownd1688a62011-10-11 16:49:52 +11005131static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07005132{
5133 unsigned long cpu;
5134 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09005135 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07005136 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07005137 int err;
5138
Dan Williams36d1c642009-07-14 11:48:22 -07005139 allcpus = alloc_percpu(struct raid5_percpu);
5140 if (!allcpus)
5141 return -ENOMEM;
5142 conf->percpu = allcpus;
5143
5144 get_online_cpus();
5145 err = 0;
5146 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07005147 if (conf->level == 6) {
5148 spare_page = alloc_page(GFP_KERNEL);
5149 if (!spare_page) {
5150 err = -ENOMEM;
5151 break;
5152 }
5153 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
5154 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11005155 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005156 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07005157 err = -ENOMEM;
5158 break;
5159 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07005160 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07005161 }
5162#ifdef CONFIG_HOTPLUG_CPU
5163 conf->cpu_notify.notifier_call = raid456_cpu_notify;
5164 conf->cpu_notify.priority = 0;
5165 if (err == 0)
5166 err = register_cpu_notifier(&conf->cpu_notify);
5167#endif
5168 put_online_cpus();
5169
5170 return err;
5171}
5172
NeilBrownd1688a62011-10-11 16:49:52 +11005173static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174{
NeilBrownd1688a62011-10-11 16:49:52 +11005175 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005176 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11005177 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10005179 char pers_name[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180
NeilBrown91adb562009-03-31 14:39:39 +11005181 if (mddev->new_level != 5
5182 && mddev->new_level != 4
5183 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10005184 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005185 mdname(mddev), mddev->new_level);
5186 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 }
NeilBrown91adb562009-03-31 14:39:39 +11005188 if ((mddev->new_level == 5
5189 && !algorithm_valid_raid5(mddev->new_layout)) ||
5190 (mddev->new_level == 6
5191 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005192 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11005193 mdname(mddev), mddev->new_layout);
5194 return ERR_PTR(-EIO);
5195 }
5196 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10005197 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005198 mdname(mddev), mddev->raid_disks);
5199 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11005200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201
Andre Noll664e7c42009-06-18 08:45:27 +10005202 if (!mddev->new_chunk_sectors ||
5203 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
5204 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005205 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
5206 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11005207 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11005208 }
5209
NeilBrownd1688a62011-10-11 16:49:52 +11005210 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11005211 if (conf == NULL)
5212 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11005213 spin_lock_init(&conf->device_lock);
5214 init_waitqueue_head(&conf->wait_for_stripe);
5215 init_waitqueue_head(&conf->wait_for_overlap);
5216 INIT_LIST_HEAD(&conf->handle_list);
5217 INIT_LIST_HEAD(&conf->hold_list);
5218 INIT_LIST_HEAD(&conf->delayed_list);
5219 INIT_LIST_HEAD(&conf->bitmap_list);
5220 INIT_LIST_HEAD(&conf->inactive_list);
Shaohua Li773ca822013-08-27 17:50:39 +08005221 init_llist_head(&conf->released_stripes);
Dan Williamsf5efd452009-10-16 15:55:38 +11005222 atomic_set(&conf->active_stripes, 0);
5223 atomic_set(&conf->preread_active_stripes, 0);
5224 atomic_set(&conf->active_aligned_reads, 0);
5225 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11005226 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11005227
5228 conf->raid_disks = mddev->raid_disks;
5229 if (mddev->reshape_position == MaxSector)
5230 conf->previous_raid_disks = mddev->raid_disks;
5231 else
5232 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005233 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
5234 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11005235
NeilBrown5e5e3e72009-10-16 16:35:30 +11005236 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11005237 GFP_KERNEL);
5238 if (!conf->disks)
5239 goto abort;
5240
5241 conf->mddev = mddev;
5242
5243 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
5244 goto abort;
5245
Dan Williams36d1c642009-07-14 11:48:22 -07005246 conf->level = mddev->new_level;
5247 if (raid5_alloc_percpu(conf) != 0)
5248 goto abort;
5249
NeilBrown0c55e022010-05-03 14:09:02 +10005250 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11005251
NeilBrowndafb20f2012-03-19 12:46:39 +11005252 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11005253 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005254 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11005255 || raid_disk < 0)
5256 continue;
5257 disk = conf->disks + raid_disk;
5258
NeilBrown17045f52011-12-23 10:17:53 +11005259 if (test_bit(Replacement, &rdev->flags)) {
5260 if (disk->replacement)
5261 goto abort;
5262 disk->replacement = rdev;
5263 } else {
5264 if (disk->rdev)
5265 goto abort;
5266 disk->rdev = rdev;
5267 }
NeilBrown91adb562009-03-31 14:39:39 +11005268
5269 if (test_bit(In_sync, &rdev->flags)) {
5270 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10005271 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
5272 " disk %d\n",
5273 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05005274 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11005275 /* Cannot rely on bitmap to complete recovery */
5276 conf->fullsync = 1;
5277 }
5278
Andre Noll09c9e5f2009-06-18 08:45:55 +10005279 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11005280 conf->level = mddev->new_level;
5281 if (conf->level == 6)
5282 conf->max_degraded = 2;
5283 else
5284 conf->max_degraded = 1;
5285 conf->algorithm = mddev->new_layout;
5286 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11005287 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11005288 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10005289 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11005290 conf->prev_algo = mddev->layout;
5291 }
NeilBrown91adb562009-03-31 14:39:39 +11005292
5293 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11005294 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11005295 if (grow_stripes(conf, conf->max_nr_stripes)) {
5296 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005297 "md/raid:%s: couldn't allocate %dkB for buffers\n",
5298 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005299 goto abort;
5300 } else
NeilBrown0c55e022010-05-03 14:09:02 +10005301 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
5302 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005303
NeilBrown02326052012-07-03 15:56:52 +10005304 sprintf(pers_name, "raid%d", mddev->new_level);
5305 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11005306 if (!conf->thread) {
5307 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005308 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11005309 mdname(mddev));
5310 goto abort;
5311 }
5312
5313 return conf;
5314
5315 abort:
5316 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10005317 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11005318 return ERR_PTR(-EIO);
5319 } else
5320 return ERR_PTR(-ENOMEM);
5321}
5322
NeilBrownc148ffd2009-11-13 17:47:00 +11005323
5324static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
5325{
5326 switch (algo) {
5327 case ALGORITHM_PARITY_0:
5328 if (raid_disk < max_degraded)
5329 return 1;
5330 break;
5331 case ALGORITHM_PARITY_N:
5332 if (raid_disk >= raid_disks - max_degraded)
5333 return 1;
5334 break;
5335 case ALGORITHM_PARITY_0_6:
5336 if (raid_disk == 0 ||
5337 raid_disk == raid_disks - 1)
5338 return 1;
5339 break;
5340 case ALGORITHM_LEFT_ASYMMETRIC_6:
5341 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5342 case ALGORITHM_LEFT_SYMMETRIC_6:
5343 case ALGORITHM_RIGHT_SYMMETRIC_6:
5344 if (raid_disk == raid_disks - 1)
5345 return 1;
5346 }
5347 return 0;
5348}
5349
NeilBrownfd01b882011-10-11 16:47:53 +11005350static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11005351{
NeilBrownd1688a62011-10-11 16:49:52 +11005352 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10005353 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11005354 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11005355 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11005356 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11005357 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10005358 long long min_offset_diff = 0;
5359 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11005360
Andre Noll8c6ac8682009-06-18 08:48:06 +10005361 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10005362 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac8682009-06-18 08:48:06 +10005363 " -- starting background reconstruction\n",
5364 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10005365
5366 rdev_for_each(rdev, mddev) {
5367 long long diff;
5368 if (rdev->raid_disk < 0)
5369 continue;
5370 diff = (rdev->new_data_offset - rdev->data_offset);
5371 if (first) {
5372 min_offset_diff = diff;
5373 first = 0;
5374 } else if (mddev->reshape_backwards &&
5375 diff < min_offset_diff)
5376 min_offset_diff = diff;
5377 else if (!mddev->reshape_backwards &&
5378 diff > min_offset_diff)
5379 min_offset_diff = diff;
5380 }
5381
NeilBrownf6705572006-03-27 01:18:11 -08005382 if (mddev->reshape_position != MaxSector) {
5383 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10005384 * Difficulties arise if the stripe we would write to
5385 * next is at or after the stripe we would read from next.
5386 * For a reshape that changes the number of devices, this
5387 * is only possible for a very short time, and mdadm makes
5388 * sure that time appears to have past before assembling
5389 * the array. So we fail if that time hasn't passed.
5390 * For a reshape that keeps the number of devices the same
5391 * mdadm must be monitoring the reshape can keeping the
5392 * critical areas read-only and backed up. It will start
5393 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08005394 */
5395 sector_t here_new, here_old;
5396 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11005397 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08005398
NeilBrown88ce4932009-03-31 15:24:23 +11005399 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10005400 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08005401 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08005402 mdname(mddev));
5403 return -EINVAL;
5404 }
NeilBrownf6705572006-03-27 01:18:11 -08005405 old_disks = mddev->raid_disks - mddev->delta_disks;
5406 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08005407 * further up in new geometry must map after here in old
5408 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08005409 */
5410 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10005411 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005412 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005413 printk(KERN_ERR "md/raid:%s: reshape_position not "
5414 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005415 return -EINVAL;
5416 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005417 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005418 /* here_new is the stripe we will write to */
5419 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10005420 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005421 (old_disks-max_degraded));
5422 /* here_old is the first stripe that we might need to read
5423 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10005424 if (mddev->delta_disks == 0) {
NeilBrownb5254dd2012-05-21 09:27:01 +10005425 if ((here_new * mddev->new_chunk_sectors !=
5426 here_old * mddev->chunk_sectors)) {
5427 printk(KERN_ERR "md/raid:%s: reshape position is"
5428 " confused - aborting\n", mdname(mddev));
5429 return -EINVAL;
5430 }
NeilBrown67ac6012009-08-13 10:06:24 +10005431 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10005432 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10005433 * and taking constant backups.
5434 * mdadm always starts a situation like this in
5435 * readonly mode so it can take control before
5436 * allowing any writes. So just check for that.
5437 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005438 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
5439 abs(min_offset_diff) >= mddev->new_chunk_sectors)
5440 /* not really in-place - so OK */;
5441 else if (mddev->ro == 0) {
5442 printk(KERN_ERR "md/raid:%s: in-place reshape "
5443 "must be started in read-only mode "
5444 "- aborting\n",
NeilBrown0c55e022010-05-03 14:09:02 +10005445 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005446 return -EINVAL;
5447 }
NeilBrown2c810cd2012-05-21 09:27:00 +10005448 } else if (mddev->reshape_backwards
NeilBrownb5254dd2012-05-21 09:27:01 +10005449 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
NeilBrown67ac6012009-08-13 10:06:24 +10005450 here_old * mddev->chunk_sectors)
5451 : (here_new * mddev->new_chunk_sectors >=
NeilBrownb5254dd2012-05-21 09:27:01 +10005452 here_old * mddev->chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08005453 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005454 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5455 "auto-recovery - aborting.\n",
5456 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005457 return -EINVAL;
5458 }
NeilBrown0c55e022010-05-03 14:09:02 +10005459 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5460 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005461 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005462 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005463 BUG_ON(mddev->level != mddev->new_level);
5464 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005465 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005466 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005467 }
5468
NeilBrown245f46c2009-03-31 14:39:39 +11005469 if (mddev->private == NULL)
5470 conf = setup_conf(mddev);
5471 else
5472 conf = mddev->private;
5473
NeilBrown91adb562009-03-31 14:39:39 +11005474 if (IS_ERR(conf))
5475 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005476
NeilBrownb5254dd2012-05-21 09:27:01 +10005477 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11005478 mddev->thread = conf->thread;
5479 conf->thread = NULL;
5480 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481
NeilBrown17045f52011-12-23 10:17:53 +11005482 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5483 i++) {
5484 rdev = conf->disks[i].rdev;
5485 if (!rdev && conf->disks[i].replacement) {
5486 /* The replacement is all we have yet */
5487 rdev = conf->disks[i].replacement;
5488 conf->disks[i].replacement = NULL;
5489 clear_bit(Replacement, &rdev->flags);
5490 conf->disks[i].rdev = rdev;
5491 }
5492 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11005493 continue;
NeilBrown17045f52011-12-23 10:17:53 +11005494 if (conf->disks[i].replacement &&
5495 conf->reshape_progress != MaxSector) {
5496 /* replacements and reshape simply do not mix. */
5497 printk(KERN_ERR "md: cannot handle concurrent "
5498 "replacement and reshape.\n");
5499 goto abort;
5500 }
NeilBrown2f115882010-06-17 17:41:03 +10005501 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005502 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005503 continue;
5504 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005505 /* This disc is not fully in-sync. However if it
5506 * just stored parity (beyond the recovery_offset),
5507 * when we don't need to be concerned about the
5508 * array being dirty.
5509 * When reshape goes 'backwards', we never have
5510 * partially completed devices, so we only need
5511 * to worry about reshape going forwards.
5512 */
5513 /* Hack because v0.91 doesn't store recovery_offset properly. */
5514 if (mddev->major_version == 0 &&
5515 mddev->minor_version > 90)
5516 rdev->recovery_offset = reshape_offset;
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07005517
NeilBrownc148ffd2009-11-13 17:47:00 +11005518 if (rdev->recovery_offset < reshape_offset) {
5519 /* We need to check old and new layout */
5520 if (!only_parity(rdev->raid_disk,
5521 conf->algorithm,
5522 conf->raid_disks,
5523 conf->max_degraded))
5524 continue;
5525 }
5526 if (!only_parity(rdev->raid_disk,
5527 conf->prev_algo,
5528 conf->previous_raid_disks,
5529 conf->max_degraded))
5530 continue;
5531 dirty_parity_disks++;
5532 }
NeilBrown91adb562009-03-31 14:39:39 +11005533
NeilBrown17045f52011-12-23 10:17:53 +11005534 /*
5535 * 0 for a fully functional array, 1 or 2 for a degraded array.
5536 */
NeilBrown908f4fb2011-12-23 10:17:50 +11005537 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538
NeilBrown674806d2010-06-16 17:17:53 +10005539 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005540 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07005542 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 goto abort;
5544 }
5545
NeilBrown91adb562009-03-31 14:39:39 +11005546 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10005547 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11005548 mddev->resync_max_sectors = mddev->dev_sectors;
5549
NeilBrownc148ffd2009-11-13 17:47:00 +11005550 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005552 if (mddev->ok_start_degraded)
5553 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10005554 "md/raid:%s: starting dirty degraded array"
5555 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005556 mdname(mddev));
5557 else {
5558 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005559 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005560 mdname(mddev));
5561 goto abort;
5562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563 }
5564
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10005566 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5567 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11005568 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5569 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 else
NeilBrown0c55e022010-05-03 14:09:02 +10005571 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5572 " out of %d devices, algorithm %d\n",
5573 mdname(mddev), conf->level,
5574 mddev->raid_disks - mddev->degraded,
5575 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576
5577 print_raid5_conf(conf);
5578
NeilBrownfef9c612009-03-31 15:16:46 +11005579 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11005580 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005581 atomic_set(&conf->reshape_stripes, 0);
5582 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5583 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5584 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5585 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5586 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005587 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005588 }
5589
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590
5591 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005592 if (mddev->to_remove == &raid5_attrs_group)
5593 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005594 else if (mddev->kobj.sd &&
5595 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005596 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10005597 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08005598 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10005599 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5600
5601 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005602 int chunk_size;
Shaohua Li620125f2012-10-11 13:49:05 +11005603 bool discard_supported = true;
NeilBrown4a5add42010-06-01 19:37:28 +10005604 /* read-ahead size must cover two whole stripes, which
5605 * is 2 * (datadisks) * chunksize where 'n' is the
5606 * number of raid devices
5607 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5609 int stripe = data_disks *
5610 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5611 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5612 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10005613
5614 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005615
5616 mddev->queue->backing_dev_info.congested_data = mddev;
5617 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10005618
5619 chunk_size = mddev->chunk_sectors << 9;
5620 blk_queue_io_min(mddev->queue, chunk_size);
5621 blk_queue_io_opt(mddev->queue, chunk_size *
5622 (conf->raid_disks - conf->max_degraded));
Shaohua Li620125f2012-10-11 13:49:05 +11005623 /*
5624 * We can only discard a whole stripe. It doesn't make sense to
5625 * discard data disk but write parity disk
5626 */
5627 stripe = stripe * PAGE_SIZE;
NeilBrown4ac68752012-11-19 13:11:26 +11005628 /* Round up to power of 2, as discard handling
5629 * currently assumes that */
5630 while ((stripe-1) & stripe)
5631 stripe = (stripe | (stripe-1)) + 1;
Shaohua Li620125f2012-10-11 13:49:05 +11005632 mddev->queue->limits.discard_alignment = stripe;
5633 mddev->queue->limits.discard_granularity = stripe;
5634 /*
5635 * unaligned part of discard request will be ignored, so can't
5636 * guarantee discard_zerors_data
5637 */
5638 mddev->queue->limits.discard_zeroes_data = 0;
NeilBrown9f7c2222010-07-26 12:04:13 +10005639
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07005640 blk_queue_max_write_same_sectors(mddev->queue, 0);
5641
NeilBrown05616be2012-05-21 09:27:00 +10005642 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005643 disk_stack_limits(mddev->gendisk, rdev->bdev,
5644 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10005645 disk_stack_limits(mddev->gendisk, rdev->bdev,
5646 rdev->new_data_offset << 9);
Shaohua Li620125f2012-10-11 13:49:05 +11005647 /*
5648 * discard_zeroes_data is required, otherwise data
5649 * could be lost. Consider a scenario: discard a stripe
5650 * (the stripe could be inconsistent if
5651 * discard_zeroes_data is 0); write one disk of the
5652 * stripe (the stripe could be inconsistent again
5653 * depending on which disks are used to calculate
5654 * parity); the disk is broken; The stripe data of this
5655 * disk is lost.
5656 */
5657 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
5658 !bdev_get_queue(rdev->bdev)->
5659 limits.discard_zeroes_data)
5660 discard_supported = false;
NeilBrown05616be2012-05-21 09:27:00 +10005661 }
Shaohua Li620125f2012-10-11 13:49:05 +11005662
5663 if (discard_supported &&
5664 mddev->queue->limits.max_discard_sectors >= stripe &&
5665 mddev->queue->limits.discard_granularity >= stripe)
5666 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
5667 mddev->queue);
5668 else
5669 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
5670 mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 }
5672
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 return 0;
5674abort:
NeilBrown01f96c02011-09-21 15:30:20 +10005675 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11005676 print_raid5_conf(conf);
5677 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005679 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 return -EIO;
5681}
5682
NeilBrownfd01b882011-10-11 16:47:53 +11005683static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684{
NeilBrownd1688a62011-10-11 16:49:52 +11005685 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686
NeilBrown01f96c02011-09-21 15:30:20 +10005687 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005688 if (mddev->queue)
5689 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10005690 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005691 mddev->private = NULL;
5692 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693 return 0;
5694}
5695
NeilBrownfd01b882011-10-11 16:47:53 +11005696static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697{
NeilBrownd1688a62011-10-11 16:49:52 +11005698 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 int i;
5700
Andre Noll9d8f0362009-06-18 08:45:01 +10005701 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5702 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005703 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 for (i = 0; i < conf->raid_disks; i++)
5705 seq_printf (seq, "%s",
5706 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005707 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709}
5710
NeilBrownd1688a62011-10-11 16:49:52 +11005711static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712{
5713 int i;
5714 struct disk_info *tmp;
5715
NeilBrown0c55e022010-05-03 14:09:02 +10005716 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 if (!conf) {
5718 printk("(conf==NULL)\n");
5719 return;
5720 }
NeilBrown0c55e022010-05-03 14:09:02 +10005721 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5722 conf->raid_disks,
5723 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724
5725 for (i = 0; i < conf->raid_disks; i++) {
5726 char b[BDEVNAME_SIZE];
5727 tmp = conf->disks + i;
5728 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005729 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5730 i, !test_bit(Faulty, &tmp->rdev->flags),
5731 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732 }
5733}
5734
NeilBrownfd01b882011-10-11 16:47:53 +11005735static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736{
5737 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005738 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005740 int count = 0;
5741 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742
5743 for (i = 0; i < conf->raid_disks; i++) {
5744 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11005745 if (tmp->replacement
5746 && tmp->replacement->recovery_offset == MaxSector
5747 && !test_bit(Faulty, &tmp->replacement->flags)
5748 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
5749 /* Replacement has just become active. */
5750 if (!tmp->rdev
5751 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
5752 count++;
5753 if (tmp->rdev) {
5754 /* Replaced device not technically faulty,
5755 * but we need to be sure it gets removed
5756 * and never re-added.
5757 */
5758 set_bit(Faulty, &tmp->rdev->flags);
5759 sysfs_notify_dirent_safe(
5760 tmp->rdev->sysfs_state);
5761 }
5762 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
5763 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005764 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005765 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005766 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005767 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005768 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 }
5770 }
NeilBrown6b965622010-08-18 11:56:59 +10005771 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005772 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005773 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005775 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776}
5777
NeilBrownb8321b62011-12-23 10:17:51 +11005778static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779{
NeilBrownd1688a62011-10-11 16:49:52 +11005780 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11005782 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11005783 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784 struct disk_info *p = conf->disks + number;
5785
5786 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11005787 if (rdev == p->rdev)
5788 rdevp = &p->rdev;
5789 else if (rdev == p->replacement)
5790 rdevp = &p->replacement;
5791 else
5792 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11005793
NeilBrown657e3e42011-12-23 10:17:52 +11005794 if (number >= conf->raid_disks &&
5795 conf->reshape_progress == MaxSector)
5796 clear_bit(In_sync, &rdev->flags);
5797
5798 if (test_bit(In_sync, &rdev->flags) ||
5799 atomic_read(&rdev->nr_pending)) {
5800 err = -EBUSY;
5801 goto abort;
5802 }
5803 /* Only remove non-faulty devices if recovery
5804 * isn't possible.
5805 */
5806 if (!test_bit(Faulty, &rdev->flags) &&
5807 mddev->recovery_disabled != conf->recovery_disabled &&
5808 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11005809 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11005810 number < conf->raid_disks) {
5811 err = -EBUSY;
5812 goto abort;
5813 }
5814 *rdevp = NULL;
5815 synchronize_rcu();
5816 if (atomic_read(&rdev->nr_pending)) {
5817 /* lost the race, try later */
5818 err = -EBUSY;
5819 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11005820 } else if (p->replacement) {
5821 /* We must have just cleared 'rdev' */
5822 p->rdev = p->replacement;
5823 clear_bit(Replacement, &p->replacement->flags);
5824 smp_mb(); /* Make sure other CPUs may see both as identical
5825 * but will never see neither - if they are careful
5826 */
5827 p->replacement = NULL;
5828 clear_bit(WantReplacement, &rdev->flags);
5829 } else
5830 /* We might have just removed the Replacement as faulty-
5831 * clear the bit just in case
5832 */
5833 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834abort:
5835
5836 print_raid5_conf(conf);
5837 return err;
5838}
5839
NeilBrownfd01b882011-10-11 16:47:53 +11005840static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841{
NeilBrownd1688a62011-10-11 16:49:52 +11005842 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005843 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844 int disk;
5845 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005846 int first = 0;
5847 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848
NeilBrown7f0da592011-07-28 11:39:22 +10005849 if (mddev->recovery_disabled == conf->recovery_disabled)
5850 return -EBUSY;
5851
NeilBrowndc10c642012-03-19 12:46:37 +11005852 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005854 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855
Neil Brown6c2fce22008-06-28 08:31:31 +10005856 if (rdev->raid_disk >= 0)
5857 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858
5859 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005860 * find the disk ... but prefer rdev->saved_raid_disk
5861 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005863 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005864 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005865 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10005866 first = rdev->saved_raid_disk;
5867
5868 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11005869 p = conf->disks + disk;
5870 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005871 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005873 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005874 if (rdev->saved_raid_disk != disk)
5875 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005876 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10005877 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005879 }
5880 for (disk = first; disk <= last; disk++) {
5881 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11005882 if (test_bit(WantReplacement, &p->rdev->flags) &&
5883 p->replacement == NULL) {
5884 clear_bit(In_sync, &rdev->flags);
5885 set_bit(Replacement, &rdev->flags);
5886 rdev->raid_disk = disk;
5887 err = 0;
5888 conf->fullsync = 1;
5889 rcu_assign_pointer(p->replacement, rdev);
5890 break;
5891 }
5892 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005893out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005895 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896}
5897
NeilBrownfd01b882011-10-11 16:47:53 +11005898static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899{
5900 /* no resync is happening, and there is enough space
5901 * on all devices, so we can resize.
5902 * We need to make sure resync covers any new space.
5903 * If the array is shrinking we should possibly wait until
5904 * any io in the removed space completes, but it hardly seems
5905 * worth it.
5906 */
NeilBrowna4a61252012-05-22 13:55:27 +10005907 sector_t newsize;
Andre Noll9d8f0362009-06-18 08:45:01 +10005908 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10005909 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
5910 if (mddev->external_size &&
5911 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11005912 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10005913 if (mddev->bitmap) {
5914 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
5915 if (ret)
5916 return ret;
5917 }
5918 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10005919 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005920 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005921 if (sectors > mddev->dev_sectors &&
5922 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005923 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5925 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005926 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005927 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928 return 0;
5929}
5930
NeilBrownfd01b882011-10-11 16:47:53 +11005931static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005932{
5933 /* Can only proceed if there are plenty of stripe_heads.
5934 * We need a minimum of one full stripe,, and for sensible progress
5935 * it is best to have about 4 times that.
5936 * If we require 4 times, then the default 256 4K stripe_heads will
5937 * allow for chunk sizes up to 256K, which is probably OK.
5938 * If the chunk size is greater, user-space should request more
5939 * stripe_heads first.
5940 */
NeilBrownd1688a62011-10-11 16:49:52 +11005941 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005942 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5943 > conf->max_nr_stripes ||
5944 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5945 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005946 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5947 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005948 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5949 / STRIPE_SIZE)*4);
5950 return 0;
5951 }
5952 return 1;
5953}
5954
NeilBrownfd01b882011-10-11 16:47:53 +11005955static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005956{
NeilBrownd1688a62011-10-11 16:49:52 +11005957 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005958
NeilBrown88ce4932009-03-31 15:24:23 +11005959 if (mddev->delta_disks == 0 &&
5960 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005961 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005962 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10005963 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005964 return -EINVAL;
NeilBrownfdcfbbb2013-07-04 16:38:16 +10005965 if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) {
NeilBrownec32a2b2009-03-31 15:17:38 +11005966 /* We might be able to shrink, but the devices must
5967 * be made bigger first.
5968 * For raid6, 4 is the minimum size.
5969 * Otherwise 2 is the minimum
5970 */
5971 int min = 2;
5972 if (mddev->level == 6)
5973 min = 4;
5974 if (mddev->raid_disks + mddev->delta_disks < min)
5975 return -EINVAL;
5976 }
NeilBrown29269552006-03-27 01:18:10 -08005977
NeilBrown01ee22b2009-06-18 08:47:20 +10005978 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005979 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005980
NeilBrowne56108d62012-10-11 14:24:13 +11005981 return resize_stripes(conf, (conf->previous_raid_disks
5982 + mddev->delta_disks));
NeilBrown63c70c42006-03-27 01:18:13 -08005983}
5984
NeilBrownfd01b882011-10-11 16:47:53 +11005985static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005986{
NeilBrownd1688a62011-10-11 16:49:52 +11005987 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005988 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005989 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005990 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005991
NeilBrownf4168852007-02-28 20:11:53 -08005992 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005993 return -EBUSY;
5994
NeilBrown01ee22b2009-06-18 08:47:20 +10005995 if (!check_stripe_cache(mddev))
5996 return -ENOSPC;
5997
NeilBrown30b67642012-05-22 13:55:28 +10005998 if (has_failed(conf))
5999 return -EINVAL;
6000
NeilBrownc6563a82012-05-21 09:27:00 +10006001 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11006002 if (!test_bit(In_sync, &rdev->flags)
6003 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08006004 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10006005 }
NeilBrown63c70c42006-03-27 01:18:13 -08006006
NeilBrownf4168852007-02-28 20:11:53 -08006007 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08006008 /* Not enough devices even to make a degraded array
6009 * of that size
6010 */
6011 return -EINVAL;
6012
NeilBrownec32a2b2009-03-31 15:17:38 +11006013 /* Refuse to reduce size of the array. Any reductions in
6014 * array size must be through explicit setting of array_size
6015 * attribute.
6016 */
6017 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
6018 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10006019 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11006020 "before number of disks\n", mdname(mddev));
6021 return -EINVAL;
6022 }
6023
NeilBrownf6705572006-03-27 01:18:11 -08006024 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08006025 spin_lock_irq(&conf->device_lock);
6026 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08006027 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006028 conf->prev_chunk_sectors = conf->chunk_sectors;
6029 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11006030 conf->prev_algo = conf->algorithm;
6031 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10006032 conf->generation++;
6033 /* Code that selects data_offset needs to see the generation update
6034 * if reshape_progress has been set - so a memory barrier needed.
6035 */
6036 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10006037 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11006038 conf->reshape_progress = raid5_size(mddev, 0, 0);
6039 else
6040 conf->reshape_progress = 0;
6041 conf->reshape_safe = conf->reshape_progress;
NeilBrown29269552006-03-27 01:18:10 -08006042 spin_unlock_irq(&conf->device_lock);
6043
6044 /* Add some new drives, as many as will fit.
6045 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10006046 * Don't add devices if we are reducing the number of
6047 * devices in the array. This is because it is not possible
6048 * to correctly record the "partially reconstructed" state of
6049 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08006050 */
NeilBrown87a8dec2011-01-31 11:57:43 +11006051 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11006052 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11006053 if (rdev->raid_disk < 0 &&
6054 !test_bit(Faulty, &rdev->flags)) {
6055 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11006056 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11006057 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11006058 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11006059 else
NeilBrown87a8dec2011-01-31 11:57:43 +11006060 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10006061
6062 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11006063 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11006064 }
NeilBrown87a8dec2011-01-31 11:57:43 +11006065 } else if (rdev->raid_disk >= conf->previous_raid_disks
6066 && !test_bit(Faulty, &rdev->flags)) {
6067 /* This is a spare that was manually added */
6068 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11006069 }
NeilBrown29269552006-03-27 01:18:10 -08006070
NeilBrown87a8dec2011-01-31 11:57:43 +11006071 /* When a reshape changes the number of devices,
6072 * ->degraded is measured against the larger of the
6073 * pre and post number of devices.
6074 */
NeilBrownec32a2b2009-03-31 15:17:38 +11006075 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11006076 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11006077 spin_unlock_irqrestore(&conf->device_lock, flags);
6078 }
NeilBrown63c70c42006-03-27 01:18:13 -08006079 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10006080 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b422006-10-03 01:15:46 -07006081 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08006082
NeilBrown29269552006-03-27 01:18:10 -08006083 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6084 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6085 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6086 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6087 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10006088 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08006089 if (!mddev->sync_thread) {
6090 mddev->recovery = 0;
6091 spin_lock_irq(&conf->device_lock);
6092 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10006093 rdev_for_each(rdev, mddev)
6094 rdev->new_data_offset = rdev->data_offset;
6095 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11006096 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11006097 mddev->reshape_position = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08006098 spin_unlock_irq(&conf->device_lock);
6099 return -EAGAIN;
6100 }
NeilBrownc8f517c2009-03-31 15:28:40 +11006101 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08006102 md_wakeup_thread(mddev->sync_thread);
6103 md_new_event(mddev);
6104 return 0;
6105}
NeilBrown29269552006-03-27 01:18:10 -08006106
NeilBrownec32a2b2009-03-31 15:17:38 +11006107/* This is called from the reshape thread and should make any
6108 * changes needed in 'conf'
6109 */
NeilBrownd1688a62011-10-11 16:49:52 +11006110static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08006111{
NeilBrown29269552006-03-27 01:18:10 -08006112
NeilBrownf6705572006-03-27 01:18:11 -08006113 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10006114 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07006115
NeilBrownf6705572006-03-27 01:18:11 -08006116 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11006117 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10006118 rdev_for_each(rdev, conf->mddev)
6119 rdev->data_offset = rdev->new_data_offset;
6120 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11006121 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08006122 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11006123 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07006124
6125 /* read-ahead size must cover two whole stripes, which is
6126 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
6127 */
NeilBrown4a5add42010-06-01 19:37:28 +10006128 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11006129 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006130 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11006131 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07006132 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
6133 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
6134 }
NeilBrown29269552006-03-27 01:18:10 -08006135 }
NeilBrown29269552006-03-27 01:18:10 -08006136}
6137
NeilBrownec32a2b2009-03-31 15:17:38 +11006138/* This is called from the raid5d thread with mddev_lock held.
6139 * It makes config changes to the device.
6140 */
NeilBrownfd01b882011-10-11 16:47:53 +11006141static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11006142{
NeilBrownd1688a62011-10-11 16:49:52 +11006143 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11006144
6145 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6146
NeilBrownec32a2b2009-03-31 15:17:38 +11006147 if (mddev->delta_disks > 0) {
6148 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
6149 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006150 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11006151 } else {
6152 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11006153 spin_lock_irq(&conf->device_lock);
6154 mddev->degraded = calc_degraded(conf);
6155 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11006156 for (d = conf->raid_disks ;
6157 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10006158 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11006159 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10006160 if (rdev)
6161 clear_bit(In_sync, &rdev->flags);
6162 rdev = conf->disks[d].replacement;
6163 if (rdev)
6164 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10006165 }
NeilBrowncea9c222009-03-31 15:15:05 +11006166 }
NeilBrown88ce4932009-03-31 15:24:23 +11006167 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006168 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11006169 mddev->reshape_position = MaxSector;
6170 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006171 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11006172 }
6173}
6174
NeilBrownfd01b882011-10-11 16:47:53 +11006175static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07006176{
NeilBrownd1688a62011-10-11 16:49:52 +11006177 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07006178
6179 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08006180 case 2: /* resume for a suspend */
6181 wake_up(&conf->wait_for_overlap);
6182 break;
6183
NeilBrown72626682005-09-09 16:23:54 -07006184 case 1: /* stop all writes */
6185 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006186 /* '2' tells resync/reshape to pause so that all
6187 * active stripes can drain
6188 */
6189 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07006190 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006191 atomic_read(&conf->active_stripes) == 0 &&
6192 atomic_read(&conf->active_aligned_reads) == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01006193 conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006194 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07006195 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006196 /* allow reshape to continue */
6197 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006198 break;
6199
6200 case 0: /* re-enable writes */
6201 spin_lock_irq(&conf->device_lock);
6202 conf->quiesce = 0;
6203 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08006204 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006205 spin_unlock_irq(&conf->device_lock);
6206 break;
6207 }
NeilBrown72626682005-09-09 16:23:54 -07006208}
NeilBrownb15c2e52006-01-06 00:20:16 -08006209
NeilBrownd562b0c2009-03-31 14:39:39 +11006210
NeilBrownfd01b882011-10-11 16:47:53 +11006211static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11006212{
NeilBrowne373ab12011-10-11 16:48:59 +11006213 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07006214 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11006215
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006216 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11006217 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10006218 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
6219 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006220 return ERR_PTR(-EINVAL);
6221 }
6222
NeilBrowne373ab12011-10-11 16:48:59 +11006223 sectors = raid0_conf->strip_zone[0].zone_end;
6224 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10006225 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006226 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11006227 mddev->new_layout = ALGORITHM_PARITY_N;
6228 mddev->new_chunk_sectors = mddev->chunk_sectors;
6229 mddev->raid_disks += 1;
6230 mddev->delta_disks = 1;
6231 /* make sure it will be not marked as dirty */
6232 mddev->recovery_cp = MaxSector;
6233
6234 return setup_conf(mddev);
6235}
6236
6237
NeilBrownfd01b882011-10-11 16:47:53 +11006238static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006239{
6240 int chunksect;
6241
6242 if (mddev->raid_disks != 2 ||
6243 mddev->degraded > 1)
6244 return ERR_PTR(-EINVAL);
6245
6246 /* Should check if there are write-behind devices? */
6247
6248 chunksect = 64*2; /* 64K by default */
6249
6250 /* The array must be an exact multiple of chunksize */
6251 while (chunksect && (mddev->array_sectors & (chunksect-1)))
6252 chunksect >>= 1;
6253
6254 if ((chunksect<<9) < STRIPE_SIZE)
6255 /* array size does not allow a suitable chunk size */
6256 return ERR_PTR(-EINVAL);
6257
6258 mddev->new_level = 5;
6259 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10006260 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11006261
6262 return setup_conf(mddev);
6263}
6264
NeilBrownfd01b882011-10-11 16:47:53 +11006265static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11006266{
6267 int new_layout;
6268
6269 switch (mddev->layout) {
6270 case ALGORITHM_LEFT_ASYMMETRIC_6:
6271 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
6272 break;
6273 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6274 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
6275 break;
6276 case ALGORITHM_LEFT_SYMMETRIC_6:
6277 new_layout = ALGORITHM_LEFT_SYMMETRIC;
6278 break;
6279 case ALGORITHM_RIGHT_SYMMETRIC_6:
6280 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
6281 break;
6282 case ALGORITHM_PARITY_0_6:
6283 new_layout = ALGORITHM_PARITY_0;
6284 break;
6285 case ALGORITHM_PARITY_N:
6286 new_layout = ALGORITHM_PARITY_N;
6287 break;
6288 default:
6289 return ERR_PTR(-EINVAL);
6290 }
6291 mddev->new_level = 5;
6292 mddev->new_layout = new_layout;
6293 mddev->delta_disks = -1;
6294 mddev->raid_disks -= 1;
6295 return setup_conf(mddev);
6296}
6297
NeilBrownd562b0c2009-03-31 14:39:39 +11006298
NeilBrownfd01b882011-10-11 16:47:53 +11006299static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11006300{
NeilBrown88ce4932009-03-31 15:24:23 +11006301 /* For a 2-drive array, the layout and chunk size can be changed
6302 * immediately as not restriping is needed.
6303 * For larger arrays we record the new value - after validation
6304 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11006305 */
NeilBrownd1688a62011-10-11 16:49:52 +11006306 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10006307 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11006308
NeilBrown597a7112009-06-18 08:47:42 +10006309 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11006310 return -EINVAL;
6311 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006312 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11006313 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006314 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11006315 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006316 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11006317 /* not factor of array size */
6318 return -EINVAL;
6319 }
6320
6321 /* They look valid */
6322
NeilBrown88ce4932009-03-31 15:24:23 +11006323 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10006324 /* can make the change immediately */
6325 if (mddev->new_layout >= 0) {
6326 conf->algorithm = mddev->new_layout;
6327 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11006328 }
6329 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10006330 conf->chunk_sectors = new_chunk ;
6331 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11006332 }
6333 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6334 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11006335 }
NeilBrown50ac1682009-06-18 08:47:55 +10006336 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11006337}
6338
NeilBrownfd01b882011-10-11 16:47:53 +11006339static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11006340{
NeilBrown597a7112009-06-18 08:47:42 +10006341 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10006342
NeilBrown597a7112009-06-18 08:47:42 +10006343 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11006344 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006345 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006346 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11006347 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006348 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11006349 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006350 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11006351 /* not factor of array size */
6352 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006353 }
NeilBrown88ce4932009-03-31 15:24:23 +11006354
6355 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10006356 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11006357}
6358
NeilBrownfd01b882011-10-11 16:47:53 +11006359static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006360{
6361 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006362 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006363 * raid1 - if there are two drives. We need to know the chunk size
6364 * raid4 - trivial - just use a raid4 layout.
6365 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006366 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006367 if (mddev->level == 0)
6368 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11006369 if (mddev->level == 1)
6370 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11006371 if (mddev->level == 4) {
6372 mddev->new_layout = ALGORITHM_PARITY_N;
6373 mddev->new_level = 5;
6374 return setup_conf(mddev);
6375 }
NeilBrownfc9739c2009-03-31 14:57:20 +11006376 if (mddev->level == 6)
6377 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11006378
6379 return ERR_PTR(-EINVAL);
6380}
6381
NeilBrownfd01b882011-10-11 16:47:53 +11006382static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11006383{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006384 /* raid4 can take over:
6385 * raid0 - if there is only one strip zone
6386 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11006387 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006388 if (mddev->level == 0)
6389 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11006390 if (mddev->level == 5 &&
6391 mddev->layout == ALGORITHM_PARITY_N) {
6392 mddev->new_layout = 0;
6393 mddev->new_level = 4;
6394 return setup_conf(mddev);
6395 }
6396 return ERR_PTR(-EINVAL);
6397}
NeilBrownd562b0c2009-03-31 14:39:39 +11006398
NeilBrown84fc4b52011-10-11 16:49:58 +11006399static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11006400
NeilBrownfd01b882011-10-11 16:47:53 +11006401static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11006402{
6403 /* Currently can only take over a raid5. We map the
6404 * personality to an equivalent raid6 personality
6405 * with the Q block at the end.
6406 */
6407 int new_layout;
6408
6409 if (mddev->pers != &raid5_personality)
6410 return ERR_PTR(-EINVAL);
6411 if (mddev->degraded > 1)
6412 return ERR_PTR(-EINVAL);
6413 if (mddev->raid_disks > 253)
6414 return ERR_PTR(-EINVAL);
6415 if (mddev->raid_disks < 3)
6416 return ERR_PTR(-EINVAL);
6417
6418 switch (mddev->layout) {
6419 case ALGORITHM_LEFT_ASYMMETRIC:
6420 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
6421 break;
6422 case ALGORITHM_RIGHT_ASYMMETRIC:
6423 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
6424 break;
6425 case ALGORITHM_LEFT_SYMMETRIC:
6426 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
6427 break;
6428 case ALGORITHM_RIGHT_SYMMETRIC:
6429 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
6430 break;
6431 case ALGORITHM_PARITY_0:
6432 new_layout = ALGORITHM_PARITY_0_6;
6433 break;
6434 case ALGORITHM_PARITY_N:
6435 new_layout = ALGORITHM_PARITY_N;
6436 break;
6437 default:
6438 return ERR_PTR(-EINVAL);
6439 }
6440 mddev->new_level = 6;
6441 mddev->new_layout = new_layout;
6442 mddev->delta_disks = 1;
6443 mddev->raid_disks += 1;
6444 return setup_conf(mddev);
6445}
6446
6447
NeilBrown84fc4b52011-10-11 16:49:58 +11006448static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07006449{
6450 .name = "raid6",
6451 .level = 6,
6452 .owner = THIS_MODULE,
6453 .make_request = make_request,
6454 .run = run,
6455 .stop = stop,
6456 .status = status,
6457 .error_handler = error,
6458 .hot_add_disk = raid5_add_disk,
6459 .hot_remove_disk= raid5_remove_disk,
6460 .spare_active = raid5_spare_active,
6461 .sync_request = sync_request,
6462 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006463 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10006464 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08006465 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006466 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07006467 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11006468 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07006469};
NeilBrown84fc4b52011-10-11 16:49:58 +11006470static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471{
6472 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08006473 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006474 .owner = THIS_MODULE,
6475 .make_request = make_request,
6476 .run = run,
6477 .stop = stop,
6478 .status = status,
6479 .error_handler = error,
6480 .hot_add_disk = raid5_add_disk,
6481 .hot_remove_disk= raid5_remove_disk,
6482 .spare_active = raid5_spare_active,
6483 .sync_request = sync_request,
6484 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006485 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08006486 .check_reshape = raid5_check_reshape,
6487 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006488 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07006489 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11006490 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006491};
6492
NeilBrown84fc4b52011-10-11 16:49:58 +11006493static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006494{
NeilBrown2604b702006-01-06 00:20:36 -08006495 .name = "raid4",
6496 .level = 4,
6497 .owner = THIS_MODULE,
6498 .make_request = make_request,
6499 .run = run,
6500 .stop = stop,
6501 .status = status,
6502 .error_handler = error,
6503 .hot_add_disk = raid5_add_disk,
6504 .hot_remove_disk= raid5_remove_disk,
6505 .spare_active = raid5_spare_active,
6506 .sync_request = sync_request,
6507 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006508 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08006509 .check_reshape = raid5_check_reshape,
6510 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006511 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08006512 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11006513 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08006514};
6515
6516static int __init raid5_init(void)
6517{
NeilBrown16a53ec2006-06-26 00:27:38 -07006518 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006519 register_md_personality(&raid5_personality);
6520 register_md_personality(&raid4_personality);
6521 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006522}
6523
NeilBrown2604b702006-01-06 00:20:36 -08006524static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006525{
NeilBrown16a53ec2006-06-26 00:27:38 -07006526 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006527 unregister_md_personality(&raid5_personality);
6528 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529}
6530
6531module_init(raid5_init);
6532module_exit(raid5_exit);
6533MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11006534MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006535MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08006536MODULE_ALIAS("md-raid5");
6537MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08006538MODULE_ALIAS("md-level-5");
6539MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07006540MODULE_ALIAS("md-personality-8"); /* RAID6 */
6541MODULE_ALIAS("md-raid6");
6542MODULE_ALIAS("md-level-6");
6543
6544/* This used to be two separate modules, they were: */
6545MODULE_ALIAS("raid5");
6546MODULE_ALIAS("raid6");