blob: d87a2de667ead28919740bff439a3fae84b57798 [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 Lid265d9d2013-08-28 14:29:05 +0800242static struct llist_node *llist_reverse_order(struct llist_node *head)
243{
244 struct llist_node *new_head = NULL;
245
246 while (head) {
247 struct llist_node *tmp = head;
248 head = head->next;
249 tmp->next = new_head;
250 new_head = tmp;
251 }
252
253 return new_head;
254}
255
Shaohua Li773ca822013-08-27 17:50:39 +0800256/* should hold conf->device_lock already */
257static int release_stripe_list(struct r5conf *conf)
258{
259 struct stripe_head *sh;
260 int count = 0;
261 struct llist_node *head;
262
263 head = llist_del_all(&conf->released_stripes);
Shaohua Lid265d9d2013-08-28 14:29:05 +0800264 head = llist_reverse_order(head);
Shaohua Li773ca822013-08-27 17:50:39 +0800265 while (head) {
266 sh = llist_entry(head, struct stripe_head, release_list);
267 head = llist_next(head);
268 /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */
269 smp_mb();
270 clear_bit(STRIPE_ON_RELEASE_LIST, &sh->state);
271 /*
272 * Don't worry the bit is set here, because if the bit is set
273 * again, the count is always > 1. This is true for
274 * STRIPE_ON_UNPLUG_LIST bit too.
275 */
276 __release_stripe(conf, sh);
277 count++;
278 }
279
280 return count;
281}
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283static void release_stripe(struct stripe_head *sh)
284{
NeilBrownd1688a62011-10-11 16:49:52 +1100285 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 unsigned long flags;
Shaohua Li773ca822013-08-27 17:50:39 +0800287 bool wakeup;
NeilBrown16a53ec2006-06-26 00:27:38 -0700288
Shaohua Li773ca822013-08-27 17:50:39 +0800289 if (test_and_set_bit(STRIPE_ON_RELEASE_LIST, &sh->state))
290 goto slow_path;
291 wakeup = llist_add(&sh->release_list, &conf->released_stripes);
292 if (wakeup)
293 md_wakeup_thread(conf->mddev->thread);
294 return;
295slow_path:
Shaohua Li4eb788d2012-07-19 16:01:31 +1000296 local_irq_save(flags);
Shaohua Li773ca822013-08-27 17:50:39 +0800297 /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */
Shaohua Li4eb788d2012-07-19 16:01:31 +1000298 if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) {
299 do_release_stripe(conf, sh);
300 spin_unlock(&conf->device_lock);
301 }
302 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
NeilBrownfccddba2006-01-06 00:20:33 -0800305static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Dan Williams45b42332007-07-09 11:56:43 -0700307 pr_debug("remove_hash(), stripe %llu\n",
308 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
NeilBrownfccddba2006-01-06 00:20:33 -0800310 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
NeilBrownd1688a62011-10-11 16:49:52 +1100313static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
NeilBrownfccddba2006-01-06 00:20:33 -0800315 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Dan Williams45b42332007-07-09 11:56:43 -0700317 pr_debug("insert_hash(), stripe %llu\n",
318 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
NeilBrownfccddba2006-01-06 00:20:33 -0800320 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
323
324/* find an idle stripe, make sure it is unhashed, and return it. */
NeilBrownd1688a62011-10-11 16:49:52 +1100325static struct stripe_head *get_free_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 struct stripe_head *sh = NULL;
328 struct list_head *first;
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (list_empty(&conf->inactive_list))
331 goto out;
332 first = conf->inactive_list.next;
333 sh = list_entry(first, struct stripe_head, lru);
334 list_del_init(first);
335 remove_hash(sh);
336 atomic_inc(&conf->active_stripes);
337out:
338 return sh;
339}
340
NeilBrowne4e11e32010-06-16 16:45:16 +1000341static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
343 struct page *p;
344 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000345 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
NeilBrowne4e11e32010-06-16 16:45:16 +1000347 for (i = 0; i < num ; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 p = sh->dev[i].page;
349 if (!p)
350 continue;
351 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800352 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354}
355
NeilBrowne4e11e32010-06-16 16:45:16 +1000356static int grow_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000359 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
NeilBrowne4e11e32010-06-16 16:45:16 +1000361 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 struct page *page;
363
364 if (!(page = alloc_page(GFP_KERNEL))) {
365 return 1;
366 }
367 sh->dev[i].page = page;
368 }
369 return 0;
370}
371
NeilBrown784052e2009-03-31 15:19:07 +1100372static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrownd1688a62011-10-11 16:49:52 +1100373static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100374 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
NeilBrownb5663ba2009-03-31 14:39:38 +1100376static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
NeilBrownd1688a62011-10-11 16:49:52 +1100378 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800379 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200381 BUG_ON(atomic_read(&sh->count) != 0);
382 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000383 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700384
Dan Williams45b42332007-07-09 11:56:43 -0700385 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 (unsigned long long)sh->sector);
387
388 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700389
NeilBrown86b42c72009-03-31 15:19:03 +1100390 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100391 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100393 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 sh->state = 0;
395
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800396
397 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 struct r5dev *dev = &sh->dev[i];
399
Dan Williamsd84e0f12007-01-02 13:52:30 -0700400 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700402 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700404 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000406 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
408 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100409 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
411 insert_hash(conf, sh);
412}
413
NeilBrownd1688a62011-10-11 16:49:52 +1100414static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100415 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
417 struct stripe_head *sh;
418
Dan Williams45b42332007-07-09 11:56:43 -0700419 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800420 hlist_for_each_entry(sh, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100421 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700423 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return NULL;
425}
426
NeilBrown674806d2010-06-16 17:17:53 +1000427/*
428 * Need to check if array has failed when deciding whether to:
429 * - start an array
430 * - remove non-faulty devices
431 * - add a spare
432 * - allow a reshape
433 * This determination is simple when no reshape is happening.
434 * However if there is a reshape, we need to carefully check
435 * both the before and after sections.
436 * This is because some failed devices may only affect one
437 * of the two sections, and some non-in_sync devices may
438 * be insync in the section most affected by failed devices.
439 */
NeilBrown908f4fb2011-12-23 10:17:50 +1100440static int calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000441{
NeilBrown908f4fb2011-12-23 10:17:50 +1100442 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000443 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000444
445 rcu_read_lock();
446 degraded = 0;
447 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100448 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000449 if (rdev && test_bit(Faulty, &rdev->flags))
450 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000451 if (!rdev || test_bit(Faulty, &rdev->flags))
452 degraded++;
453 else if (test_bit(In_sync, &rdev->flags))
454 ;
455 else
456 /* not in-sync or faulty.
457 * If the reshape increases the number of devices,
458 * this is being recovered by the reshape, so
459 * this 'previous' section is not in_sync.
460 * If the number of devices is being reduced however,
461 * the device can only be part of the array if
462 * we are reverting a reshape, so this section will
463 * be in-sync.
464 */
465 if (conf->raid_disks >= conf->previous_raid_disks)
466 degraded++;
467 }
468 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100469 if (conf->raid_disks == conf->previous_raid_disks)
470 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000471 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100472 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000473 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100474 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000475 if (rdev && test_bit(Faulty, &rdev->flags))
476 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000477 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100478 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000479 else if (test_bit(In_sync, &rdev->flags))
480 ;
481 else
482 /* not in-sync or faulty.
483 * If reshape increases the number of devices, this
484 * section has already been recovered, else it
485 * almost certainly hasn't.
486 */
487 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100488 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000489 }
490 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100491 if (degraded2 > degraded)
492 return degraded2;
493 return degraded;
494}
495
496static int has_failed(struct r5conf *conf)
497{
498 int degraded;
499
500 if (conf->mddev->reshape_position == MaxSector)
501 return conf->mddev->degraded > conf->max_degraded;
502
503 degraded = calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000504 if (degraded > conf->max_degraded)
505 return 1;
506 return 0;
507}
508
NeilBrownb5663ba2009-03-31 14:39:38 +1100509static struct stripe_head *
NeilBrownd1688a62011-10-11 16:49:52 +1100510get_active_stripe(struct r5conf *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000511 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
513 struct stripe_head *sh;
514
Dan Williams45b42332007-07-09 11:56:43 -0700515 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 spin_lock_irq(&conf->device_lock);
518
519 do {
NeilBrown72626682005-09-09 16:23:54 -0700520 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000521 conf->quiesce == 0 || noquiesce,
Lukas Czernereed8c022012-11-30 11:42:40 +0100522 conf->device_lock);
NeilBrown86b42c72009-03-31 15:19:03 +1100523 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (!sh) {
525 if (!conf->inactive_blocked)
526 sh = get_free_stripe(conf);
527 if (noblock && sh == NULL)
528 break;
529 if (!sh) {
530 conf->inactive_blocked = 1;
531 wait_event_lock_irq(conf->wait_for_stripe,
532 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800533 (atomic_read(&conf->active_stripes)
534 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 || !conf->inactive_blocked),
Lukas Czernereed8c022012-11-30 11:42:40 +0100536 conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 conf->inactive_blocked = 0;
538 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100539 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 } else {
541 if (atomic_read(&sh->count)) {
NeilBrownab69ae12009-03-31 15:26:47 +1100542 BUG_ON(!list_empty(&sh->lru)
Shaohua Li8811b592012-08-02 08:33:00 +1000543 && !test_bit(STRIPE_EXPANDING, &sh->state)
Shaohua Li773ca822013-08-27 17:50:39 +0800544 && !test_bit(STRIPE_ON_UNPLUG_LIST, &sh->state)
545 && !test_bit(STRIPE_ON_RELEASE_LIST, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 } else {
547 if (!test_bit(STRIPE_HANDLE, &sh->state))
548 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700549 if (list_empty(&sh->lru) &&
550 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700551 BUG();
552 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
554 }
555 } while (sh == NULL);
556
557 if (sh)
558 atomic_inc(&sh->count);
559
560 spin_unlock_irq(&conf->device_lock);
561 return sh;
562}
563
NeilBrown05616be2012-05-21 09:27:00 +1000564/* Determine if 'data_offset' or 'new_data_offset' should be used
565 * in this stripe_head.
566 */
567static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
568{
569 sector_t progress = conf->reshape_progress;
570 /* Need a memory barrier to make sure we see the value
571 * of conf->generation, or ->data_offset that was set before
572 * reshape_progress was updated.
573 */
574 smp_rmb();
575 if (progress == MaxSector)
576 return 0;
577 if (sh->generation == conf->generation - 1)
578 return 0;
579 /* We are in a reshape, and this is a new-generation stripe,
580 * so use new_data_offset.
581 */
582 return 1;
583}
584
NeilBrown6712ecf2007-09-27 12:47:43 +0200585static void
586raid5_end_read_request(struct bio *bi, int error);
587static void
588raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700589
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000590static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700591{
NeilBrownd1688a62011-10-11 16:49:52 +1100592 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700593 int i, disks = sh->disks;
594
595 might_sleep();
596
597 for (i = disks; i--; ) {
598 int rw;
NeilBrown9a3e1102011-12-23 10:17:53 +1100599 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100600 struct bio *bi, *rbi;
601 struct md_rdev *rdev, *rrdev = NULL;
Tejun Heoe9c74692010-09-03 11:56:18 +0200602 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
603 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
604 rw = WRITE_FUA;
605 else
606 rw = WRITE;
Shaohua Li9e4447682012-10-11 13:49:49 +1100607 if (test_bit(R5_Discard, &sh->dev[i].flags))
Shaohua Li620125f2012-10-11 13:49:05 +1100608 rw |= REQ_DISCARD;
Tejun Heoe9c74692010-09-03 11:56:18 +0200609 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700610 rw = READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100611 else if (test_and_clear_bit(R5_WantReplace,
612 &sh->dev[i].flags)) {
613 rw = WRITE;
614 replace_only = 1;
615 } else
Dan Williams91c00922007-01-02 13:52:30 -0700616 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +1000617 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
618 rw |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -0700619
620 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100621 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700622
Dan Williams91c00922007-01-02 13:52:30 -0700623 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100624 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100625 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
626 rdev = rcu_dereference(conf->disks[i].rdev);
627 if (!rdev) {
628 rdev = rrdev;
629 rrdev = NULL;
630 }
NeilBrown9a3e1102011-12-23 10:17:53 +1100631 if (rw & WRITE) {
632 if (replace_only)
633 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100634 if (rdev == rrdev)
635 /* We raced and saw duplicates */
636 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100637 } else {
NeilBrowndd054fc2011-12-23 10:17:53 +1100638 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100639 rdev = rrdev;
640 rrdev = NULL;
641 }
NeilBrown977df362011-12-23 10:17:53 +1100642
Dan Williams91c00922007-01-02 13:52:30 -0700643 if (rdev && test_bit(Faulty, &rdev->flags))
644 rdev = NULL;
645 if (rdev)
646 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100647 if (rrdev && test_bit(Faulty, &rrdev->flags))
648 rrdev = NULL;
649 if (rrdev)
650 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700651 rcu_read_unlock();
652
NeilBrown73e92e52011-07-28 11:39:22 +1000653 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100654 * need to check for writes. We never accept write errors
655 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000656 */
657 while ((rw & WRITE) && rdev &&
658 test_bit(WriteErrorSeen, &rdev->flags)) {
659 sector_t first_bad;
660 int bad_sectors;
661 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
662 &first_bad, &bad_sectors);
663 if (!bad)
664 break;
665
666 if (bad < 0) {
667 set_bit(BlockedBadBlocks, &rdev->flags);
668 if (!conf->mddev->external &&
669 conf->mddev->flags) {
670 /* It is very unlikely, but we might
671 * still need to write out the
672 * bad block log - better give it
673 * a chance*/
674 md_check_recovery(conf->mddev);
675 }
majianpeng18507532012-07-03 12:11:54 +1000676 /*
677 * Because md_wait_for_blocked_rdev
678 * will dec nr_pending, we must
679 * increment it first.
680 */
681 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +1000682 md_wait_for_blocked_rdev(rdev, conf->mddev);
683 } else {
684 /* Acknowledged bad block - skip the write */
685 rdev_dec_pending(rdev, conf->mddev);
686 rdev = NULL;
687 }
688 }
689
Dan Williams91c00922007-01-02 13:52:30 -0700690 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100691 if (s->syncing || s->expanding || s->expanded
692 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700693 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
694
Dan Williams2b7497f2008-06-28 08:31:52 +1000695 set_bit(STRIPE_IO_STARTED, &sh->state);
696
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700697 bio_reset(bi);
Dan Williams91c00922007-01-02 13:52:30 -0700698 bi->bi_bdev = rdev->bdev;
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700699 bi->bi_rw = rw;
700 bi->bi_end_io = (rw & WRITE)
701 ? raid5_end_write_request
702 : raid5_end_read_request;
703 bi->bi_private = sh;
704
Dan Williams91c00922007-01-02 13:52:30 -0700705 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700706 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700707 bi->bi_rw, i);
708 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000709 if (use_new_offset(conf, sh))
710 bi->bi_sector = (sh->sector
711 + rdev->new_data_offset);
712 else
713 bi->bi_sector = (sh->sector
714 + rdev->data_offset);
majianpeng3f9e7c12012-07-31 10:04:21 +1000715 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
716 bi->bi_rw |= REQ_FLUSH;
717
Kent Overstreet4997b722013-05-30 08:44:39 +0200718 bi->bi_vcnt = 1;
Dan Williams91c00922007-01-02 13:52:30 -0700719 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
720 bi->bi_io_vec[0].bv_offset = 0;
721 bi->bi_size = STRIPE_SIZE;
NeilBrown977df362011-12-23 10:17:53 +1100722 if (rrdev)
723 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Jonathan Brassowe3620a32013-03-07 16:22:01 -0600724
725 if (conf->mddev->gendisk)
726 trace_block_bio_remap(bdev_get_queue(bi->bi_bdev),
727 bi, disk_devt(conf->mddev->gendisk),
728 sh->dev[i].sector);
Dan Williams91c00922007-01-02 13:52:30 -0700729 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100730 }
731 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100732 if (s->syncing || s->expanding || s->expanded
733 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +1100734 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
735
736 set_bit(STRIPE_IO_STARTED, &sh->state);
737
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700738 bio_reset(rbi);
NeilBrown977df362011-12-23 10:17:53 +1100739 rbi->bi_bdev = rrdev->bdev;
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700740 rbi->bi_rw = rw;
741 BUG_ON(!(rw & WRITE));
742 rbi->bi_end_io = raid5_end_write_request;
743 rbi->bi_private = sh;
744
NeilBrown977df362011-12-23 10:17:53 +1100745 pr_debug("%s: for %llu schedule op %ld on "
746 "replacement disc %d\n",
747 __func__, (unsigned long long)sh->sector,
748 rbi->bi_rw, i);
749 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000750 if (use_new_offset(conf, sh))
751 rbi->bi_sector = (sh->sector
752 + rrdev->new_data_offset);
753 else
754 rbi->bi_sector = (sh->sector
755 + rrdev->data_offset);
Kent Overstreet4997b722013-05-30 08:44:39 +0200756 rbi->bi_vcnt = 1;
NeilBrown977df362011-12-23 10:17:53 +1100757 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
758 rbi->bi_io_vec[0].bv_offset = 0;
759 rbi->bi_size = STRIPE_SIZE;
Jonathan Brassowe3620a32013-03-07 16:22:01 -0600760 if (conf->mddev->gendisk)
761 trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev),
762 rbi, disk_devt(conf->mddev->gendisk),
763 sh->dev[i].sector);
NeilBrown977df362011-12-23 10:17:53 +1100764 generic_make_request(rbi);
765 }
766 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000767 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700768 set_bit(STRIPE_DEGRADED, &sh->state);
769 pr_debug("skip op %ld on disc %d for sector %llu\n",
770 bi->bi_rw, i, (unsigned long long)sh->sector);
771 clear_bit(R5_LOCKED, &sh->dev[i].flags);
772 set_bit(STRIPE_HANDLE, &sh->state);
773 }
774 }
775}
776
777static struct dma_async_tx_descriptor *
778async_copy_data(int frombio, struct bio *bio, struct page *page,
779 sector_t sector, struct dma_async_tx_descriptor *tx)
780{
781 struct bio_vec *bvl;
782 struct page *bio_page;
783 int i;
784 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700785 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700786 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700787
788 if (bio->bi_sector >= sector)
789 page_offset = (signed)(bio->bi_sector - sector) * 512;
790 else
791 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700792
Dan Williams0403e382009-09-08 17:42:50 -0700793 if (frombio)
794 flags |= ASYNC_TX_FENCE;
795 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
796
Dan Williams91c00922007-01-02 13:52:30 -0700797 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000798 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700799 int clen;
800 int b_offset = 0;
801
802 if (page_offset < 0) {
803 b_offset = -page_offset;
804 page_offset += b_offset;
805 len -= b_offset;
806 }
807
808 if (len > 0 && page_offset + len > STRIPE_SIZE)
809 clen = STRIPE_SIZE - page_offset;
810 else
811 clen = len;
812
813 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000814 b_offset += bvl->bv_offset;
815 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700816 if (frombio)
817 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700818 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700819 else
820 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700821 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700822 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700823 /* chain the operations */
824 submit.depend_tx = tx;
825
Dan Williams91c00922007-01-02 13:52:30 -0700826 if (clen < len) /* hit end of page */
827 break;
828 page_offset += len;
829 }
830
831 return tx;
832}
833
834static void ops_complete_biofill(void *stripe_head_ref)
835{
836 struct stripe_head *sh = stripe_head_ref;
837 struct bio *return_bi = NULL;
Dan Williamse4d84902007-09-24 10:06:13 -0700838 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700839
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700840 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700841 (unsigned long long)sh->sector);
842
843 /* clear completed biofills */
844 for (i = sh->disks; i--; ) {
845 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700846
847 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700848 /* and check if we need to reply to a read request,
849 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000850 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700851 */
852 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700853 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700854
Dan Williams91c00922007-01-02 13:52:30 -0700855 BUG_ON(!dev->read);
856 rbi = dev->read;
857 dev->read = NULL;
858 while (rbi && rbi->bi_sector <
859 dev->sector + STRIPE_SECTORS) {
860 rbi2 = r5_next_bio(rbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +1000861 if (!raid5_dec_bi_active_stripes(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700862 rbi->bi_next = return_bi;
863 return_bi = rbi;
864 }
Dan Williams91c00922007-01-02 13:52:30 -0700865 rbi = rbi2;
866 }
867 }
868 }
Dan Williams83de75c2008-06-28 08:31:58 +1000869 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700870
871 return_io(return_bi);
872
Dan Williamse4d84902007-09-24 10:06:13 -0700873 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700874 release_stripe(sh);
875}
876
877static void ops_run_biofill(struct stripe_head *sh)
878{
879 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa08abd82009-06-03 11:43:59 -0700880 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700881 int i;
882
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700883 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700884 (unsigned long long)sh->sector);
885
886 for (i = sh->disks; i--; ) {
887 struct r5dev *dev = &sh->dev[i];
888 if (test_bit(R5_Wantfill, &dev->flags)) {
889 struct bio *rbi;
Shaohua Lib17459c2012-07-19 16:01:31 +1000890 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700891 dev->read = rbi = dev->toread;
892 dev->toread = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +1000893 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700894 while (rbi && rbi->bi_sector <
895 dev->sector + STRIPE_SECTORS) {
896 tx = async_copy_data(0, rbi, dev->page,
897 dev->sector, tx);
898 rbi = r5_next_bio(rbi, dev->sector);
899 }
900 }
901 }
902
903 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700904 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
905 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700906}
907
Dan Williams4e7d2c02009-08-29 19:13:11 -0700908static void mark_target_uptodate(struct stripe_head *sh, int target)
909{
910 struct r5dev *tgt;
911
912 if (target < 0)
913 return;
914
915 tgt = &sh->dev[target];
916 set_bit(R5_UPTODATE, &tgt->flags);
917 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
918 clear_bit(R5_Wantcompute, &tgt->flags);
919}
920
Dan Williamsac6b53b2009-07-14 13:40:19 -0700921static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700922{
923 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700924
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700925 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700926 (unsigned long long)sh->sector);
927
Dan Williamsac6b53b2009-07-14 13:40:19 -0700928 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700929 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700930 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700931
Dan Williamsecc65c92008-06-28 08:31:57 +1000932 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
933 if (sh->check_state == check_state_compute_run)
934 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700935 set_bit(STRIPE_HANDLE, &sh->state);
936 release_stripe(sh);
937}
938
Dan Williamsd6f38f32009-07-14 11:50:52 -0700939/* return a pointer to the address conversion region of the scribble buffer */
940static addr_conv_t *to_addr_conv(struct stripe_head *sh,
941 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700942{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700943 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
944}
945
946static struct dma_async_tx_descriptor *
947ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
948{
Dan Williams91c00922007-01-02 13:52:30 -0700949 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700950 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700951 int target = sh->ops.target;
952 struct r5dev *tgt = &sh->dev[target];
953 struct page *xor_dest = tgt->page;
954 int count = 0;
955 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700956 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700957 int i;
958
959 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700960 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700961 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
962
963 for (i = disks; i--; )
964 if (i != target)
965 xor_srcs[count++] = sh->dev[i].page;
966
967 atomic_inc(&sh->count);
968
Dan Williams0403e382009-09-08 17:42:50 -0700969 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700970 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700971 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700972 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700973 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700974 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700975
Dan Williams91c00922007-01-02 13:52:30 -0700976 return tx;
977}
978
Dan Williamsac6b53b2009-07-14 13:40:19 -0700979/* set_syndrome_sources - populate source buffers for gen_syndrome
980 * @srcs - (struct page *) array of size sh->disks
981 * @sh - stripe_head to parse
982 *
983 * Populates srcs in proper layout order for the stripe and returns the
984 * 'count' of sources to be used in a call to async_gen_syndrome. The P
985 * destination buffer is recorded in srcs[count] and the Q destination
986 * is recorded in srcs[count+1]].
987 */
988static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
989{
990 int disks = sh->disks;
991 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
992 int d0_idx = raid6_d0(sh);
993 int count;
994 int i;
995
996 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100997 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700998
999 count = 0;
1000 i = d0_idx;
1001 do {
1002 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1003
1004 srcs[slot] = sh->dev[i].page;
1005 i = raid6_next_disk(i, disks);
1006 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001007
NeilBrowne4424fe2009-10-16 16:27:34 +11001008 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001009}
1010
1011static struct dma_async_tx_descriptor *
1012ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
1013{
1014 int disks = sh->disks;
1015 struct page **blocks = percpu->scribble;
1016 int target;
1017 int qd_idx = sh->qd_idx;
1018 struct dma_async_tx_descriptor *tx;
1019 struct async_submit_ctl submit;
1020 struct r5dev *tgt;
1021 struct page *dest;
1022 int i;
1023 int count;
1024
1025 if (sh->ops.target < 0)
1026 target = sh->ops.target2;
1027 else if (sh->ops.target2 < 0)
1028 target = sh->ops.target;
1029 else
1030 /* we should only have one valid target */
1031 BUG();
1032 BUG_ON(target < 0);
1033 pr_debug("%s: stripe %llu block: %d\n",
1034 __func__, (unsigned long long)sh->sector, target);
1035
1036 tgt = &sh->dev[target];
1037 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1038 dest = tgt->page;
1039
1040 atomic_inc(&sh->count);
1041
1042 if (target == qd_idx) {
1043 count = set_syndrome_sources(blocks, sh);
1044 blocks[count] = NULL; /* regenerating p is not necessary */
1045 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -07001046 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1047 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001048 to_addr_conv(sh, percpu));
1049 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1050 } else {
1051 /* Compute any data- or p-drive using XOR */
1052 count = 0;
1053 for (i = disks; i-- ; ) {
1054 if (i == target || i == qd_idx)
1055 continue;
1056 blocks[count++] = sh->dev[i].page;
1057 }
1058
Dan Williams0403e382009-09-08 17:42:50 -07001059 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1060 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001061 to_addr_conv(sh, percpu));
1062 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
1063 }
1064
1065 return tx;
1066}
1067
1068static struct dma_async_tx_descriptor *
1069ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1070{
1071 int i, count, disks = sh->disks;
1072 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1073 int d0_idx = raid6_d0(sh);
1074 int faila = -1, failb = -1;
1075 int target = sh->ops.target;
1076 int target2 = sh->ops.target2;
1077 struct r5dev *tgt = &sh->dev[target];
1078 struct r5dev *tgt2 = &sh->dev[target2];
1079 struct dma_async_tx_descriptor *tx;
1080 struct page **blocks = percpu->scribble;
1081 struct async_submit_ctl submit;
1082
1083 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1084 __func__, (unsigned long long)sh->sector, target, target2);
1085 BUG_ON(target < 0 || target2 < 0);
1086 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1087 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1088
Dan Williams6c910a72009-09-16 12:24:54 -07001089 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001090 * slot number conversion for 'faila' and 'failb'
1091 */
1092 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001093 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001094 count = 0;
1095 i = d0_idx;
1096 do {
1097 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1098
1099 blocks[slot] = sh->dev[i].page;
1100
1101 if (i == target)
1102 faila = slot;
1103 if (i == target2)
1104 failb = slot;
1105 i = raid6_next_disk(i, disks);
1106 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001107
1108 BUG_ON(faila == failb);
1109 if (failb < faila)
1110 swap(faila, failb);
1111 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1112 __func__, (unsigned long long)sh->sector, faila, failb);
1113
1114 atomic_inc(&sh->count);
1115
1116 if (failb == syndrome_disks+1) {
1117 /* Q disk is one of the missing disks */
1118 if (faila == syndrome_disks) {
1119 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001120 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1121 ops_complete_compute, sh,
1122 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +11001123 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001124 STRIPE_SIZE, &submit);
1125 } else {
1126 struct page *dest;
1127 int data_target;
1128 int qd_idx = sh->qd_idx;
1129
1130 /* Missing D+Q: recompute D from P, then recompute Q */
1131 if (target == qd_idx)
1132 data_target = target2;
1133 else
1134 data_target = target;
1135
1136 count = 0;
1137 for (i = disks; i-- ; ) {
1138 if (i == data_target || i == qd_idx)
1139 continue;
1140 blocks[count++] = sh->dev[i].page;
1141 }
1142 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001143 init_async_submit(&submit,
1144 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1145 NULL, NULL, NULL,
1146 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001147 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1148 &submit);
1149
1150 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001151 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1152 ops_complete_compute, sh,
1153 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001154 return async_gen_syndrome(blocks, 0, count+2,
1155 STRIPE_SIZE, &submit);
1156 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001157 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001158 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1159 ops_complete_compute, sh,
1160 to_addr_conv(sh, percpu));
1161 if (failb == syndrome_disks) {
1162 /* We're missing D+P. */
1163 return async_raid6_datap_recov(syndrome_disks+2,
1164 STRIPE_SIZE, faila,
1165 blocks, &submit);
1166 } else {
1167 /* We're missing D+D. */
1168 return async_raid6_2data_recov(syndrome_disks+2,
1169 STRIPE_SIZE, faila, failb,
1170 blocks, &submit);
1171 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001172 }
1173}
1174
1175
Dan Williams91c00922007-01-02 13:52:30 -07001176static void ops_complete_prexor(void *stripe_head_ref)
1177{
1178 struct stripe_head *sh = stripe_head_ref;
1179
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001180 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001181 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001182}
1183
1184static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001185ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1186 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001187{
Dan Williams91c00922007-01-02 13:52:30 -07001188 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001189 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001190 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001191 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001192
1193 /* existing parity data subtracted */
1194 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1195
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001196 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001197 (unsigned long long)sh->sector);
1198
1199 for (i = disks; i--; ) {
1200 struct r5dev *dev = &sh->dev[i];
1201 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001202 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001203 xor_srcs[count++] = dev->page;
1204 }
1205
Dan Williams0403e382009-09-08 17:42:50 -07001206 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001207 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001208 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001209
1210 return tx;
1211}
1212
1213static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001214ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001215{
1216 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001217 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001218
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001219 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001220 (unsigned long long)sh->sector);
1221
1222 for (i = disks; i--; ) {
1223 struct r5dev *dev = &sh->dev[i];
1224 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001225
Dan Williamsd8ee0722008-06-28 08:32:06 +10001226 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001227 struct bio *wbi;
1228
Shaohua Lib17459c2012-07-19 16:01:31 +10001229 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001230 chosen = dev->towrite;
1231 dev->towrite = NULL;
1232 BUG_ON(dev->written);
1233 wbi = dev->written = chosen;
Shaohua Lib17459c2012-07-19 16:01:31 +10001234 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001235
1236 while (wbi && wbi->bi_sector <
1237 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001238 if (wbi->bi_rw & REQ_FUA)
1239 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001240 if (wbi->bi_rw & REQ_SYNC)
1241 set_bit(R5_SyncIO, &dev->flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001242 if (wbi->bi_rw & REQ_DISCARD)
Shaohua Li620125f2012-10-11 13:49:05 +11001243 set_bit(R5_Discard, &dev->flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001244 else
Shaohua Li620125f2012-10-11 13:49:05 +11001245 tx = async_copy_data(1, wbi, dev->page,
1246 dev->sector, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001247 wbi = r5_next_bio(wbi, dev->sector);
1248 }
1249 }
1250 }
1251
1252 return tx;
1253}
1254
Dan Williamsac6b53b2009-07-14 13:40:19 -07001255static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001256{
1257 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001258 int disks = sh->disks;
1259 int pd_idx = sh->pd_idx;
1260 int qd_idx = sh->qd_idx;
1261 int i;
Shaohua Li9e4447682012-10-11 13:49:49 +11001262 bool fua = false, sync = false, discard = false;
Dan Williams91c00922007-01-02 13:52:30 -07001263
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001264 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001265 (unsigned long long)sh->sector);
1266
Shaohua Libc0934f2012-05-22 13:55:05 +10001267 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001268 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001269 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001270 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001271 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001272
Dan Williams91c00922007-01-02 13:52:30 -07001273 for (i = disks; i--; ) {
1274 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001275
Tejun Heoe9c74692010-09-03 11:56:18 +02001276 if (dev->written || i == pd_idx || i == qd_idx) {
Shaohua Li9e4447682012-10-11 13:49:49 +11001277 if (!discard)
1278 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001279 if (fua)
1280 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001281 if (sync)
1282 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001283 }
Dan Williams91c00922007-01-02 13:52:30 -07001284 }
1285
Dan Williamsd8ee0722008-06-28 08:32:06 +10001286 if (sh->reconstruct_state == reconstruct_state_drain_run)
1287 sh->reconstruct_state = reconstruct_state_drain_result;
1288 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1289 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1290 else {
1291 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1292 sh->reconstruct_state = reconstruct_state_result;
1293 }
Dan Williams91c00922007-01-02 13:52:30 -07001294
1295 set_bit(STRIPE_HANDLE, &sh->state);
1296 release_stripe(sh);
1297}
1298
1299static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001300ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1301 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001302{
Dan Williams91c00922007-01-02 13:52:30 -07001303 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001304 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001305 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001306 int count = 0, pd_idx = sh->pd_idx, i;
1307 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001308 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001309 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001310
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001311 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001312 (unsigned long long)sh->sector);
1313
Shaohua Li620125f2012-10-11 13:49:05 +11001314 for (i = 0; i < sh->disks; i++) {
1315 if (pd_idx == i)
1316 continue;
1317 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1318 break;
1319 }
1320 if (i >= sh->disks) {
1321 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001322 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1323 ops_complete_reconstruct(sh);
1324 return;
1325 }
Dan Williams91c00922007-01-02 13:52:30 -07001326 /* check if prexor is active which means only process blocks
1327 * that are part of a read-modify-write (written)
1328 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001329 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1330 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001331 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1332 for (i = disks; i--; ) {
1333 struct r5dev *dev = &sh->dev[i];
1334 if (dev->written)
1335 xor_srcs[count++] = dev->page;
1336 }
1337 } else {
1338 xor_dest = sh->dev[pd_idx].page;
1339 for (i = disks; i--; ) {
1340 struct r5dev *dev = &sh->dev[i];
1341 if (i != pd_idx)
1342 xor_srcs[count++] = dev->page;
1343 }
1344 }
1345
Dan Williams91c00922007-01-02 13:52:30 -07001346 /* 1/ if we prexor'd then the dest is reused as a source
1347 * 2/ if we did not prexor then we are redoing the parity
1348 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1349 * for the synchronous xor case
1350 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001351 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001352 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1353
1354 atomic_inc(&sh->count);
1355
Dan Williamsac6b53b2009-07-14 13:40:19 -07001356 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001357 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001358 if (unlikely(count == 1))
1359 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1360 else
1361 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001362}
1363
Dan Williamsac6b53b2009-07-14 13:40:19 -07001364static void
1365ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1366 struct dma_async_tx_descriptor *tx)
1367{
1368 struct async_submit_ctl submit;
1369 struct page **blocks = percpu->scribble;
Shaohua Li620125f2012-10-11 13:49:05 +11001370 int count, i;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001371
1372 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1373
Shaohua Li620125f2012-10-11 13:49:05 +11001374 for (i = 0; i < sh->disks; i++) {
1375 if (sh->pd_idx == i || sh->qd_idx == i)
1376 continue;
1377 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1378 break;
1379 }
1380 if (i >= sh->disks) {
1381 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001382 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1383 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1384 ops_complete_reconstruct(sh);
1385 return;
1386 }
1387
Dan Williamsac6b53b2009-07-14 13:40:19 -07001388 count = set_syndrome_sources(blocks, sh);
1389
1390 atomic_inc(&sh->count);
1391
1392 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1393 sh, to_addr_conv(sh, percpu));
1394 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001395}
1396
1397static void ops_complete_check(void *stripe_head_ref)
1398{
1399 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001400
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001401 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001402 (unsigned long long)sh->sector);
1403
Dan Williamsecc65c92008-06-28 08:31:57 +10001404 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001405 set_bit(STRIPE_HANDLE, &sh->state);
1406 release_stripe(sh);
1407}
1408
Dan Williamsac6b53b2009-07-14 13:40:19 -07001409static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001410{
Dan Williams91c00922007-01-02 13:52:30 -07001411 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001412 int pd_idx = sh->pd_idx;
1413 int qd_idx = sh->qd_idx;
1414 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001415 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001416 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001417 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001418 int count;
1419 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001420
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001421 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001422 (unsigned long long)sh->sector);
1423
Dan Williamsac6b53b2009-07-14 13:40:19 -07001424 count = 0;
1425 xor_dest = sh->dev[pd_idx].page;
1426 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001427 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001428 if (i == pd_idx || i == qd_idx)
1429 continue;
1430 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001431 }
1432
Dan Williamsd6f38f32009-07-14 11:50:52 -07001433 init_async_submit(&submit, 0, NULL, NULL, NULL,
1434 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001435 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001436 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001437
Dan Williams91c00922007-01-02 13:52:30 -07001438 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001439 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1440 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001441}
1442
Dan Williamsac6b53b2009-07-14 13:40:19 -07001443static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1444{
1445 struct page **srcs = percpu->scribble;
1446 struct async_submit_ctl submit;
1447 int count;
1448
1449 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1450 (unsigned long long)sh->sector, checkp);
1451
1452 count = set_syndrome_sources(srcs, sh);
1453 if (!checkp)
1454 srcs[count] = NULL;
1455
1456 atomic_inc(&sh->count);
1457 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1458 sh, to_addr_conv(sh, percpu));
1459 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1460 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1461}
1462
NeilBrown51acbce2013-02-28 09:08:34 +11001463static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001464{
1465 int overlap_clear = 0, i, disks = sh->disks;
1466 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001467 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001468 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001469 struct raid5_percpu *percpu;
1470 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001471
Dan Williamsd6f38f32009-07-14 11:50:52 -07001472 cpu = get_cpu();
1473 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001474 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001475 ops_run_biofill(sh);
1476 overlap_clear++;
1477 }
1478
Dan Williams7b3a8712008-06-28 08:32:09 +10001479 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001480 if (level < 6)
1481 tx = ops_run_compute5(sh, percpu);
1482 else {
1483 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1484 tx = ops_run_compute6_1(sh, percpu);
1485 else
1486 tx = ops_run_compute6_2(sh, percpu);
1487 }
1488 /* terminate the chain if reconstruct is not set to be run */
1489 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001490 async_tx_ack(tx);
1491 }
Dan Williams91c00922007-01-02 13:52:30 -07001492
Dan Williams600aa102008-06-28 08:32:05 +10001493 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001494 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001495
Dan Williams600aa102008-06-28 08:32:05 +10001496 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001497 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001498 overlap_clear++;
1499 }
1500
Dan Williamsac6b53b2009-07-14 13:40:19 -07001501 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1502 if (level < 6)
1503 ops_run_reconstruct5(sh, percpu, tx);
1504 else
1505 ops_run_reconstruct6(sh, percpu, tx);
1506 }
Dan Williams91c00922007-01-02 13:52:30 -07001507
Dan Williamsac6b53b2009-07-14 13:40:19 -07001508 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1509 if (sh->check_state == check_state_run)
1510 ops_run_check_p(sh, percpu);
1511 else if (sh->check_state == check_state_run_q)
1512 ops_run_check_pq(sh, percpu, 0);
1513 else if (sh->check_state == check_state_run_pq)
1514 ops_run_check_pq(sh, percpu, 1);
1515 else
1516 BUG();
1517 }
Dan Williams91c00922007-01-02 13:52:30 -07001518
Dan Williams91c00922007-01-02 13:52:30 -07001519 if (overlap_clear)
1520 for (i = disks; i--; ) {
1521 struct r5dev *dev = &sh->dev[i];
1522 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1523 wake_up(&sh->raid_conf->wait_for_overlap);
1524 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001525 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001526}
1527
NeilBrownd1688a62011-10-11 16:49:52 +11001528static int grow_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
1530 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001531 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001532 if (!sh)
1533 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001534
NeilBrown3f294f42005-11-08 21:39:25 -08001535 sh->raid_conf = conf;
NeilBrown3f294f42005-11-08 21:39:25 -08001536
Shaohua Lib17459c2012-07-19 16:01:31 +10001537 spin_lock_init(&sh->stripe_lock);
1538
NeilBrowne4e11e32010-06-16 16:45:16 +10001539 if (grow_buffers(sh)) {
1540 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001541 kmem_cache_free(conf->slab_cache, sh);
1542 return 0;
1543 }
1544 /* we just created an active stripe so... */
1545 atomic_set(&sh->count, 1);
1546 atomic_inc(&conf->active_stripes);
1547 INIT_LIST_HEAD(&sh->lru);
1548 release_stripe(sh);
1549 return 1;
1550}
1551
NeilBrownd1688a62011-10-11 16:49:52 +11001552static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001553{
Christoph Lametere18b8902006-12-06 20:33:20 -08001554 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001555 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
NeilBrownf4be6b42010-06-01 19:37:25 +10001557 if (conf->mddev->gendisk)
1558 sprintf(conf->cache_name[0],
1559 "raid%d-%s", conf->level, mdname(conf->mddev));
1560 else
1561 sprintf(conf->cache_name[0],
1562 "raid%d-%p", conf->level, conf->mddev);
1563 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1564
NeilBrownad01c9e2006-03-27 01:18:07 -08001565 conf->active_name = 0;
1566 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001568 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 if (!sc)
1570 return 1;
1571 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001572 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001573 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001574 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 return 0;
1577}
NeilBrown29269552006-03-27 01:18:10 -08001578
Dan Williamsd6f38f32009-07-14 11:50:52 -07001579/**
1580 * scribble_len - return the required size of the scribble region
1581 * @num - total number of disks in the array
1582 *
1583 * The size must be enough to contain:
1584 * 1/ a struct page pointer for each device in the array +2
1585 * 2/ room to convert each entry in (1) to its corresponding dma
1586 * (dma_map_page()) or page (page_address()) address.
1587 *
1588 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1589 * calculate over all devices (not just the data blocks), using zeros in place
1590 * of the P and Q blocks.
1591 */
1592static size_t scribble_len(int num)
1593{
1594 size_t len;
1595
1596 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1597
1598 return len;
1599}
1600
NeilBrownd1688a62011-10-11 16:49:52 +11001601static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001602{
1603 /* Make all the stripes able to hold 'newsize' devices.
1604 * New slots in each stripe get 'page' set to a new page.
1605 *
1606 * This happens in stages:
1607 * 1/ create a new kmem_cache and allocate the required number of
1608 * stripe_heads.
Masanari Iida83f0d772012-10-30 00:18:08 +09001609 * 2/ gather all the old stripe_heads and transfer the pages across
NeilBrownad01c9e2006-03-27 01:18:07 -08001610 * to the new stripe_heads. This will have the side effect of
1611 * freezing the array as once all stripe_heads have been collected,
1612 * no IO will be possible. Old stripe heads are freed once their
1613 * pages have been transferred over, and the old kmem_cache is
1614 * freed when all stripes are done.
1615 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1616 * we simple return a failre status - no need to clean anything up.
1617 * 4/ allocate new pages for the new slots in the new stripe_heads.
1618 * If this fails, we don't bother trying the shrink the
1619 * stripe_heads down again, we just leave them as they are.
1620 * As each stripe_head is processed the new one is released into
1621 * active service.
1622 *
1623 * Once step2 is started, we cannot afford to wait for a write,
1624 * so we use GFP_NOIO allocations.
1625 */
1626 struct stripe_head *osh, *nsh;
1627 LIST_HEAD(newstripes);
1628 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001629 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001630 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001631 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001632 int i;
1633
1634 if (newsize <= conf->pool_size)
1635 return 0; /* never bother to shrink */
1636
Dan Williamsb5470dc2008-06-27 21:44:04 -07001637 err = md_allow_write(conf->mddev);
1638 if (err)
1639 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001640
NeilBrownad01c9e2006-03-27 01:18:07 -08001641 /* Step 1 */
1642 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1643 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001644 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001645 if (!sc)
1646 return -ENOMEM;
1647
1648 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001649 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001650 if (!nsh)
1651 break;
1652
NeilBrownad01c9e2006-03-27 01:18:07 -08001653 nsh->raid_conf = conf;
NeilBrowncb13ff62012-09-24 16:27:20 +10001654 spin_lock_init(&nsh->stripe_lock);
NeilBrownad01c9e2006-03-27 01:18:07 -08001655
1656 list_add(&nsh->lru, &newstripes);
1657 }
1658 if (i) {
1659 /* didn't get enough, give up */
1660 while (!list_empty(&newstripes)) {
1661 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1662 list_del(&nsh->lru);
1663 kmem_cache_free(sc, nsh);
1664 }
1665 kmem_cache_destroy(sc);
1666 return -ENOMEM;
1667 }
1668 /* Step 2 - Must use GFP_NOIO now.
1669 * OK, we have enough stripes, start collecting inactive
1670 * stripes and copying them over
1671 */
1672 list_for_each_entry(nsh, &newstripes, lru) {
1673 spin_lock_irq(&conf->device_lock);
1674 wait_event_lock_irq(conf->wait_for_stripe,
1675 !list_empty(&conf->inactive_list),
Lukas Czernereed8c022012-11-30 11:42:40 +01001676 conf->device_lock);
NeilBrownad01c9e2006-03-27 01:18:07 -08001677 osh = get_free_stripe(conf);
1678 spin_unlock_irq(&conf->device_lock);
1679 atomic_set(&nsh->count, 1);
1680 for(i=0; i<conf->pool_size; i++)
1681 nsh->dev[i].page = osh->dev[i].page;
1682 for( ; i<newsize; i++)
1683 nsh->dev[i].page = NULL;
1684 kmem_cache_free(conf->slab_cache, osh);
1685 }
1686 kmem_cache_destroy(conf->slab_cache);
1687
1688 /* Step 3.
1689 * At this point, we are holding all the stripes so the array
1690 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001691 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001692 */
1693 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1694 if (ndisks) {
1695 for (i=0; i<conf->raid_disks; i++)
1696 ndisks[i] = conf->disks[i];
1697 kfree(conf->disks);
1698 conf->disks = ndisks;
1699 } else
1700 err = -ENOMEM;
1701
Dan Williamsd6f38f32009-07-14 11:50:52 -07001702 get_online_cpus();
1703 conf->scribble_len = scribble_len(newsize);
1704 for_each_present_cpu(cpu) {
1705 struct raid5_percpu *percpu;
1706 void *scribble;
1707
1708 percpu = per_cpu_ptr(conf->percpu, cpu);
1709 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1710
1711 if (scribble) {
1712 kfree(percpu->scribble);
1713 percpu->scribble = scribble;
1714 } else {
1715 err = -ENOMEM;
1716 break;
1717 }
1718 }
1719 put_online_cpus();
1720
NeilBrownad01c9e2006-03-27 01:18:07 -08001721 /* Step 4, return new stripes to service */
1722 while(!list_empty(&newstripes)) {
1723 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1724 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001725
NeilBrownad01c9e2006-03-27 01:18:07 -08001726 for (i=conf->raid_disks; i < newsize; i++)
1727 if (nsh->dev[i].page == NULL) {
1728 struct page *p = alloc_page(GFP_NOIO);
1729 nsh->dev[i].page = p;
1730 if (!p)
1731 err = -ENOMEM;
1732 }
1733 release_stripe(nsh);
1734 }
1735 /* critical section pass, GFP_NOIO no longer needed */
1736
1737 conf->slab_cache = sc;
1738 conf->active_name = 1-conf->active_name;
1739 conf->pool_size = newsize;
1740 return err;
1741}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
NeilBrownd1688a62011-10-11 16:49:52 +11001743static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
1745 struct stripe_head *sh;
1746
NeilBrown3f294f42005-11-08 21:39:25 -08001747 spin_lock_irq(&conf->device_lock);
1748 sh = get_free_stripe(conf);
1749 spin_unlock_irq(&conf->device_lock);
1750 if (!sh)
1751 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001752 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001753 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001754 kmem_cache_free(conf->slab_cache, sh);
1755 atomic_dec(&conf->active_stripes);
1756 return 1;
1757}
1758
NeilBrownd1688a62011-10-11 16:49:52 +11001759static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001760{
1761 while (drop_one_stripe(conf))
1762 ;
1763
NeilBrown29fc7e32006-02-03 03:03:41 -08001764 if (conf->slab_cache)
1765 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 conf->slab_cache = NULL;
1767}
1768
NeilBrown6712ecf2007-09-27 12:47:43 +02001769static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
NeilBrown99c0fb52009-03-31 14:39:38 +11001771 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001772 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001773 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001775 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11001776 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10001777 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
1779 for (i=0 ; i<disks; i++)
1780 if (bi == &sh->dev[i].req)
1781 break;
1782
Dan Williams45b42332007-07-09 11:56:43 -07001783 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1784 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 uptodate);
1786 if (i == disks) {
1787 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001788 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 }
NeilBrown14a75d32011-12-23 10:17:52 +11001790 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11001791 /* If replacement finished while this request was outstanding,
1792 * 'replacement' might be NULL already.
1793 * In that case it moved down to 'rdev'.
1794 * rdev is not removed until all requests are finished.
1795 */
NeilBrown14a75d32011-12-23 10:17:52 +11001796 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001797 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11001798 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
NeilBrown05616be2012-05-21 09:27:00 +10001800 if (use_new_offset(conf, sh))
1801 s = sh->sector + rdev->new_data_offset;
1802 else
1803 s = sh->sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001806 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11001807 /* Note that this cannot happen on a
1808 * replacement device. We just fail those on
1809 * any error
1810 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001811 printk_ratelimited(
1812 KERN_INFO
1813 "md/raid:%s: read error corrected"
1814 " (%lu sectors at %llu on %s)\n",
1815 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10001816 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001817 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001818 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001819 clear_bit(R5_ReadError, &sh->dev[i].flags);
1820 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng3f9e7c12012-07-31 10:04:21 +10001821 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
1822 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1823
NeilBrown14a75d32011-12-23 10:17:52 +11001824 if (atomic_read(&rdev->read_errors))
1825 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11001827 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001828 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10001829 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001832 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11001833 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1834 printk_ratelimited(
1835 KERN_WARNING
1836 "md/raid:%s: read error on replacement device "
1837 "(sector %llu on %s).\n",
1838 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001839 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11001840 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001841 else if (conf->mddev->degraded >= conf->max_degraded) {
1842 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001843 printk_ratelimited(
1844 KERN_WARNING
1845 "md/raid:%s: read error not correctable "
1846 "(sector %llu on %s).\n",
1847 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001848 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001849 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001850 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08001851 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10001852 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001853 printk_ratelimited(
1854 KERN_WARNING
1855 "md/raid:%s: read error NOT corrected!! "
1856 "(sector %llu on %s).\n",
1857 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001858 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001859 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001860 } else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001861 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001862 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001863 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001864 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001865 else
1866 retry = 1;
1867 if (retry)
majianpeng3f9e7c12012-07-31 10:04:21 +10001868 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
1869 set_bit(R5_ReadError, &sh->dev[i].flags);
1870 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1871 } else
1872 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -08001873 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001874 clear_bit(R5_ReadError, &sh->dev[i].flags);
1875 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10001876 if (!(set_bad
1877 && test_bit(In_sync, &rdev->flags)
1878 && rdev_set_badblocks(
1879 rdev, sh->sector, STRIPE_SECTORS, 0)))
1880 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 }
NeilBrown14a75d32011-12-23 10:17:52 +11001883 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1885 set_bit(STRIPE_HANDLE, &sh->state);
1886 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887}
1888
NeilBrownd710e132008-10-13 11:55:12 +11001889static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890{
NeilBrown99c0fb52009-03-31 14:39:38 +11001891 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001892 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001893 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11001894 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001896 sector_t first_bad;
1897 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11001898 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
NeilBrown977df362011-12-23 10:17:53 +11001900 for (i = 0 ; i < disks; i++) {
1901 if (bi == &sh->dev[i].req) {
1902 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 break;
NeilBrown977df362011-12-23 10:17:53 +11001904 }
1905 if (bi == &sh->dev[i].rreq) {
1906 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001907 if (rdev)
1908 replacement = 1;
1909 else
1910 /* rdev was removed and 'replacement'
1911 * replaced it. rdev is not removed
1912 * until all requests are finished.
1913 */
1914 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11001915 break;
1916 }
1917 }
Dan Williams45b42332007-07-09 11:56:43 -07001918 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1920 uptodate);
1921 if (i == disks) {
1922 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001923 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 }
1925
NeilBrown977df362011-12-23 10:17:53 +11001926 if (replacement) {
1927 if (!uptodate)
1928 md_error(conf->mddev, rdev);
1929 else if (is_badblock(rdev, sh->sector,
1930 STRIPE_SECTORS,
1931 &first_bad, &bad_sectors))
1932 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1933 } else {
1934 if (!uptodate) {
1935 set_bit(WriteErrorSeen, &rdev->flags);
1936 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11001937 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1938 set_bit(MD_RECOVERY_NEEDED,
1939 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11001940 } else if (is_badblock(rdev, sh->sector,
1941 STRIPE_SECTORS,
NeilBrownc0b32972013-04-24 11:42:42 +10001942 &first_bad, &bad_sectors)) {
NeilBrown977df362011-12-23 10:17:53 +11001943 set_bit(R5_MadeGood, &sh->dev[i].flags);
NeilBrownc0b32972013-04-24 11:42:42 +10001944 if (test_bit(R5_ReadError, &sh->dev[i].flags))
1945 /* That was a successful write so make
1946 * sure it looks like we already did
1947 * a re-write.
1948 */
1949 set_bit(R5_ReWrite, &sh->dev[i].flags);
1950 }
NeilBrown977df362011-12-23 10:17:53 +11001951 }
1952 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
NeilBrown977df362011-12-23 10:17:53 +11001954 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1955 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001957 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958}
1959
NeilBrown784052e2009-03-31 15:19:07 +11001960static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
NeilBrown784052e2009-03-31 15:19:07 +11001962static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963{
1964 struct r5dev *dev = &sh->dev[i];
1965
1966 bio_init(&dev->req);
1967 dev->req.bi_io_vec = &dev->vec;
1968 dev->req.bi_vcnt++;
1969 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11001971 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
NeilBrown977df362011-12-23 10:17:53 +11001973 bio_init(&dev->rreq);
1974 dev->rreq.bi_io_vec = &dev->rvec;
1975 dev->rreq.bi_vcnt++;
1976 dev->rreq.bi_max_vecs++;
1977 dev->rreq.bi_private = sh;
1978 dev->rvec.bv_page = dev->page;
1979
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001981 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982}
1983
NeilBrownfd01b882011-10-11 16:47:53 +11001984static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985{
1986 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11001987 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11001988 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10001989 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
NeilBrown908f4fb2011-12-23 10:17:50 +11001991 spin_lock_irqsave(&conf->device_lock, flags);
1992 clear_bit(In_sync, &rdev->flags);
1993 mddev->degraded = calc_degraded(conf);
1994 spin_unlock_irqrestore(&conf->device_lock, flags);
1995 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1996
NeilBrownde393cd2011-07-28 11:31:48 +10001997 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001998 set_bit(Faulty, &rdev->flags);
1999 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2000 printk(KERN_ALERT
2001 "md/raid:%s: Disk failure on %s, disabling device.\n"
2002 "md/raid:%s: Operation continuing on %d devices.\n",
2003 mdname(mddev),
2004 bdevname(rdev->bdev, b),
2005 mdname(mddev),
2006 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07002007}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
2009/*
2010 * Input: a 'big' sector number,
2011 * Output: index of the data and parity disk, and the sector # in them.
2012 */
NeilBrownd1688a62011-10-11 16:49:52 +11002013static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11002014 int previous, int *dd_idx,
2015 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016{
NeilBrown6e3b96e2010-04-23 07:08:28 +10002017 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10002018 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11002020 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002021 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11002023 int algorithm = previous ? conf->prev_algo
2024 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002025 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2026 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11002027 int raid_disks = previous ? conf->previous_raid_disks
2028 : conf->raid_disks;
2029 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
2031 /* First compute the information on this sector */
2032
2033 /*
2034 * Compute the chunk number and the sector offset inside the chunk
2035 */
2036 chunk_offset = sector_div(r_sector, sectors_per_chunk);
2037 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
2039 /*
2040 * Compute the stripe number
2041 */
NeilBrown35f2a592010-04-20 14:13:34 +10002042 stripe = chunk_number;
2043 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10002044 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 /*
2046 * Select the parity disk based on the user selected algorithm.
2047 */
NeilBrown84789552011-07-27 11:00:36 +10002048 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07002049 switch(conf->level) {
2050 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11002051 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002052 break;
2053 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002054 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002056 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002057 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 (*dd_idx)++;
2059 break;
2060 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002061 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002062 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 (*dd_idx)++;
2064 break;
2065 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002066 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002067 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 break;
2069 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002070 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002071 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002073 case ALGORITHM_PARITY_0:
2074 pd_idx = 0;
2075 (*dd_idx)++;
2076 break;
2077 case ALGORITHM_PARITY_N:
2078 pd_idx = data_disks;
2079 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002081 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002082 }
2083 break;
2084 case 6:
2085
NeilBrowne183eae2009-03-31 15:20:22 +11002086 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002087 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002088 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002089 qd_idx = pd_idx + 1;
2090 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002091 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002092 qd_idx = 0;
2093 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002094 (*dd_idx) += 2; /* D D P Q D */
2095 break;
2096 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002097 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002098 qd_idx = pd_idx + 1;
2099 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002100 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002101 qd_idx = 0;
2102 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002103 (*dd_idx) += 2; /* D D P Q D */
2104 break;
2105 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002106 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002107 qd_idx = (pd_idx + 1) % raid_disks;
2108 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002109 break;
2110 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002111 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002112 qd_idx = (pd_idx + 1) % raid_disks;
2113 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002114 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002115
2116 case ALGORITHM_PARITY_0:
2117 pd_idx = 0;
2118 qd_idx = 1;
2119 (*dd_idx) += 2;
2120 break;
2121 case ALGORITHM_PARITY_N:
2122 pd_idx = data_disks;
2123 qd_idx = data_disks + 1;
2124 break;
2125
2126 case ALGORITHM_ROTATING_ZERO_RESTART:
2127 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2128 * of blocks for computing Q is different.
2129 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002130 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002131 qd_idx = pd_idx + 1;
2132 if (pd_idx == raid_disks-1) {
2133 (*dd_idx)++; /* Q D D D P */
2134 qd_idx = 0;
2135 } else if (*dd_idx >= pd_idx)
2136 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002137 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002138 break;
2139
2140 case ALGORITHM_ROTATING_N_RESTART:
2141 /* Same a left_asymmetric, by first stripe is
2142 * D D D P Q rather than
2143 * Q D D D P
2144 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002145 stripe2 += 1;
2146 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002147 qd_idx = pd_idx + 1;
2148 if (pd_idx == raid_disks-1) {
2149 (*dd_idx)++; /* Q D D D P */
2150 qd_idx = 0;
2151 } else if (*dd_idx >= pd_idx)
2152 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002153 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002154 break;
2155
2156 case ALGORITHM_ROTATING_N_CONTINUE:
2157 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002158 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002159 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2160 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002161 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002162 break;
2163
2164 case ALGORITHM_LEFT_ASYMMETRIC_6:
2165 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002166 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002167 if (*dd_idx >= pd_idx)
2168 (*dd_idx)++;
2169 qd_idx = raid_disks - 1;
2170 break;
2171
2172 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002173 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002174 if (*dd_idx >= pd_idx)
2175 (*dd_idx)++;
2176 qd_idx = raid_disks - 1;
2177 break;
2178
2179 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002180 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002181 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2182 qd_idx = raid_disks - 1;
2183 break;
2184
2185 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002186 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002187 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2188 qd_idx = raid_disks - 1;
2189 break;
2190
2191 case ALGORITHM_PARITY_0_6:
2192 pd_idx = 0;
2193 (*dd_idx)++;
2194 qd_idx = raid_disks - 1;
2195 break;
2196
NeilBrown16a53ec2006-06-26 00:27:38 -07002197 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002198 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002199 }
2200 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 }
2202
NeilBrown911d4ee2009-03-31 14:39:38 +11002203 if (sh) {
2204 sh->pd_idx = pd_idx;
2205 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002206 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 /*
2209 * Finally, compute the new sector number
2210 */
2211 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2212 return new_sector;
2213}
2214
2215
NeilBrown784052e2009-03-31 15:19:07 +11002216static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217{
NeilBrownd1688a62011-10-11 16:49:52 +11002218 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002219 int raid_disks = sh->disks;
2220 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002222 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2223 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002224 int algorithm = previous ? conf->prev_algo
2225 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 sector_t stripe;
2227 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002228 sector_t chunk_number;
2229 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002231 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
NeilBrown16a53ec2006-06-26 00:27:38 -07002233
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2235 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
NeilBrown16a53ec2006-06-26 00:27:38 -07002237 if (i == sh->pd_idx)
2238 return 0;
2239 switch(conf->level) {
2240 case 4: break;
2241 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002242 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 case ALGORITHM_LEFT_ASYMMETRIC:
2244 case ALGORITHM_RIGHT_ASYMMETRIC:
2245 if (i > sh->pd_idx)
2246 i--;
2247 break;
2248 case ALGORITHM_LEFT_SYMMETRIC:
2249 case ALGORITHM_RIGHT_SYMMETRIC:
2250 if (i < sh->pd_idx)
2251 i += raid_disks;
2252 i -= (sh->pd_idx + 1);
2253 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002254 case ALGORITHM_PARITY_0:
2255 i -= 1;
2256 break;
2257 case ALGORITHM_PARITY_N:
2258 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002260 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002261 }
2262 break;
2263 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002264 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002265 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002266 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002267 case ALGORITHM_LEFT_ASYMMETRIC:
2268 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002269 case ALGORITHM_ROTATING_ZERO_RESTART:
2270 case ALGORITHM_ROTATING_N_RESTART:
2271 if (sh->pd_idx == raid_disks-1)
2272 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002273 else if (i > sh->pd_idx)
2274 i -= 2; /* D D P Q D */
2275 break;
2276 case ALGORITHM_LEFT_SYMMETRIC:
2277 case ALGORITHM_RIGHT_SYMMETRIC:
2278 if (sh->pd_idx == raid_disks-1)
2279 i--; /* Q D D D P */
2280 else {
2281 /* D D P Q D */
2282 if (i < sh->pd_idx)
2283 i += raid_disks;
2284 i -= (sh->pd_idx + 2);
2285 }
2286 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002287 case ALGORITHM_PARITY_0:
2288 i -= 2;
2289 break;
2290 case ALGORITHM_PARITY_N:
2291 break;
2292 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002293 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002294 if (sh->pd_idx == 0)
2295 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002296 else {
2297 /* D D Q P D */
2298 if (i < sh->pd_idx)
2299 i += raid_disks;
2300 i -= (sh->pd_idx + 1);
2301 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002302 break;
2303 case ALGORITHM_LEFT_ASYMMETRIC_6:
2304 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2305 if (i > sh->pd_idx)
2306 i--;
2307 break;
2308 case ALGORITHM_LEFT_SYMMETRIC_6:
2309 case ALGORITHM_RIGHT_SYMMETRIC_6:
2310 if (i < sh->pd_idx)
2311 i += data_disks + 1;
2312 i -= (sh->pd_idx + 1);
2313 break;
2314 case ALGORITHM_PARITY_0_6:
2315 i -= 1;
2316 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002317 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002318 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002319 }
2320 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 }
2322
2323 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002324 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
NeilBrown112bf892009-03-31 14:39:38 +11002326 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002327 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002328 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2329 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002330 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2331 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 return 0;
2333 }
2334 return r_sector;
2335}
2336
2337
Dan Williams600aa102008-06-28 08:32:05 +10002338static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002339schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002340 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002341{
2342 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002343 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002344 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002345
Dan Williamse33129d2007-01-02 13:52:30 -07002346 if (rcw) {
Dan Williamse33129d2007-01-02 13:52:30 -07002347
2348 for (i = disks; i--; ) {
2349 struct r5dev *dev = &sh->dev[i];
2350
2351 if (dev->towrite) {
2352 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002353 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002354 if (!expand)
2355 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002356 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002357 }
2358 }
NeilBrownce7d3632013-03-04 12:37:14 +11002359 /* if we are not expanding this is a proper write request, and
2360 * there will be bios with new data to be drained into the
2361 * stripe cache
2362 */
2363 if (!expand) {
2364 if (!s->locked)
2365 /* False alarm, nothing to do */
2366 return;
2367 sh->reconstruct_state = reconstruct_state_drain_run;
2368 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2369 } else
2370 sh->reconstruct_state = reconstruct_state_run;
2371
2372 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
2373
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002374 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002375 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002376 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002377 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002378 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002379 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2380 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2381
Dan Williamse33129d2007-01-02 13:52:30 -07002382 for (i = disks; i--; ) {
2383 struct r5dev *dev = &sh->dev[i];
2384 if (i == pd_idx)
2385 continue;
2386
Dan Williamse33129d2007-01-02 13:52:30 -07002387 if (dev->towrite &&
2388 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002389 test_bit(R5_Wantcompute, &dev->flags))) {
2390 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002391 set_bit(R5_LOCKED, &dev->flags);
2392 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002393 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002394 }
2395 }
NeilBrownce7d3632013-03-04 12:37:14 +11002396 if (!s->locked)
2397 /* False alarm - nothing to do */
2398 return;
2399 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
2400 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2401 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2402 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002403 }
2404
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002405 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002406 * are in flight
2407 */
2408 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2409 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002410 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002411
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002412 if (level == 6) {
2413 int qd_idx = sh->qd_idx;
2414 struct r5dev *dev = &sh->dev[qd_idx];
2415
2416 set_bit(R5_LOCKED, &dev->flags);
2417 clear_bit(R5_UPTODATE, &dev->flags);
2418 s->locked++;
2419 }
2420
Dan Williams600aa102008-06-28 08:32:05 +10002421 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07002422 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002423 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002424}
NeilBrown16a53ec2006-06-26 00:27:38 -07002425
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426/*
2427 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002428 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 * The bi_next chain must be in order.
2430 */
2431static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2432{
2433 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002434 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002435 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
NeilBrowncbe47ec2011-07-26 11:20:35 +10002437 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 (unsigned long long)bi->bi_sector,
2439 (unsigned long long)sh->sector);
2440
Shaohua Lib17459c2012-07-19 16:01:31 +10002441 /*
2442 * If several bio share a stripe. The bio bi_phys_segments acts as a
2443 * reference count to avoid race. The reference count should already be
2444 * increased before this function is called (for example, in
2445 * make_request()), so other bio sharing this stripe will not free the
2446 * stripe. If a stripe is owned by one stripe, the stripe lock will
2447 * protect it.
2448 */
2449 spin_lock_irq(&sh->stripe_lock);
NeilBrown72626682005-09-09 16:23:54 -07002450 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002452 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07002453 firstwrite = 1;
2454 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 bip = &sh->dev[dd_idx].toread;
2456 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002457 if (bio_end_sector(*bip) > bi->bi_sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 goto overlap;
2459 bip = & (*bip)->bi_next;
2460 }
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002461 if (*bip && (*bip)->bi_sector < bio_end_sector(bi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 goto overlap;
2463
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002464 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 if (*bip)
2466 bi->bi_next = *bip;
2467 *bip = bi;
Shaohua Lie7836bd62012-07-19 16:01:31 +10002468 raid5_inc_bi_active_stripes(bi);
NeilBrown72626682005-09-09 16:23:54 -07002469
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 if (forwrite) {
2471 /* check if page is covered */
2472 sector_t sector = sh->dev[dd_idx].sector;
2473 for (bi=sh->dev[dd_idx].towrite;
2474 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2475 bi && bi->bi_sector <= sector;
2476 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002477 if (bio_end_sector(bi) >= sector)
2478 sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 }
2480 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2481 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2482 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002483
2484 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2485 (unsigned long long)(*bip)->bi_sector,
2486 (unsigned long long)sh->sector, dd_idx);
NeilBrownb97390a2012-10-11 13:50:12 +11002487 spin_unlock_irq(&sh->stripe_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002488
2489 if (conf->mddev->bitmap && firstwrite) {
2490 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2491 STRIPE_SECTORS, 0);
2492 sh->bm_seq = conf->seq_flush+1;
2493 set_bit(STRIPE_BIT_DELAY, &sh->state);
2494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 return 1;
2496
2497 overlap:
2498 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10002499 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 return 0;
2501}
2502
NeilBrownd1688a62011-10-11 16:49:52 +11002503static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002504
NeilBrownd1688a62011-10-11 16:49:52 +11002505static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002506 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002507{
NeilBrown784052e2009-03-31 15:19:07 +11002508 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002509 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002510 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002511 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002512 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002513
NeilBrown112bf892009-03-31 14:39:38 +11002514 raid5_compute_sector(conf,
2515 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002516 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002517 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002518 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002519}
2520
Dan Williamsa4456852007-07-09 11:56:43 -07002521static void
NeilBrownd1688a62011-10-11 16:49:52 +11002522handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002523 struct stripe_head_state *s, int disks,
2524 struct bio **return_bi)
2525{
2526 int i;
2527 for (i = disks; i--; ) {
2528 struct bio *bi;
2529 int bitmap_end = 0;
2530
2531 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002532 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002533 rcu_read_lock();
2534 rdev = rcu_dereference(conf->disks[i].rdev);
2535 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002536 atomic_inc(&rdev->nr_pending);
2537 else
2538 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002539 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002540 if (rdev) {
2541 if (!rdev_set_badblocks(
2542 rdev,
2543 sh->sector,
2544 STRIPE_SECTORS, 0))
2545 md_error(conf->mddev, rdev);
2546 rdev_dec_pending(rdev, conf->mddev);
2547 }
Dan Williamsa4456852007-07-09 11:56:43 -07002548 }
Shaohua Lib17459c2012-07-19 16:01:31 +10002549 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002550 /* fail all writes first */
2551 bi = sh->dev[i].towrite;
2552 sh->dev[i].towrite = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10002553 spin_unlock_irq(&sh->stripe_lock);
NeilBrown1ed850f2012-10-11 13:50:13 +11002554 if (bi)
Dan Williamsa4456852007-07-09 11:56:43 -07002555 bitmap_end = 1;
Dan Williamsa4456852007-07-09 11:56:43 -07002556
2557 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2558 wake_up(&conf->wait_for_overlap);
2559
2560 while (bi && bi->bi_sector <
2561 sh->dev[i].sector + STRIPE_SECTORS) {
2562 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2563 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002564 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002565 md_write_end(conf->mddev);
2566 bi->bi_next = *return_bi;
2567 *return_bi = bi;
2568 }
2569 bi = nextbi;
2570 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002571 if (bitmap_end)
2572 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2573 STRIPE_SECTORS, 0, 0);
2574 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002575 /* and fail all 'written' */
2576 bi = sh->dev[i].written;
2577 sh->dev[i].written = NULL;
2578 if (bi) bitmap_end = 1;
2579 while (bi && bi->bi_sector <
2580 sh->dev[i].sector + STRIPE_SECTORS) {
2581 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2582 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002583 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002584 md_write_end(conf->mddev);
2585 bi->bi_next = *return_bi;
2586 *return_bi = bi;
2587 }
2588 bi = bi2;
2589 }
2590
Dan Williamsb5e98d62007-01-02 13:52:31 -07002591 /* fail any reads if this device is non-operational and
2592 * the data has not reached the cache yet.
2593 */
2594 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2595 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2596 test_bit(R5_ReadError, &sh->dev[i].flags))) {
NeilBrown143c4d02012-10-11 13:50:12 +11002597 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002598 bi = sh->dev[i].toread;
2599 sh->dev[i].toread = NULL;
NeilBrown143c4d02012-10-11 13:50:12 +11002600 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002601 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2602 wake_up(&conf->wait_for_overlap);
Dan Williamsa4456852007-07-09 11:56:43 -07002603 while (bi && bi->bi_sector <
2604 sh->dev[i].sector + STRIPE_SECTORS) {
2605 struct bio *nextbi =
2606 r5_next_bio(bi, sh->dev[i].sector);
2607 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002608 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002609 bi->bi_next = *return_bi;
2610 *return_bi = bi;
2611 }
2612 bi = nextbi;
2613 }
2614 }
Dan Williamsa4456852007-07-09 11:56:43 -07002615 if (bitmap_end)
2616 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2617 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002618 /* If we were in the middle of a write the parity block might
2619 * still be locked - so just clear all R5_LOCKED flags
2620 */
2621 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002622 }
2623
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002624 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2625 if (atomic_dec_and_test(&conf->pending_full_writes))
2626 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002627}
2628
NeilBrown7f0da592011-07-28 11:39:22 +10002629static void
NeilBrownd1688a62011-10-11 16:49:52 +11002630handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002631 struct stripe_head_state *s)
2632{
2633 int abort = 0;
2634 int i;
2635
NeilBrown7f0da592011-07-28 11:39:22 +10002636 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11002637 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
2638 wake_up(&conf->wait_for_overlap);
NeilBrown7f0da592011-07-28 11:39:22 +10002639 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002640 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002641 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10002642 * Don't even need to abort as that is handled elsewhere
2643 * if needed, and not always wanted e.g. if there is a known
2644 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11002645 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002646 * non-sync devices, or abort the recovery
2647 */
NeilBrown18b98372012-04-01 23:48:38 +10002648 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2649 /* During recovery devices cannot be removed, so
2650 * locking and refcounting of rdevs is not needed
2651 */
2652 for (i = 0; i < conf->raid_disks; i++) {
2653 struct md_rdev *rdev = conf->disks[i].rdev;
2654 if (rdev
2655 && !test_bit(Faulty, &rdev->flags)
2656 && !test_bit(In_sync, &rdev->flags)
2657 && !rdev_set_badblocks(rdev, sh->sector,
2658 STRIPE_SECTORS, 0))
2659 abort = 1;
2660 rdev = conf->disks[i].replacement;
2661 if (rdev
2662 && !test_bit(Faulty, &rdev->flags)
2663 && !test_bit(In_sync, &rdev->flags)
2664 && !rdev_set_badblocks(rdev, sh->sector,
2665 STRIPE_SECTORS, 0))
2666 abort = 1;
2667 }
2668 if (abort)
2669 conf->recovery_disabled =
2670 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10002671 }
NeilBrown18b98372012-04-01 23:48:38 +10002672 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10002673}
2674
NeilBrown9a3e1102011-12-23 10:17:53 +11002675static int want_replace(struct stripe_head *sh, int disk_idx)
2676{
2677 struct md_rdev *rdev;
2678 int rv = 0;
2679 /* Doing recovery so rcu locking not required */
2680 rdev = sh->raid_conf->disks[disk_idx].replacement;
2681 if (rdev
2682 && !test_bit(Faulty, &rdev->flags)
2683 && !test_bit(In_sync, &rdev->flags)
2684 && (rdev->recovery_offset <= sh->sector
2685 || rdev->mddev->recovery_cp <= sh->sector))
2686 rv = 1;
2687
2688 return rv;
2689}
2690
NeilBrown93b3dbc2011-07-27 11:00:36 +10002691/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002692 * to be read or computed to satisfy a request.
2693 *
2694 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002695 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002696 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002697static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2698 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002699{
2700 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002701 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2702 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002703
Dan Williamsf38e1212007-01-02 13:52:30 -07002704 /* is the data in this block needed, and can we get it? */
2705 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002706 !test_bit(R5_UPTODATE, &dev->flags) &&
2707 (dev->toread ||
2708 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2709 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002710 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002711 (s->failed >= 1 && fdev[0]->toread) ||
2712 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002713 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2714 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2715 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002716 /* we would like to get this block, possibly by computing it,
2717 * otherwise read it if the backing disk is insync
2718 */
2719 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2720 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2721 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002722 (s->failed && (disk_idx == s->failed_num[0] ||
2723 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002724 /* have disk failed, and we're requested to fetch it;
2725 * do compute it
2726 */
2727 pr_debug("Computing stripe %llu block %d\n",
2728 (unsigned long long)sh->sector, disk_idx);
2729 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2730 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2731 set_bit(R5_Wantcompute, &dev->flags);
2732 sh->ops.target = disk_idx;
2733 sh->ops.target2 = -1; /* no 2nd target */
2734 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002735 /* Careful: from this point on 'uptodate' is in the eye
2736 * of raid_run_ops which services 'compute' operations
2737 * before writes. R5_Wantcompute flags a block that will
2738 * be R5_UPTODATE by the time it is needed for a
2739 * subsequent operation.
2740 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002741 s->uptodate++;
2742 return 1;
2743 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2744 /* Computing 2-failure is *very* expensive; only
2745 * do it if failed >= 2
2746 */
2747 int other;
2748 for (other = disks; other--; ) {
2749 if (other == disk_idx)
2750 continue;
2751 if (!test_bit(R5_UPTODATE,
2752 &sh->dev[other].flags))
2753 break;
2754 }
2755 BUG_ON(other < 0);
2756 pr_debug("Computing stripe %llu blocks %d,%d\n",
2757 (unsigned long long)sh->sector,
2758 disk_idx, other);
2759 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2760 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2761 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2762 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2763 sh->ops.target = disk_idx;
2764 sh->ops.target2 = other;
2765 s->uptodate += 2;
2766 s->req_compute = 1;
2767 return 1;
2768 } else if (test_bit(R5_Insync, &dev->flags)) {
2769 set_bit(R5_LOCKED, &dev->flags);
2770 set_bit(R5_Wantread, &dev->flags);
2771 s->locked++;
2772 pr_debug("Reading block %d (sync=%d)\n",
2773 disk_idx, s->syncing);
2774 }
2775 }
2776
2777 return 0;
2778}
2779
2780/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002781 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002782 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002783static void handle_stripe_fill(struct stripe_head *sh,
2784 struct stripe_head_state *s,
2785 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002786{
2787 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002788
2789 /* look for blocks to read/compute, skip this if a compute
2790 * is already in flight, or if the stripe contents are in the
2791 * midst of changing due to a write
2792 */
2793 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2794 !sh->reconstruct_state)
2795 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002796 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002797 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002798 set_bit(STRIPE_HANDLE, &sh->state);
2799}
2800
2801
Dan Williams1fe797e2008-06-28 09:16:30 +10002802/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002803 * any written block on an uptodate or failed drive can be returned.
2804 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2805 * never LOCKED, so we don't need to test 'failed' directly.
2806 */
NeilBrownd1688a62011-10-11 16:49:52 +11002807static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002808 struct stripe_head *sh, int disks, struct bio **return_bi)
2809{
2810 int i;
2811 struct r5dev *dev;
NeilBrownf8dfcff2013-03-12 12:18:06 +11002812 int discard_pending = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002813
2814 for (i = disks; i--; )
2815 if (sh->dev[i].written) {
2816 dev = &sh->dev[i];
2817 if (!test_bit(R5_LOCKED, &dev->flags) &&
Shaohua Li9e4447682012-10-11 13:49:49 +11002818 (test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownca64cae2012-11-21 16:33:40 +11002819 test_bit(R5_Discard, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002820 /* We can return any write requests */
2821 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07002822 pr_debug("Return write for disc %d\n", i);
NeilBrownca64cae2012-11-21 16:33:40 +11002823 if (test_and_clear_bit(R5_Discard, &dev->flags))
2824 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002825 wbi = dev->written;
2826 dev->written = NULL;
2827 while (wbi && wbi->bi_sector <
2828 dev->sector + STRIPE_SECTORS) {
2829 wbi2 = r5_next_bio(wbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002830 if (!raid5_dec_bi_active_stripes(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002831 md_write_end(conf->mddev);
2832 wbi->bi_next = *return_bi;
2833 *return_bi = wbi;
2834 }
2835 wbi = wbi2;
2836 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002837 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2838 STRIPE_SECTORS,
Dan Williamsa4456852007-07-09 11:56:43 -07002839 !test_bit(STRIPE_DEGRADED, &sh->state),
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002840 0);
NeilBrownf8dfcff2013-03-12 12:18:06 +11002841 } else if (test_bit(R5_Discard, &dev->flags))
2842 discard_pending = 1;
2843 }
2844 if (!discard_pending &&
2845 test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) {
2846 clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
2847 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
2848 if (sh->qd_idx >= 0) {
2849 clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
2850 clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags);
2851 }
2852 /* now that discard is done we can proceed with any sync */
2853 clear_bit(STRIPE_DISCARD, &sh->state);
2854 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state))
2855 set_bit(STRIPE_HANDLE, &sh->state);
2856
2857 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002858
2859 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2860 if (atomic_dec_and_test(&conf->pending_full_writes))
2861 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002862}
2863
NeilBrownd1688a62011-10-11 16:49:52 +11002864static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002865 struct stripe_head *sh,
2866 struct stripe_head_state *s,
2867 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002868{
2869 int rmw = 0, rcw = 0, i;
Alexander Lyakasa7854482012-10-11 13:50:12 +11002870 sector_t recovery_cp = conf->mddev->recovery_cp;
2871
2872 /* RAID6 requires 'rcw' in current implementation.
2873 * Otherwise, check whether resync is now happening or should start.
2874 * If yes, then the array is dirty (after unclean shutdown or
2875 * initial creation), so parity in some stripes might be inconsistent.
2876 * In this case, we need to always do reconstruct-write, to ensure
2877 * that in case of drive failure or read-error correction, we
2878 * generate correct data from the parity.
2879 */
2880 if (conf->max_degraded == 2 ||
2881 (recovery_cp < MaxSector && sh->sector >= recovery_cp)) {
2882 /* Calculate the real rcw later - for now make it
NeilBrownc8ac1802011-07-27 11:00:36 +10002883 * look like rcw is cheaper
2884 */
2885 rcw = 1; rmw = 2;
Alexander Lyakasa7854482012-10-11 13:50:12 +11002886 pr_debug("force RCW max_degraded=%u, recovery_cp=%llu sh->sector=%llu\n",
2887 conf->max_degraded, (unsigned long long)recovery_cp,
2888 (unsigned long long)sh->sector);
NeilBrownc8ac1802011-07-27 11:00:36 +10002889 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002890 /* would I have to read this buffer for read_modify_write */
2891 struct r5dev *dev = &sh->dev[i];
2892 if ((dev->towrite || i == sh->pd_idx) &&
2893 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002894 !(test_bit(R5_UPTODATE, &dev->flags) ||
2895 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002896 if (test_bit(R5_Insync, &dev->flags))
2897 rmw++;
2898 else
2899 rmw += 2*disks; /* cannot read it */
2900 }
2901 /* Would I have to read this buffer for reconstruct_write */
2902 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2903 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002904 !(test_bit(R5_UPTODATE, &dev->flags) ||
2905 test_bit(R5_Wantcompute, &dev->flags))) {
2906 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002907 else
2908 rcw += 2*disks;
2909 }
2910 }
Dan Williams45b42332007-07-09 11:56:43 -07002911 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002912 (unsigned long long)sh->sector, rmw, rcw);
2913 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrowna9add5d2012-10-31 11:59:09 +11002914 if (rmw < rcw && rmw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002915 /* prefer read-modify-write, but need to get some data */
Jonathan Brassowe3620a32013-03-07 16:22:01 -06002916 if (conf->mddev->queue)
2917 blk_add_trace_msg(conf->mddev->queue,
2918 "raid5 rmw %llu %d",
2919 (unsigned long long)sh->sector, rmw);
Dan Williamsa4456852007-07-09 11:56:43 -07002920 for (i = disks; i--; ) {
2921 struct r5dev *dev = &sh->dev[i];
2922 if ((dev->towrite || i == sh->pd_idx) &&
2923 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002924 !(test_bit(R5_UPTODATE, &dev->flags) ||
2925 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002926 test_bit(R5_Insync, &dev->flags)) {
2927 if (
2928 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002929 pr_debug("Read_old block "
NeilBrowna9add5d2012-10-31 11:59:09 +11002930 "%d for r-m-w\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002931 set_bit(R5_LOCKED, &dev->flags);
2932 set_bit(R5_Wantread, &dev->flags);
2933 s->locked++;
2934 } else {
2935 set_bit(STRIPE_DELAYED, &sh->state);
2936 set_bit(STRIPE_HANDLE, &sh->state);
2937 }
2938 }
2939 }
NeilBrowna9add5d2012-10-31 11:59:09 +11002940 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002941 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002942 /* want reconstruct write, but need to get some data */
NeilBrowna9add5d2012-10-31 11:59:09 +11002943 int qread =0;
NeilBrownc8ac1802011-07-27 11:00:36 +10002944 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002945 for (i = disks; i--; ) {
2946 struct r5dev *dev = &sh->dev[i];
2947 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002948 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002949 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002950 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002951 test_bit(R5_Wantcompute, &dev->flags))) {
2952 rcw++;
2953 if (!test_bit(R5_Insync, &dev->flags))
2954 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002955 if (
2956 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002957 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002958 "%d for Reconstruct\n", i);
2959 set_bit(R5_LOCKED, &dev->flags);
2960 set_bit(R5_Wantread, &dev->flags);
2961 s->locked++;
NeilBrowna9add5d2012-10-31 11:59:09 +11002962 qread++;
Dan Williamsa4456852007-07-09 11:56:43 -07002963 } else {
2964 set_bit(STRIPE_DELAYED, &sh->state);
2965 set_bit(STRIPE_HANDLE, &sh->state);
2966 }
2967 }
2968 }
Jonathan Brassowe3620a32013-03-07 16:22:01 -06002969 if (rcw && conf->mddev->queue)
NeilBrowna9add5d2012-10-31 11:59:09 +11002970 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
2971 (unsigned long long)sh->sector,
2972 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
NeilBrownc8ac1802011-07-27 11:00:36 +10002973 }
Dan Williamsa4456852007-07-09 11:56:43 -07002974 /* now if nothing is locked, and if we have enough data,
2975 * we can start a write request
2976 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002977 /* since handle_stripe can be called at any time we need to handle the
2978 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002979 * subsequent call wants to start a write request. raid_run_ops only
2980 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002981 * simultaneously. If this is not the case then new writes need to be
2982 * held off until the compute completes.
2983 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002984 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2985 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2986 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002987 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002988}
2989
NeilBrownd1688a62011-10-11 16:49:52 +11002990static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002991 struct stripe_head_state *s, int disks)
2992{
Dan Williamsecc65c92008-06-28 08:31:57 +10002993 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002994
Dan Williamsbd2ab672008-04-10 21:29:27 -07002995 set_bit(STRIPE_HANDLE, &sh->state);
2996
Dan Williamsecc65c92008-06-28 08:31:57 +10002997 switch (sh->check_state) {
2998 case check_state_idle:
2999 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07003000 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07003001 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10003002 sh->check_state = check_state_run;
3003 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07003004 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07003005 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10003006 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07003007 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003008 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10003009 /* fall through */
3010 case check_state_compute_result:
3011 sh->check_state = check_state_idle;
3012 if (!dev)
3013 dev = &sh->dev[sh->pd_idx];
3014
3015 /* check that a write has not made the stripe insync */
3016 if (test_bit(STRIPE_INSYNC, &sh->state))
3017 break;
3018
3019 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07003020 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
3021 BUG_ON(s->uptodate != disks);
3022
3023 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10003024 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07003025 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07003026
Dan Williamsa4456852007-07-09 11:56:43 -07003027 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003028 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003029 break;
3030 case check_state_run:
3031 break; /* we will be called again upon completion */
3032 case check_state_check_result:
3033 sh->check_state = check_state_idle;
3034
3035 /* if a failure occurred during the check operation, leave
3036 * STRIPE_INSYNC not set and let the stripe be handled again
3037 */
3038 if (s->failed)
3039 break;
3040
3041 /* handle a successful check operation, if parity is correct
3042 * we are done. Otherwise update the mismatch count and repair
3043 * parity if !MD_RECOVERY_CHECK
3044 */
Dan Williamsad283ea2009-08-29 19:09:26 -07003045 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10003046 /* parity is correct (on disc,
3047 * not in buffer any more)
3048 */
3049 set_bit(STRIPE_INSYNC, &sh->state);
3050 else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003051 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsecc65c92008-06-28 08:31:57 +10003052 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3053 /* don't try to repair!! */
3054 set_bit(STRIPE_INSYNC, &sh->state);
3055 else {
3056 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10003057 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003058 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3059 set_bit(R5_Wantcompute,
3060 &sh->dev[sh->pd_idx].flags);
3061 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07003062 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10003063 s->uptodate++;
3064 }
3065 }
3066 break;
3067 case check_state_compute_run:
3068 break;
3069 default:
3070 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3071 __func__, sh->check_state,
3072 (unsigned long long) sh->sector);
3073 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003074 }
3075}
3076
3077
NeilBrownd1688a62011-10-11 16:49:52 +11003078static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07003079 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10003080 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003081{
Dan Williamsa4456852007-07-09 11:56:43 -07003082 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11003083 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07003084 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07003085
3086 set_bit(STRIPE_HANDLE, &sh->state);
3087
3088 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003089
Dan Williamsa4456852007-07-09 11:56:43 -07003090 /* Want to check and possibly repair P and Q.
3091 * However there could be one 'failed' device, in which
3092 * case we can only check one of them, possibly using the
3093 * other to generate missing data
3094 */
3095
Dan Williamsd82dfee2009-07-14 13:40:57 -07003096 switch (sh->check_state) {
3097 case check_state_idle:
3098 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10003099 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003100 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07003101 * makes sense to check P (If anything else were failed,
3102 * we would have used P to recreate it).
3103 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003104 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07003105 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003106 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003107 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07003108 * anything, so it makes sense to check it
3109 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003110 if (sh->check_state == check_state_run)
3111 sh->check_state = check_state_run_pq;
3112 else
3113 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07003114 }
Dan Williams36d1c642009-07-14 11:48:22 -07003115
Dan Williamsd82dfee2009-07-14 13:40:57 -07003116 /* discard potentially stale zero_sum_result */
3117 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07003118
Dan Williamsd82dfee2009-07-14 13:40:57 -07003119 if (sh->check_state == check_state_run) {
3120 /* async_xor_zero_sum destroys the contents of P */
3121 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
3122 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07003123 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003124 if (sh->check_state >= check_state_run &&
3125 sh->check_state <= check_state_run_pq) {
3126 /* async_syndrome_zero_sum preserves P and Q, so
3127 * no need to mark them !uptodate here
3128 */
3129 set_bit(STRIPE_OP_CHECK, &s->ops_request);
3130 break;
3131 }
Dan Williams36d1c642009-07-14 11:48:22 -07003132
Dan Williamsd82dfee2009-07-14 13:40:57 -07003133 /* we have 2-disk failure */
3134 BUG_ON(s->failed != 2);
3135 /* fall through */
3136 case check_state_compute_result:
3137 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07003138
Dan Williamsd82dfee2009-07-14 13:40:57 -07003139 /* check that a write has not made the stripe insync */
3140 if (test_bit(STRIPE_INSYNC, &sh->state))
3141 break;
Dan Williamsa4456852007-07-09 11:56:43 -07003142
3143 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07003144 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07003145 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003146 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07003147 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003148 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07003149 s->locked++;
3150 set_bit(R5_LOCKED, &dev->flags);
3151 set_bit(R5_Wantwrite, &dev->flags);
3152 }
3153 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003154 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07003155 s->locked++;
3156 set_bit(R5_LOCKED, &dev->flags);
3157 set_bit(R5_Wantwrite, &dev->flags);
3158 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003159 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003160 dev = &sh->dev[pd_idx];
3161 s->locked++;
3162 set_bit(R5_LOCKED, &dev->flags);
3163 set_bit(R5_Wantwrite, &dev->flags);
3164 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003165 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003166 dev = &sh->dev[qd_idx];
3167 s->locked++;
3168 set_bit(R5_LOCKED, &dev->flags);
3169 set_bit(R5_Wantwrite, &dev->flags);
3170 }
3171 clear_bit(STRIPE_DEGRADED, &sh->state);
3172
3173 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003174 break;
3175 case check_state_run:
3176 case check_state_run_q:
3177 case check_state_run_pq:
3178 break; /* we will be called again upon completion */
3179 case check_state_check_result:
3180 sh->check_state = check_state_idle;
3181
3182 /* handle a successful check operation, if parity is correct
3183 * we are done. Otherwise update the mismatch count and repair
3184 * parity if !MD_RECOVERY_CHECK
3185 */
3186 if (sh->ops.zero_sum_result == 0) {
3187 /* both parities are correct */
3188 if (!s->failed)
3189 set_bit(STRIPE_INSYNC, &sh->state);
3190 else {
3191 /* in contrast to the raid5 case we can validate
3192 * parity, but still have a failure to write
3193 * back
3194 */
3195 sh->check_state = check_state_compute_result;
3196 /* Returning at this point means that we may go
3197 * off and bring p and/or q uptodate again so
3198 * we make sure to check zero_sum_result again
3199 * to verify if p or q need writeback
3200 */
3201 }
3202 } else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003203 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003204 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3205 /* don't try to repair!! */
3206 set_bit(STRIPE_INSYNC, &sh->state);
3207 else {
3208 int *target = &sh->ops.target;
3209
3210 sh->ops.target = -1;
3211 sh->ops.target2 = -1;
3212 sh->check_state = check_state_compute_run;
3213 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3214 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3215 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3216 set_bit(R5_Wantcompute,
3217 &sh->dev[pd_idx].flags);
3218 *target = pd_idx;
3219 target = &sh->ops.target2;
3220 s->uptodate++;
3221 }
3222 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3223 set_bit(R5_Wantcompute,
3224 &sh->dev[qd_idx].flags);
3225 *target = qd_idx;
3226 s->uptodate++;
3227 }
3228 }
3229 }
3230 break;
3231 case check_state_compute_run:
3232 break;
3233 default:
3234 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3235 __func__, sh->check_state,
3236 (unsigned long long) sh->sector);
3237 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003238 }
3239}
3240
NeilBrownd1688a62011-10-11 16:49:52 +11003241static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003242{
3243 int i;
3244
3245 /* We have read all the blocks in this stripe and now we need to
3246 * copy some of them into a target stripe for expand.
3247 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003248 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003249 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3250 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003251 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003252 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003253 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003254 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003255
NeilBrown784052e2009-03-31 15:19:07 +11003256 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003257 sector_t s = raid5_compute_sector(conf, bn, 0,
3258 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003259 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003260 if (sh2 == NULL)
3261 /* so far only the early blocks of this stripe
3262 * have been requested. When later blocks
3263 * get requested, we will try again
3264 */
3265 continue;
3266 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3267 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3268 /* must have already done this block */
3269 release_stripe(sh2);
3270 continue;
3271 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003272
3273 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003274 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003275 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003276 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003277 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003278
Dan Williamsa4456852007-07-09 11:56:43 -07003279 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3280 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3281 for (j = 0; j < conf->raid_disks; j++)
3282 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003283 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003284 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3285 break;
3286 if (j == conf->raid_disks) {
3287 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3288 set_bit(STRIPE_HANDLE, &sh2->state);
3289 }
3290 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003291
Dan Williamsa4456852007-07-09 11:56:43 -07003292 }
NeilBrowna2e08552007-09-11 15:23:36 -07003293 /* done submitting copies, wait for them to complete */
NeilBrown749586b2012-11-20 14:11:15 +11003294 async_tx_quiesce(&tx);
Dan Williamsa4456852007-07-09 11:56:43 -07003295}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296
3297/*
3298 * handle_stripe - do things to a stripe.
3299 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003300 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3301 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003303 * return some read requests which now have data
3304 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 * schedule a read on some buffers
3306 * schedule a write of some buffers
3307 * return confirmation of parity correctness
3308 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 */
Dan Williamsa4456852007-07-09 11:56:43 -07003310
NeilBrownacfe7262011-07-27 11:00:36 +10003311static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003312{
NeilBrownd1688a62011-10-11 16:49:52 +11003313 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003314 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003315 struct r5dev *dev;
3316 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003317 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003318
NeilBrownacfe7262011-07-27 11:00:36 +10003319 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003320
NeilBrownacfe7262011-07-27 11:00:36 +10003321 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3322 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3323 s->failed_num[0] = -1;
3324 s->failed_num[1] = -1;
3325
3326 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003327 rcu_read_lock();
3328 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003329 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003330 sector_t first_bad;
3331 int bad_sectors;
3332 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003333
NeilBrown16a53ec2006-06-26 00:27:38 -07003334 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003335
Dan Williams45b42332007-07-09 11:56:43 -07003336 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003337 i, dev->flags,
3338 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003339 /* maybe we can reply to a read
3340 *
3341 * new wantfill requests are only permitted while
3342 * ops_complete_biofill is guaranteed to be inactive
3343 */
3344 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3345 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3346 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003347
3348 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003349 if (test_bit(R5_LOCKED, &dev->flags))
3350 s->locked++;
3351 if (test_bit(R5_UPTODATE, &dev->flags))
3352 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003353 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003354 s->compute++;
3355 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003356 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003357
NeilBrownacfe7262011-07-27 11:00:36 +10003358 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003359 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003360 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003361 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003362 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003363 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003364 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003365 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003366 }
Dan Williamsa4456852007-07-09 11:56:43 -07003367 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003368 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003369 /* Prefer to use the replacement for reads, but only
3370 * if it is recovered enough and has no bad blocks.
3371 */
3372 rdev = rcu_dereference(conf->disks[i].replacement);
3373 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3374 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3375 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3376 &first_bad, &bad_sectors))
3377 set_bit(R5_ReadRepl, &dev->flags);
3378 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003379 if (rdev)
3380 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003381 rdev = rcu_dereference(conf->disks[i].rdev);
3382 clear_bit(R5_ReadRepl, &dev->flags);
3383 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003384 if (rdev && test_bit(Faulty, &rdev->flags))
3385 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003386 if (rdev) {
3387 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3388 &first_bad, &bad_sectors);
3389 if (s->blocked_rdev == NULL
3390 && (test_bit(Blocked, &rdev->flags)
3391 || is_bad < 0)) {
3392 if (is_bad < 0)
3393 set_bit(BlockedBadBlocks,
3394 &rdev->flags);
3395 s->blocked_rdev = rdev;
3396 atomic_inc(&rdev->nr_pending);
3397 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003398 }
NeilBrown415e72d2010-06-17 17:25:21 +10003399 clear_bit(R5_Insync, &dev->flags);
3400 if (!rdev)
3401 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003402 else if (is_bad) {
3403 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10003404 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3405 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003406 /* treat as in-sync, but with a read error
3407 * which we can now try to correct
3408 */
3409 set_bit(R5_Insync, &dev->flags);
3410 set_bit(R5_ReadError, &dev->flags);
3411 }
3412 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003413 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003414 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003415 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003416 set_bit(R5_Insync, &dev->flags);
3417 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3418 test_bit(R5_Expanded, &dev->flags))
3419 /* If we've reshaped into here, we assume it is Insync.
3420 * We will shortly update recovery_offset to make
3421 * it official.
3422 */
3423 set_bit(R5_Insync, &dev->flags);
3424
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003425 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003426 /* This flag does not apply to '.replacement'
3427 * only to .rdev, so make sure to check that*/
3428 struct md_rdev *rdev2 = rcu_dereference(
3429 conf->disks[i].rdev);
3430 if (rdev2 == rdev)
3431 clear_bit(R5_Insync, &dev->flags);
3432 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003433 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003434 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003435 } else
3436 clear_bit(R5_WriteError, &dev->flags);
3437 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003438 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003439 /* This flag does not apply to '.replacement'
3440 * only to .rdev, so make sure to check that*/
3441 struct md_rdev *rdev2 = rcu_dereference(
3442 conf->disks[i].rdev);
3443 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003444 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003445 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003446 } else
3447 clear_bit(R5_MadeGood, &dev->flags);
3448 }
NeilBrown977df362011-12-23 10:17:53 +11003449 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3450 struct md_rdev *rdev2 = rcu_dereference(
3451 conf->disks[i].replacement);
3452 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3453 s->handle_bad_blocks = 1;
3454 atomic_inc(&rdev2->nr_pending);
3455 } else
3456 clear_bit(R5_MadeGoodRepl, &dev->flags);
3457 }
NeilBrown415e72d2010-06-17 17:25:21 +10003458 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003459 /* The ReadError flag will just be confusing now */
3460 clear_bit(R5_ReadError, &dev->flags);
3461 clear_bit(R5_ReWrite, &dev->flags);
3462 }
NeilBrown415e72d2010-06-17 17:25:21 +10003463 if (test_bit(R5_ReadError, &dev->flags))
3464 clear_bit(R5_Insync, &dev->flags);
3465 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003466 if (s->failed < 2)
3467 s->failed_num[s->failed] = i;
3468 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003469 if (rdev && !test_bit(Faulty, &rdev->flags))
3470 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003471 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003472 }
NeilBrown9a3e1102011-12-23 10:17:53 +11003473 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3474 /* If there is a failed device being replaced,
3475 * we must be recovering.
3476 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10003477 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11003478 * else we can only be replacing
3479 * sync and recovery both need to read all devices, and so
3480 * use the same flag.
3481 */
3482 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10003483 sh->sector >= conf->mddev->recovery_cp ||
3484 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11003485 s->syncing = 1;
3486 else
3487 s->replacing = 1;
3488 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003489 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003490}
NeilBrownf4168852007-02-28 20:11:53 -08003491
NeilBrowncc940152011-07-26 11:35:35 +10003492static void handle_stripe(struct stripe_head *sh)
3493{
3494 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003495 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003496 int i;
NeilBrown84789552011-07-27 11:00:36 +10003497 int prexor;
3498 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003499 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003500
3501 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003502 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003503 /* already being handled, ensure it gets handled
3504 * again when current action finishes */
3505 set_bit(STRIPE_HANDLE, &sh->state);
3506 return;
3507 }
3508
NeilBrownf8dfcff2013-03-12 12:18:06 +11003509 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3510 spin_lock(&sh->stripe_lock);
3511 /* Cannot process 'sync' concurrently with 'discard' */
3512 if (!test_bit(STRIPE_DISCARD, &sh->state) &&
3513 test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3514 set_bit(STRIPE_SYNCING, &sh->state);
3515 clear_bit(STRIPE_INSYNC, &sh->state);
NeilBrownf94c0b62013-07-22 12:57:21 +10003516 clear_bit(STRIPE_REPLACED, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003517 }
3518 spin_unlock(&sh->stripe_lock);
NeilBrowncc940152011-07-26 11:35:35 +10003519 }
3520 clear_bit(STRIPE_DELAYED, &sh->state);
3521
3522 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3523 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3524 (unsigned long long)sh->sector, sh->state,
3525 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3526 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003527
NeilBrownacfe7262011-07-27 11:00:36 +10003528 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003529
NeilBrownbc2607f2011-07-28 11:39:22 +10003530 if (s.handle_bad_blocks) {
3531 set_bit(STRIPE_HANDLE, &sh->state);
3532 goto finish;
3533 }
3534
NeilBrown474af965fe2011-07-27 11:00:36 +10003535 if (unlikely(s.blocked_rdev)) {
3536 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003537 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003538 set_bit(STRIPE_HANDLE, &sh->state);
3539 goto finish;
3540 }
3541 /* There is nothing for the blocked_rdev to block */
3542 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3543 s.blocked_rdev = NULL;
3544 }
3545
3546 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3547 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3548 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3549 }
3550
3551 pr_debug("locked=%d uptodate=%d to_read=%d"
3552 " to_write=%d failed=%d failed_num=%d,%d\n",
3553 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3554 s.failed_num[0], s.failed_num[1]);
3555 /* check if the array has lost more than max_degraded devices and,
3556 * if so, some requests might need to be failed.
3557 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003558 if (s.failed > conf->max_degraded) {
3559 sh->check_state = 0;
3560 sh->reconstruct_state = 0;
3561 if (s.to_read+s.to_write+s.written)
3562 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003563 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003564 handle_failed_sync(conf, sh, &s);
3565 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003566
NeilBrown84789552011-07-27 11:00:36 +10003567 /* Now we check to see if any write operations have recently
3568 * completed
3569 */
3570 prexor = 0;
3571 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3572 prexor = 1;
3573 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3574 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3575 sh->reconstruct_state = reconstruct_state_idle;
3576
3577 /* All the 'written' buffers and the parity block are ready to
3578 * be written back to disk
3579 */
Shaohua Li9e4447682012-10-11 13:49:49 +11003580 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
3581 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003582 BUG_ON(sh->qd_idx >= 0 &&
Shaohua Li9e4447682012-10-11 13:49:49 +11003583 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
3584 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003585 for (i = disks; i--; ) {
3586 struct r5dev *dev = &sh->dev[i];
3587 if (test_bit(R5_LOCKED, &dev->flags) &&
3588 (i == sh->pd_idx || i == sh->qd_idx ||
3589 dev->written)) {
3590 pr_debug("Writing block %d\n", i);
3591 set_bit(R5_Wantwrite, &dev->flags);
3592 if (prexor)
3593 continue;
3594 if (!test_bit(R5_Insync, &dev->flags) ||
3595 ((i == sh->pd_idx || i == sh->qd_idx) &&
3596 s.failed == 0))
3597 set_bit(STRIPE_INSYNC, &sh->state);
3598 }
3599 }
3600 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3601 s.dec_preread_active = 1;
3602 }
3603
NeilBrownef5b7c62012-11-22 09:13:36 +11003604 /*
3605 * might be able to return some write requests if the parity blocks
3606 * are safe, or on a failed drive
3607 */
3608 pdev = &sh->dev[sh->pd_idx];
3609 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3610 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3611 qdev = &sh->dev[sh->qd_idx];
3612 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3613 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3614 || conf->level < 6;
3615
3616 if (s.written &&
3617 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3618 && !test_bit(R5_LOCKED, &pdev->flags)
3619 && (test_bit(R5_UPTODATE, &pdev->flags) ||
3620 test_bit(R5_Discard, &pdev->flags))))) &&
3621 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3622 && !test_bit(R5_LOCKED, &qdev->flags)
3623 && (test_bit(R5_UPTODATE, &qdev->flags) ||
3624 test_bit(R5_Discard, &qdev->flags))))))
3625 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3626
3627 /* Now we might consider reading some blocks, either to check/generate
3628 * parity, or to satisfy requests
3629 * or to load a block that is being partially written.
3630 */
3631 if (s.to_read || s.non_overwrite
3632 || (conf->level == 6 && s.to_write && s.failed)
3633 || (s.syncing && (s.uptodate + s.compute < disks))
3634 || s.replacing
3635 || s.expanding)
3636 handle_stripe_fill(sh, &s, disks);
3637
NeilBrown84789552011-07-27 11:00:36 +10003638 /* Now to consider new write requests and what else, if anything
3639 * should be read. We do not handle new writes when:
3640 * 1/ A 'write' operation (copy+xor) is already in flight.
3641 * 2/ A 'check' operation is in flight, as it may clobber the parity
3642 * block.
3643 */
3644 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3645 handle_stripe_dirtying(conf, sh, &s, disks);
3646
3647 /* maybe we need to check and possibly fix the parity for this stripe
3648 * Any reads will already have been scheduled, so we just see if enough
3649 * data is available. The parity check is held off while parity
3650 * dependent operations are in flight.
3651 */
3652 if (sh->check_state ||
3653 (s.syncing && s.locked == 0 &&
3654 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3655 !test_bit(STRIPE_INSYNC, &sh->state))) {
3656 if (conf->level == 6)
3657 handle_parity_checks6(conf, sh, &s, disks);
3658 else
3659 handle_parity_checks5(conf, sh, &s, disks);
3660 }
NeilBrownc5a31002011-07-27 11:00:36 +10003661
NeilBrownf94c0b62013-07-22 12:57:21 +10003662 if ((s.replacing || s.syncing) && s.locked == 0
3663 && !test_bit(STRIPE_COMPUTE_RUN, &sh->state)
3664 && !test_bit(STRIPE_REPLACED, &sh->state)) {
NeilBrown9a3e1102011-12-23 10:17:53 +11003665 /* Write out to replacement devices where possible */
3666 for (i = 0; i < conf->raid_disks; i++)
NeilBrownf94c0b62013-07-22 12:57:21 +10003667 if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3668 WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags));
NeilBrown9a3e1102011-12-23 10:17:53 +11003669 set_bit(R5_WantReplace, &sh->dev[i].flags);
3670 set_bit(R5_LOCKED, &sh->dev[i].flags);
3671 s.locked++;
3672 }
NeilBrownf94c0b62013-07-22 12:57:21 +10003673 if (s.replacing)
3674 set_bit(STRIPE_INSYNC, &sh->state);
3675 set_bit(STRIPE_REPLACED, &sh->state);
NeilBrown9a3e1102011-12-23 10:17:53 +11003676 }
3677 if ((s.syncing || s.replacing) && s.locked == 0 &&
NeilBrownf94c0b62013-07-22 12:57:21 +10003678 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
NeilBrown9a3e1102011-12-23 10:17:53 +11003679 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003680 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3681 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003682 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
3683 wake_up(&conf->wait_for_overlap);
NeilBrownc5a31002011-07-27 11:00:36 +10003684 }
3685
3686 /* If the failed drives are just a ReadError, then we might need
3687 * to progress the repair/check process
3688 */
3689 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3690 for (i = 0; i < s.failed; i++) {
3691 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3692 if (test_bit(R5_ReadError, &dev->flags)
3693 && !test_bit(R5_LOCKED, &dev->flags)
3694 && test_bit(R5_UPTODATE, &dev->flags)
3695 ) {
3696 if (!test_bit(R5_ReWrite, &dev->flags)) {
3697 set_bit(R5_Wantwrite, &dev->flags);
3698 set_bit(R5_ReWrite, &dev->flags);
3699 set_bit(R5_LOCKED, &dev->flags);
3700 s.locked++;
3701 } else {
3702 /* let's read it back */
3703 set_bit(R5_Wantread, &dev->flags);
3704 set_bit(R5_LOCKED, &dev->flags);
3705 s.locked++;
3706 }
3707 }
3708 }
3709
3710
NeilBrown3687c062011-07-27 11:00:36 +10003711 /* Finish reconstruct operations initiated by the expansion process */
3712 if (sh->reconstruct_state == reconstruct_state_result) {
3713 struct stripe_head *sh_src
3714 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3715 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3716 /* sh cannot be written until sh_src has been read.
3717 * so arrange for sh to be delayed a little
3718 */
3719 set_bit(STRIPE_DELAYED, &sh->state);
3720 set_bit(STRIPE_HANDLE, &sh->state);
3721 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3722 &sh_src->state))
3723 atomic_inc(&conf->preread_active_stripes);
3724 release_stripe(sh_src);
3725 goto finish;
3726 }
3727 if (sh_src)
3728 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003729
NeilBrown3687c062011-07-27 11:00:36 +10003730 sh->reconstruct_state = reconstruct_state_idle;
3731 clear_bit(STRIPE_EXPANDING, &sh->state);
3732 for (i = conf->raid_disks; i--; ) {
3733 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3734 set_bit(R5_LOCKED, &sh->dev[i].flags);
3735 s.locked++;
3736 }
3737 }
3738
3739 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3740 !sh->reconstruct_state) {
3741 /* Need to write out all blocks after computing parity */
3742 sh->disks = conf->raid_disks;
3743 stripe_set_idx(sh->sector, conf, 0, sh);
3744 schedule_reconstruction(sh, &s, 1, 1);
3745 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3746 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3747 atomic_dec(&conf->reshape_stripes);
3748 wake_up(&conf->wait_for_overlap);
3749 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3750 }
3751
3752 if (s.expanding && s.locked == 0 &&
3753 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3754 handle_stripe_expansion(conf, sh);
3755
3756finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003757 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10003758 if (unlikely(s.blocked_rdev)) {
3759 if (conf->mddev->external)
3760 md_wait_for_blocked_rdev(s.blocked_rdev,
3761 conf->mddev);
3762 else
3763 /* Internal metadata will immediately
3764 * be written by raid5d, so we don't
3765 * need to wait here.
3766 */
3767 rdev_dec_pending(s.blocked_rdev,
3768 conf->mddev);
3769 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003770
NeilBrownbc2607f2011-07-28 11:39:22 +10003771 if (s.handle_bad_blocks)
3772 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003773 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003774 struct r5dev *dev = &sh->dev[i];
3775 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3776 /* We own a safe reference to the rdev */
3777 rdev = conf->disks[i].rdev;
3778 if (!rdev_set_badblocks(rdev, sh->sector,
3779 STRIPE_SECTORS, 0))
3780 md_error(conf->mddev, rdev);
3781 rdev_dec_pending(rdev, conf->mddev);
3782 }
NeilBrownb84db562011-07-28 11:39:23 +10003783 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3784 rdev = conf->disks[i].rdev;
3785 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003786 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10003787 rdev_dec_pending(rdev, conf->mddev);
3788 }
NeilBrown977df362011-12-23 10:17:53 +11003789 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3790 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11003791 if (!rdev)
3792 /* rdev have been moved down */
3793 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11003794 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003795 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11003796 rdev_dec_pending(rdev, conf->mddev);
3797 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003798 }
3799
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003800 if (s.ops_request)
3801 raid_run_ops(sh, s.ops_request);
3802
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003803 ops_run_io(sh, &s);
3804
NeilBrownc5709ef2011-07-26 11:35:20 +10003805 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003806 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003807 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003808 * have actually been submitted.
3809 */
3810 atomic_dec(&conf->preread_active_stripes);
3811 if (atomic_read(&conf->preread_active_stripes) <
3812 IO_THRESHOLD)
3813 md_wakeup_thread(conf->mddev->thread);
3814 }
3815
NeilBrownc5709ef2011-07-26 11:35:20 +10003816 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003817
Dan Williams257a4b42011-11-08 16:22:06 +11003818 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003819}
3820
NeilBrownd1688a62011-10-11 16:49:52 +11003821static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822{
3823 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3824 while (!list_empty(&conf->delayed_list)) {
3825 struct list_head *l = conf->delayed_list.next;
3826 struct stripe_head *sh;
3827 sh = list_entry(l, struct stripe_head, lru);
3828 list_del_init(l);
3829 clear_bit(STRIPE_DELAYED, &sh->state);
3830 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3831 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003832 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 }
NeilBrown482c0832011-04-18 18:25:42 +10003834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835}
3836
NeilBrownd1688a62011-10-11 16:49:52 +11003837static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003838{
3839 /* device_lock is held */
3840 struct list_head head;
3841 list_add(&head, &conf->bitmap_list);
3842 list_del_init(&conf->bitmap_list);
3843 while (!list_empty(&head)) {
3844 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3845 list_del_init(&sh->lru);
3846 atomic_inc(&sh->count);
3847 __release_stripe(conf, sh);
3848 }
3849}
3850
NeilBrownfd01b882011-10-11 16:47:53 +11003851int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003852{
NeilBrownd1688a62011-10-11 16:49:52 +11003853 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003854
3855 /* No difference between reads and writes. Just check
3856 * how busy the stripe_cache is
3857 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003858
NeilBrownf022b2f2006-10-03 01:15:56 -07003859 if (conf->inactive_blocked)
3860 return 1;
3861 if (conf->quiesce)
3862 return 1;
3863 if (list_empty_careful(&conf->inactive_list))
3864 return 1;
3865
3866 return 0;
3867}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003868EXPORT_SYMBOL_GPL(md_raid5_congested);
3869
3870static int raid5_congested(void *data, int bits)
3871{
NeilBrownfd01b882011-10-11 16:47:53 +11003872 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003873
3874 return mddev_congested(mddev, bits) ||
3875 md_raid5_congested(mddev, bits);
3876}
NeilBrownf022b2f2006-10-03 01:15:56 -07003877
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003878/* We want read requests to align with chunks where possible,
3879 * but write requests don't need to.
3880 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003881static int raid5_mergeable_bvec(struct request_queue *q,
3882 struct bvec_merge_data *bvm,
3883 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003884{
NeilBrownfd01b882011-10-11 16:47:53 +11003885 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003886 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003887 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003888 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003889 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003890
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003891 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003892 return biovec->bv_len; /* always allow writes to be mergeable */
3893
Andre Noll664e7c42009-06-18 08:45:27 +10003894 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3895 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003896 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3897 if (max < 0) max = 0;
3898 if (max <= biovec->bv_len && bio_sectors == 0)
3899 return biovec->bv_len;
3900 else
3901 return max;
3902}
3903
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003904
NeilBrownfd01b882011-10-11 16:47:53 +11003905static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003906{
3907 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003908 unsigned int chunk_sectors = mddev->chunk_sectors;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08003909 unsigned int bio_sectors = bio_sectors(bio);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003910
Andre Noll664e7c42009-06-18 08:45:27 +10003911 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3912 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003913 return chunk_sectors >=
3914 ((sector & (chunk_sectors - 1)) + bio_sectors);
3915}
3916
3917/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003918 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3919 * later sampled by raid5d.
3920 */
NeilBrownd1688a62011-10-11 16:49:52 +11003921static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003922{
3923 unsigned long flags;
3924
3925 spin_lock_irqsave(&conf->device_lock, flags);
3926
3927 bi->bi_next = conf->retry_read_aligned_list;
3928 conf->retry_read_aligned_list = bi;
3929
3930 spin_unlock_irqrestore(&conf->device_lock, flags);
3931 md_wakeup_thread(conf->mddev->thread);
3932}
3933
3934
NeilBrownd1688a62011-10-11 16:49:52 +11003935static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003936{
3937 struct bio *bi;
3938
3939 bi = conf->retry_read_aligned;
3940 if (bi) {
3941 conf->retry_read_aligned = NULL;
3942 return bi;
3943 }
3944 bi = conf->retry_read_aligned_list;
3945 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003946 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003947 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003948 /*
3949 * this sets the active strip count to 1 and the processed
3950 * strip count to zero (upper 8 bits)
3951 */
Shaohua Lie7836bd62012-07-19 16:01:31 +10003952 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003953 }
3954
3955 return bi;
3956}
3957
3958
3959/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003960 * The "raid5_align_endio" should check if the read succeeded and if it
3961 * did, call bio_endio on the original bio (having bio_put the new bio
3962 * first).
3963 * If the read failed..
3964 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003965static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003966{
3967 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003968 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003969 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003970 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003971 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003972
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003973 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003974
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003975 rdev = (void*)raid_bi->bi_next;
3976 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003977 mddev = rdev->mddev;
3978 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003979
3980 rdev_dec_pending(rdev, conf->mddev);
3981
3982 if (!error && uptodate) {
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07003983 trace_block_bio_complete(bdev_get_queue(raid_bi->bi_bdev),
3984 raid_bi, 0);
NeilBrown6712ecf2007-09-27 12:47:43 +02003985 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003986 if (atomic_dec_and_test(&conf->active_aligned_reads))
3987 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003988 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003989 }
3990
3991
Dan Williams45b42332007-07-09 11:56:43 -07003992 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003993
3994 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003995}
3996
Neil Brown387bb172007-02-08 14:20:29 -08003997static int bio_fits_rdev(struct bio *bi)
3998{
Jens Axboe165125e2007-07-24 09:28:11 +02003999 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08004000
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08004001 if (bio_sectors(bi) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08004002 return 0;
4003 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05004004 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08004005 return 0;
4006
4007 if (q->merge_bvec_fn)
4008 /* it's too hard to apply the merge_bvec_fn at this stage,
4009 * just just give up
4010 */
4011 return 0;
4012
4013 return 1;
4014}
4015
4016
NeilBrownfd01b882011-10-11 16:47:53 +11004017static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004018{
NeilBrownd1688a62011-10-11 16:49:52 +11004019 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11004020 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004021 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11004022 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11004023 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004024
4025 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07004026 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004027 return 0;
4028 }
4029 /*
NeilBrowna167f662010-10-26 18:31:13 +11004030 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004031 */
NeilBrowna167f662010-10-26 18:31:13 +11004032 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004033 if (!align_bi)
4034 return 0;
4035 /*
4036 * set bi_end_io to a new function, and set bi_private to the
4037 * original bio.
4038 */
4039 align_bi->bi_end_io = raid5_align_endio;
4040 align_bi->bi_private = raid_bio;
4041 /*
4042 * compute position
4043 */
NeilBrown112bf892009-03-31 14:39:38 +11004044 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
4045 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11004046 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004047
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004048 end_sector = bio_end_sector(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004049 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11004050 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
4051 if (!rdev || test_bit(Faulty, &rdev->flags) ||
4052 rdev->recovery_offset < end_sector) {
4053 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
4054 if (rdev &&
4055 (test_bit(Faulty, &rdev->flags) ||
4056 !(test_bit(In_sync, &rdev->flags) ||
4057 rdev->recovery_offset >= end_sector)))
4058 rdev = NULL;
4059 }
4060 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10004061 sector_t first_bad;
4062 int bad_sectors;
4063
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004064 atomic_inc(&rdev->nr_pending);
4065 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004066 raid_bio->bi_next = (void*)rdev;
4067 align_bi->bi_bdev = rdev->bdev;
4068 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004069
NeilBrown31c176e2011-07-28 11:39:22 +10004070 if (!bio_fits_rdev(align_bi) ||
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08004071 is_badblock(rdev, align_bi->bi_sector, bio_sectors(align_bi),
NeilBrown31c176e2011-07-28 11:39:22 +10004072 &first_bad, &bad_sectors)) {
4073 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08004074 bio_put(align_bi);
4075 rdev_dec_pending(rdev, mddev);
4076 return 0;
4077 }
4078
majianpeng6c0544e2012-06-12 08:31:10 +08004079 /* No reshape active, so we can trust rdev->data_offset */
4080 align_bi->bi_sector += rdev->data_offset;
4081
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004082 spin_lock_irq(&conf->device_lock);
4083 wait_event_lock_irq(conf->wait_for_stripe,
4084 conf->quiesce == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01004085 conf->device_lock);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004086 atomic_inc(&conf->active_aligned_reads);
4087 spin_unlock_irq(&conf->device_lock);
4088
Jonathan Brassowe3620a32013-03-07 16:22:01 -06004089 if (mddev->gendisk)
4090 trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev),
4091 align_bi, disk_devt(mddev->gendisk),
4092 raid_bio->bi_sector);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004093 generic_make_request(align_bi);
4094 return 1;
4095 } else {
4096 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004097 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004098 return 0;
4099 }
4100}
4101
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004102/* __get_priority_stripe - get the next stripe to process
4103 *
4104 * Full stripe writes are allowed to pass preread active stripes up until
4105 * the bypass_threshold is exceeded. In general the bypass_count
4106 * increments when the handle_list is handled before the hold_list; however, it
4107 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
4108 * stripe with in flight i/o. The bypass_count will be reset when the
4109 * head of the hold_list has changed, i.e. the head was promoted to the
4110 * handle_list.
4111 */
NeilBrownd1688a62011-10-11 16:49:52 +11004112static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004113{
4114 struct stripe_head *sh;
4115
4116 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
4117 __func__,
4118 list_empty(&conf->handle_list) ? "empty" : "busy",
4119 list_empty(&conf->hold_list) ? "empty" : "busy",
4120 atomic_read(&conf->pending_full_writes), conf->bypass_count);
4121
4122 if (!list_empty(&conf->handle_list)) {
4123 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
4124
4125 if (list_empty(&conf->hold_list))
4126 conf->bypass_count = 0;
4127 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
4128 if (conf->hold_list.next == conf->last_hold)
4129 conf->bypass_count++;
4130 else {
4131 conf->last_hold = conf->hold_list.next;
4132 conf->bypass_count -= conf->bypass_threshold;
4133 if (conf->bypass_count < 0)
4134 conf->bypass_count = 0;
4135 }
4136 }
4137 } else if (!list_empty(&conf->hold_list) &&
4138 ((conf->bypass_threshold &&
4139 conf->bypass_count > conf->bypass_threshold) ||
4140 atomic_read(&conf->pending_full_writes) == 0)) {
4141 sh = list_entry(conf->hold_list.next,
4142 typeof(*sh), lru);
4143 conf->bypass_count -= conf->bypass_threshold;
4144 if (conf->bypass_count < 0)
4145 conf->bypass_count = 0;
4146 } else
4147 return NULL;
4148
4149 list_del_init(&sh->lru);
4150 atomic_inc(&sh->count);
4151 BUG_ON(atomic_read(&sh->count) != 1);
4152 return sh;
4153}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004154
Shaohua Li8811b592012-08-02 08:33:00 +10004155struct raid5_plug_cb {
4156 struct blk_plug_cb cb;
4157 struct list_head list;
4158};
4159
4160static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
4161{
4162 struct raid5_plug_cb *cb = container_of(
4163 blk_cb, struct raid5_plug_cb, cb);
4164 struct stripe_head *sh;
4165 struct mddev *mddev = cb->cb.data;
4166 struct r5conf *conf = mddev->private;
NeilBrowna9add5d2012-10-31 11:59:09 +11004167 int cnt = 0;
Shaohua Li8811b592012-08-02 08:33:00 +10004168
4169 if (cb->list.next && !list_empty(&cb->list)) {
4170 spin_lock_irq(&conf->device_lock);
4171 while (!list_empty(&cb->list)) {
4172 sh = list_first_entry(&cb->list, struct stripe_head, lru);
4173 list_del_init(&sh->lru);
4174 /*
4175 * avoid race release_stripe_plug() sees
4176 * STRIPE_ON_UNPLUG_LIST clear but the stripe
4177 * is still in our list
4178 */
4179 smp_mb__before_clear_bit();
4180 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
Shaohua Li773ca822013-08-27 17:50:39 +08004181 /*
4182 * STRIPE_ON_RELEASE_LIST could be set here. In that
4183 * case, the count is always > 1 here
4184 */
Shaohua Li8811b592012-08-02 08:33:00 +10004185 __release_stripe(conf, sh);
NeilBrowna9add5d2012-10-31 11:59:09 +11004186 cnt++;
Shaohua Li8811b592012-08-02 08:33:00 +10004187 }
4188 spin_unlock_irq(&conf->device_lock);
4189 }
Jonathan Brassowe3620a32013-03-07 16:22:01 -06004190 if (mddev->queue)
4191 trace_block_unplug(mddev->queue, cnt, !from_schedule);
Shaohua Li8811b592012-08-02 08:33:00 +10004192 kfree(cb);
4193}
4194
4195static void release_stripe_plug(struct mddev *mddev,
4196 struct stripe_head *sh)
4197{
4198 struct blk_plug_cb *blk_cb = blk_check_plugged(
4199 raid5_unplug, mddev,
4200 sizeof(struct raid5_plug_cb));
4201 struct raid5_plug_cb *cb;
4202
4203 if (!blk_cb) {
4204 release_stripe(sh);
4205 return;
4206 }
4207
4208 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
4209
4210 if (cb->list.next == NULL)
4211 INIT_LIST_HEAD(&cb->list);
4212
4213 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
4214 list_add_tail(&sh->lru, &cb->list);
4215 else
4216 release_stripe(sh);
4217}
4218
Shaohua Li620125f2012-10-11 13:49:05 +11004219static void make_discard_request(struct mddev *mddev, struct bio *bi)
4220{
4221 struct r5conf *conf = mddev->private;
4222 sector_t logical_sector, last_sector;
4223 struct stripe_head *sh;
4224 int remaining;
4225 int stripe_sectors;
4226
4227 if (mddev->reshape_position != MaxSector)
4228 /* Skip discard while reshape is happening */
4229 return;
4230
4231 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4232 last_sector = bi->bi_sector + (bi->bi_size>>9);
4233
4234 bi->bi_next = NULL;
4235 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
4236
4237 stripe_sectors = conf->chunk_sectors *
4238 (conf->raid_disks - conf->max_degraded);
4239 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
4240 stripe_sectors);
4241 sector_div(last_sector, stripe_sectors);
4242
4243 logical_sector *= conf->chunk_sectors;
4244 last_sector *= conf->chunk_sectors;
4245
4246 for (; logical_sector < last_sector;
4247 logical_sector += STRIPE_SECTORS) {
4248 DEFINE_WAIT(w);
4249 int d;
4250 again:
4251 sh = get_active_stripe(conf, logical_sector, 0, 0, 0);
4252 prepare_to_wait(&conf->wait_for_overlap, &w,
4253 TASK_UNINTERRUPTIBLE);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004254 set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
4255 if (test_bit(STRIPE_SYNCING, &sh->state)) {
4256 release_stripe(sh);
4257 schedule();
4258 goto again;
4259 }
4260 clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
Shaohua Li620125f2012-10-11 13:49:05 +11004261 spin_lock_irq(&sh->stripe_lock);
4262 for (d = 0; d < conf->raid_disks; d++) {
4263 if (d == sh->pd_idx || d == sh->qd_idx)
4264 continue;
4265 if (sh->dev[d].towrite || sh->dev[d].toread) {
4266 set_bit(R5_Overlap, &sh->dev[d].flags);
4267 spin_unlock_irq(&sh->stripe_lock);
4268 release_stripe(sh);
4269 schedule();
4270 goto again;
4271 }
4272 }
NeilBrownf8dfcff2013-03-12 12:18:06 +11004273 set_bit(STRIPE_DISCARD, &sh->state);
Shaohua Li620125f2012-10-11 13:49:05 +11004274 finish_wait(&conf->wait_for_overlap, &w);
4275 for (d = 0; d < conf->raid_disks; d++) {
4276 if (d == sh->pd_idx || d == sh->qd_idx)
4277 continue;
4278 sh->dev[d].towrite = bi;
4279 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
4280 raid5_inc_bi_active_stripes(bi);
4281 }
4282 spin_unlock_irq(&sh->stripe_lock);
4283 if (conf->mddev->bitmap) {
4284 for (d = 0;
4285 d < conf->raid_disks - conf->max_degraded;
4286 d++)
4287 bitmap_startwrite(mddev->bitmap,
4288 sh->sector,
4289 STRIPE_SECTORS,
4290 0);
4291 sh->bm_seq = conf->seq_flush + 1;
4292 set_bit(STRIPE_BIT_DELAY, &sh->state);
4293 }
4294
4295 set_bit(STRIPE_HANDLE, &sh->state);
4296 clear_bit(STRIPE_DELAYED, &sh->state);
4297 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4298 atomic_inc(&conf->preread_active_stripes);
4299 release_stripe_plug(mddev, sh);
4300 }
4301
4302 remaining = raid5_dec_bi_active_stripes(bi);
4303 if (remaining == 0) {
4304 md_write_end(mddev);
4305 bio_endio(bi, 0);
4306 }
4307}
4308
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07004309static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310{
NeilBrownd1688a62011-10-11 16:49:52 +11004311 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11004312 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 sector_t new_sector;
4314 sector_t logical_sector, last_sector;
4315 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01004316 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11004317 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318
Tejun Heoe9c74692010-09-03 11:56:18 +02004319 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
4320 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004321 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07004322 }
4323
NeilBrown3d310eb2005-06-21 17:17:26 -07004324 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07004325
NeilBrown802ba062006-12-13 00:34:13 -08004326 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004327 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11004328 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004329 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004330
Shaohua Li620125f2012-10-11 13:49:05 +11004331 if (unlikely(bi->bi_rw & REQ_DISCARD)) {
4332 make_discard_request(mddev, bi);
4333 return;
4334 }
4335
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004337 last_sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 bi->bi_next = NULL;
4339 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07004340
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
4342 DEFINE_WAIT(w);
NeilBrownb5663ba2009-03-31 14:39:38 +11004343 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08004344
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004345 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11004346 previous = 0;
NeilBrownb578d552006-03-27 01:18:12 -08004347 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004348 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11004349 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f762006-03-27 01:18:15 -08004350 * 64bit on a 32bit platform, and so it might be
4351 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02004352 * Of course reshape_progress could change after
NeilBrowndf8e7f762006-03-27 01:18:15 -08004353 * the lock is dropped, so once we get a reference
4354 * to the stripe that we think it is, we will have
4355 * to check again.
4356 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004357 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004358 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004359 ? logical_sector < conf->reshape_progress
4360 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004361 previous = 1;
4362 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10004363 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004364 ? logical_sector < conf->reshape_safe
4365 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08004366 spin_unlock_irq(&conf->device_lock);
4367 schedule();
4368 goto retry;
4369 }
4370 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004371 spin_unlock_irq(&conf->device_lock);
4372 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004373
NeilBrown112bf892009-03-31 14:39:38 +11004374 new_sector = raid5_compute_sector(conf, logical_sector,
4375 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11004376 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10004377 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 (unsigned long long)new_sector,
4379 (unsigned long long)logical_sector);
4380
NeilBrownb5663ba2009-03-31 14:39:38 +11004381 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10004382 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004384 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004385 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f762006-03-27 01:18:15 -08004386 * stripe, so we must do the range check again.
4387 * Expansion could still move past after this
4388 * test, but as we are holding a reference to
4389 * 'sh', we know that if that happens,
4390 * STRIPE_EXPANDING will get set and the expansion
4391 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004392 */
4393 int must_retry = 0;
4394 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004395 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11004396 ? logical_sector >= conf->reshape_progress
4397 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004398 /* mismatch, need to try again */
4399 must_retry = 1;
4400 spin_unlock_irq(&conf->device_lock);
4401 if (must_retry) {
4402 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004403 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004404 goto retry;
4405 }
4406 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004407
Namhyung Kimffd96e32011-07-18 17:38:51 +10004408 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004409 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004410 logical_sector < mddev->suspend_hi) {
4411 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004412 /* As the suspend_* range is controlled by
4413 * userspace, we want an interruptible
4414 * wait.
4415 */
4416 flush_signals(current);
4417 prepare_to_wait(&conf->wait_for_overlap,
4418 &w, TASK_INTERRUPTIBLE);
4419 if (logical_sector >= mddev->suspend_lo &&
4420 logical_sector < mddev->suspend_hi)
4421 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004422 goto retry;
4423 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004424
4425 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004426 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004427 /* Stripe is busy expanding or
4428 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 * and wait a while
4430 */
NeilBrown482c0832011-04-18 18:25:42 +10004431 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 release_stripe(sh);
4433 schedule();
4434 goto retry;
4435 }
4436 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004437 set_bit(STRIPE_HANDLE, &sh->state);
4438 clear_bit(STRIPE_DELAYED, &sh->state);
NeilBrowna852d7b2012-09-19 12:48:30 +10004439 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004440 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4441 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10004442 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 } else {
4444 /* cannot get stripe for read-ahead, just give-up */
4445 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4446 finish_wait(&conf->wait_for_overlap, &w);
4447 break;
4448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004450
Shaohua Lie7836bd62012-07-19 16:01:31 +10004451 remaining = raid5_dec_bi_active_stripes(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004452 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453
NeilBrown16a53ec2006-06-26 00:27:38 -07004454 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004456
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07004457 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
4458 bi, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +10004459 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461}
4462
NeilBrownfd01b882011-10-11 16:47:53 +11004463static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004464
NeilBrownfd01b882011-10-11 16:47:53 +11004465static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466{
NeilBrown52c03292006-06-26 00:27:43 -07004467 /* reshaping is quite different to recovery/resync so it is
4468 * handled quite separately ... here.
4469 *
4470 * On each call to sync_request, we gather one chunk worth of
4471 * destination stripes and flag them as expanding.
4472 * Then we find all the source stripes and request reads.
4473 * As the reads complete, handle_stripe will copy the data
4474 * into the destination stripe and release that stripe.
4475 */
NeilBrownd1688a62011-10-11 16:49:52 +11004476 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004478 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004479 int raid_disks = conf->previous_raid_disks;
4480 int data_disks = raid_disks - conf->max_degraded;
4481 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004482 int i;
4483 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004484 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004485 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004486 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004487 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004488
NeilBrownfef9c612009-03-31 15:16:46 +11004489 if (sector_nr == 0) {
4490 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10004491 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004492 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4493 sector_nr = raid5_size(mddev, 0, 0)
4494 - conf->reshape_progress;
NeilBrown2c810cd2012-05-21 09:27:00 +10004495 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004496 conf->reshape_progress > 0)
4497 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004498 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004499 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004500 mddev->curr_resync_completed = sector_nr;
4501 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004502 *skipped = 1;
4503 return sector_nr;
4504 }
NeilBrown52c03292006-06-26 00:27:43 -07004505 }
4506
NeilBrown7a661382009-03-31 15:21:40 +11004507 /* We need to process a full chunk at a time.
4508 * If old and new chunk sizes differ, we need to process the
4509 * largest of these
4510 */
Andre Noll664e7c42009-06-18 08:45:27 +10004511 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4512 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004513 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004514 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004515
NeilBrownb5254dd2012-05-21 09:27:01 +10004516 /* We update the metadata at least every 10 seconds, or when
4517 * the data about to be copied would over-write the source of
4518 * the data at the front of the range. i.e. one new_stripe
4519 * along from reshape_progress new_maps to after where
4520 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004521 */
NeilBrownfef9c612009-03-31 15:16:46 +11004522 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004523 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004524 readpos = conf->reshape_progress;
4525 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004526 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004527 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10004528 if (mddev->reshape_backwards) {
NeilBrowned37d832009-05-27 21:39:05 +10004529 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004530 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004531 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004532 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004533 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004534 readpos -= min_t(sector_t, reshape_sectors, readpos);
4535 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004536 }
NeilBrown52c03292006-06-26 00:27:43 -07004537
NeilBrownb5254dd2012-05-21 09:27:01 +10004538 /* Having calculated the 'writepos' possibly use it
4539 * to set 'stripe_addr' which is where we will write to.
4540 */
4541 if (mddev->reshape_backwards) {
4542 BUG_ON(conf->reshape_progress == 0);
4543 stripe_addr = writepos;
4544 BUG_ON((mddev->dev_sectors &
4545 ~((sector_t)reshape_sectors - 1))
4546 - reshape_sectors - stripe_addr
4547 != sector_nr);
4548 } else {
4549 BUG_ON(writepos != sector_nr + reshape_sectors);
4550 stripe_addr = sector_nr;
4551 }
4552
NeilBrownc8f517c2009-03-31 15:28:40 +11004553 /* 'writepos' is the most advanced device address we might write.
4554 * 'readpos' is the least advanced device address we might read.
4555 * 'safepos' is the least address recorded in the metadata as having
4556 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10004557 * If there is a min_offset_diff, these are adjusted either by
4558 * increasing the safepos/readpos if diff is negative, or
4559 * increasing writepos if diff is positive.
4560 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11004561 * ensure safety in the face of a crash - that must be done by userspace
4562 * making a backup of the data. So in that case there is no particular
4563 * rush to update metadata.
4564 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4565 * update the metadata to advance 'safepos' to match 'readpos' so that
4566 * we can be safe in the event of a crash.
4567 * So we insist on updating metadata if safepos is behind writepos and
4568 * readpos is beyond writepos.
4569 * In any case, update the metadata every 10 seconds.
4570 * Maybe that number should be configurable, but I'm not sure it is
4571 * worth it.... maybe it could be a multiple of safemode_delay???
4572 */
NeilBrownb5254dd2012-05-21 09:27:01 +10004573 if (conf->min_offset_diff < 0) {
4574 safepos += -conf->min_offset_diff;
4575 readpos += -conf->min_offset_diff;
4576 } else
4577 writepos += conf->min_offset_diff;
4578
NeilBrown2c810cd2012-05-21 09:27:00 +10004579 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11004580 ? (safepos > writepos && readpos < writepos)
4581 : (safepos < writepos && readpos > writepos)) ||
4582 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004583 /* Cannot proceed until we've updated the superblock... */
4584 wait_event(conf->wait_for_overlap,
4585 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004586 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004587 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004588 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b422006-10-03 01:15:46 -07004589 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004590 md_wakeup_thread(mddev->thread);
NeilBrown850b2b422006-10-03 01:15:46 -07004591 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004592 kthread_should_stop());
4593 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004594 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004595 spin_unlock_irq(&conf->device_lock);
4596 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004597 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004598 }
4599
NeilBrownab69ae12009-03-31 15:26:47 +11004600 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004601 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004602 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004603 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004604 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004605 set_bit(STRIPE_EXPANDING, &sh->state);
4606 atomic_inc(&conf->reshape_stripes);
4607 /* If any of this stripe is beyond the end of the old
4608 * array, then we need to zero those blocks
4609 */
4610 for (j=sh->disks; j--;) {
4611 sector_t s;
4612 if (j == sh->pd_idx)
4613 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004614 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004615 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004616 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004617 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004618 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004619 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004620 continue;
4621 }
4622 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4623 set_bit(R5_Expanded, &sh->dev[j].flags);
4624 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4625 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004626 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004627 set_bit(STRIPE_EXPAND_READY, &sh->state);
4628 set_bit(STRIPE_HANDLE, &sh->state);
4629 }
NeilBrownab69ae12009-03-31 15:26:47 +11004630 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004631 }
4632 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004633 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11004634 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004635 else
NeilBrown7a661382009-03-31 15:21:40 +11004636 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004637 spin_unlock_irq(&conf->device_lock);
4638 /* Ok, those stripe are ready. We can start scheduling
4639 * reads on the source stripes.
4640 * The source stripes are determined by mapping the first and last
4641 * block on the destination stripes.
4642 */
NeilBrown52c03292006-06-26 00:27:43 -07004643 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004644 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004645 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004646 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004647 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004648 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004649 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004650 if (last_sector >= mddev->dev_sectors)
4651 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004652 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004653 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004654 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4655 set_bit(STRIPE_HANDLE, &sh->state);
4656 release_stripe(sh);
4657 first_sector += STRIPE_SECTORS;
4658 }
NeilBrownab69ae12009-03-31 15:26:47 +11004659 /* Now that the sources are clearly marked, we can release
4660 * the destination stripes
4661 */
4662 while (!list_empty(&stripes)) {
4663 sh = list_entry(stripes.next, struct stripe_head, lru);
4664 list_del_init(&sh->lru);
4665 release_stripe(sh);
4666 }
NeilBrownc6207272008-02-06 01:39:52 -08004667 /* If this takes us to the resync_max point where we have to pause,
4668 * then we need to write out the superblock.
4669 */
NeilBrown7a661382009-03-31 15:21:40 +11004670 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004671 if ((sector_nr - mddev->curr_resync_completed) * 2
4672 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004673 /* Cannot proceed until we've updated the superblock... */
4674 wait_event(conf->wait_for_overlap,
4675 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004676 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004677 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004678 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004679 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4680 md_wakeup_thread(mddev->thread);
4681 wait_event(mddev->sb_wait,
4682 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4683 || kthread_should_stop());
4684 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004685 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004686 spin_unlock_irq(&conf->device_lock);
4687 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004688 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004689 }
NeilBrown7a661382009-03-31 15:21:40 +11004690 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004691}
4692
4693/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004694static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004695{
NeilBrownd1688a62011-10-11 16:49:52 +11004696 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004697 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004698 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004699 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004700 int still_degraded = 0;
4701 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702
NeilBrown72626682005-09-09 16:23:54 -07004703 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004705
NeilBrown29269552006-03-27 01:18:10 -08004706 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4707 end_reshape(conf);
4708 return 0;
4709 }
NeilBrown72626682005-09-09 16:23:54 -07004710
4711 if (mddev->curr_resync < max_sector) /* aborted */
4712 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4713 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004714 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004715 conf->fullsync = 0;
4716 bitmap_close_sync(mddev->bitmap);
4717
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 return 0;
4719 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004720
NeilBrown64bd6602009-08-03 10:59:58 +10004721 /* Allow raid5_quiesce to complete */
4722 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4723
NeilBrown52c03292006-06-26 00:27:43 -07004724 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4725 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004726
NeilBrownc6207272008-02-06 01:39:52 -08004727 /* No need to check resync_max as we never do more than one
4728 * stripe, and as resync_max will always be on a chunk boundary,
4729 * if the check in md_do_sync didn't fire, there is no chance
4730 * of overstepping resync_max here
4731 */
4732
NeilBrown16a53ec2006-06-26 00:27:38 -07004733 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 * to resync, then assert that we are finished, because there is
4735 * nothing we can do.
4736 */
NeilBrown3285edf2006-06-26 00:27:55 -07004737 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004738 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004739 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004740 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 return rv;
4742 }
majianpeng6f608042013-04-24 11:42:41 +10004743 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
4744 !conf->fullsync &&
4745 !bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
4746 sync_blocks >= STRIPE_SECTORS) {
NeilBrown72626682005-09-09 16:23:54 -07004747 /* we can skip this block, and probably more */
4748 sync_blocks /= STRIPE_SECTORS;
4749 *skipped = 1;
4750 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752
NeilBrownb47490c2008-02-06 01:39:50 -08004753 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4754
NeilBrowna8c906c2009-06-09 14:39:59 +10004755 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004757 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004759 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004761 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004763 /* Need to check if array will still be degraded after recovery/resync
4764 * We don't need to check the 'failed' flag as when that gets set,
4765 * recovery aborts.
4766 */
NeilBrownf001a702009-06-09 14:30:31 +10004767 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004768 if (conf->disks[i].rdev == NULL)
4769 still_degraded = 1;
4770
4771 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4772
NeilBrown83206d62011-07-26 11:19:49 +10004773 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774
NeilBrown14425772009-10-16 15:55:25 +11004775 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 release_stripe(sh);
4777
4778 return STRIPE_SECTORS;
4779}
4780
NeilBrownd1688a62011-10-11 16:49:52 +11004781static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004782{
4783 /* We may not be able to submit a whole bio at once as there
4784 * may not be enough stripe_heads available.
4785 * We cannot pre-allocate enough stripe_heads as we may need
4786 * more than exist in the cache (if we allow ever large chunks).
4787 * So we do one stripe head at a time and record in
4788 * ->bi_hw_segments how many have been done.
4789 *
4790 * We *know* that this entire raid_bio is in one chunk, so
4791 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4792 */
4793 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004794 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004795 sector_t sector, logical_sector, last_sector;
4796 int scnt = 0;
4797 int remaining;
4798 int handled = 0;
4799
4800 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004801 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004802 0, &dd_idx, NULL);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004803 last_sector = bio_end_sector(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004804
4805 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004806 logical_sector += STRIPE_SECTORS,
4807 sector += STRIPE_SECTORS,
4808 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004809
Shaohua Lie7836bd62012-07-19 16:01:31 +10004810 if (scnt < raid5_bi_processed_stripes(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004811 /* already done this stripe */
4812 continue;
4813
NeilBrowna8c906c2009-06-09 14:39:59 +10004814 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004815
4816 if (!sh) {
4817 /* failed to get a stripe - must wait */
Shaohua Lie7836bd62012-07-19 16:01:31 +10004818 raid5_set_bi_processed_stripes(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004819 conf->retry_read_aligned = raid_bio;
4820 return handled;
4821 }
4822
Neil Brown387bb172007-02-08 14:20:29 -08004823 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4824 release_stripe(sh);
Shaohua Lie7836bd62012-07-19 16:01:31 +10004825 raid5_set_bi_processed_stripes(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004826 conf->retry_read_aligned = raid_bio;
4827 return handled;
4828 }
4829
majianpeng3f9e7c12012-07-31 10:04:21 +10004830 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07004831 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004832 release_stripe(sh);
4833 handled++;
4834 }
Shaohua Lie7836bd62012-07-19 16:01:31 +10004835 remaining = raid5_dec_bi_active_stripes(raid_bio);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07004836 if (remaining == 0) {
4837 trace_block_bio_complete(bdev_get_queue(raid_bio->bi_bdev),
4838 raid_bio, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +10004839 bio_endio(raid_bio, 0);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07004840 }
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004841 if (atomic_dec_and_test(&conf->active_aligned_reads))
4842 wake_up(&conf->wait_for_stripe);
4843 return handled;
4844}
4845
Shaohua Li46a06402012-08-02 08:33:15 +10004846#define MAX_STRIPE_BATCH 8
4847static int handle_active_stripes(struct r5conf *conf)
4848{
4849 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
4850 int i, batch_size = 0;
4851
4852 while (batch_size < MAX_STRIPE_BATCH &&
4853 (sh = __get_priority_stripe(conf)) != NULL)
4854 batch[batch_size++] = sh;
4855
4856 if (batch_size == 0)
4857 return batch_size;
4858 spin_unlock_irq(&conf->device_lock);
4859
4860 for (i = 0; i < batch_size; i++)
4861 handle_stripe(batch[i]);
4862
4863 cond_resched();
4864
4865 spin_lock_irq(&conf->device_lock);
4866 for (i = 0; i < batch_size; i++)
4867 __release_stripe(conf, batch[i]);
4868 return batch_size;
4869}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004870
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871/*
4872 * This is our raid5 kernel thread.
4873 *
4874 * We scan the hash table for stripes which can be handled now.
4875 * During the scan, completed stripes are saved for us by the interrupt
4876 * handler, so that they will not have to wait for our next wakeup.
4877 */
Shaohua Li4ed87312012-10-11 13:34:00 +11004878static void raid5d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879{
Shaohua Li4ed87312012-10-11 13:34:00 +11004880 struct mddev *mddev = thread->mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11004881 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004883 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884
Dan Williams45b42332007-07-09 11:56:43 -07004885 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886
4887 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004889 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 handled = 0;
4891 spin_lock_irq(&conf->device_lock);
4892 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004893 struct bio *bio;
Shaohua Li773ca822013-08-27 17:50:39 +08004894 int batch_size, released;
4895
4896 released = release_stripe_list(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897
NeilBrown0021b7b2012-07-31 09:08:14 +02004898 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10004899 !list_empty(&conf->bitmap_list)) {
4900 /* Now is a good time to flush some bitmap updates */
4901 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004902 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004903 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004904 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004905 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004906 activate_bit_delay(conf);
4907 }
NeilBrown0021b7b2012-07-31 09:08:14 +02004908 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004909
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004910 while ((bio = remove_bio_from_retry(conf))) {
4911 int ok;
4912 spin_unlock_irq(&conf->device_lock);
4913 ok = retry_aligned_read(conf, bio);
4914 spin_lock_irq(&conf->device_lock);
4915 if (!ok)
4916 break;
4917 handled++;
4918 }
4919
Shaohua Li46a06402012-08-02 08:33:15 +10004920 batch_size = handle_active_stripes(conf);
Shaohua Li773ca822013-08-27 17:50:39 +08004921 if (!batch_size && !released)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 break;
Shaohua Li46a06402012-08-02 08:33:15 +10004923 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924
Shaohua Li46a06402012-08-02 08:33:15 +10004925 if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) {
4926 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10004927 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10004928 spin_lock_irq(&conf->device_lock);
4929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 }
Dan Williams45b42332007-07-09 11:56:43 -07004931 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932
4933 spin_unlock_irq(&conf->device_lock);
4934
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004935 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004936 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937
Dan Williams45b42332007-07-09 11:56:43 -07004938 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939}
4940
NeilBrown3f294f42005-11-08 21:39:25 -08004941static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004942raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004943{
NeilBrownd1688a62011-10-11 16:49:52 +11004944 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004945 if (conf)
4946 return sprintf(page, "%d\n", conf->max_nr_stripes);
4947 else
4948 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004949}
4950
NeilBrownc41d4ac2010-06-01 19:37:24 +10004951int
NeilBrownfd01b882011-10-11 16:47:53 +11004952raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004953{
NeilBrownd1688a62011-10-11 16:49:52 +11004954 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004955 int err;
4956
4957 if (size <= 16 || size > 32768)
4958 return -EINVAL;
4959 while (size < conf->max_nr_stripes) {
4960 if (drop_one_stripe(conf))
4961 conf->max_nr_stripes--;
4962 else
4963 break;
4964 }
4965 err = md_allow_write(mddev);
4966 if (err)
4967 return err;
4968 while (size > conf->max_nr_stripes) {
4969 if (grow_one_stripe(conf))
4970 conf->max_nr_stripes++;
4971 else break;
4972 }
4973 return 0;
4974}
4975EXPORT_SYMBOL(raid5_set_cache_size);
4976
NeilBrown3f294f42005-11-08 21:39:25 -08004977static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004978raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004979{
NeilBrownd1688a62011-10-11 16:49:52 +11004980 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004981 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004982 int err;
4983
NeilBrown3f294f42005-11-08 21:39:25 -08004984 if (len >= PAGE_SIZE)
4985 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004986 if (!conf)
4987 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004988
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004989 if (kstrtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004990 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004991 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004992 if (err)
4993 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004994 return len;
4995}
NeilBrown007583c2005-11-08 21:39:30 -08004996
NeilBrown96de1e62005-11-08 21:39:39 -08004997static struct md_sysfs_entry
4998raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4999 raid5_show_stripe_cache_size,
5000 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08005001
5002static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005003raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005004{
NeilBrownd1688a62011-10-11 16:49:52 +11005005 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005006 if (conf)
5007 return sprintf(page, "%d\n", conf->bypass_threshold);
5008 else
5009 return 0;
5010}
5011
5012static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005013raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005014{
NeilBrownd1688a62011-10-11 16:49:52 +11005015 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07005016 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005017 if (len >= PAGE_SIZE)
5018 return -EINVAL;
5019 if (!conf)
5020 return -ENODEV;
5021
Jingoo Hanb29bebd2013-06-01 16:15:16 +09005022 if (kstrtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005023 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07005024 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005025 return -EINVAL;
5026 conf->bypass_threshold = new;
5027 return len;
5028}
5029
5030static struct md_sysfs_entry
5031raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
5032 S_IRUGO | S_IWUSR,
5033 raid5_show_preread_threshold,
5034 raid5_store_preread_threshold);
5035
5036static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005037stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08005038{
NeilBrownd1688a62011-10-11 16:49:52 +11005039 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08005040 if (conf)
5041 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
5042 else
5043 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08005044}
5045
NeilBrown96de1e62005-11-08 21:39:39 -08005046static struct md_sysfs_entry
5047raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08005048
NeilBrown007583c2005-11-08 21:39:30 -08005049static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08005050 &raid5_stripecache_size.attr,
5051 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005052 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08005053 NULL,
5054};
NeilBrown007583c2005-11-08 21:39:30 -08005055static struct attribute_group raid5_attrs_group = {
5056 .name = NULL,
5057 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08005058};
5059
Dan Williams80c3a6c2009-03-17 18:10:40 -07005060static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11005061raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07005062{
NeilBrownd1688a62011-10-11 16:49:52 +11005063 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07005064
5065 if (!sectors)
5066 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005067 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11005068 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11005069 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07005070
Andre Noll9d8f0362009-06-18 08:45:01 +10005071 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10005072 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07005073 return sectors * (raid_disks - conf->max_degraded);
5074}
5075
NeilBrownd1688a62011-10-11 16:49:52 +11005076static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07005077{
5078 struct raid5_percpu *percpu;
5079 unsigned long cpu;
5080
5081 if (!conf->percpu)
5082 return;
5083
5084 get_online_cpus();
5085 for_each_possible_cpu(cpu) {
5086 percpu = per_cpu_ptr(conf->percpu, cpu);
5087 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005088 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005089 }
5090#ifdef CONFIG_HOTPLUG_CPU
5091 unregister_cpu_notifier(&conf->cpu_notify);
5092#endif
5093 put_online_cpus();
5094
5095 free_percpu(conf->percpu);
5096}
5097
NeilBrownd1688a62011-10-11 16:49:52 +11005098static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10005099{
5100 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07005101 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10005102 kfree(conf->disks);
5103 kfree(conf->stripe_hashtbl);
5104 kfree(conf);
5105}
5106
Dan Williams36d1c642009-07-14 11:48:22 -07005107#ifdef CONFIG_HOTPLUG_CPU
5108static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
5109 void *hcpu)
5110{
NeilBrownd1688a62011-10-11 16:49:52 +11005111 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07005112 long cpu = (long)hcpu;
5113 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
5114
5115 switch (action) {
5116 case CPU_UP_PREPARE:
5117 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07005118 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07005119 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005120 if (!percpu->scribble)
5121 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
5122
5123 if (!percpu->scribble ||
5124 (conf->level == 6 && !percpu->spare_page)) {
5125 safe_put_page(percpu->spare_page);
5126 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005127 pr_err("%s: failed memory allocation for cpu%ld\n",
5128 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07005129 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07005130 }
5131 break;
5132 case CPU_DEAD:
5133 case CPU_DEAD_FROZEN:
5134 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005135 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005136 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07005137 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07005138 break;
5139 default:
5140 break;
5141 }
5142 return NOTIFY_OK;
5143}
5144#endif
5145
NeilBrownd1688a62011-10-11 16:49:52 +11005146static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07005147{
5148 unsigned long cpu;
5149 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09005150 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07005151 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07005152 int err;
5153
Dan Williams36d1c642009-07-14 11:48:22 -07005154 allcpus = alloc_percpu(struct raid5_percpu);
5155 if (!allcpus)
5156 return -ENOMEM;
5157 conf->percpu = allcpus;
5158
5159 get_online_cpus();
5160 err = 0;
5161 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07005162 if (conf->level == 6) {
5163 spare_page = alloc_page(GFP_KERNEL);
5164 if (!spare_page) {
5165 err = -ENOMEM;
5166 break;
5167 }
5168 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
5169 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11005170 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005171 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07005172 err = -ENOMEM;
5173 break;
5174 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07005175 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07005176 }
5177#ifdef CONFIG_HOTPLUG_CPU
5178 conf->cpu_notify.notifier_call = raid456_cpu_notify;
5179 conf->cpu_notify.priority = 0;
5180 if (err == 0)
5181 err = register_cpu_notifier(&conf->cpu_notify);
5182#endif
5183 put_online_cpus();
5184
5185 return err;
5186}
5187
NeilBrownd1688a62011-10-11 16:49:52 +11005188static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189{
NeilBrownd1688a62011-10-11 16:49:52 +11005190 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005191 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11005192 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10005194 char pers_name[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195
NeilBrown91adb562009-03-31 14:39:39 +11005196 if (mddev->new_level != 5
5197 && mddev->new_level != 4
5198 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10005199 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005200 mdname(mddev), mddev->new_level);
5201 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 }
NeilBrown91adb562009-03-31 14:39:39 +11005203 if ((mddev->new_level == 5
5204 && !algorithm_valid_raid5(mddev->new_layout)) ||
5205 (mddev->new_level == 6
5206 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005207 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11005208 mdname(mddev), mddev->new_layout);
5209 return ERR_PTR(-EIO);
5210 }
5211 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10005212 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005213 mdname(mddev), mddev->raid_disks);
5214 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11005215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216
Andre Noll664e7c42009-06-18 08:45:27 +10005217 if (!mddev->new_chunk_sectors ||
5218 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
5219 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005220 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
5221 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11005222 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11005223 }
5224
NeilBrownd1688a62011-10-11 16:49:52 +11005225 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11005226 if (conf == NULL)
5227 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11005228 spin_lock_init(&conf->device_lock);
5229 init_waitqueue_head(&conf->wait_for_stripe);
5230 init_waitqueue_head(&conf->wait_for_overlap);
5231 INIT_LIST_HEAD(&conf->handle_list);
5232 INIT_LIST_HEAD(&conf->hold_list);
5233 INIT_LIST_HEAD(&conf->delayed_list);
5234 INIT_LIST_HEAD(&conf->bitmap_list);
5235 INIT_LIST_HEAD(&conf->inactive_list);
Shaohua Li773ca822013-08-27 17:50:39 +08005236 init_llist_head(&conf->released_stripes);
Dan Williamsf5efd452009-10-16 15:55:38 +11005237 atomic_set(&conf->active_stripes, 0);
5238 atomic_set(&conf->preread_active_stripes, 0);
5239 atomic_set(&conf->active_aligned_reads, 0);
5240 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11005241 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11005242
5243 conf->raid_disks = mddev->raid_disks;
5244 if (mddev->reshape_position == MaxSector)
5245 conf->previous_raid_disks = mddev->raid_disks;
5246 else
5247 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005248 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
5249 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11005250
NeilBrown5e5e3e72009-10-16 16:35:30 +11005251 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11005252 GFP_KERNEL);
5253 if (!conf->disks)
5254 goto abort;
5255
5256 conf->mddev = mddev;
5257
5258 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
5259 goto abort;
5260
Dan Williams36d1c642009-07-14 11:48:22 -07005261 conf->level = mddev->new_level;
5262 if (raid5_alloc_percpu(conf) != 0)
5263 goto abort;
5264
NeilBrown0c55e022010-05-03 14:09:02 +10005265 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11005266
NeilBrowndafb20f2012-03-19 12:46:39 +11005267 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11005268 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005269 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11005270 || raid_disk < 0)
5271 continue;
5272 disk = conf->disks + raid_disk;
5273
NeilBrown17045f52011-12-23 10:17:53 +11005274 if (test_bit(Replacement, &rdev->flags)) {
5275 if (disk->replacement)
5276 goto abort;
5277 disk->replacement = rdev;
5278 } else {
5279 if (disk->rdev)
5280 goto abort;
5281 disk->rdev = rdev;
5282 }
NeilBrown91adb562009-03-31 14:39:39 +11005283
5284 if (test_bit(In_sync, &rdev->flags)) {
5285 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10005286 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
5287 " disk %d\n",
5288 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05005289 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11005290 /* Cannot rely on bitmap to complete recovery */
5291 conf->fullsync = 1;
5292 }
5293
Andre Noll09c9e5f2009-06-18 08:45:55 +10005294 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11005295 conf->level = mddev->new_level;
5296 if (conf->level == 6)
5297 conf->max_degraded = 2;
5298 else
5299 conf->max_degraded = 1;
5300 conf->algorithm = mddev->new_layout;
5301 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11005302 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11005303 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10005304 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11005305 conf->prev_algo = mddev->layout;
5306 }
NeilBrown91adb562009-03-31 14:39:39 +11005307
5308 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11005309 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11005310 if (grow_stripes(conf, conf->max_nr_stripes)) {
5311 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005312 "md/raid:%s: couldn't allocate %dkB for buffers\n",
5313 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005314 goto abort;
5315 } else
NeilBrown0c55e022010-05-03 14:09:02 +10005316 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
5317 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005318
NeilBrown02326052012-07-03 15:56:52 +10005319 sprintf(pers_name, "raid%d", mddev->new_level);
5320 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11005321 if (!conf->thread) {
5322 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005323 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11005324 mdname(mddev));
5325 goto abort;
5326 }
5327
5328 return conf;
5329
5330 abort:
5331 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10005332 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11005333 return ERR_PTR(-EIO);
5334 } else
5335 return ERR_PTR(-ENOMEM);
5336}
5337
NeilBrownc148ffd2009-11-13 17:47:00 +11005338
5339static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
5340{
5341 switch (algo) {
5342 case ALGORITHM_PARITY_0:
5343 if (raid_disk < max_degraded)
5344 return 1;
5345 break;
5346 case ALGORITHM_PARITY_N:
5347 if (raid_disk >= raid_disks - max_degraded)
5348 return 1;
5349 break;
5350 case ALGORITHM_PARITY_0_6:
5351 if (raid_disk == 0 ||
5352 raid_disk == raid_disks - 1)
5353 return 1;
5354 break;
5355 case ALGORITHM_LEFT_ASYMMETRIC_6:
5356 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5357 case ALGORITHM_LEFT_SYMMETRIC_6:
5358 case ALGORITHM_RIGHT_SYMMETRIC_6:
5359 if (raid_disk == raid_disks - 1)
5360 return 1;
5361 }
5362 return 0;
5363}
5364
NeilBrownfd01b882011-10-11 16:47:53 +11005365static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11005366{
NeilBrownd1688a62011-10-11 16:49:52 +11005367 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10005368 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11005369 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11005370 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11005371 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11005372 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10005373 long long min_offset_diff = 0;
5374 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11005375
Andre Noll8c6ac8682009-06-18 08:48:06 +10005376 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10005377 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac8682009-06-18 08:48:06 +10005378 " -- starting background reconstruction\n",
5379 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10005380
5381 rdev_for_each(rdev, mddev) {
5382 long long diff;
5383 if (rdev->raid_disk < 0)
5384 continue;
5385 diff = (rdev->new_data_offset - rdev->data_offset);
5386 if (first) {
5387 min_offset_diff = diff;
5388 first = 0;
5389 } else if (mddev->reshape_backwards &&
5390 diff < min_offset_diff)
5391 min_offset_diff = diff;
5392 else if (!mddev->reshape_backwards &&
5393 diff > min_offset_diff)
5394 min_offset_diff = diff;
5395 }
5396
NeilBrownf6705572006-03-27 01:18:11 -08005397 if (mddev->reshape_position != MaxSector) {
5398 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10005399 * Difficulties arise if the stripe we would write to
5400 * next is at or after the stripe we would read from next.
5401 * For a reshape that changes the number of devices, this
5402 * is only possible for a very short time, and mdadm makes
5403 * sure that time appears to have past before assembling
5404 * the array. So we fail if that time hasn't passed.
5405 * For a reshape that keeps the number of devices the same
5406 * mdadm must be monitoring the reshape can keeping the
5407 * critical areas read-only and backed up. It will start
5408 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08005409 */
5410 sector_t here_new, here_old;
5411 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11005412 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08005413
NeilBrown88ce4932009-03-31 15:24:23 +11005414 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10005415 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08005416 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08005417 mdname(mddev));
5418 return -EINVAL;
5419 }
NeilBrownf6705572006-03-27 01:18:11 -08005420 old_disks = mddev->raid_disks - mddev->delta_disks;
5421 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08005422 * further up in new geometry must map after here in old
5423 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08005424 */
5425 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10005426 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005427 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005428 printk(KERN_ERR "md/raid:%s: reshape_position not "
5429 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005430 return -EINVAL;
5431 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005432 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005433 /* here_new is the stripe we will write to */
5434 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10005435 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005436 (old_disks-max_degraded));
5437 /* here_old is the first stripe that we might need to read
5438 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10005439 if (mddev->delta_disks == 0) {
NeilBrownb5254dd2012-05-21 09:27:01 +10005440 if ((here_new * mddev->new_chunk_sectors !=
5441 here_old * mddev->chunk_sectors)) {
5442 printk(KERN_ERR "md/raid:%s: reshape position is"
5443 " confused - aborting\n", mdname(mddev));
5444 return -EINVAL;
5445 }
NeilBrown67ac6012009-08-13 10:06:24 +10005446 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10005447 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10005448 * and taking constant backups.
5449 * mdadm always starts a situation like this in
5450 * readonly mode so it can take control before
5451 * allowing any writes. So just check for that.
5452 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005453 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
5454 abs(min_offset_diff) >= mddev->new_chunk_sectors)
5455 /* not really in-place - so OK */;
5456 else if (mddev->ro == 0) {
5457 printk(KERN_ERR "md/raid:%s: in-place reshape "
5458 "must be started in read-only mode "
5459 "- aborting\n",
NeilBrown0c55e022010-05-03 14:09:02 +10005460 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005461 return -EINVAL;
5462 }
NeilBrown2c810cd2012-05-21 09:27:00 +10005463 } else if (mddev->reshape_backwards
NeilBrownb5254dd2012-05-21 09:27:01 +10005464 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
NeilBrown67ac6012009-08-13 10:06:24 +10005465 here_old * mddev->chunk_sectors)
5466 : (here_new * mddev->new_chunk_sectors >=
NeilBrownb5254dd2012-05-21 09:27:01 +10005467 here_old * mddev->chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08005468 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005469 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5470 "auto-recovery - aborting.\n",
5471 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005472 return -EINVAL;
5473 }
NeilBrown0c55e022010-05-03 14:09:02 +10005474 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5475 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005476 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005477 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005478 BUG_ON(mddev->level != mddev->new_level);
5479 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005480 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005481 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005482 }
5483
NeilBrown245f46c2009-03-31 14:39:39 +11005484 if (mddev->private == NULL)
5485 conf = setup_conf(mddev);
5486 else
5487 conf = mddev->private;
5488
NeilBrown91adb562009-03-31 14:39:39 +11005489 if (IS_ERR(conf))
5490 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005491
NeilBrownb5254dd2012-05-21 09:27:01 +10005492 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11005493 mddev->thread = conf->thread;
5494 conf->thread = NULL;
5495 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496
NeilBrown17045f52011-12-23 10:17:53 +11005497 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5498 i++) {
5499 rdev = conf->disks[i].rdev;
5500 if (!rdev && conf->disks[i].replacement) {
5501 /* The replacement is all we have yet */
5502 rdev = conf->disks[i].replacement;
5503 conf->disks[i].replacement = NULL;
5504 clear_bit(Replacement, &rdev->flags);
5505 conf->disks[i].rdev = rdev;
5506 }
5507 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11005508 continue;
NeilBrown17045f52011-12-23 10:17:53 +11005509 if (conf->disks[i].replacement &&
5510 conf->reshape_progress != MaxSector) {
5511 /* replacements and reshape simply do not mix. */
5512 printk(KERN_ERR "md: cannot handle concurrent "
5513 "replacement and reshape.\n");
5514 goto abort;
5515 }
NeilBrown2f115882010-06-17 17:41:03 +10005516 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005517 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005518 continue;
5519 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005520 /* This disc is not fully in-sync. However if it
5521 * just stored parity (beyond the recovery_offset),
5522 * when we don't need to be concerned about the
5523 * array being dirty.
5524 * When reshape goes 'backwards', we never have
5525 * partially completed devices, so we only need
5526 * to worry about reshape going forwards.
5527 */
5528 /* Hack because v0.91 doesn't store recovery_offset properly. */
5529 if (mddev->major_version == 0 &&
5530 mddev->minor_version > 90)
5531 rdev->recovery_offset = reshape_offset;
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07005532
NeilBrownc148ffd2009-11-13 17:47:00 +11005533 if (rdev->recovery_offset < reshape_offset) {
5534 /* We need to check old and new layout */
5535 if (!only_parity(rdev->raid_disk,
5536 conf->algorithm,
5537 conf->raid_disks,
5538 conf->max_degraded))
5539 continue;
5540 }
5541 if (!only_parity(rdev->raid_disk,
5542 conf->prev_algo,
5543 conf->previous_raid_disks,
5544 conf->max_degraded))
5545 continue;
5546 dirty_parity_disks++;
5547 }
NeilBrown91adb562009-03-31 14:39:39 +11005548
NeilBrown17045f52011-12-23 10:17:53 +11005549 /*
5550 * 0 for a fully functional array, 1 or 2 for a degraded array.
5551 */
NeilBrown908f4fb2011-12-23 10:17:50 +11005552 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553
NeilBrown674806d2010-06-16 17:17:53 +10005554 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005555 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07005557 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558 goto abort;
5559 }
5560
NeilBrown91adb562009-03-31 14:39:39 +11005561 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10005562 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11005563 mddev->resync_max_sectors = mddev->dev_sectors;
5564
NeilBrownc148ffd2009-11-13 17:47:00 +11005565 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005567 if (mddev->ok_start_degraded)
5568 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10005569 "md/raid:%s: starting dirty degraded array"
5570 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005571 mdname(mddev));
5572 else {
5573 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005574 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005575 mdname(mddev));
5576 goto abort;
5577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 }
5579
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10005581 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5582 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11005583 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5584 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 else
NeilBrown0c55e022010-05-03 14:09:02 +10005586 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5587 " out of %d devices, algorithm %d\n",
5588 mdname(mddev), conf->level,
5589 mddev->raid_disks - mddev->degraded,
5590 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591
5592 print_raid5_conf(conf);
5593
NeilBrownfef9c612009-03-31 15:16:46 +11005594 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11005595 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005596 atomic_set(&conf->reshape_stripes, 0);
5597 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5598 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5599 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5600 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5601 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005602 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005603 }
5604
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605
5606 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005607 if (mddev->to_remove == &raid5_attrs_group)
5608 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005609 else if (mddev->kobj.sd &&
5610 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005611 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10005612 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08005613 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10005614 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5615
5616 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005617 int chunk_size;
Shaohua Li620125f2012-10-11 13:49:05 +11005618 bool discard_supported = true;
NeilBrown4a5add42010-06-01 19:37:28 +10005619 /* read-ahead size must cover two whole stripes, which
5620 * is 2 * (datadisks) * chunksize where 'n' is the
5621 * number of raid devices
5622 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5624 int stripe = data_disks *
5625 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5626 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5627 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10005628
5629 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005630
5631 mddev->queue->backing_dev_info.congested_data = mddev;
5632 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10005633
5634 chunk_size = mddev->chunk_sectors << 9;
5635 blk_queue_io_min(mddev->queue, chunk_size);
5636 blk_queue_io_opt(mddev->queue, chunk_size *
5637 (conf->raid_disks - conf->max_degraded));
Shaohua Li620125f2012-10-11 13:49:05 +11005638 /*
5639 * We can only discard a whole stripe. It doesn't make sense to
5640 * discard data disk but write parity disk
5641 */
5642 stripe = stripe * PAGE_SIZE;
NeilBrown4ac68752012-11-19 13:11:26 +11005643 /* Round up to power of 2, as discard handling
5644 * currently assumes that */
5645 while ((stripe-1) & stripe)
5646 stripe = (stripe | (stripe-1)) + 1;
Shaohua Li620125f2012-10-11 13:49:05 +11005647 mddev->queue->limits.discard_alignment = stripe;
5648 mddev->queue->limits.discard_granularity = stripe;
5649 /*
5650 * unaligned part of discard request will be ignored, so can't
5651 * guarantee discard_zerors_data
5652 */
5653 mddev->queue->limits.discard_zeroes_data = 0;
NeilBrown9f7c2222010-07-26 12:04:13 +10005654
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07005655 blk_queue_max_write_same_sectors(mddev->queue, 0);
5656
NeilBrown05616be2012-05-21 09:27:00 +10005657 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005658 disk_stack_limits(mddev->gendisk, rdev->bdev,
5659 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10005660 disk_stack_limits(mddev->gendisk, rdev->bdev,
5661 rdev->new_data_offset << 9);
Shaohua Li620125f2012-10-11 13:49:05 +11005662 /*
5663 * discard_zeroes_data is required, otherwise data
5664 * could be lost. Consider a scenario: discard a stripe
5665 * (the stripe could be inconsistent if
5666 * discard_zeroes_data is 0); write one disk of the
5667 * stripe (the stripe could be inconsistent again
5668 * depending on which disks are used to calculate
5669 * parity); the disk is broken; The stripe data of this
5670 * disk is lost.
5671 */
5672 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
5673 !bdev_get_queue(rdev->bdev)->
5674 limits.discard_zeroes_data)
5675 discard_supported = false;
NeilBrown05616be2012-05-21 09:27:00 +10005676 }
Shaohua Li620125f2012-10-11 13:49:05 +11005677
5678 if (discard_supported &&
5679 mddev->queue->limits.max_discard_sectors >= stripe &&
5680 mddev->queue->limits.discard_granularity >= stripe)
5681 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
5682 mddev->queue);
5683 else
5684 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
5685 mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 }
5687
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 return 0;
5689abort:
NeilBrown01f96c02011-09-21 15:30:20 +10005690 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11005691 print_raid5_conf(conf);
5692 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005694 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695 return -EIO;
5696}
5697
NeilBrownfd01b882011-10-11 16:47:53 +11005698static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699{
NeilBrownd1688a62011-10-11 16:49:52 +11005700 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701
NeilBrown01f96c02011-09-21 15:30:20 +10005702 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005703 if (mddev->queue)
5704 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10005705 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005706 mddev->private = NULL;
5707 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 return 0;
5709}
5710
NeilBrownfd01b882011-10-11 16:47:53 +11005711static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712{
NeilBrownd1688a62011-10-11 16:49:52 +11005713 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714 int i;
5715
Andre Noll9d8f0362009-06-18 08:45:01 +10005716 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5717 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005718 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719 for (i = 0; i < conf->raid_disks; i++)
5720 seq_printf (seq, "%s",
5721 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005722 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724}
5725
NeilBrownd1688a62011-10-11 16:49:52 +11005726static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727{
5728 int i;
5729 struct disk_info *tmp;
5730
NeilBrown0c55e022010-05-03 14:09:02 +10005731 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732 if (!conf) {
5733 printk("(conf==NULL)\n");
5734 return;
5735 }
NeilBrown0c55e022010-05-03 14:09:02 +10005736 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5737 conf->raid_disks,
5738 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739
5740 for (i = 0; i < conf->raid_disks; i++) {
5741 char b[BDEVNAME_SIZE];
5742 tmp = conf->disks + i;
5743 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005744 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5745 i, !test_bit(Faulty, &tmp->rdev->flags),
5746 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747 }
5748}
5749
NeilBrownfd01b882011-10-11 16:47:53 +11005750static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751{
5752 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005753 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005755 int count = 0;
5756 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757
5758 for (i = 0; i < conf->raid_disks; i++) {
5759 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11005760 if (tmp->replacement
5761 && tmp->replacement->recovery_offset == MaxSector
5762 && !test_bit(Faulty, &tmp->replacement->flags)
5763 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
5764 /* Replacement has just become active. */
5765 if (!tmp->rdev
5766 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
5767 count++;
5768 if (tmp->rdev) {
5769 /* Replaced device not technically faulty,
5770 * but we need to be sure it gets removed
5771 * and never re-added.
5772 */
5773 set_bit(Faulty, &tmp->rdev->flags);
5774 sysfs_notify_dirent_safe(
5775 tmp->rdev->sysfs_state);
5776 }
5777 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
5778 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005779 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005780 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005781 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005782 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005783 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784 }
5785 }
NeilBrown6b965622010-08-18 11:56:59 +10005786 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005787 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005788 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005790 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791}
5792
NeilBrownb8321b62011-12-23 10:17:51 +11005793static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794{
NeilBrownd1688a62011-10-11 16:49:52 +11005795 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11005797 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11005798 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 struct disk_info *p = conf->disks + number;
5800
5801 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11005802 if (rdev == p->rdev)
5803 rdevp = &p->rdev;
5804 else if (rdev == p->replacement)
5805 rdevp = &p->replacement;
5806 else
5807 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11005808
NeilBrown657e3e42011-12-23 10:17:52 +11005809 if (number >= conf->raid_disks &&
5810 conf->reshape_progress == MaxSector)
5811 clear_bit(In_sync, &rdev->flags);
5812
5813 if (test_bit(In_sync, &rdev->flags) ||
5814 atomic_read(&rdev->nr_pending)) {
5815 err = -EBUSY;
5816 goto abort;
5817 }
5818 /* Only remove non-faulty devices if recovery
5819 * isn't possible.
5820 */
5821 if (!test_bit(Faulty, &rdev->flags) &&
5822 mddev->recovery_disabled != conf->recovery_disabled &&
5823 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11005824 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11005825 number < conf->raid_disks) {
5826 err = -EBUSY;
5827 goto abort;
5828 }
5829 *rdevp = NULL;
5830 synchronize_rcu();
5831 if (atomic_read(&rdev->nr_pending)) {
5832 /* lost the race, try later */
5833 err = -EBUSY;
5834 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11005835 } else if (p->replacement) {
5836 /* We must have just cleared 'rdev' */
5837 p->rdev = p->replacement;
5838 clear_bit(Replacement, &p->replacement->flags);
5839 smp_mb(); /* Make sure other CPUs may see both as identical
5840 * but will never see neither - if they are careful
5841 */
5842 p->replacement = NULL;
5843 clear_bit(WantReplacement, &rdev->flags);
5844 } else
5845 /* We might have just removed the Replacement as faulty-
5846 * clear the bit just in case
5847 */
5848 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849abort:
5850
5851 print_raid5_conf(conf);
5852 return err;
5853}
5854
NeilBrownfd01b882011-10-11 16:47:53 +11005855static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856{
NeilBrownd1688a62011-10-11 16:49:52 +11005857 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005858 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859 int disk;
5860 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005861 int first = 0;
5862 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863
NeilBrown7f0da592011-07-28 11:39:22 +10005864 if (mddev->recovery_disabled == conf->recovery_disabled)
5865 return -EBUSY;
5866
NeilBrowndc10c642012-03-19 12:46:37 +11005867 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005869 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870
Neil Brown6c2fce22008-06-28 08:31:31 +10005871 if (rdev->raid_disk >= 0)
5872 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873
5874 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005875 * find the disk ... but prefer rdev->saved_raid_disk
5876 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005878 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005879 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005880 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10005881 first = rdev->saved_raid_disk;
5882
5883 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11005884 p = conf->disks + disk;
5885 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005886 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005888 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005889 if (rdev->saved_raid_disk != disk)
5890 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005891 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10005892 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005894 }
5895 for (disk = first; disk <= last; disk++) {
5896 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11005897 if (test_bit(WantReplacement, &p->rdev->flags) &&
5898 p->replacement == NULL) {
5899 clear_bit(In_sync, &rdev->flags);
5900 set_bit(Replacement, &rdev->flags);
5901 rdev->raid_disk = disk;
5902 err = 0;
5903 conf->fullsync = 1;
5904 rcu_assign_pointer(p->replacement, rdev);
5905 break;
5906 }
5907 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005908out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005910 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911}
5912
NeilBrownfd01b882011-10-11 16:47:53 +11005913static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914{
5915 /* no resync is happening, and there is enough space
5916 * on all devices, so we can resize.
5917 * We need to make sure resync covers any new space.
5918 * If the array is shrinking we should possibly wait until
5919 * any io in the removed space completes, but it hardly seems
5920 * worth it.
5921 */
NeilBrowna4a61252012-05-22 13:55:27 +10005922 sector_t newsize;
Andre Noll9d8f0362009-06-18 08:45:01 +10005923 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10005924 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
5925 if (mddev->external_size &&
5926 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11005927 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10005928 if (mddev->bitmap) {
5929 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
5930 if (ret)
5931 return ret;
5932 }
5933 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10005934 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005935 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005936 if (sectors > mddev->dev_sectors &&
5937 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005938 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5940 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005941 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005942 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943 return 0;
5944}
5945
NeilBrownfd01b882011-10-11 16:47:53 +11005946static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005947{
5948 /* Can only proceed if there are plenty of stripe_heads.
5949 * We need a minimum of one full stripe,, and for sensible progress
5950 * it is best to have about 4 times that.
5951 * If we require 4 times, then the default 256 4K stripe_heads will
5952 * allow for chunk sizes up to 256K, which is probably OK.
5953 * If the chunk size is greater, user-space should request more
5954 * stripe_heads first.
5955 */
NeilBrownd1688a62011-10-11 16:49:52 +11005956 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005957 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5958 > conf->max_nr_stripes ||
5959 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5960 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005961 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5962 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005963 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5964 / STRIPE_SIZE)*4);
5965 return 0;
5966 }
5967 return 1;
5968}
5969
NeilBrownfd01b882011-10-11 16:47:53 +11005970static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005971{
NeilBrownd1688a62011-10-11 16:49:52 +11005972 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005973
NeilBrown88ce4932009-03-31 15:24:23 +11005974 if (mddev->delta_disks == 0 &&
5975 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005976 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005977 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10005978 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005979 return -EINVAL;
NeilBrownfdcfbbb2013-07-04 16:38:16 +10005980 if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) {
NeilBrownec32a2b2009-03-31 15:17:38 +11005981 /* We might be able to shrink, but the devices must
5982 * be made bigger first.
5983 * For raid6, 4 is the minimum size.
5984 * Otherwise 2 is the minimum
5985 */
5986 int min = 2;
5987 if (mddev->level == 6)
5988 min = 4;
5989 if (mddev->raid_disks + mddev->delta_disks < min)
5990 return -EINVAL;
5991 }
NeilBrown29269552006-03-27 01:18:10 -08005992
NeilBrown01ee22b2009-06-18 08:47:20 +10005993 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005994 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005995
NeilBrowne56108d62012-10-11 14:24:13 +11005996 return resize_stripes(conf, (conf->previous_raid_disks
5997 + mddev->delta_disks));
NeilBrown63c70c42006-03-27 01:18:13 -08005998}
5999
NeilBrownfd01b882011-10-11 16:47:53 +11006000static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08006001{
NeilBrownd1688a62011-10-11 16:49:52 +11006002 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11006003 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08006004 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07006005 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08006006
NeilBrownf4168852007-02-28 20:11:53 -08006007 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08006008 return -EBUSY;
6009
NeilBrown01ee22b2009-06-18 08:47:20 +10006010 if (!check_stripe_cache(mddev))
6011 return -ENOSPC;
6012
NeilBrown30b67642012-05-22 13:55:28 +10006013 if (has_failed(conf))
6014 return -EINVAL;
6015
NeilBrownc6563a82012-05-21 09:27:00 +10006016 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11006017 if (!test_bit(In_sync, &rdev->flags)
6018 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08006019 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10006020 }
NeilBrown63c70c42006-03-27 01:18:13 -08006021
NeilBrownf4168852007-02-28 20:11:53 -08006022 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08006023 /* Not enough devices even to make a degraded array
6024 * of that size
6025 */
6026 return -EINVAL;
6027
NeilBrownec32a2b2009-03-31 15:17:38 +11006028 /* Refuse to reduce size of the array. Any reductions in
6029 * array size must be through explicit setting of array_size
6030 * attribute.
6031 */
6032 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
6033 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10006034 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11006035 "before number of disks\n", mdname(mddev));
6036 return -EINVAL;
6037 }
6038
NeilBrownf6705572006-03-27 01:18:11 -08006039 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08006040 spin_lock_irq(&conf->device_lock);
6041 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08006042 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006043 conf->prev_chunk_sectors = conf->chunk_sectors;
6044 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11006045 conf->prev_algo = conf->algorithm;
6046 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10006047 conf->generation++;
6048 /* Code that selects data_offset needs to see the generation update
6049 * if reshape_progress has been set - so a memory barrier needed.
6050 */
6051 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10006052 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11006053 conf->reshape_progress = raid5_size(mddev, 0, 0);
6054 else
6055 conf->reshape_progress = 0;
6056 conf->reshape_safe = conf->reshape_progress;
NeilBrown29269552006-03-27 01:18:10 -08006057 spin_unlock_irq(&conf->device_lock);
6058
6059 /* Add some new drives, as many as will fit.
6060 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10006061 * Don't add devices if we are reducing the number of
6062 * devices in the array. This is because it is not possible
6063 * to correctly record the "partially reconstructed" state of
6064 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08006065 */
NeilBrown87a8dec2011-01-31 11:57:43 +11006066 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11006067 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11006068 if (rdev->raid_disk < 0 &&
6069 !test_bit(Faulty, &rdev->flags)) {
6070 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11006071 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11006072 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11006073 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11006074 else
NeilBrown87a8dec2011-01-31 11:57:43 +11006075 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10006076
6077 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11006078 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11006079 }
NeilBrown87a8dec2011-01-31 11:57:43 +11006080 } else if (rdev->raid_disk >= conf->previous_raid_disks
6081 && !test_bit(Faulty, &rdev->flags)) {
6082 /* This is a spare that was manually added */
6083 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11006084 }
NeilBrown29269552006-03-27 01:18:10 -08006085
NeilBrown87a8dec2011-01-31 11:57:43 +11006086 /* When a reshape changes the number of devices,
6087 * ->degraded is measured against the larger of the
6088 * pre and post number of devices.
6089 */
NeilBrownec32a2b2009-03-31 15:17:38 +11006090 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11006091 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11006092 spin_unlock_irqrestore(&conf->device_lock, flags);
6093 }
NeilBrown63c70c42006-03-27 01:18:13 -08006094 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10006095 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b422006-10-03 01:15:46 -07006096 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08006097
NeilBrown29269552006-03-27 01:18:10 -08006098 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6099 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6100 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6101 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6102 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10006103 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08006104 if (!mddev->sync_thread) {
6105 mddev->recovery = 0;
6106 spin_lock_irq(&conf->device_lock);
6107 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10006108 rdev_for_each(rdev, mddev)
6109 rdev->new_data_offset = rdev->data_offset;
6110 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11006111 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11006112 mddev->reshape_position = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08006113 spin_unlock_irq(&conf->device_lock);
6114 return -EAGAIN;
6115 }
NeilBrownc8f517c2009-03-31 15:28:40 +11006116 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08006117 md_wakeup_thread(mddev->sync_thread);
6118 md_new_event(mddev);
6119 return 0;
6120}
NeilBrown29269552006-03-27 01:18:10 -08006121
NeilBrownec32a2b2009-03-31 15:17:38 +11006122/* This is called from the reshape thread and should make any
6123 * changes needed in 'conf'
6124 */
NeilBrownd1688a62011-10-11 16:49:52 +11006125static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08006126{
NeilBrown29269552006-03-27 01:18:10 -08006127
NeilBrownf6705572006-03-27 01:18:11 -08006128 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10006129 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07006130
NeilBrownf6705572006-03-27 01:18:11 -08006131 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11006132 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10006133 rdev_for_each(rdev, conf->mddev)
6134 rdev->data_offset = rdev->new_data_offset;
6135 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11006136 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08006137 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11006138 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07006139
6140 /* read-ahead size must cover two whole stripes, which is
6141 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
6142 */
NeilBrown4a5add42010-06-01 19:37:28 +10006143 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11006144 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006145 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11006146 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07006147 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
6148 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
6149 }
NeilBrown29269552006-03-27 01:18:10 -08006150 }
NeilBrown29269552006-03-27 01:18:10 -08006151}
6152
NeilBrownec32a2b2009-03-31 15:17:38 +11006153/* This is called from the raid5d thread with mddev_lock held.
6154 * It makes config changes to the device.
6155 */
NeilBrownfd01b882011-10-11 16:47:53 +11006156static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11006157{
NeilBrownd1688a62011-10-11 16:49:52 +11006158 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11006159
6160 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6161
NeilBrownec32a2b2009-03-31 15:17:38 +11006162 if (mddev->delta_disks > 0) {
6163 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
6164 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006165 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11006166 } else {
6167 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11006168 spin_lock_irq(&conf->device_lock);
6169 mddev->degraded = calc_degraded(conf);
6170 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11006171 for (d = conf->raid_disks ;
6172 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10006173 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11006174 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10006175 if (rdev)
6176 clear_bit(In_sync, &rdev->flags);
6177 rdev = conf->disks[d].replacement;
6178 if (rdev)
6179 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10006180 }
NeilBrowncea9c222009-03-31 15:15:05 +11006181 }
NeilBrown88ce4932009-03-31 15:24:23 +11006182 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006183 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11006184 mddev->reshape_position = MaxSector;
6185 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006186 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11006187 }
6188}
6189
NeilBrownfd01b882011-10-11 16:47:53 +11006190static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07006191{
NeilBrownd1688a62011-10-11 16:49:52 +11006192 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07006193
6194 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08006195 case 2: /* resume for a suspend */
6196 wake_up(&conf->wait_for_overlap);
6197 break;
6198
NeilBrown72626682005-09-09 16:23:54 -07006199 case 1: /* stop all writes */
6200 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006201 /* '2' tells resync/reshape to pause so that all
6202 * active stripes can drain
6203 */
6204 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07006205 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006206 atomic_read(&conf->active_stripes) == 0 &&
6207 atomic_read(&conf->active_aligned_reads) == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01006208 conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006209 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07006210 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006211 /* allow reshape to continue */
6212 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006213 break;
6214
6215 case 0: /* re-enable writes */
6216 spin_lock_irq(&conf->device_lock);
6217 conf->quiesce = 0;
6218 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08006219 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006220 spin_unlock_irq(&conf->device_lock);
6221 break;
6222 }
NeilBrown72626682005-09-09 16:23:54 -07006223}
NeilBrownb15c2e52006-01-06 00:20:16 -08006224
NeilBrownd562b0c2009-03-31 14:39:39 +11006225
NeilBrownfd01b882011-10-11 16:47:53 +11006226static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11006227{
NeilBrowne373ab12011-10-11 16:48:59 +11006228 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07006229 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11006230
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006231 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11006232 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10006233 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
6234 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006235 return ERR_PTR(-EINVAL);
6236 }
6237
NeilBrowne373ab12011-10-11 16:48:59 +11006238 sectors = raid0_conf->strip_zone[0].zone_end;
6239 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10006240 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006241 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11006242 mddev->new_layout = ALGORITHM_PARITY_N;
6243 mddev->new_chunk_sectors = mddev->chunk_sectors;
6244 mddev->raid_disks += 1;
6245 mddev->delta_disks = 1;
6246 /* make sure it will be not marked as dirty */
6247 mddev->recovery_cp = MaxSector;
6248
6249 return setup_conf(mddev);
6250}
6251
6252
NeilBrownfd01b882011-10-11 16:47:53 +11006253static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006254{
6255 int chunksect;
6256
6257 if (mddev->raid_disks != 2 ||
6258 mddev->degraded > 1)
6259 return ERR_PTR(-EINVAL);
6260
6261 /* Should check if there are write-behind devices? */
6262
6263 chunksect = 64*2; /* 64K by default */
6264
6265 /* The array must be an exact multiple of chunksize */
6266 while (chunksect && (mddev->array_sectors & (chunksect-1)))
6267 chunksect >>= 1;
6268
6269 if ((chunksect<<9) < STRIPE_SIZE)
6270 /* array size does not allow a suitable chunk size */
6271 return ERR_PTR(-EINVAL);
6272
6273 mddev->new_level = 5;
6274 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10006275 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11006276
6277 return setup_conf(mddev);
6278}
6279
NeilBrownfd01b882011-10-11 16:47:53 +11006280static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11006281{
6282 int new_layout;
6283
6284 switch (mddev->layout) {
6285 case ALGORITHM_LEFT_ASYMMETRIC_6:
6286 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
6287 break;
6288 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6289 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
6290 break;
6291 case ALGORITHM_LEFT_SYMMETRIC_6:
6292 new_layout = ALGORITHM_LEFT_SYMMETRIC;
6293 break;
6294 case ALGORITHM_RIGHT_SYMMETRIC_6:
6295 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
6296 break;
6297 case ALGORITHM_PARITY_0_6:
6298 new_layout = ALGORITHM_PARITY_0;
6299 break;
6300 case ALGORITHM_PARITY_N:
6301 new_layout = ALGORITHM_PARITY_N;
6302 break;
6303 default:
6304 return ERR_PTR(-EINVAL);
6305 }
6306 mddev->new_level = 5;
6307 mddev->new_layout = new_layout;
6308 mddev->delta_disks = -1;
6309 mddev->raid_disks -= 1;
6310 return setup_conf(mddev);
6311}
6312
NeilBrownd562b0c2009-03-31 14:39:39 +11006313
NeilBrownfd01b882011-10-11 16:47:53 +11006314static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11006315{
NeilBrown88ce4932009-03-31 15:24:23 +11006316 /* For a 2-drive array, the layout and chunk size can be changed
6317 * immediately as not restriping is needed.
6318 * For larger arrays we record the new value - after validation
6319 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11006320 */
NeilBrownd1688a62011-10-11 16:49:52 +11006321 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10006322 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11006323
NeilBrown597a7112009-06-18 08:47:42 +10006324 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11006325 return -EINVAL;
6326 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006327 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11006328 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006329 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11006330 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006331 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11006332 /* not factor of array size */
6333 return -EINVAL;
6334 }
6335
6336 /* They look valid */
6337
NeilBrown88ce4932009-03-31 15:24:23 +11006338 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10006339 /* can make the change immediately */
6340 if (mddev->new_layout >= 0) {
6341 conf->algorithm = mddev->new_layout;
6342 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11006343 }
6344 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10006345 conf->chunk_sectors = new_chunk ;
6346 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11006347 }
6348 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6349 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11006350 }
NeilBrown50ac1682009-06-18 08:47:55 +10006351 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11006352}
6353
NeilBrownfd01b882011-10-11 16:47:53 +11006354static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11006355{
NeilBrown597a7112009-06-18 08:47:42 +10006356 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10006357
NeilBrown597a7112009-06-18 08:47:42 +10006358 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11006359 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006360 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006361 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11006362 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006363 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11006364 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006365 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11006366 /* not factor of array size */
6367 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006368 }
NeilBrown88ce4932009-03-31 15:24:23 +11006369
6370 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10006371 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11006372}
6373
NeilBrownfd01b882011-10-11 16:47:53 +11006374static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006375{
6376 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006377 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006378 * raid1 - if there are two drives. We need to know the chunk size
6379 * raid4 - trivial - just use a raid4 layout.
6380 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006381 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006382 if (mddev->level == 0)
6383 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11006384 if (mddev->level == 1)
6385 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11006386 if (mddev->level == 4) {
6387 mddev->new_layout = ALGORITHM_PARITY_N;
6388 mddev->new_level = 5;
6389 return setup_conf(mddev);
6390 }
NeilBrownfc9739c2009-03-31 14:57:20 +11006391 if (mddev->level == 6)
6392 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11006393
6394 return ERR_PTR(-EINVAL);
6395}
6396
NeilBrownfd01b882011-10-11 16:47:53 +11006397static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11006398{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006399 /* raid4 can take over:
6400 * raid0 - if there is only one strip zone
6401 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11006402 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006403 if (mddev->level == 0)
6404 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11006405 if (mddev->level == 5 &&
6406 mddev->layout == ALGORITHM_PARITY_N) {
6407 mddev->new_layout = 0;
6408 mddev->new_level = 4;
6409 return setup_conf(mddev);
6410 }
6411 return ERR_PTR(-EINVAL);
6412}
NeilBrownd562b0c2009-03-31 14:39:39 +11006413
NeilBrown84fc4b52011-10-11 16:49:58 +11006414static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11006415
NeilBrownfd01b882011-10-11 16:47:53 +11006416static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11006417{
6418 /* Currently can only take over a raid5. We map the
6419 * personality to an equivalent raid6 personality
6420 * with the Q block at the end.
6421 */
6422 int new_layout;
6423
6424 if (mddev->pers != &raid5_personality)
6425 return ERR_PTR(-EINVAL);
6426 if (mddev->degraded > 1)
6427 return ERR_PTR(-EINVAL);
6428 if (mddev->raid_disks > 253)
6429 return ERR_PTR(-EINVAL);
6430 if (mddev->raid_disks < 3)
6431 return ERR_PTR(-EINVAL);
6432
6433 switch (mddev->layout) {
6434 case ALGORITHM_LEFT_ASYMMETRIC:
6435 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
6436 break;
6437 case ALGORITHM_RIGHT_ASYMMETRIC:
6438 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
6439 break;
6440 case ALGORITHM_LEFT_SYMMETRIC:
6441 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
6442 break;
6443 case ALGORITHM_RIGHT_SYMMETRIC:
6444 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
6445 break;
6446 case ALGORITHM_PARITY_0:
6447 new_layout = ALGORITHM_PARITY_0_6;
6448 break;
6449 case ALGORITHM_PARITY_N:
6450 new_layout = ALGORITHM_PARITY_N;
6451 break;
6452 default:
6453 return ERR_PTR(-EINVAL);
6454 }
6455 mddev->new_level = 6;
6456 mddev->new_layout = new_layout;
6457 mddev->delta_disks = 1;
6458 mddev->raid_disks += 1;
6459 return setup_conf(mddev);
6460}
6461
6462
NeilBrown84fc4b52011-10-11 16:49:58 +11006463static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07006464{
6465 .name = "raid6",
6466 .level = 6,
6467 .owner = THIS_MODULE,
6468 .make_request = make_request,
6469 .run = run,
6470 .stop = stop,
6471 .status = status,
6472 .error_handler = error,
6473 .hot_add_disk = raid5_add_disk,
6474 .hot_remove_disk= raid5_remove_disk,
6475 .spare_active = raid5_spare_active,
6476 .sync_request = sync_request,
6477 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006478 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10006479 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08006480 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006481 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07006482 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11006483 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07006484};
NeilBrown84fc4b52011-10-11 16:49:58 +11006485static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006486{
6487 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08006488 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006489 .owner = THIS_MODULE,
6490 .make_request = make_request,
6491 .run = run,
6492 .stop = stop,
6493 .status = status,
6494 .error_handler = error,
6495 .hot_add_disk = raid5_add_disk,
6496 .hot_remove_disk= raid5_remove_disk,
6497 .spare_active = raid5_spare_active,
6498 .sync_request = sync_request,
6499 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006500 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08006501 .check_reshape = raid5_check_reshape,
6502 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006503 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07006504 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11006505 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506};
6507
NeilBrown84fc4b52011-10-11 16:49:58 +11006508static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006509{
NeilBrown2604b702006-01-06 00:20:36 -08006510 .name = "raid4",
6511 .level = 4,
6512 .owner = THIS_MODULE,
6513 .make_request = make_request,
6514 .run = run,
6515 .stop = stop,
6516 .status = status,
6517 .error_handler = error,
6518 .hot_add_disk = raid5_add_disk,
6519 .hot_remove_disk= raid5_remove_disk,
6520 .spare_active = raid5_spare_active,
6521 .sync_request = sync_request,
6522 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006523 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08006524 .check_reshape = raid5_check_reshape,
6525 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006526 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08006527 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11006528 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08006529};
6530
6531static int __init raid5_init(void)
6532{
NeilBrown16a53ec2006-06-26 00:27:38 -07006533 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006534 register_md_personality(&raid5_personality);
6535 register_md_personality(&raid4_personality);
6536 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006537}
6538
NeilBrown2604b702006-01-06 00:20:36 -08006539static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006540{
NeilBrown16a53ec2006-06-26 00:27:38 -07006541 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006542 unregister_md_personality(&raid5_personality);
6543 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006544}
6545
6546module_init(raid5_init);
6547module_exit(raid5_exit);
6548MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11006549MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08006551MODULE_ALIAS("md-raid5");
6552MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08006553MODULE_ALIAS("md-level-5");
6554MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07006555MODULE_ALIAS("md-personality-8"); /* RAID6 */
6556MODULE_ALIAS("md-raid6");
6557MODULE_ALIAS("md-level-6");
6558
6559/* This used to be two separate modules, they were: */
6560MODULE_ALIAS("raid5");
6561MODULE_ALIAS("raid6");