blob: 848034666342a0e9b8ec744955d4659ee6b5f48f [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>
NeilBrown43b2e5d2009-03-31 14:33:13 +110056#include "md.h"
NeilBrownbff61972009-03-31 14:33:13 +110057#include "raid5.h"
Trela Maciej54071b32010-03-08 16:02:42 +110058#include "raid0.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110059#include "bitmap.h"
NeilBrown72626682005-09-09 16:23:54 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/*
62 * Stripe cache
63 */
64
65#define NR_STRIPES 256
66#define STRIPE_SIZE PAGE_SIZE
67#define STRIPE_SHIFT (PAGE_SHIFT - 9)
68#define STRIPE_SECTORS (STRIPE_SIZE>>9)
69#define IO_THRESHOLD 1
Dan Williams8b3e6cd2008-04-28 02:15:53 -070070#define BYPASS_THRESHOLD 1
NeilBrownfccddba2006-01-06 00:20:33 -080071#define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define HASH_MASK (NR_HASH - 1)
73
NeilBrownd1688a62011-10-11 16:49:52 +110074static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
NeilBrowndb298e12011-10-07 14:23:00 +110075{
76 int hash = (sect >> STRIPE_SHIFT) & HASH_MASK;
77 return &conf->stripe_hashtbl[hash];
78}
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80/* bio's attached to a stripe+device for I/O are linked together in bi_sector
81 * order without overlap. There may be several bio's per stripe+device, and
82 * a bio could span several devices.
83 * When walking this list for a particular stripe+device, we must never proceed
84 * beyond a bio that extends past this device, as the next bio might no longer
85 * be valid.
NeilBrowndb298e12011-10-07 14:23:00 +110086 * This function is used to determine the 'next' bio in the list, given the sector
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * of the current stripe+device
88 */
NeilBrowndb298e12011-10-07 14:23:00 +110089static inline struct bio *r5_next_bio(struct bio *bio, sector_t sector)
90{
91 int sectors = bio->bi_size >> 9;
92 if (bio->bi_sector + sectors < sector + STRIPE_SECTORS)
93 return bio->bi_next;
94 else
95 return NULL;
96}
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Jens Axboe960e7392008-08-15 10:41:18 +020098/*
Jens Axboe5b99c2f2008-08-15 10:56:11 +020099 * We maintain a biased count of active stripes in the bottom 16 bits of
100 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
Jens Axboe960e7392008-08-15 10:41:18 +0200101 */
102static inline int raid5_bi_phys_segments(struct bio *bio)
103{
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200104 return bio->bi_phys_segments & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200105}
106
107static inline int raid5_bi_hw_segments(struct bio *bio)
108{
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200109 return (bio->bi_phys_segments >> 16) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200110}
111
112static inline int raid5_dec_bi_phys_segments(struct bio *bio)
113{
114 --bio->bi_phys_segments;
115 return raid5_bi_phys_segments(bio);
116}
117
118static inline int raid5_dec_bi_hw_segments(struct bio *bio)
119{
120 unsigned short val = raid5_bi_hw_segments(bio);
121
122 --val;
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200123 bio->bi_phys_segments = (val << 16) | raid5_bi_phys_segments(bio);
Jens Axboe960e7392008-08-15 10:41:18 +0200124 return val;
125}
126
127static inline void raid5_set_bi_hw_segments(struct bio *bio, unsigned int cnt)
128{
Namhyung Kim9b2dc8b2011-06-13 14:48:22 +0900129 bio->bi_phys_segments = raid5_bi_phys_segments(bio) | (cnt << 16);
Jens Axboe960e7392008-08-15 10:41:18 +0200130}
131
NeilBrownd0dabf72009-03-31 14:39:38 +1100132/* Find first data disk in a raid6 stripe */
133static inline int raid6_d0(struct stripe_head *sh)
134{
NeilBrown67cc2b82009-03-31 14:39:38 +1100135 if (sh->ddf_layout)
136 /* ddf always start from first device */
137 return 0;
138 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100139 if (sh->qd_idx == sh->disks - 1)
140 return 0;
141 else
142 return sh->qd_idx + 1;
143}
NeilBrown16a53ec2006-06-26 00:27:38 -0700144static inline int raid6_next_disk(int disk, int raid_disks)
145{
146 disk++;
147 return (disk < raid_disks) ? disk : 0;
148}
Dan Williamsa4456852007-07-09 11:56:43 -0700149
NeilBrownd0dabf72009-03-31 14:39:38 +1100150/* When walking through the disks in a raid5, starting at raid6_d0,
151 * We need to map each disk to a 'slot', where the data disks are slot
152 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
153 * is raid_disks-1. This help does that mapping.
154 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100155static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
156 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100157{
Dan Williams66295422009-10-19 18:09:32 -0700158 int slot = *count;
NeilBrown67cc2b82009-03-31 14:39:38 +1100159
NeilBrowne4424fe2009-10-16 16:27:34 +1100160 if (sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700161 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100162 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100163 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100164 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100165 return syndrome_disks + 1;
NeilBrowne4424fe2009-10-16 16:27:34 +1100166 if (!sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700167 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100168 return slot;
169}
170
Dan Williamsa4456852007-07-09 11:56:43 -0700171static void return_io(struct bio *return_bi)
172{
173 struct bio *bi = return_bi;
174 while (bi) {
Dan Williamsa4456852007-07-09 11:56:43 -0700175
176 return_bi = bi->bi_next;
177 bi->bi_next = NULL;
178 bi->bi_size = 0;
Neil Brown0e13fe232008-06-28 08:31:20 +1000179 bio_endio(bi, 0);
Dan Williamsa4456852007-07-09 11:56:43 -0700180 bi = return_bi;
181 }
182}
183
NeilBrownd1688a62011-10-11 16:49:52 +1100184static void print_raid5_conf (struct r5conf *conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Dan Williams600aa102008-06-28 08:32:05 +1000186static int stripe_operations_active(struct stripe_head *sh)
187{
188 return sh->check_state || sh->reconstruct_state ||
189 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
190 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
191}
192
Shaohua Li4eb788d2012-07-19 16:01:31 +1000193static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Shaohua Li4eb788d2012-07-19 16:01:31 +1000195 BUG_ON(!list_empty(&sh->lru));
196 BUG_ON(atomic_read(&conf->active_stripes)==0);
197 if (test_bit(STRIPE_HANDLE, &sh->state)) {
198 if (test_bit(STRIPE_DELAYED, &sh->state) &&
199 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
200 list_add_tail(&sh->lru, &conf->delayed_list);
201 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
202 sh->bm_seq - conf->seq_write > 0)
203 list_add_tail(&sh->lru, &conf->bitmap_list);
204 else {
205 clear_bit(STRIPE_DELAYED, &sh->state);
206 clear_bit(STRIPE_BIT_DELAY, &sh->state);
207 list_add_tail(&sh->lru, &conf->handle_list);
208 }
209 md_wakeup_thread(conf->mddev->thread);
210 } else {
211 BUG_ON(stripe_operations_active(sh));
212 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
213 if (atomic_dec_return(&conf->preread_active_stripes)
214 < IO_THRESHOLD)
215 md_wakeup_thread(conf->mddev->thread);
216 atomic_dec(&conf->active_stripes);
217 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
218 list_add_tail(&sh->lru, &conf->inactive_list);
219 wake_up(&conf->wait_for_stripe);
220 if (conf->retry_read_aligned)
221 md_wakeup_thread(conf->mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
223 }
224}
NeilBrownd0dabf72009-03-31 14:39:38 +1100225
Shaohua Li4eb788d2012-07-19 16:01:31 +1000226static void __release_stripe(struct r5conf *conf, struct stripe_head *sh)
227{
228 if (atomic_dec_and_test(&sh->count))
229 do_release_stripe(conf, sh);
230}
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232static void release_stripe(struct stripe_head *sh)
233{
NeilBrownd1688a62011-10-11 16:49:52 +1100234 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 unsigned long flags;
NeilBrown16a53ec2006-06-26 00:27:38 -0700236
Shaohua Li4eb788d2012-07-19 16:01:31 +1000237 local_irq_save(flags);
238 if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) {
239 do_release_stripe(conf, sh);
240 spin_unlock(&conf->device_lock);
241 }
242 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
NeilBrownfccddba2006-01-06 00:20:33 -0800245static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
Dan Williams45b42332007-07-09 11:56:43 -0700247 pr_debug("remove_hash(), stripe %llu\n",
248 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
NeilBrownfccddba2006-01-06 00:20:33 -0800250 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
NeilBrownd1688a62011-10-11 16:49:52 +1100253static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
NeilBrownfccddba2006-01-06 00:20:33 -0800255 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Dan Williams45b42332007-07-09 11:56:43 -0700257 pr_debug("insert_hash(), stripe %llu\n",
258 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
NeilBrownfccddba2006-01-06 00:20:33 -0800260 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
263
264/* find an idle stripe, make sure it is unhashed, and return it. */
NeilBrownd1688a62011-10-11 16:49:52 +1100265static struct stripe_head *get_free_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 struct stripe_head *sh = NULL;
268 struct list_head *first;
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (list_empty(&conf->inactive_list))
271 goto out;
272 first = conf->inactive_list.next;
273 sh = list_entry(first, struct stripe_head, lru);
274 list_del_init(first);
275 remove_hash(sh);
276 atomic_inc(&conf->active_stripes);
277out:
278 return sh;
279}
280
NeilBrowne4e11e32010-06-16 16:45:16 +1000281static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 struct page *p;
284 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000285 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
NeilBrowne4e11e32010-06-16 16:45:16 +1000287 for (i = 0; i < num ; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 p = sh->dev[i].page;
289 if (!p)
290 continue;
291 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800292 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294}
295
NeilBrowne4e11e32010-06-16 16:45:16 +1000296static int grow_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000299 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
NeilBrowne4e11e32010-06-16 16:45:16 +1000301 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 struct page *page;
303
304 if (!(page = alloc_page(GFP_KERNEL))) {
305 return 1;
306 }
307 sh->dev[i].page = page;
308 }
309 return 0;
310}
311
NeilBrown784052e2009-03-31 15:19:07 +1100312static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrownd1688a62011-10-11 16:49:52 +1100313static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100314 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
NeilBrownb5663ba2009-03-31 14:39:38 +1100316static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
NeilBrownd1688a62011-10-11 16:49:52 +1100318 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800319 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200321 BUG_ON(atomic_read(&sh->count) != 0);
322 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000323 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700324
Dan Williams45b42332007-07-09 11:56:43 -0700325 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 (unsigned long long)sh->sector);
327
328 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700329
NeilBrown86b42c72009-03-31 15:19:03 +1100330 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100331 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100333 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 sh->state = 0;
335
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800336
337 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 struct r5dev *dev = &sh->dev[i];
339
Dan Williamsd84e0f12007-01-02 13:52:30 -0700340 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700342 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700344 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000346 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100349 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
351 insert_hash(conf, sh);
352}
353
NeilBrownd1688a62011-10-11 16:49:52 +1100354static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100355 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
357 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -0800358 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Dan Williams45b42332007-07-09 11:56:43 -0700360 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
NeilBrownfccddba2006-01-06 00:20:33 -0800361 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100362 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700364 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return NULL;
366}
367
NeilBrown674806d2010-06-16 17:17:53 +1000368/*
369 * Need to check if array has failed when deciding whether to:
370 * - start an array
371 * - remove non-faulty devices
372 * - add a spare
373 * - allow a reshape
374 * This determination is simple when no reshape is happening.
375 * However if there is a reshape, we need to carefully check
376 * both the before and after sections.
377 * This is because some failed devices may only affect one
378 * of the two sections, and some non-in_sync devices may
379 * be insync in the section most affected by failed devices.
380 */
NeilBrown908f4fb2011-12-23 10:17:50 +1100381static int calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000382{
NeilBrown908f4fb2011-12-23 10:17:50 +1100383 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000384 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000385
386 rcu_read_lock();
387 degraded = 0;
388 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100389 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown674806d2010-06-16 17:17:53 +1000390 if (!rdev || test_bit(Faulty, &rdev->flags))
391 degraded++;
392 else if (test_bit(In_sync, &rdev->flags))
393 ;
394 else
395 /* not in-sync or faulty.
396 * If the reshape increases the number of devices,
397 * this is being recovered by the reshape, so
398 * this 'previous' section is not in_sync.
399 * If the number of devices is being reduced however,
400 * the device can only be part of the array if
401 * we are reverting a reshape, so this section will
402 * be in-sync.
403 */
404 if (conf->raid_disks >= conf->previous_raid_disks)
405 degraded++;
406 }
407 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100408 if (conf->raid_disks == conf->previous_raid_disks)
409 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000410 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100411 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000412 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100413 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown674806d2010-06-16 17:17:53 +1000414 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100415 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000416 else if (test_bit(In_sync, &rdev->flags))
417 ;
418 else
419 /* not in-sync or faulty.
420 * If reshape increases the number of devices, this
421 * section has already been recovered, else it
422 * almost certainly hasn't.
423 */
424 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100425 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000426 }
427 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100428 if (degraded2 > degraded)
429 return degraded2;
430 return degraded;
431}
432
433static int has_failed(struct r5conf *conf)
434{
435 int degraded;
436
437 if (conf->mddev->reshape_position == MaxSector)
438 return conf->mddev->degraded > conf->max_degraded;
439
440 degraded = calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000441 if (degraded > conf->max_degraded)
442 return 1;
443 return 0;
444}
445
NeilBrownb5663ba2009-03-31 14:39:38 +1100446static struct stripe_head *
NeilBrownd1688a62011-10-11 16:49:52 +1100447get_active_stripe(struct r5conf *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000448 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
450 struct stripe_head *sh;
451
Dan Williams45b42332007-07-09 11:56:43 -0700452 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 spin_lock_irq(&conf->device_lock);
455
456 do {
NeilBrown72626682005-09-09 16:23:54 -0700457 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000458 conf->quiesce == 0 || noquiesce,
NeilBrown72626682005-09-09 16:23:54 -0700459 conf->device_lock, /* nothing */);
NeilBrown86b42c72009-03-31 15:19:03 +1100460 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (!sh) {
462 if (!conf->inactive_blocked)
463 sh = get_free_stripe(conf);
464 if (noblock && sh == NULL)
465 break;
466 if (!sh) {
467 conf->inactive_blocked = 1;
468 wait_event_lock_irq(conf->wait_for_stripe,
469 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800470 (atomic_read(&conf->active_stripes)
471 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 || !conf->inactive_blocked),
473 conf->device_lock,
NeilBrown7c13edc2011-04-18 18:25:43 +1000474 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 conf->inactive_blocked = 0;
476 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100477 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 } else {
479 if (atomic_read(&sh->count)) {
NeilBrownab69ae12009-03-31 15:26:47 +1100480 BUG_ON(!list_empty(&sh->lru)
481 && !test_bit(STRIPE_EXPANDING, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 } else {
483 if (!test_bit(STRIPE_HANDLE, &sh->state))
484 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700485 if (list_empty(&sh->lru) &&
486 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700487 BUG();
488 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
490 }
491 } while (sh == NULL);
492
493 if (sh)
494 atomic_inc(&sh->count);
495
496 spin_unlock_irq(&conf->device_lock);
497 return sh;
498}
499
NeilBrown05616be2012-05-21 09:27:00 +1000500/* Determine if 'data_offset' or 'new_data_offset' should be used
501 * in this stripe_head.
502 */
503static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
504{
505 sector_t progress = conf->reshape_progress;
506 /* Need a memory barrier to make sure we see the value
507 * of conf->generation, or ->data_offset that was set before
508 * reshape_progress was updated.
509 */
510 smp_rmb();
511 if (progress == MaxSector)
512 return 0;
513 if (sh->generation == conf->generation - 1)
514 return 0;
515 /* We are in a reshape, and this is a new-generation stripe,
516 * so use new_data_offset.
517 */
518 return 1;
519}
520
NeilBrown6712ecf2007-09-27 12:47:43 +0200521static void
522raid5_end_read_request(struct bio *bi, int error);
523static void
524raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700525
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000526static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700527{
NeilBrownd1688a62011-10-11 16:49:52 +1100528 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700529 int i, disks = sh->disks;
530
531 might_sleep();
532
533 for (i = disks; i--; ) {
534 int rw;
NeilBrown9a3e1102011-12-23 10:17:53 +1100535 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100536 struct bio *bi, *rbi;
537 struct md_rdev *rdev, *rrdev = NULL;
Tejun Heoe9c74692010-09-03 11:56:18 +0200538 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
539 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
540 rw = WRITE_FUA;
541 else
542 rw = WRITE;
543 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700544 rw = READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100545 else if (test_and_clear_bit(R5_WantReplace,
546 &sh->dev[i].flags)) {
547 rw = WRITE;
548 replace_only = 1;
549 } else
Dan Williams91c00922007-01-02 13:52:30 -0700550 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +1000551 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
552 rw |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -0700553
554 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100555 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700556
557 bi->bi_rw = rw;
NeilBrown977df362011-12-23 10:17:53 +1100558 rbi->bi_rw = rw;
559 if (rw & WRITE) {
Dan Williams91c00922007-01-02 13:52:30 -0700560 bi->bi_end_io = raid5_end_write_request;
NeilBrown977df362011-12-23 10:17:53 +1100561 rbi->bi_end_io = raid5_end_write_request;
562 } else
Dan Williams91c00922007-01-02 13:52:30 -0700563 bi->bi_end_io = raid5_end_read_request;
564
565 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100566 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100567 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
568 rdev = rcu_dereference(conf->disks[i].rdev);
569 if (!rdev) {
570 rdev = rrdev;
571 rrdev = NULL;
572 }
NeilBrown9a3e1102011-12-23 10:17:53 +1100573 if (rw & WRITE) {
574 if (replace_only)
575 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100576 if (rdev == rrdev)
577 /* We raced and saw duplicates */
578 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100579 } else {
NeilBrowndd054fc2011-12-23 10:17:53 +1100580 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100581 rdev = rrdev;
582 rrdev = NULL;
583 }
NeilBrown977df362011-12-23 10:17:53 +1100584
Dan Williams91c00922007-01-02 13:52:30 -0700585 if (rdev && test_bit(Faulty, &rdev->flags))
586 rdev = NULL;
587 if (rdev)
588 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100589 if (rrdev && test_bit(Faulty, &rrdev->flags))
590 rrdev = NULL;
591 if (rrdev)
592 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700593 rcu_read_unlock();
594
NeilBrown73e92e52011-07-28 11:39:22 +1000595 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100596 * need to check for writes. We never accept write errors
597 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000598 */
599 while ((rw & WRITE) && rdev &&
600 test_bit(WriteErrorSeen, &rdev->flags)) {
601 sector_t first_bad;
602 int bad_sectors;
603 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
604 &first_bad, &bad_sectors);
605 if (!bad)
606 break;
607
608 if (bad < 0) {
609 set_bit(BlockedBadBlocks, &rdev->flags);
610 if (!conf->mddev->external &&
611 conf->mddev->flags) {
612 /* It is very unlikely, but we might
613 * still need to write out the
614 * bad block log - better give it
615 * a chance*/
616 md_check_recovery(conf->mddev);
617 }
majianpeng18507532012-07-03 12:11:54 +1000618 /*
619 * Because md_wait_for_blocked_rdev
620 * will dec nr_pending, we must
621 * increment it first.
622 */
623 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +1000624 md_wait_for_blocked_rdev(rdev, conf->mddev);
625 } else {
626 /* Acknowledged bad block - skip the write */
627 rdev_dec_pending(rdev, conf->mddev);
628 rdev = NULL;
629 }
630 }
631
Dan Williams91c00922007-01-02 13:52:30 -0700632 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100633 if (s->syncing || s->expanding || s->expanded
634 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700635 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
636
Dan Williams2b7497f2008-06-28 08:31:52 +1000637 set_bit(STRIPE_IO_STARTED, &sh->state);
638
Dan Williams91c00922007-01-02 13:52:30 -0700639 bi->bi_bdev = rdev->bdev;
640 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700641 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700642 bi->bi_rw, i);
643 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000644 if (use_new_offset(conf, sh))
645 bi->bi_sector = (sh->sector
646 + rdev->new_data_offset);
647 else
648 bi->bi_sector = (sh->sector
649 + rdev->data_offset);
Dan Williams91c00922007-01-02 13:52:30 -0700650 bi->bi_flags = 1 << BIO_UPTODATE;
Dan Williams91c00922007-01-02 13:52:30 -0700651 bi->bi_idx = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700652 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
653 bi->bi_io_vec[0].bv_offset = 0;
654 bi->bi_size = STRIPE_SIZE;
655 bi->bi_next = NULL;
NeilBrown977df362011-12-23 10:17:53 +1100656 if (rrdev)
657 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Dan Williams91c00922007-01-02 13:52:30 -0700658 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100659 }
660 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100661 if (s->syncing || s->expanding || s->expanded
662 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +1100663 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
664
665 set_bit(STRIPE_IO_STARTED, &sh->state);
666
667 rbi->bi_bdev = rrdev->bdev;
668 pr_debug("%s: for %llu schedule op %ld on "
669 "replacement disc %d\n",
670 __func__, (unsigned long long)sh->sector,
671 rbi->bi_rw, i);
672 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000673 if (use_new_offset(conf, sh))
674 rbi->bi_sector = (sh->sector
675 + rrdev->new_data_offset);
676 else
677 rbi->bi_sector = (sh->sector
678 + rrdev->data_offset);
NeilBrown977df362011-12-23 10:17:53 +1100679 rbi->bi_flags = 1 << BIO_UPTODATE;
680 rbi->bi_idx = 0;
681 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
682 rbi->bi_io_vec[0].bv_offset = 0;
683 rbi->bi_size = STRIPE_SIZE;
684 rbi->bi_next = NULL;
685 generic_make_request(rbi);
686 }
687 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000688 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700689 set_bit(STRIPE_DEGRADED, &sh->state);
690 pr_debug("skip op %ld on disc %d for sector %llu\n",
691 bi->bi_rw, i, (unsigned long long)sh->sector);
692 clear_bit(R5_LOCKED, &sh->dev[i].flags);
693 set_bit(STRIPE_HANDLE, &sh->state);
694 }
695 }
696}
697
698static struct dma_async_tx_descriptor *
699async_copy_data(int frombio, struct bio *bio, struct page *page,
700 sector_t sector, struct dma_async_tx_descriptor *tx)
701{
702 struct bio_vec *bvl;
703 struct page *bio_page;
704 int i;
705 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700706 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700707 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700708
709 if (bio->bi_sector >= sector)
710 page_offset = (signed)(bio->bi_sector - sector) * 512;
711 else
712 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700713
Dan Williams0403e382009-09-08 17:42:50 -0700714 if (frombio)
715 flags |= ASYNC_TX_FENCE;
716 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
717
Dan Williams91c00922007-01-02 13:52:30 -0700718 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000719 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700720 int clen;
721 int b_offset = 0;
722
723 if (page_offset < 0) {
724 b_offset = -page_offset;
725 page_offset += b_offset;
726 len -= b_offset;
727 }
728
729 if (len > 0 && page_offset + len > STRIPE_SIZE)
730 clen = STRIPE_SIZE - page_offset;
731 else
732 clen = len;
733
734 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000735 b_offset += bvl->bv_offset;
736 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700737 if (frombio)
738 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700739 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700740 else
741 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700742 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700743 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700744 /* chain the operations */
745 submit.depend_tx = tx;
746
Dan Williams91c00922007-01-02 13:52:30 -0700747 if (clen < len) /* hit end of page */
748 break;
749 page_offset += len;
750 }
751
752 return tx;
753}
754
755static void ops_complete_biofill(void *stripe_head_ref)
756{
757 struct stripe_head *sh = stripe_head_ref;
758 struct bio *return_bi = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +1100759 struct r5conf *conf = sh->raid_conf;
Dan Williamse4d84902007-09-24 10:06:13 -0700760 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700761
Harvey Harrisone46b2722008-04-28 02:15:50 -0700762 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700763 (unsigned long long)sh->sector);
764
765 /* clear completed biofills */
Dan Williams83de75c2008-06-28 08:31:58 +1000766 spin_lock_irq(&conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700767 for (i = sh->disks; i--; ) {
768 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700769
770 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700771 /* and check if we need to reply to a read request,
772 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000773 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700774 */
775 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700776 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700777
Dan Williams91c00922007-01-02 13:52:30 -0700778 BUG_ON(!dev->read);
779 rbi = dev->read;
780 dev->read = NULL;
781 while (rbi && rbi->bi_sector <
782 dev->sector + STRIPE_SECTORS) {
783 rbi2 = r5_next_bio(rbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +0200784 if (!raid5_dec_bi_phys_segments(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700785 rbi->bi_next = return_bi;
786 return_bi = rbi;
787 }
Dan Williams91c00922007-01-02 13:52:30 -0700788 rbi = rbi2;
789 }
790 }
791 }
Dan Williams83de75c2008-06-28 08:31:58 +1000792 spin_unlock_irq(&conf->device_lock);
793 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700794
795 return_io(return_bi);
796
Dan Williamse4d84902007-09-24 10:06:13 -0700797 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700798 release_stripe(sh);
799}
800
801static void ops_run_biofill(struct stripe_head *sh)
802{
803 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +1100804 struct r5conf *conf = sh->raid_conf;
Dan Williamsa08abd82009-06-03 11:43:59 -0700805 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700806 int i;
807
Harvey Harrisone46b2722008-04-28 02:15:50 -0700808 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700809 (unsigned long long)sh->sector);
810
811 for (i = sh->disks; i--; ) {
812 struct r5dev *dev = &sh->dev[i];
813 if (test_bit(R5_Wantfill, &dev->flags)) {
814 struct bio *rbi;
815 spin_lock_irq(&conf->device_lock);
816 dev->read = rbi = dev->toread;
817 dev->toread = NULL;
818 spin_unlock_irq(&conf->device_lock);
819 while (rbi && rbi->bi_sector <
820 dev->sector + STRIPE_SECTORS) {
821 tx = async_copy_data(0, rbi, dev->page,
822 dev->sector, tx);
823 rbi = r5_next_bio(rbi, dev->sector);
824 }
825 }
826 }
827
828 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700829 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
830 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700831}
832
Dan Williams4e7d2c02009-08-29 19:13:11 -0700833static void mark_target_uptodate(struct stripe_head *sh, int target)
834{
835 struct r5dev *tgt;
836
837 if (target < 0)
838 return;
839
840 tgt = &sh->dev[target];
841 set_bit(R5_UPTODATE, &tgt->flags);
842 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
843 clear_bit(R5_Wantcompute, &tgt->flags);
844}
845
Dan Williamsac6b53b2009-07-14 13:40:19 -0700846static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700847{
848 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700849
Harvey Harrisone46b2722008-04-28 02:15:50 -0700850 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700851 (unsigned long long)sh->sector);
852
Dan Williamsac6b53b2009-07-14 13:40:19 -0700853 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700854 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700855 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700856
Dan Williamsecc65c92008-06-28 08:31:57 +1000857 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
858 if (sh->check_state == check_state_compute_run)
859 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700860 set_bit(STRIPE_HANDLE, &sh->state);
861 release_stripe(sh);
862}
863
Dan Williamsd6f38f32009-07-14 11:50:52 -0700864/* return a pointer to the address conversion region of the scribble buffer */
865static addr_conv_t *to_addr_conv(struct stripe_head *sh,
866 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700867{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700868 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
869}
870
871static struct dma_async_tx_descriptor *
872ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
873{
Dan Williams91c00922007-01-02 13:52:30 -0700874 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700875 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700876 int target = sh->ops.target;
877 struct r5dev *tgt = &sh->dev[target];
878 struct page *xor_dest = tgt->page;
879 int count = 0;
880 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700881 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700882 int i;
883
884 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700885 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700886 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
887
888 for (i = disks; i--; )
889 if (i != target)
890 xor_srcs[count++] = sh->dev[i].page;
891
892 atomic_inc(&sh->count);
893
Dan Williams0403e382009-09-08 17:42:50 -0700894 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700895 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700896 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700897 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700898 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700899 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700900
Dan Williams91c00922007-01-02 13:52:30 -0700901 return tx;
902}
903
Dan Williamsac6b53b2009-07-14 13:40:19 -0700904/* set_syndrome_sources - populate source buffers for gen_syndrome
905 * @srcs - (struct page *) array of size sh->disks
906 * @sh - stripe_head to parse
907 *
908 * Populates srcs in proper layout order for the stripe and returns the
909 * 'count' of sources to be used in a call to async_gen_syndrome. The P
910 * destination buffer is recorded in srcs[count] and the Q destination
911 * is recorded in srcs[count+1]].
912 */
913static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
914{
915 int disks = sh->disks;
916 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
917 int d0_idx = raid6_d0(sh);
918 int count;
919 int i;
920
921 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100922 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700923
924 count = 0;
925 i = d0_idx;
926 do {
927 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
928
929 srcs[slot] = sh->dev[i].page;
930 i = raid6_next_disk(i, disks);
931 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700932
NeilBrowne4424fe2009-10-16 16:27:34 +1100933 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700934}
935
936static struct dma_async_tx_descriptor *
937ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
938{
939 int disks = sh->disks;
940 struct page **blocks = percpu->scribble;
941 int target;
942 int qd_idx = sh->qd_idx;
943 struct dma_async_tx_descriptor *tx;
944 struct async_submit_ctl submit;
945 struct r5dev *tgt;
946 struct page *dest;
947 int i;
948 int count;
949
950 if (sh->ops.target < 0)
951 target = sh->ops.target2;
952 else if (sh->ops.target2 < 0)
953 target = sh->ops.target;
954 else
955 /* we should only have one valid target */
956 BUG();
957 BUG_ON(target < 0);
958 pr_debug("%s: stripe %llu block: %d\n",
959 __func__, (unsigned long long)sh->sector, target);
960
961 tgt = &sh->dev[target];
962 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
963 dest = tgt->page;
964
965 atomic_inc(&sh->count);
966
967 if (target == qd_idx) {
968 count = set_syndrome_sources(blocks, sh);
969 blocks[count] = NULL; /* regenerating p is not necessary */
970 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700971 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
972 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700973 to_addr_conv(sh, percpu));
974 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
975 } else {
976 /* Compute any data- or p-drive using XOR */
977 count = 0;
978 for (i = disks; i-- ; ) {
979 if (i == target || i == qd_idx)
980 continue;
981 blocks[count++] = sh->dev[i].page;
982 }
983
Dan Williams0403e382009-09-08 17:42:50 -0700984 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
985 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700986 to_addr_conv(sh, percpu));
987 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
988 }
989
990 return tx;
991}
992
993static struct dma_async_tx_descriptor *
994ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
995{
996 int i, count, disks = sh->disks;
997 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
998 int d0_idx = raid6_d0(sh);
999 int faila = -1, failb = -1;
1000 int target = sh->ops.target;
1001 int target2 = sh->ops.target2;
1002 struct r5dev *tgt = &sh->dev[target];
1003 struct r5dev *tgt2 = &sh->dev[target2];
1004 struct dma_async_tx_descriptor *tx;
1005 struct page **blocks = percpu->scribble;
1006 struct async_submit_ctl submit;
1007
1008 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1009 __func__, (unsigned long long)sh->sector, target, target2);
1010 BUG_ON(target < 0 || target2 < 0);
1011 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1012 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1013
Dan Williams6c910a72009-09-16 12:24:54 -07001014 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001015 * slot number conversion for 'faila' and 'failb'
1016 */
1017 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001018 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001019 count = 0;
1020 i = d0_idx;
1021 do {
1022 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1023
1024 blocks[slot] = sh->dev[i].page;
1025
1026 if (i == target)
1027 faila = slot;
1028 if (i == target2)
1029 failb = slot;
1030 i = raid6_next_disk(i, disks);
1031 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001032
1033 BUG_ON(faila == failb);
1034 if (failb < faila)
1035 swap(faila, failb);
1036 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1037 __func__, (unsigned long long)sh->sector, faila, failb);
1038
1039 atomic_inc(&sh->count);
1040
1041 if (failb == syndrome_disks+1) {
1042 /* Q disk is one of the missing disks */
1043 if (faila == syndrome_disks) {
1044 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001045 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1046 ops_complete_compute, sh,
1047 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +11001048 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001049 STRIPE_SIZE, &submit);
1050 } else {
1051 struct page *dest;
1052 int data_target;
1053 int qd_idx = sh->qd_idx;
1054
1055 /* Missing D+Q: recompute D from P, then recompute Q */
1056 if (target == qd_idx)
1057 data_target = target2;
1058 else
1059 data_target = target;
1060
1061 count = 0;
1062 for (i = disks; i-- ; ) {
1063 if (i == data_target || i == qd_idx)
1064 continue;
1065 blocks[count++] = sh->dev[i].page;
1066 }
1067 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001068 init_async_submit(&submit,
1069 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1070 NULL, NULL, NULL,
1071 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001072 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1073 &submit);
1074
1075 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001076 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1077 ops_complete_compute, sh,
1078 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001079 return async_gen_syndrome(blocks, 0, count+2,
1080 STRIPE_SIZE, &submit);
1081 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001082 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001083 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1084 ops_complete_compute, sh,
1085 to_addr_conv(sh, percpu));
1086 if (failb == syndrome_disks) {
1087 /* We're missing D+P. */
1088 return async_raid6_datap_recov(syndrome_disks+2,
1089 STRIPE_SIZE, faila,
1090 blocks, &submit);
1091 } else {
1092 /* We're missing D+D. */
1093 return async_raid6_2data_recov(syndrome_disks+2,
1094 STRIPE_SIZE, faila, failb,
1095 blocks, &submit);
1096 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001097 }
1098}
1099
1100
Dan Williams91c00922007-01-02 13:52:30 -07001101static void ops_complete_prexor(void *stripe_head_ref)
1102{
1103 struct stripe_head *sh = stripe_head_ref;
1104
Harvey Harrisone46b2722008-04-28 02:15:50 -07001105 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001106 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001107}
1108
1109static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001110ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1111 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001112{
Dan Williams91c00922007-01-02 13:52:30 -07001113 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001114 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001115 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001116 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001117
1118 /* existing parity data subtracted */
1119 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1120
Harvey Harrisone46b2722008-04-28 02:15:50 -07001121 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001122 (unsigned long long)sh->sector);
1123
1124 for (i = disks; i--; ) {
1125 struct r5dev *dev = &sh->dev[i];
1126 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001127 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001128 xor_srcs[count++] = dev->page;
1129 }
1130
Dan Williams0403e382009-09-08 17:42:50 -07001131 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001132 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001133 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001134
1135 return tx;
1136}
1137
1138static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001139ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001140{
1141 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001142 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001143
Harvey Harrisone46b2722008-04-28 02:15:50 -07001144 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001145 (unsigned long long)sh->sector);
1146
1147 for (i = disks; i--; ) {
1148 struct r5dev *dev = &sh->dev[i];
1149 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001150
Dan Williamsd8ee0722008-06-28 08:32:06 +10001151 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001152 struct bio *wbi;
1153
NeilBrowncbe47ec2011-07-26 11:20:35 +10001154 spin_lock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001155 chosen = dev->towrite;
1156 dev->towrite = NULL;
1157 BUG_ON(dev->written);
1158 wbi = dev->written = chosen;
NeilBrowncbe47ec2011-07-26 11:20:35 +10001159 spin_unlock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001160
1161 while (wbi && wbi->bi_sector <
1162 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001163 if (wbi->bi_rw & REQ_FUA)
1164 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001165 if (wbi->bi_rw & REQ_SYNC)
1166 set_bit(R5_SyncIO, &dev->flags);
Dan Williams91c00922007-01-02 13:52:30 -07001167 tx = async_copy_data(1, wbi, dev->page,
1168 dev->sector, tx);
1169 wbi = r5_next_bio(wbi, dev->sector);
1170 }
1171 }
1172 }
1173
1174 return tx;
1175}
1176
Dan Williamsac6b53b2009-07-14 13:40:19 -07001177static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001178{
1179 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001180 int disks = sh->disks;
1181 int pd_idx = sh->pd_idx;
1182 int qd_idx = sh->qd_idx;
1183 int i;
Shaohua Libc0934f2012-05-22 13:55:05 +10001184 bool fua = false, sync = false;
Dan Williams91c00922007-01-02 13:52:30 -07001185
Harvey Harrisone46b2722008-04-28 02:15:50 -07001186 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001187 (unsigned long long)sh->sector);
1188
Shaohua Libc0934f2012-05-22 13:55:05 +10001189 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001190 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001191 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
1192 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001193
Dan Williams91c00922007-01-02 13:52:30 -07001194 for (i = disks; i--; ) {
1195 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001196
Tejun Heoe9c74692010-09-03 11:56:18 +02001197 if (dev->written || i == pd_idx || i == qd_idx) {
Dan Williams91c00922007-01-02 13:52:30 -07001198 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001199 if (fua)
1200 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001201 if (sync)
1202 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001203 }
Dan Williams91c00922007-01-02 13:52:30 -07001204 }
1205
Dan Williamsd8ee0722008-06-28 08:32:06 +10001206 if (sh->reconstruct_state == reconstruct_state_drain_run)
1207 sh->reconstruct_state = reconstruct_state_drain_result;
1208 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1209 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1210 else {
1211 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1212 sh->reconstruct_state = reconstruct_state_result;
1213 }
Dan Williams91c00922007-01-02 13:52:30 -07001214
1215 set_bit(STRIPE_HANDLE, &sh->state);
1216 release_stripe(sh);
1217}
1218
1219static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001220ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1221 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001222{
Dan Williams91c00922007-01-02 13:52:30 -07001223 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001224 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001225 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001226 int count = 0, pd_idx = sh->pd_idx, i;
1227 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001228 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001229 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001230
Harvey Harrisone46b2722008-04-28 02:15:50 -07001231 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001232 (unsigned long long)sh->sector);
1233
1234 /* check if prexor is active which means only process blocks
1235 * that are part of a read-modify-write (written)
1236 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001237 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1238 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001239 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1240 for (i = disks; i--; ) {
1241 struct r5dev *dev = &sh->dev[i];
1242 if (dev->written)
1243 xor_srcs[count++] = dev->page;
1244 }
1245 } else {
1246 xor_dest = sh->dev[pd_idx].page;
1247 for (i = disks; i--; ) {
1248 struct r5dev *dev = &sh->dev[i];
1249 if (i != pd_idx)
1250 xor_srcs[count++] = dev->page;
1251 }
1252 }
1253
Dan Williams91c00922007-01-02 13:52:30 -07001254 /* 1/ if we prexor'd then the dest is reused as a source
1255 * 2/ if we did not prexor then we are redoing the parity
1256 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1257 * for the synchronous xor case
1258 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001259 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001260 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1261
1262 atomic_inc(&sh->count);
1263
Dan Williamsac6b53b2009-07-14 13:40:19 -07001264 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001265 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001266 if (unlikely(count == 1))
1267 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1268 else
1269 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001270}
1271
Dan Williamsac6b53b2009-07-14 13:40:19 -07001272static void
1273ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1274 struct dma_async_tx_descriptor *tx)
1275{
1276 struct async_submit_ctl submit;
1277 struct page **blocks = percpu->scribble;
1278 int count;
1279
1280 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1281
1282 count = set_syndrome_sources(blocks, sh);
1283
1284 atomic_inc(&sh->count);
1285
1286 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1287 sh, to_addr_conv(sh, percpu));
1288 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001289}
1290
1291static void ops_complete_check(void *stripe_head_ref)
1292{
1293 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001294
Harvey Harrisone46b2722008-04-28 02:15:50 -07001295 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001296 (unsigned long long)sh->sector);
1297
Dan Williamsecc65c92008-06-28 08:31:57 +10001298 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001299 set_bit(STRIPE_HANDLE, &sh->state);
1300 release_stripe(sh);
1301}
1302
Dan Williamsac6b53b2009-07-14 13:40:19 -07001303static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001304{
Dan Williams91c00922007-01-02 13:52:30 -07001305 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001306 int pd_idx = sh->pd_idx;
1307 int qd_idx = sh->qd_idx;
1308 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001309 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001310 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001311 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001312 int count;
1313 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001314
Harvey Harrisone46b2722008-04-28 02:15:50 -07001315 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001316 (unsigned long long)sh->sector);
1317
Dan Williamsac6b53b2009-07-14 13:40:19 -07001318 count = 0;
1319 xor_dest = sh->dev[pd_idx].page;
1320 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001321 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001322 if (i == pd_idx || i == qd_idx)
1323 continue;
1324 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001325 }
1326
Dan Williamsd6f38f32009-07-14 11:50:52 -07001327 init_async_submit(&submit, 0, NULL, NULL, NULL,
1328 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001329 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001330 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001331
Dan Williams91c00922007-01-02 13:52:30 -07001332 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001333 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1334 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001335}
1336
Dan Williamsac6b53b2009-07-14 13:40:19 -07001337static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1338{
1339 struct page **srcs = percpu->scribble;
1340 struct async_submit_ctl submit;
1341 int count;
1342
1343 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1344 (unsigned long long)sh->sector, checkp);
1345
1346 count = set_syndrome_sources(srcs, sh);
1347 if (!checkp)
1348 srcs[count] = NULL;
1349
1350 atomic_inc(&sh->count);
1351 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1352 sh, to_addr_conv(sh, percpu));
1353 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1354 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1355}
1356
Dan Williams417b8d42009-10-16 16:25:22 +11001357static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001358{
1359 int overlap_clear = 0, i, disks = sh->disks;
1360 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001361 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001362 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001363 struct raid5_percpu *percpu;
1364 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001365
Dan Williamsd6f38f32009-07-14 11:50:52 -07001366 cpu = get_cpu();
1367 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001368 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001369 ops_run_biofill(sh);
1370 overlap_clear++;
1371 }
1372
Dan Williams7b3a8712008-06-28 08:32:09 +10001373 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001374 if (level < 6)
1375 tx = ops_run_compute5(sh, percpu);
1376 else {
1377 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1378 tx = ops_run_compute6_1(sh, percpu);
1379 else
1380 tx = ops_run_compute6_2(sh, percpu);
1381 }
1382 /* terminate the chain if reconstruct is not set to be run */
1383 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001384 async_tx_ack(tx);
1385 }
Dan Williams91c00922007-01-02 13:52:30 -07001386
Dan Williams600aa102008-06-28 08:32:05 +10001387 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001388 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001389
Dan Williams600aa102008-06-28 08:32:05 +10001390 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001391 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001392 overlap_clear++;
1393 }
1394
Dan Williamsac6b53b2009-07-14 13:40:19 -07001395 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1396 if (level < 6)
1397 ops_run_reconstruct5(sh, percpu, tx);
1398 else
1399 ops_run_reconstruct6(sh, percpu, tx);
1400 }
Dan Williams91c00922007-01-02 13:52:30 -07001401
Dan Williamsac6b53b2009-07-14 13:40:19 -07001402 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1403 if (sh->check_state == check_state_run)
1404 ops_run_check_p(sh, percpu);
1405 else if (sh->check_state == check_state_run_q)
1406 ops_run_check_pq(sh, percpu, 0);
1407 else if (sh->check_state == check_state_run_pq)
1408 ops_run_check_pq(sh, percpu, 1);
1409 else
1410 BUG();
1411 }
Dan Williams91c00922007-01-02 13:52:30 -07001412
Dan Williams91c00922007-01-02 13:52:30 -07001413 if (overlap_clear)
1414 for (i = disks; i--; ) {
1415 struct r5dev *dev = &sh->dev[i];
1416 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1417 wake_up(&sh->raid_conf->wait_for_overlap);
1418 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001419 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001420}
1421
Dan Williams417b8d42009-10-16 16:25:22 +11001422#ifdef CONFIG_MULTICORE_RAID456
1423static void async_run_ops(void *param, async_cookie_t cookie)
1424{
1425 struct stripe_head *sh = param;
1426 unsigned long ops_request = sh->ops.request;
1427
1428 clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
1429 wake_up(&sh->ops.wait_for_ops);
1430
1431 __raid_run_ops(sh, ops_request);
1432 release_stripe(sh);
1433}
1434
1435static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1436{
1437 /* since handle_stripe can be called outside of raid5d context
1438 * we need to ensure sh->ops.request is de-staged before another
1439 * request arrives
1440 */
1441 wait_event(sh->ops.wait_for_ops,
1442 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
1443 sh->ops.request = ops_request;
1444
1445 atomic_inc(&sh->count);
1446 async_schedule(async_run_ops, sh);
1447}
1448#else
1449#define raid_run_ops __raid_run_ops
1450#endif
1451
NeilBrownd1688a62011-10-11 16:49:52 +11001452static int grow_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
1454 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001455 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001456 if (!sh)
1457 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001458
NeilBrown3f294f42005-11-08 21:39:25 -08001459 sh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001460 #ifdef CONFIG_MULTICORE_RAID456
1461 init_waitqueue_head(&sh->ops.wait_for_ops);
1462 #endif
NeilBrown3f294f42005-11-08 21:39:25 -08001463
NeilBrowne4e11e32010-06-16 16:45:16 +10001464 if (grow_buffers(sh)) {
1465 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001466 kmem_cache_free(conf->slab_cache, sh);
1467 return 0;
1468 }
1469 /* we just created an active stripe so... */
1470 atomic_set(&sh->count, 1);
1471 atomic_inc(&conf->active_stripes);
1472 INIT_LIST_HEAD(&sh->lru);
1473 release_stripe(sh);
1474 return 1;
1475}
1476
NeilBrownd1688a62011-10-11 16:49:52 +11001477static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001478{
Christoph Lametere18b8902006-12-06 20:33:20 -08001479 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001480 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
NeilBrownf4be6b42010-06-01 19:37:25 +10001482 if (conf->mddev->gendisk)
1483 sprintf(conf->cache_name[0],
1484 "raid%d-%s", conf->level, mdname(conf->mddev));
1485 else
1486 sprintf(conf->cache_name[0],
1487 "raid%d-%p", conf->level, conf->mddev);
1488 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1489
NeilBrownad01c9e2006-03-27 01:18:07 -08001490 conf->active_name = 0;
1491 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001493 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if (!sc)
1495 return 1;
1496 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001497 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001498 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001499 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 return 0;
1502}
NeilBrown29269552006-03-27 01:18:10 -08001503
Dan Williamsd6f38f32009-07-14 11:50:52 -07001504/**
1505 * scribble_len - return the required size of the scribble region
1506 * @num - total number of disks in the array
1507 *
1508 * The size must be enough to contain:
1509 * 1/ a struct page pointer for each device in the array +2
1510 * 2/ room to convert each entry in (1) to its corresponding dma
1511 * (dma_map_page()) or page (page_address()) address.
1512 *
1513 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1514 * calculate over all devices (not just the data blocks), using zeros in place
1515 * of the P and Q blocks.
1516 */
1517static size_t scribble_len(int num)
1518{
1519 size_t len;
1520
1521 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1522
1523 return len;
1524}
1525
NeilBrownd1688a62011-10-11 16:49:52 +11001526static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001527{
1528 /* Make all the stripes able to hold 'newsize' devices.
1529 * New slots in each stripe get 'page' set to a new page.
1530 *
1531 * This happens in stages:
1532 * 1/ create a new kmem_cache and allocate the required number of
1533 * stripe_heads.
1534 * 2/ gather all the old stripe_heads and tranfer the pages across
1535 * to the new stripe_heads. This will have the side effect of
1536 * freezing the array as once all stripe_heads have been collected,
1537 * no IO will be possible. Old stripe heads are freed once their
1538 * pages have been transferred over, and the old kmem_cache is
1539 * freed when all stripes are done.
1540 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1541 * we simple return a failre status - no need to clean anything up.
1542 * 4/ allocate new pages for the new slots in the new stripe_heads.
1543 * If this fails, we don't bother trying the shrink the
1544 * stripe_heads down again, we just leave them as they are.
1545 * As each stripe_head is processed the new one is released into
1546 * active service.
1547 *
1548 * Once step2 is started, we cannot afford to wait for a write,
1549 * so we use GFP_NOIO allocations.
1550 */
1551 struct stripe_head *osh, *nsh;
1552 LIST_HEAD(newstripes);
1553 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001554 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001555 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001556 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001557 int i;
1558
1559 if (newsize <= conf->pool_size)
1560 return 0; /* never bother to shrink */
1561
Dan Williamsb5470dc2008-06-27 21:44:04 -07001562 err = md_allow_write(conf->mddev);
1563 if (err)
1564 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001565
NeilBrownad01c9e2006-03-27 01:18:07 -08001566 /* Step 1 */
1567 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1568 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001569 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001570 if (!sc)
1571 return -ENOMEM;
1572
1573 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001574 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001575 if (!nsh)
1576 break;
1577
NeilBrownad01c9e2006-03-27 01:18:07 -08001578 nsh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001579 #ifdef CONFIG_MULTICORE_RAID456
1580 init_waitqueue_head(&nsh->ops.wait_for_ops);
1581 #endif
NeilBrownad01c9e2006-03-27 01:18:07 -08001582
1583 list_add(&nsh->lru, &newstripes);
1584 }
1585 if (i) {
1586 /* didn't get enough, give up */
1587 while (!list_empty(&newstripes)) {
1588 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1589 list_del(&nsh->lru);
1590 kmem_cache_free(sc, nsh);
1591 }
1592 kmem_cache_destroy(sc);
1593 return -ENOMEM;
1594 }
1595 /* Step 2 - Must use GFP_NOIO now.
1596 * OK, we have enough stripes, start collecting inactive
1597 * stripes and copying them over
1598 */
1599 list_for_each_entry(nsh, &newstripes, lru) {
1600 spin_lock_irq(&conf->device_lock);
1601 wait_event_lock_irq(conf->wait_for_stripe,
1602 !list_empty(&conf->inactive_list),
1603 conf->device_lock,
NeilBrown482c0832011-04-18 18:25:42 +10001604 );
NeilBrownad01c9e2006-03-27 01:18:07 -08001605 osh = get_free_stripe(conf);
1606 spin_unlock_irq(&conf->device_lock);
1607 atomic_set(&nsh->count, 1);
1608 for(i=0; i<conf->pool_size; i++)
1609 nsh->dev[i].page = osh->dev[i].page;
1610 for( ; i<newsize; i++)
1611 nsh->dev[i].page = NULL;
1612 kmem_cache_free(conf->slab_cache, osh);
1613 }
1614 kmem_cache_destroy(conf->slab_cache);
1615
1616 /* Step 3.
1617 * At this point, we are holding all the stripes so the array
1618 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001619 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001620 */
1621 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1622 if (ndisks) {
1623 for (i=0; i<conf->raid_disks; i++)
1624 ndisks[i] = conf->disks[i];
1625 kfree(conf->disks);
1626 conf->disks = ndisks;
1627 } else
1628 err = -ENOMEM;
1629
Dan Williamsd6f38f32009-07-14 11:50:52 -07001630 get_online_cpus();
1631 conf->scribble_len = scribble_len(newsize);
1632 for_each_present_cpu(cpu) {
1633 struct raid5_percpu *percpu;
1634 void *scribble;
1635
1636 percpu = per_cpu_ptr(conf->percpu, cpu);
1637 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1638
1639 if (scribble) {
1640 kfree(percpu->scribble);
1641 percpu->scribble = scribble;
1642 } else {
1643 err = -ENOMEM;
1644 break;
1645 }
1646 }
1647 put_online_cpus();
1648
NeilBrownad01c9e2006-03-27 01:18:07 -08001649 /* Step 4, return new stripes to service */
1650 while(!list_empty(&newstripes)) {
1651 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1652 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001653
NeilBrownad01c9e2006-03-27 01:18:07 -08001654 for (i=conf->raid_disks; i < newsize; i++)
1655 if (nsh->dev[i].page == NULL) {
1656 struct page *p = alloc_page(GFP_NOIO);
1657 nsh->dev[i].page = p;
1658 if (!p)
1659 err = -ENOMEM;
1660 }
1661 release_stripe(nsh);
1662 }
1663 /* critical section pass, GFP_NOIO no longer needed */
1664
1665 conf->slab_cache = sc;
1666 conf->active_name = 1-conf->active_name;
1667 conf->pool_size = newsize;
1668 return err;
1669}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
NeilBrownd1688a62011-10-11 16:49:52 +11001671static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672{
1673 struct stripe_head *sh;
1674
NeilBrown3f294f42005-11-08 21:39:25 -08001675 spin_lock_irq(&conf->device_lock);
1676 sh = get_free_stripe(conf);
1677 spin_unlock_irq(&conf->device_lock);
1678 if (!sh)
1679 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001680 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001681 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001682 kmem_cache_free(conf->slab_cache, sh);
1683 atomic_dec(&conf->active_stripes);
1684 return 1;
1685}
1686
NeilBrownd1688a62011-10-11 16:49:52 +11001687static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001688{
1689 while (drop_one_stripe(conf))
1690 ;
1691
NeilBrown29fc7e32006-02-03 03:03:41 -08001692 if (conf->slab_cache)
1693 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 conf->slab_cache = NULL;
1695}
1696
NeilBrown6712ecf2007-09-27 12:47:43 +02001697static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
NeilBrown99c0fb52009-03-31 14:39:38 +11001699 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001700 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001701 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001703 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11001704 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10001705 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
1707 for (i=0 ; i<disks; i++)
1708 if (bi == &sh->dev[i].req)
1709 break;
1710
Dan Williams45b42332007-07-09 11:56:43 -07001711 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1712 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 uptodate);
1714 if (i == disks) {
1715 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001716 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 }
NeilBrown14a75d32011-12-23 10:17:52 +11001718 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11001719 /* If replacement finished while this request was outstanding,
1720 * 'replacement' might be NULL already.
1721 * In that case it moved down to 'rdev'.
1722 * rdev is not removed until all requests are finished.
1723 */
NeilBrown14a75d32011-12-23 10:17:52 +11001724 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001725 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11001726 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
NeilBrown05616be2012-05-21 09:27:00 +10001728 if (use_new_offset(conf, sh))
1729 s = sh->sector + rdev->new_data_offset;
1730 else
1731 s = sh->sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001734 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11001735 /* Note that this cannot happen on a
1736 * replacement device. We just fail those on
1737 * any error
1738 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001739 printk_ratelimited(
1740 KERN_INFO
1741 "md/raid:%s: read error corrected"
1742 " (%lu sectors at %llu on %s)\n",
1743 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10001744 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001745 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001746 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001747 clear_bit(R5_ReadError, &sh->dev[i].flags);
1748 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1749 }
NeilBrown14a75d32011-12-23 10:17:52 +11001750 if (atomic_read(&rdev->read_errors))
1751 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11001753 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001754 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10001755 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001758 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11001759 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1760 printk_ratelimited(
1761 KERN_WARNING
1762 "md/raid:%s: read error on replacement device "
1763 "(sector %llu on %s).\n",
1764 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001765 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11001766 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001767 else if (conf->mddev->degraded >= conf->max_degraded) {
1768 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001769 printk_ratelimited(
1770 KERN_WARNING
1771 "md/raid:%s: read error not correctable "
1772 "(sector %llu on %s).\n",
1773 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001774 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001775 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001776 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08001777 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10001778 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001779 printk_ratelimited(
1780 KERN_WARNING
1781 "md/raid:%s: read error NOT corrected!! "
1782 "(sector %llu on %s).\n",
1783 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001784 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001785 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001786 } else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001787 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001788 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001789 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001790 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001791 else
1792 retry = 1;
1793 if (retry)
1794 set_bit(R5_ReadError, &sh->dev[i].flags);
1795 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001796 clear_bit(R5_ReadError, &sh->dev[i].flags);
1797 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10001798 if (!(set_bad
1799 && test_bit(In_sync, &rdev->flags)
1800 && rdev_set_badblocks(
1801 rdev, sh->sector, STRIPE_SECTORS, 0)))
1802 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 }
NeilBrown14a75d32011-12-23 10:17:52 +11001805 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1807 set_bit(STRIPE_HANDLE, &sh->state);
1808 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809}
1810
NeilBrownd710e132008-10-13 11:55:12 +11001811static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812{
NeilBrown99c0fb52009-03-31 14:39:38 +11001813 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001814 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001815 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11001816 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001818 sector_t first_bad;
1819 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11001820 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
NeilBrown977df362011-12-23 10:17:53 +11001822 for (i = 0 ; i < disks; i++) {
1823 if (bi == &sh->dev[i].req) {
1824 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 break;
NeilBrown977df362011-12-23 10:17:53 +11001826 }
1827 if (bi == &sh->dev[i].rreq) {
1828 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001829 if (rdev)
1830 replacement = 1;
1831 else
1832 /* rdev was removed and 'replacement'
1833 * replaced it. rdev is not removed
1834 * until all requests are finished.
1835 */
1836 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11001837 break;
1838 }
1839 }
Dan Williams45b42332007-07-09 11:56:43 -07001840 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1842 uptodate);
1843 if (i == disks) {
1844 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001845 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 }
1847
NeilBrown977df362011-12-23 10:17:53 +11001848 if (replacement) {
1849 if (!uptodate)
1850 md_error(conf->mddev, rdev);
1851 else if (is_badblock(rdev, sh->sector,
1852 STRIPE_SECTORS,
1853 &first_bad, &bad_sectors))
1854 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1855 } else {
1856 if (!uptodate) {
1857 set_bit(WriteErrorSeen, &rdev->flags);
1858 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11001859 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1860 set_bit(MD_RECOVERY_NEEDED,
1861 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11001862 } else if (is_badblock(rdev, sh->sector,
1863 STRIPE_SECTORS,
1864 &first_bad, &bad_sectors))
1865 set_bit(R5_MadeGood, &sh->dev[i].flags);
1866 }
1867 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
NeilBrown977df362011-12-23 10:17:53 +11001869 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1870 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001872 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873}
1874
NeilBrown784052e2009-03-31 15:19:07 +11001875static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
NeilBrown784052e2009-03-31 15:19:07 +11001877static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
1879 struct r5dev *dev = &sh->dev[i];
1880
1881 bio_init(&dev->req);
1882 dev->req.bi_io_vec = &dev->vec;
1883 dev->req.bi_vcnt++;
1884 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11001886 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
NeilBrown977df362011-12-23 10:17:53 +11001888 bio_init(&dev->rreq);
1889 dev->rreq.bi_io_vec = &dev->rvec;
1890 dev->rreq.bi_vcnt++;
1891 dev->rreq.bi_max_vecs++;
1892 dev->rreq.bi_private = sh;
1893 dev->rvec.bv_page = dev->page;
1894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001896 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897}
1898
NeilBrownfd01b882011-10-11 16:47:53 +11001899static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900{
1901 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11001902 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11001903 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10001904 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
NeilBrown908f4fb2011-12-23 10:17:50 +11001906 spin_lock_irqsave(&conf->device_lock, flags);
1907 clear_bit(In_sync, &rdev->flags);
1908 mddev->degraded = calc_degraded(conf);
1909 spin_unlock_irqrestore(&conf->device_lock, flags);
1910 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1911
NeilBrownde393cd2011-07-28 11:31:48 +10001912 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001913 set_bit(Faulty, &rdev->flags);
1914 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1915 printk(KERN_ALERT
1916 "md/raid:%s: Disk failure on %s, disabling device.\n"
1917 "md/raid:%s: Operation continuing on %d devices.\n",
1918 mdname(mddev),
1919 bdevname(rdev->bdev, b),
1920 mdname(mddev),
1921 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001922}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
1924/*
1925 * Input: a 'big' sector number,
1926 * Output: index of the data and parity disk, and the sector # in them.
1927 */
NeilBrownd1688a62011-10-11 16:49:52 +11001928static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001929 int previous, int *dd_idx,
1930 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001932 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001933 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001935 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001936 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001938 int algorithm = previous ? conf->prev_algo
1939 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001940 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1941 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001942 int raid_disks = previous ? conf->previous_raid_disks
1943 : conf->raid_disks;
1944 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946 /* First compute the information on this sector */
1947
1948 /*
1949 * Compute the chunk number and the sector offset inside the chunk
1950 */
1951 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1952 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
1954 /*
1955 * Compute the stripe number
1956 */
NeilBrown35f2a592010-04-20 14:13:34 +10001957 stripe = chunk_number;
1958 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10001959 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 /*
1961 * Select the parity disk based on the user selected algorithm.
1962 */
NeilBrown84789552011-07-27 11:00:36 +10001963 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07001964 switch(conf->level) {
1965 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001966 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001967 break;
1968 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001969 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001971 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001972 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 (*dd_idx)++;
1974 break;
1975 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001976 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001977 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 (*dd_idx)++;
1979 break;
1980 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001981 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001982 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 break;
1984 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001985 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001986 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001988 case ALGORITHM_PARITY_0:
1989 pd_idx = 0;
1990 (*dd_idx)++;
1991 break;
1992 case ALGORITHM_PARITY_N:
1993 pd_idx = data_disks;
1994 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11001996 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001997 }
1998 break;
1999 case 6:
2000
NeilBrowne183eae2009-03-31 15:20:22 +11002001 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002002 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002003 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002004 qd_idx = pd_idx + 1;
2005 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002006 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002007 qd_idx = 0;
2008 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002009 (*dd_idx) += 2; /* D D P Q D */
2010 break;
2011 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002012 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002013 qd_idx = pd_idx + 1;
2014 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002015 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002016 qd_idx = 0;
2017 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002018 (*dd_idx) += 2; /* D D P Q D */
2019 break;
2020 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002021 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002022 qd_idx = (pd_idx + 1) % raid_disks;
2023 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002024 break;
2025 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002026 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002027 qd_idx = (pd_idx + 1) % raid_disks;
2028 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002029 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002030
2031 case ALGORITHM_PARITY_0:
2032 pd_idx = 0;
2033 qd_idx = 1;
2034 (*dd_idx) += 2;
2035 break;
2036 case ALGORITHM_PARITY_N:
2037 pd_idx = data_disks;
2038 qd_idx = data_disks + 1;
2039 break;
2040
2041 case ALGORITHM_ROTATING_ZERO_RESTART:
2042 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2043 * of blocks for computing Q is different.
2044 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002045 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002046 qd_idx = pd_idx + 1;
2047 if (pd_idx == raid_disks-1) {
2048 (*dd_idx)++; /* Q D D D P */
2049 qd_idx = 0;
2050 } else if (*dd_idx >= pd_idx)
2051 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002052 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002053 break;
2054
2055 case ALGORITHM_ROTATING_N_RESTART:
2056 /* Same a left_asymmetric, by first stripe is
2057 * D D D P Q rather than
2058 * Q D D D P
2059 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002060 stripe2 += 1;
2061 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002062 qd_idx = pd_idx + 1;
2063 if (pd_idx == raid_disks-1) {
2064 (*dd_idx)++; /* Q D D D P */
2065 qd_idx = 0;
2066 } else if (*dd_idx >= pd_idx)
2067 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002068 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002069 break;
2070
2071 case ALGORITHM_ROTATING_N_CONTINUE:
2072 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002073 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002074 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2075 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002076 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002077 break;
2078
2079 case ALGORITHM_LEFT_ASYMMETRIC_6:
2080 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002081 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002082 if (*dd_idx >= pd_idx)
2083 (*dd_idx)++;
2084 qd_idx = raid_disks - 1;
2085 break;
2086
2087 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002088 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002089 if (*dd_idx >= pd_idx)
2090 (*dd_idx)++;
2091 qd_idx = raid_disks - 1;
2092 break;
2093
2094 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002095 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002096 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2097 qd_idx = raid_disks - 1;
2098 break;
2099
2100 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002101 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002102 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2103 qd_idx = raid_disks - 1;
2104 break;
2105
2106 case ALGORITHM_PARITY_0_6:
2107 pd_idx = 0;
2108 (*dd_idx)++;
2109 qd_idx = raid_disks - 1;
2110 break;
2111
NeilBrown16a53ec2006-06-26 00:27:38 -07002112 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002113 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002114 }
2115 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 }
2117
NeilBrown911d4ee2009-03-31 14:39:38 +11002118 if (sh) {
2119 sh->pd_idx = pd_idx;
2120 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002121 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 /*
2124 * Finally, compute the new sector number
2125 */
2126 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2127 return new_sector;
2128}
2129
2130
NeilBrown784052e2009-03-31 15:19:07 +11002131static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
NeilBrownd1688a62011-10-11 16:49:52 +11002133 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002134 int raid_disks = sh->disks;
2135 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002137 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2138 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002139 int algorithm = previous ? conf->prev_algo
2140 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 sector_t stripe;
2142 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002143 sector_t chunk_number;
2144 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002146 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
NeilBrown16a53ec2006-06-26 00:27:38 -07002148
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2150 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
NeilBrown16a53ec2006-06-26 00:27:38 -07002152 if (i == sh->pd_idx)
2153 return 0;
2154 switch(conf->level) {
2155 case 4: break;
2156 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002157 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 case ALGORITHM_LEFT_ASYMMETRIC:
2159 case ALGORITHM_RIGHT_ASYMMETRIC:
2160 if (i > sh->pd_idx)
2161 i--;
2162 break;
2163 case ALGORITHM_LEFT_SYMMETRIC:
2164 case ALGORITHM_RIGHT_SYMMETRIC:
2165 if (i < sh->pd_idx)
2166 i += raid_disks;
2167 i -= (sh->pd_idx + 1);
2168 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002169 case ALGORITHM_PARITY_0:
2170 i -= 1;
2171 break;
2172 case ALGORITHM_PARITY_N:
2173 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002175 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002176 }
2177 break;
2178 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002179 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002180 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002181 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002182 case ALGORITHM_LEFT_ASYMMETRIC:
2183 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002184 case ALGORITHM_ROTATING_ZERO_RESTART:
2185 case ALGORITHM_ROTATING_N_RESTART:
2186 if (sh->pd_idx == raid_disks-1)
2187 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002188 else if (i > sh->pd_idx)
2189 i -= 2; /* D D P Q D */
2190 break;
2191 case ALGORITHM_LEFT_SYMMETRIC:
2192 case ALGORITHM_RIGHT_SYMMETRIC:
2193 if (sh->pd_idx == raid_disks-1)
2194 i--; /* Q D D D P */
2195 else {
2196 /* D D P Q D */
2197 if (i < sh->pd_idx)
2198 i += raid_disks;
2199 i -= (sh->pd_idx + 2);
2200 }
2201 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002202 case ALGORITHM_PARITY_0:
2203 i -= 2;
2204 break;
2205 case ALGORITHM_PARITY_N:
2206 break;
2207 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002208 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002209 if (sh->pd_idx == 0)
2210 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002211 else {
2212 /* D D Q P D */
2213 if (i < sh->pd_idx)
2214 i += raid_disks;
2215 i -= (sh->pd_idx + 1);
2216 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002217 break;
2218 case ALGORITHM_LEFT_ASYMMETRIC_6:
2219 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2220 if (i > sh->pd_idx)
2221 i--;
2222 break;
2223 case ALGORITHM_LEFT_SYMMETRIC_6:
2224 case ALGORITHM_RIGHT_SYMMETRIC_6:
2225 if (i < sh->pd_idx)
2226 i += data_disks + 1;
2227 i -= (sh->pd_idx + 1);
2228 break;
2229 case ALGORITHM_PARITY_0_6:
2230 i -= 1;
2231 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002232 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002233 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002234 }
2235 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 }
2237
2238 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002239 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
NeilBrown112bf892009-03-31 14:39:38 +11002241 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002242 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002243 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2244 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002245 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2246 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 return 0;
2248 }
2249 return r_sector;
2250}
2251
2252
Dan Williams600aa102008-06-28 08:32:05 +10002253static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002254schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002255 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002256{
2257 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002258 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002259 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002260
Dan Williamse33129d2007-01-02 13:52:30 -07002261 if (rcw) {
2262 /* if we are not expanding this is a proper write request, and
2263 * there will be bios with new data to be drained into the
2264 * stripe cache
2265 */
2266 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10002267 sh->reconstruct_state = reconstruct_state_drain_run;
2268 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2269 } else
2270 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07002271
Dan Williamsac6b53b2009-07-14 13:40:19 -07002272 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002273
2274 for (i = disks; i--; ) {
2275 struct r5dev *dev = &sh->dev[i];
2276
2277 if (dev->towrite) {
2278 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002279 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002280 if (!expand)
2281 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002282 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002283 }
2284 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002285 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002286 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002287 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002288 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002289 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002290 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2291 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2292
Dan Williamsd8ee0722008-06-28 08:32:06 +10002293 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002294 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2295 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002296 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002297
2298 for (i = disks; i--; ) {
2299 struct r5dev *dev = &sh->dev[i];
2300 if (i == pd_idx)
2301 continue;
2302
Dan Williamse33129d2007-01-02 13:52:30 -07002303 if (dev->towrite &&
2304 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002305 test_bit(R5_Wantcompute, &dev->flags))) {
2306 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002307 set_bit(R5_LOCKED, &dev->flags);
2308 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002309 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002310 }
2311 }
2312 }
2313
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002314 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002315 * are in flight
2316 */
2317 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2318 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002319 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002320
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002321 if (level == 6) {
2322 int qd_idx = sh->qd_idx;
2323 struct r5dev *dev = &sh->dev[qd_idx];
2324
2325 set_bit(R5_LOCKED, &dev->flags);
2326 clear_bit(R5_UPTODATE, &dev->flags);
2327 s->locked++;
2328 }
2329
Dan Williams600aa102008-06-28 08:32:05 +10002330 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002331 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002332 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002333}
NeilBrown16a53ec2006-06-26 00:27:38 -07002334
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335/*
2336 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002337 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 * The bi_next chain must be in order.
2339 */
2340static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2341{
2342 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002343 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002344 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
NeilBrowncbe47ec2011-07-26 11:20:35 +10002346 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 (unsigned long long)bi->bi_sector,
2348 (unsigned long long)sh->sector);
2349
2350
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07002352 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -07002354 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
2355 firstwrite = 1;
2356 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 bip = &sh->dev[dd_idx].toread;
2358 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2359 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2360 goto overlap;
2361 bip = & (*bip)->bi_next;
2362 }
2363 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2364 goto overlap;
2365
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002366 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 if (*bip)
2368 bi->bi_next = *bip;
2369 *bip = bi;
Jens Axboe960e7392008-08-15 10:41:18 +02002370 bi->bi_phys_segments++;
NeilBrown72626682005-09-09 16:23:54 -07002371
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 if (forwrite) {
2373 /* check if page is covered */
2374 sector_t sector = sh->dev[dd_idx].sector;
2375 for (bi=sh->dev[dd_idx].towrite;
2376 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2377 bi && bi->bi_sector <= sector;
2378 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2379 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2380 sector = bi->bi_sector + (bi->bi_size>>9);
2381 }
2382 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2383 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2384 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002385 spin_unlock_irq(&conf->device_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002386
2387 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2388 (unsigned long long)(*bip)->bi_sector,
2389 (unsigned long long)sh->sector, dd_idx);
2390
2391 if (conf->mddev->bitmap && firstwrite) {
2392 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2393 STRIPE_SECTORS, 0);
2394 sh->bm_seq = conf->seq_flush+1;
2395 set_bit(STRIPE_BIT_DELAY, &sh->state);
2396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 return 1;
2398
2399 overlap:
2400 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
2401 spin_unlock_irq(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 return 0;
2403}
2404
NeilBrownd1688a62011-10-11 16:49:52 +11002405static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002406
NeilBrownd1688a62011-10-11 16:49:52 +11002407static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002408 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002409{
NeilBrown784052e2009-03-31 15:19:07 +11002410 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002411 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002412 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002413 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002414 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002415
NeilBrown112bf892009-03-31 14:39:38 +11002416 raid5_compute_sector(conf,
2417 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002418 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002419 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002420 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002421}
2422
Dan Williamsa4456852007-07-09 11:56:43 -07002423static void
NeilBrownd1688a62011-10-11 16:49:52 +11002424handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002425 struct stripe_head_state *s, int disks,
2426 struct bio **return_bi)
2427{
2428 int i;
2429 for (i = disks; i--; ) {
2430 struct bio *bi;
2431 int bitmap_end = 0;
2432
2433 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002434 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002435 rcu_read_lock();
2436 rdev = rcu_dereference(conf->disks[i].rdev);
2437 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002438 atomic_inc(&rdev->nr_pending);
2439 else
2440 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002441 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002442 if (rdev) {
2443 if (!rdev_set_badblocks(
2444 rdev,
2445 sh->sector,
2446 STRIPE_SECTORS, 0))
2447 md_error(conf->mddev, rdev);
2448 rdev_dec_pending(rdev, conf->mddev);
2449 }
Dan Williamsa4456852007-07-09 11:56:43 -07002450 }
2451 spin_lock_irq(&conf->device_lock);
2452 /* fail all writes first */
2453 bi = sh->dev[i].towrite;
2454 sh->dev[i].towrite = NULL;
2455 if (bi) {
2456 s->to_write--;
2457 bitmap_end = 1;
2458 }
2459
2460 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2461 wake_up(&conf->wait_for_overlap);
2462
2463 while (bi && bi->bi_sector <
2464 sh->dev[i].sector + STRIPE_SECTORS) {
2465 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2466 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002467 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002468 md_write_end(conf->mddev);
2469 bi->bi_next = *return_bi;
2470 *return_bi = bi;
2471 }
2472 bi = nextbi;
2473 }
2474 /* and fail all 'written' */
2475 bi = sh->dev[i].written;
2476 sh->dev[i].written = NULL;
2477 if (bi) bitmap_end = 1;
2478 while (bi && bi->bi_sector <
2479 sh->dev[i].sector + STRIPE_SECTORS) {
2480 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2481 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002482 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002483 md_write_end(conf->mddev);
2484 bi->bi_next = *return_bi;
2485 *return_bi = bi;
2486 }
2487 bi = bi2;
2488 }
2489
Dan Williamsb5e98d62007-01-02 13:52:31 -07002490 /* fail any reads if this device is non-operational and
2491 * the data has not reached the cache yet.
2492 */
2493 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2494 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2495 test_bit(R5_ReadError, &sh->dev[i].flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002496 bi = sh->dev[i].toread;
2497 sh->dev[i].toread = NULL;
2498 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2499 wake_up(&conf->wait_for_overlap);
2500 if (bi) s->to_read--;
2501 while (bi && bi->bi_sector <
2502 sh->dev[i].sector + STRIPE_SECTORS) {
2503 struct bio *nextbi =
2504 r5_next_bio(bi, sh->dev[i].sector);
2505 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002506 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002507 bi->bi_next = *return_bi;
2508 *return_bi = bi;
2509 }
2510 bi = nextbi;
2511 }
2512 }
2513 spin_unlock_irq(&conf->device_lock);
2514 if (bitmap_end)
2515 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2516 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002517 /* If we were in the middle of a write the parity block might
2518 * still be locked - so just clear all R5_LOCKED flags
2519 */
2520 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002521 }
2522
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002523 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2524 if (atomic_dec_and_test(&conf->pending_full_writes))
2525 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002526}
2527
NeilBrown7f0da592011-07-28 11:39:22 +10002528static void
NeilBrownd1688a62011-10-11 16:49:52 +11002529handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002530 struct stripe_head_state *s)
2531{
2532 int abort = 0;
2533 int i;
2534
NeilBrown7f0da592011-07-28 11:39:22 +10002535 clear_bit(STRIPE_SYNCING, &sh->state);
2536 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002537 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002538 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10002539 * Don't even need to abort as that is handled elsewhere
2540 * if needed, and not always wanted e.g. if there is a known
2541 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11002542 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002543 * non-sync devices, or abort the recovery
2544 */
NeilBrown18b98372012-04-01 23:48:38 +10002545 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2546 /* During recovery devices cannot be removed, so
2547 * locking and refcounting of rdevs is not needed
2548 */
2549 for (i = 0; i < conf->raid_disks; i++) {
2550 struct md_rdev *rdev = conf->disks[i].rdev;
2551 if (rdev
2552 && !test_bit(Faulty, &rdev->flags)
2553 && !test_bit(In_sync, &rdev->flags)
2554 && !rdev_set_badblocks(rdev, sh->sector,
2555 STRIPE_SECTORS, 0))
2556 abort = 1;
2557 rdev = conf->disks[i].replacement;
2558 if (rdev
2559 && !test_bit(Faulty, &rdev->flags)
2560 && !test_bit(In_sync, &rdev->flags)
2561 && !rdev_set_badblocks(rdev, sh->sector,
2562 STRIPE_SECTORS, 0))
2563 abort = 1;
2564 }
2565 if (abort)
2566 conf->recovery_disabled =
2567 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10002568 }
NeilBrown18b98372012-04-01 23:48:38 +10002569 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10002570}
2571
NeilBrown9a3e1102011-12-23 10:17:53 +11002572static int want_replace(struct stripe_head *sh, int disk_idx)
2573{
2574 struct md_rdev *rdev;
2575 int rv = 0;
2576 /* Doing recovery so rcu locking not required */
2577 rdev = sh->raid_conf->disks[disk_idx].replacement;
2578 if (rdev
2579 && !test_bit(Faulty, &rdev->flags)
2580 && !test_bit(In_sync, &rdev->flags)
2581 && (rdev->recovery_offset <= sh->sector
2582 || rdev->mddev->recovery_cp <= sh->sector))
2583 rv = 1;
2584
2585 return rv;
2586}
2587
NeilBrown93b3dbc2011-07-27 11:00:36 +10002588/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002589 * to be read or computed to satisfy a request.
2590 *
2591 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002592 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002593 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002594static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2595 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002596{
2597 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002598 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2599 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002600
Dan Williamsf38e1212007-01-02 13:52:30 -07002601 /* is the data in this block needed, and can we get it? */
2602 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002603 !test_bit(R5_UPTODATE, &dev->flags) &&
2604 (dev->toread ||
2605 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2606 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002607 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002608 (s->failed >= 1 && fdev[0]->toread) ||
2609 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002610 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2611 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2612 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002613 /* we would like to get this block, possibly by computing it,
2614 * otherwise read it if the backing disk is insync
2615 */
2616 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2617 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2618 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002619 (s->failed && (disk_idx == s->failed_num[0] ||
2620 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002621 /* have disk failed, and we're requested to fetch it;
2622 * do compute it
2623 */
2624 pr_debug("Computing stripe %llu block %d\n",
2625 (unsigned long long)sh->sector, disk_idx);
2626 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2627 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2628 set_bit(R5_Wantcompute, &dev->flags);
2629 sh->ops.target = disk_idx;
2630 sh->ops.target2 = -1; /* no 2nd target */
2631 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002632 /* Careful: from this point on 'uptodate' is in the eye
2633 * of raid_run_ops which services 'compute' operations
2634 * before writes. R5_Wantcompute flags a block that will
2635 * be R5_UPTODATE by the time it is needed for a
2636 * subsequent operation.
2637 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002638 s->uptodate++;
2639 return 1;
2640 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2641 /* Computing 2-failure is *very* expensive; only
2642 * do it if failed >= 2
2643 */
2644 int other;
2645 for (other = disks; other--; ) {
2646 if (other == disk_idx)
2647 continue;
2648 if (!test_bit(R5_UPTODATE,
2649 &sh->dev[other].flags))
2650 break;
2651 }
2652 BUG_ON(other < 0);
2653 pr_debug("Computing stripe %llu blocks %d,%d\n",
2654 (unsigned long long)sh->sector,
2655 disk_idx, other);
2656 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2657 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2658 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2659 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2660 sh->ops.target = disk_idx;
2661 sh->ops.target2 = other;
2662 s->uptodate += 2;
2663 s->req_compute = 1;
2664 return 1;
2665 } else if (test_bit(R5_Insync, &dev->flags)) {
2666 set_bit(R5_LOCKED, &dev->flags);
2667 set_bit(R5_Wantread, &dev->flags);
2668 s->locked++;
2669 pr_debug("Reading block %d (sync=%d)\n",
2670 disk_idx, s->syncing);
2671 }
2672 }
2673
2674 return 0;
2675}
2676
2677/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002678 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002679 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002680static void handle_stripe_fill(struct stripe_head *sh,
2681 struct stripe_head_state *s,
2682 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002683{
2684 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002685
2686 /* look for blocks to read/compute, skip this if a compute
2687 * is already in flight, or if the stripe contents are in the
2688 * midst of changing due to a write
2689 */
2690 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2691 !sh->reconstruct_state)
2692 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002693 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002694 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002695 set_bit(STRIPE_HANDLE, &sh->state);
2696}
2697
2698
Dan Williams1fe797e2008-06-28 09:16:30 +10002699/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002700 * any written block on an uptodate or failed drive can be returned.
2701 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2702 * never LOCKED, so we don't need to test 'failed' directly.
2703 */
NeilBrownd1688a62011-10-11 16:49:52 +11002704static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002705 struct stripe_head *sh, int disks, struct bio **return_bi)
2706{
2707 int i;
2708 struct r5dev *dev;
2709
2710 for (i = disks; i--; )
2711 if (sh->dev[i].written) {
2712 dev = &sh->dev[i];
2713 if (!test_bit(R5_LOCKED, &dev->flags) &&
2714 test_bit(R5_UPTODATE, &dev->flags)) {
2715 /* We can return any write requests */
2716 struct bio *wbi, *wbi2;
2717 int bitmap_end = 0;
Dan Williams45b42332007-07-09 11:56:43 -07002718 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002719 spin_lock_irq(&conf->device_lock);
2720 wbi = dev->written;
2721 dev->written = NULL;
2722 while (wbi && wbi->bi_sector <
2723 dev->sector + STRIPE_SECTORS) {
2724 wbi2 = r5_next_bio(wbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +02002725 if (!raid5_dec_bi_phys_segments(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002726 md_write_end(conf->mddev);
2727 wbi->bi_next = *return_bi;
2728 *return_bi = wbi;
2729 }
2730 wbi = wbi2;
2731 }
2732 if (dev->towrite == NULL)
2733 bitmap_end = 1;
2734 spin_unlock_irq(&conf->device_lock);
2735 if (bitmap_end)
2736 bitmap_endwrite(conf->mddev->bitmap,
2737 sh->sector,
2738 STRIPE_SECTORS,
2739 !test_bit(STRIPE_DEGRADED, &sh->state),
2740 0);
2741 }
2742 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002743
2744 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2745 if (atomic_dec_and_test(&conf->pending_full_writes))
2746 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002747}
2748
NeilBrownd1688a62011-10-11 16:49:52 +11002749static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002750 struct stripe_head *sh,
2751 struct stripe_head_state *s,
2752 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002753{
2754 int rmw = 0, rcw = 0, i;
NeilBrownc8ac1802011-07-27 11:00:36 +10002755 if (conf->max_degraded == 2) {
2756 /* RAID6 requires 'rcw' in current implementation
2757 * Calculate the real rcw later - for now fake it
2758 * look like rcw is cheaper
2759 */
2760 rcw = 1; rmw = 2;
2761 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002762 /* would I have to read this buffer for read_modify_write */
2763 struct r5dev *dev = &sh->dev[i];
2764 if ((dev->towrite || i == sh->pd_idx) &&
2765 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002766 !(test_bit(R5_UPTODATE, &dev->flags) ||
2767 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002768 if (test_bit(R5_Insync, &dev->flags))
2769 rmw++;
2770 else
2771 rmw += 2*disks; /* cannot read it */
2772 }
2773 /* Would I have to read this buffer for reconstruct_write */
2774 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2775 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002776 !(test_bit(R5_UPTODATE, &dev->flags) ||
2777 test_bit(R5_Wantcompute, &dev->flags))) {
2778 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002779 else
2780 rcw += 2*disks;
2781 }
2782 }
Dan Williams45b42332007-07-09 11:56:43 -07002783 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002784 (unsigned long long)sh->sector, rmw, rcw);
2785 set_bit(STRIPE_HANDLE, &sh->state);
2786 if (rmw < rcw && rmw > 0)
2787 /* prefer read-modify-write, but need to get some data */
2788 for (i = disks; i--; ) {
2789 struct r5dev *dev = &sh->dev[i];
2790 if ((dev->towrite || i == sh->pd_idx) &&
2791 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002792 !(test_bit(R5_UPTODATE, &dev->flags) ||
2793 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002794 test_bit(R5_Insync, &dev->flags)) {
2795 if (
2796 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002797 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002798 "%d for r-m-w\n", i);
2799 set_bit(R5_LOCKED, &dev->flags);
2800 set_bit(R5_Wantread, &dev->flags);
2801 s->locked++;
2802 } else {
2803 set_bit(STRIPE_DELAYED, &sh->state);
2804 set_bit(STRIPE_HANDLE, &sh->state);
2805 }
2806 }
2807 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002808 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002809 /* want reconstruct write, but need to get some data */
NeilBrownc8ac1802011-07-27 11:00:36 +10002810 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002811 for (i = disks; i--; ) {
2812 struct r5dev *dev = &sh->dev[i];
2813 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002814 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002815 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002816 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002817 test_bit(R5_Wantcompute, &dev->flags))) {
2818 rcw++;
2819 if (!test_bit(R5_Insync, &dev->flags))
2820 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002821 if (
2822 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002823 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002824 "%d for Reconstruct\n", i);
2825 set_bit(R5_LOCKED, &dev->flags);
2826 set_bit(R5_Wantread, &dev->flags);
2827 s->locked++;
2828 } else {
2829 set_bit(STRIPE_DELAYED, &sh->state);
2830 set_bit(STRIPE_HANDLE, &sh->state);
2831 }
2832 }
2833 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002834 }
Dan Williamsa4456852007-07-09 11:56:43 -07002835 /* now if nothing is locked, and if we have enough data,
2836 * we can start a write request
2837 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002838 /* since handle_stripe can be called at any time we need to handle the
2839 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002840 * subsequent call wants to start a write request. raid_run_ops only
2841 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002842 * simultaneously. If this is not the case then new writes need to be
2843 * held off until the compute completes.
2844 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002845 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2846 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2847 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002848 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002849}
2850
NeilBrownd1688a62011-10-11 16:49:52 +11002851static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002852 struct stripe_head_state *s, int disks)
2853{
Dan Williamsecc65c92008-06-28 08:31:57 +10002854 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002855
Dan Williamsbd2ab672008-04-10 21:29:27 -07002856 set_bit(STRIPE_HANDLE, &sh->state);
2857
Dan Williamsecc65c92008-06-28 08:31:57 +10002858 switch (sh->check_state) {
2859 case check_state_idle:
2860 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002861 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002862 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002863 sh->check_state = check_state_run;
2864 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002865 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002866 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002867 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002868 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002869 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002870 /* fall through */
2871 case check_state_compute_result:
2872 sh->check_state = check_state_idle;
2873 if (!dev)
2874 dev = &sh->dev[sh->pd_idx];
2875
2876 /* check that a write has not made the stripe insync */
2877 if (test_bit(STRIPE_INSYNC, &sh->state))
2878 break;
2879
2880 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002881 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2882 BUG_ON(s->uptodate != disks);
2883
2884 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002885 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002886 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002887
Dan Williamsa4456852007-07-09 11:56:43 -07002888 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002889 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002890 break;
2891 case check_state_run:
2892 break; /* we will be called again upon completion */
2893 case check_state_check_result:
2894 sh->check_state = check_state_idle;
2895
2896 /* if a failure occurred during the check operation, leave
2897 * STRIPE_INSYNC not set and let the stripe be handled again
2898 */
2899 if (s->failed)
2900 break;
2901
2902 /* handle a successful check operation, if parity is correct
2903 * we are done. Otherwise update the mismatch count and repair
2904 * parity if !MD_RECOVERY_CHECK
2905 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002906 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002907 /* parity is correct (on disc,
2908 * not in buffer any more)
2909 */
2910 set_bit(STRIPE_INSYNC, &sh->state);
2911 else {
2912 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2913 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2914 /* don't try to repair!! */
2915 set_bit(STRIPE_INSYNC, &sh->state);
2916 else {
2917 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002918 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002919 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2920 set_bit(R5_Wantcompute,
2921 &sh->dev[sh->pd_idx].flags);
2922 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002923 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10002924 s->uptodate++;
2925 }
2926 }
2927 break;
2928 case check_state_compute_run:
2929 break;
2930 default:
2931 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2932 __func__, sh->check_state,
2933 (unsigned long long) sh->sector);
2934 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002935 }
2936}
2937
2938
NeilBrownd1688a62011-10-11 16:49:52 +11002939static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07002940 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10002941 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002942{
Dan Williamsa4456852007-07-09 11:56:43 -07002943 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11002944 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07002945 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07002946
2947 set_bit(STRIPE_HANDLE, &sh->state);
2948
2949 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002950
Dan Williamsa4456852007-07-09 11:56:43 -07002951 /* Want to check and possibly repair P and Q.
2952 * However there could be one 'failed' device, in which
2953 * case we can only check one of them, possibly using the
2954 * other to generate missing data
2955 */
2956
Dan Williamsd82dfee2009-07-14 13:40:57 -07002957 switch (sh->check_state) {
2958 case check_state_idle:
2959 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10002960 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002961 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07002962 * makes sense to check P (If anything else were failed,
2963 * we would have used P to recreate it).
2964 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002965 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07002966 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002967 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002968 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07002969 * anything, so it makes sense to check it
2970 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002971 if (sh->check_state == check_state_run)
2972 sh->check_state = check_state_run_pq;
2973 else
2974 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07002975 }
Dan Williams36d1c642009-07-14 11:48:22 -07002976
Dan Williamsd82dfee2009-07-14 13:40:57 -07002977 /* discard potentially stale zero_sum_result */
2978 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07002979
Dan Williamsd82dfee2009-07-14 13:40:57 -07002980 if (sh->check_state == check_state_run) {
2981 /* async_xor_zero_sum destroys the contents of P */
2982 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
2983 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07002984 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002985 if (sh->check_state >= check_state_run &&
2986 sh->check_state <= check_state_run_pq) {
2987 /* async_syndrome_zero_sum preserves P and Q, so
2988 * no need to mark them !uptodate here
2989 */
2990 set_bit(STRIPE_OP_CHECK, &s->ops_request);
2991 break;
2992 }
Dan Williams36d1c642009-07-14 11:48:22 -07002993
Dan Williamsd82dfee2009-07-14 13:40:57 -07002994 /* we have 2-disk failure */
2995 BUG_ON(s->failed != 2);
2996 /* fall through */
2997 case check_state_compute_result:
2998 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07002999
Dan Williamsd82dfee2009-07-14 13:40:57 -07003000 /* check that a write has not made the stripe insync */
3001 if (test_bit(STRIPE_INSYNC, &sh->state))
3002 break;
Dan Williamsa4456852007-07-09 11:56:43 -07003003
3004 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07003005 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07003006 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003007 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07003008 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003009 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07003010 s->locked++;
3011 set_bit(R5_LOCKED, &dev->flags);
3012 set_bit(R5_Wantwrite, &dev->flags);
3013 }
3014 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003015 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07003016 s->locked++;
3017 set_bit(R5_LOCKED, &dev->flags);
3018 set_bit(R5_Wantwrite, &dev->flags);
3019 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003020 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003021 dev = &sh->dev[pd_idx];
3022 s->locked++;
3023 set_bit(R5_LOCKED, &dev->flags);
3024 set_bit(R5_Wantwrite, &dev->flags);
3025 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003026 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003027 dev = &sh->dev[qd_idx];
3028 s->locked++;
3029 set_bit(R5_LOCKED, &dev->flags);
3030 set_bit(R5_Wantwrite, &dev->flags);
3031 }
3032 clear_bit(STRIPE_DEGRADED, &sh->state);
3033
3034 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003035 break;
3036 case check_state_run:
3037 case check_state_run_q:
3038 case check_state_run_pq:
3039 break; /* we will be called again upon completion */
3040 case check_state_check_result:
3041 sh->check_state = check_state_idle;
3042
3043 /* handle a successful check operation, if parity is correct
3044 * we are done. Otherwise update the mismatch count and repair
3045 * parity if !MD_RECOVERY_CHECK
3046 */
3047 if (sh->ops.zero_sum_result == 0) {
3048 /* both parities are correct */
3049 if (!s->failed)
3050 set_bit(STRIPE_INSYNC, &sh->state);
3051 else {
3052 /* in contrast to the raid5 case we can validate
3053 * parity, but still have a failure to write
3054 * back
3055 */
3056 sh->check_state = check_state_compute_result;
3057 /* Returning at this point means that we may go
3058 * off and bring p and/or q uptodate again so
3059 * we make sure to check zero_sum_result again
3060 * to verify if p or q need writeback
3061 */
3062 }
3063 } else {
3064 conf->mddev->resync_mismatches += STRIPE_SECTORS;
3065 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3066 /* don't try to repair!! */
3067 set_bit(STRIPE_INSYNC, &sh->state);
3068 else {
3069 int *target = &sh->ops.target;
3070
3071 sh->ops.target = -1;
3072 sh->ops.target2 = -1;
3073 sh->check_state = check_state_compute_run;
3074 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3075 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3076 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3077 set_bit(R5_Wantcompute,
3078 &sh->dev[pd_idx].flags);
3079 *target = pd_idx;
3080 target = &sh->ops.target2;
3081 s->uptodate++;
3082 }
3083 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3084 set_bit(R5_Wantcompute,
3085 &sh->dev[qd_idx].flags);
3086 *target = qd_idx;
3087 s->uptodate++;
3088 }
3089 }
3090 }
3091 break;
3092 case check_state_compute_run:
3093 break;
3094 default:
3095 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3096 __func__, sh->check_state,
3097 (unsigned long long) sh->sector);
3098 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003099 }
3100}
3101
NeilBrownd1688a62011-10-11 16:49:52 +11003102static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003103{
3104 int i;
3105
3106 /* We have read all the blocks in this stripe and now we need to
3107 * copy some of them into a target stripe for expand.
3108 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003109 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003110 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3111 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003112 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003113 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003114 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003115 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003116
NeilBrown784052e2009-03-31 15:19:07 +11003117 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003118 sector_t s = raid5_compute_sector(conf, bn, 0,
3119 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003120 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003121 if (sh2 == NULL)
3122 /* so far only the early blocks of this stripe
3123 * have been requested. When later blocks
3124 * get requested, we will try again
3125 */
3126 continue;
3127 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3128 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3129 /* must have already done this block */
3130 release_stripe(sh2);
3131 continue;
3132 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003133
3134 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003135 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003136 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003137 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003138 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003139
Dan Williamsa4456852007-07-09 11:56:43 -07003140 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3141 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3142 for (j = 0; j < conf->raid_disks; j++)
3143 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003144 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003145 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3146 break;
3147 if (j == conf->raid_disks) {
3148 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3149 set_bit(STRIPE_HANDLE, &sh2->state);
3150 }
3151 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003152
Dan Williamsa4456852007-07-09 11:56:43 -07003153 }
NeilBrowna2e08552007-09-11 15:23:36 -07003154 /* done submitting copies, wait for them to complete */
3155 if (tx) {
3156 async_tx_ack(tx);
3157 dma_wait_for_async_tx(tx);
3158 }
Dan Williamsa4456852007-07-09 11:56:43 -07003159}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160
3161/*
3162 * handle_stripe - do things to a stripe.
3163 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003164 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3165 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003167 * return some read requests which now have data
3168 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 * schedule a read on some buffers
3170 * schedule a write of some buffers
3171 * return confirmation of parity correctness
3172 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 */
Dan Williamsa4456852007-07-09 11:56:43 -07003174
NeilBrownacfe7262011-07-27 11:00:36 +10003175static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003176{
NeilBrownd1688a62011-10-11 16:49:52 +11003177 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003178 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003179 struct r5dev *dev;
3180 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003181 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003182
NeilBrownacfe7262011-07-27 11:00:36 +10003183 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003184
NeilBrownacfe7262011-07-27 11:00:36 +10003185 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3186 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3187 s->failed_num[0] = -1;
3188 s->failed_num[1] = -1;
3189
3190 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003191 rcu_read_lock();
NeilBrownc4c16632011-07-26 11:34:20 +10003192 spin_lock_irq(&conf->device_lock);
NeilBrown16a53ec2006-06-26 00:27:38 -07003193 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003194 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003195 sector_t first_bad;
3196 int bad_sectors;
3197 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003198
NeilBrown16a53ec2006-06-26 00:27:38 -07003199 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003200
Dan Williams45b42332007-07-09 11:56:43 -07003201 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003202 i, dev->flags,
3203 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003204 /* maybe we can reply to a read
3205 *
3206 * new wantfill requests are only permitted while
3207 * ops_complete_biofill is guaranteed to be inactive
3208 */
3209 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3210 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3211 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003212
3213 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003214 if (test_bit(R5_LOCKED, &dev->flags))
3215 s->locked++;
3216 if (test_bit(R5_UPTODATE, &dev->flags))
3217 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003218 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003219 s->compute++;
3220 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003221 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003222
NeilBrownacfe7262011-07-27 11:00:36 +10003223 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003224 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003225 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003226 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003227 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003228 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003229 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003230 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003231 }
Dan Williamsa4456852007-07-09 11:56:43 -07003232 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003233 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003234 /* Prefer to use the replacement for reads, but only
3235 * if it is recovered enough and has no bad blocks.
3236 */
3237 rdev = rcu_dereference(conf->disks[i].replacement);
3238 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3239 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3240 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3241 &first_bad, &bad_sectors))
3242 set_bit(R5_ReadRepl, &dev->flags);
3243 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003244 if (rdev)
3245 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003246 rdev = rcu_dereference(conf->disks[i].rdev);
3247 clear_bit(R5_ReadRepl, &dev->flags);
3248 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003249 if (rdev && test_bit(Faulty, &rdev->flags))
3250 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003251 if (rdev) {
3252 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3253 &first_bad, &bad_sectors);
3254 if (s->blocked_rdev == NULL
3255 && (test_bit(Blocked, &rdev->flags)
3256 || is_bad < 0)) {
3257 if (is_bad < 0)
3258 set_bit(BlockedBadBlocks,
3259 &rdev->flags);
3260 s->blocked_rdev = rdev;
3261 atomic_inc(&rdev->nr_pending);
3262 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003263 }
NeilBrown415e72d2010-06-17 17:25:21 +10003264 clear_bit(R5_Insync, &dev->flags);
3265 if (!rdev)
3266 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003267 else if (is_bad) {
3268 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10003269 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3270 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003271 /* treat as in-sync, but with a read error
3272 * which we can now try to correct
3273 */
3274 set_bit(R5_Insync, &dev->flags);
3275 set_bit(R5_ReadError, &dev->flags);
3276 }
3277 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003278 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003279 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003280 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003281 set_bit(R5_Insync, &dev->flags);
3282 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3283 test_bit(R5_Expanded, &dev->flags))
3284 /* If we've reshaped into here, we assume it is Insync.
3285 * We will shortly update recovery_offset to make
3286 * it official.
3287 */
3288 set_bit(R5_Insync, &dev->flags);
3289
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003290 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003291 /* This flag does not apply to '.replacement'
3292 * only to .rdev, so make sure to check that*/
3293 struct md_rdev *rdev2 = rcu_dereference(
3294 conf->disks[i].rdev);
3295 if (rdev2 == rdev)
3296 clear_bit(R5_Insync, &dev->flags);
3297 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003298 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003299 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003300 } else
3301 clear_bit(R5_WriteError, &dev->flags);
3302 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003303 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003304 /* This flag does not apply to '.replacement'
3305 * only to .rdev, so make sure to check that*/
3306 struct md_rdev *rdev2 = rcu_dereference(
3307 conf->disks[i].rdev);
3308 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003309 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003310 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003311 } else
3312 clear_bit(R5_MadeGood, &dev->flags);
3313 }
NeilBrown977df362011-12-23 10:17:53 +11003314 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3315 struct md_rdev *rdev2 = rcu_dereference(
3316 conf->disks[i].replacement);
3317 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3318 s->handle_bad_blocks = 1;
3319 atomic_inc(&rdev2->nr_pending);
3320 } else
3321 clear_bit(R5_MadeGoodRepl, &dev->flags);
3322 }
NeilBrown415e72d2010-06-17 17:25:21 +10003323 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003324 /* The ReadError flag will just be confusing now */
3325 clear_bit(R5_ReadError, &dev->flags);
3326 clear_bit(R5_ReWrite, &dev->flags);
3327 }
NeilBrown415e72d2010-06-17 17:25:21 +10003328 if (test_bit(R5_ReadError, &dev->flags))
3329 clear_bit(R5_Insync, &dev->flags);
3330 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003331 if (s->failed < 2)
3332 s->failed_num[s->failed] = i;
3333 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003334 if (rdev && !test_bit(Faulty, &rdev->flags))
3335 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003336 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003337 }
NeilBrownc4c16632011-07-26 11:34:20 +10003338 spin_unlock_irq(&conf->device_lock);
NeilBrown9a3e1102011-12-23 10:17:53 +11003339 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3340 /* If there is a failed device being replaced,
3341 * we must be recovering.
3342 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10003343 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11003344 * else we can only be replacing
3345 * sync and recovery both need to read all devices, and so
3346 * use the same flag.
3347 */
3348 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10003349 sh->sector >= conf->mddev->recovery_cp ||
3350 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11003351 s->syncing = 1;
3352 else
3353 s->replacing = 1;
3354 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003355 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003356}
NeilBrownf4168852007-02-28 20:11:53 -08003357
NeilBrowncc940152011-07-26 11:35:35 +10003358static void handle_stripe(struct stripe_head *sh)
3359{
3360 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003361 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003362 int i;
NeilBrown84789552011-07-27 11:00:36 +10003363 int prexor;
3364 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003365 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003366
3367 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003368 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003369 /* already being handled, ensure it gets handled
3370 * again when current action finishes */
3371 set_bit(STRIPE_HANDLE, &sh->state);
3372 return;
3373 }
3374
3375 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3376 set_bit(STRIPE_SYNCING, &sh->state);
3377 clear_bit(STRIPE_INSYNC, &sh->state);
3378 }
3379 clear_bit(STRIPE_DELAYED, &sh->state);
3380
3381 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3382 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3383 (unsigned long long)sh->sector, sh->state,
3384 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3385 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003386
NeilBrownacfe7262011-07-27 11:00:36 +10003387 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003388
NeilBrownbc2607f2011-07-28 11:39:22 +10003389 if (s.handle_bad_blocks) {
3390 set_bit(STRIPE_HANDLE, &sh->state);
3391 goto finish;
3392 }
3393
NeilBrown474af965fe2011-07-27 11:00:36 +10003394 if (unlikely(s.blocked_rdev)) {
3395 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003396 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003397 set_bit(STRIPE_HANDLE, &sh->state);
3398 goto finish;
3399 }
3400 /* There is nothing for the blocked_rdev to block */
3401 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3402 s.blocked_rdev = NULL;
3403 }
3404
3405 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3406 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3407 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3408 }
3409
3410 pr_debug("locked=%d uptodate=%d to_read=%d"
3411 " to_write=%d failed=%d failed_num=%d,%d\n",
3412 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3413 s.failed_num[0], s.failed_num[1]);
3414 /* check if the array has lost more than max_degraded devices and,
3415 * if so, some requests might need to be failed.
3416 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003417 if (s.failed > conf->max_degraded) {
3418 sh->check_state = 0;
3419 sh->reconstruct_state = 0;
3420 if (s.to_read+s.to_write+s.written)
3421 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003422 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003423 handle_failed_sync(conf, sh, &s);
3424 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003425
3426 /*
3427 * might be able to return some write requests if the parity blocks
3428 * are safe, or on a failed drive
3429 */
3430 pdev = &sh->dev[sh->pd_idx];
3431 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3432 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3433 qdev = &sh->dev[sh->qd_idx];
3434 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3435 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3436 || conf->level < 6;
3437
3438 if (s.written &&
3439 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3440 && !test_bit(R5_LOCKED, &pdev->flags)
3441 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3442 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3443 && !test_bit(R5_LOCKED, &qdev->flags)
3444 && test_bit(R5_UPTODATE, &qdev->flags)))))
3445 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3446
3447 /* Now we might consider reading some blocks, either to check/generate
3448 * parity, or to satisfy requests
3449 * or to load a block that is being partially written.
3450 */
3451 if (s.to_read || s.non_overwrite
3452 || (conf->level == 6 && s.to_write && s.failed)
NeilBrown9a3e1102011-12-23 10:17:53 +11003453 || (s.syncing && (s.uptodate + s.compute < disks))
3454 || s.replacing
3455 || s.expanding)
NeilBrown474af965fe2011-07-27 11:00:36 +10003456 handle_stripe_fill(sh, &s, disks);
3457
NeilBrown84789552011-07-27 11:00:36 +10003458 /* Now we check to see if any write operations have recently
3459 * completed
3460 */
3461 prexor = 0;
3462 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3463 prexor = 1;
3464 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3465 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3466 sh->reconstruct_state = reconstruct_state_idle;
3467
3468 /* All the 'written' buffers and the parity block are ready to
3469 * be written back to disk
3470 */
3471 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3472 BUG_ON(sh->qd_idx >= 0 &&
3473 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags));
3474 for (i = disks; i--; ) {
3475 struct r5dev *dev = &sh->dev[i];
3476 if (test_bit(R5_LOCKED, &dev->flags) &&
3477 (i == sh->pd_idx || i == sh->qd_idx ||
3478 dev->written)) {
3479 pr_debug("Writing block %d\n", i);
3480 set_bit(R5_Wantwrite, &dev->flags);
3481 if (prexor)
3482 continue;
3483 if (!test_bit(R5_Insync, &dev->flags) ||
3484 ((i == sh->pd_idx || i == sh->qd_idx) &&
3485 s.failed == 0))
3486 set_bit(STRIPE_INSYNC, &sh->state);
3487 }
3488 }
3489 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3490 s.dec_preread_active = 1;
3491 }
3492
3493 /* Now to consider new write requests and what else, if anything
3494 * should be read. We do not handle new writes when:
3495 * 1/ A 'write' operation (copy+xor) is already in flight.
3496 * 2/ A 'check' operation is in flight, as it may clobber the parity
3497 * block.
3498 */
3499 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3500 handle_stripe_dirtying(conf, sh, &s, disks);
3501
3502 /* maybe we need to check and possibly fix the parity for this stripe
3503 * Any reads will already have been scheduled, so we just see if enough
3504 * data is available. The parity check is held off while parity
3505 * dependent operations are in flight.
3506 */
3507 if (sh->check_state ||
3508 (s.syncing && s.locked == 0 &&
3509 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3510 !test_bit(STRIPE_INSYNC, &sh->state))) {
3511 if (conf->level == 6)
3512 handle_parity_checks6(conf, sh, &s, disks);
3513 else
3514 handle_parity_checks5(conf, sh, &s, disks);
3515 }
NeilBrownc5a31002011-07-27 11:00:36 +10003516
NeilBrown9a3e1102011-12-23 10:17:53 +11003517 if (s.replacing && s.locked == 0
3518 && !test_bit(STRIPE_INSYNC, &sh->state)) {
3519 /* Write out to replacement devices where possible */
3520 for (i = 0; i < conf->raid_disks; i++)
3521 if (test_bit(R5_UPTODATE, &sh->dev[i].flags) &&
3522 test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3523 set_bit(R5_WantReplace, &sh->dev[i].flags);
3524 set_bit(R5_LOCKED, &sh->dev[i].flags);
3525 s.locked++;
3526 }
3527 set_bit(STRIPE_INSYNC, &sh->state);
3528 }
3529 if ((s.syncing || s.replacing) && s.locked == 0 &&
3530 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003531 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3532 clear_bit(STRIPE_SYNCING, &sh->state);
3533 }
3534
3535 /* If the failed drives are just a ReadError, then we might need
3536 * to progress the repair/check process
3537 */
3538 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3539 for (i = 0; i < s.failed; i++) {
3540 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3541 if (test_bit(R5_ReadError, &dev->flags)
3542 && !test_bit(R5_LOCKED, &dev->flags)
3543 && test_bit(R5_UPTODATE, &dev->flags)
3544 ) {
3545 if (!test_bit(R5_ReWrite, &dev->flags)) {
3546 set_bit(R5_Wantwrite, &dev->flags);
3547 set_bit(R5_ReWrite, &dev->flags);
3548 set_bit(R5_LOCKED, &dev->flags);
3549 s.locked++;
3550 } else {
3551 /* let's read it back */
3552 set_bit(R5_Wantread, &dev->flags);
3553 set_bit(R5_LOCKED, &dev->flags);
3554 s.locked++;
3555 }
3556 }
3557 }
3558
3559
NeilBrown3687c062011-07-27 11:00:36 +10003560 /* Finish reconstruct operations initiated by the expansion process */
3561 if (sh->reconstruct_state == reconstruct_state_result) {
3562 struct stripe_head *sh_src
3563 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3564 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3565 /* sh cannot be written until sh_src has been read.
3566 * so arrange for sh to be delayed a little
3567 */
3568 set_bit(STRIPE_DELAYED, &sh->state);
3569 set_bit(STRIPE_HANDLE, &sh->state);
3570 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3571 &sh_src->state))
3572 atomic_inc(&conf->preread_active_stripes);
3573 release_stripe(sh_src);
3574 goto finish;
3575 }
3576 if (sh_src)
3577 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003578
NeilBrown3687c062011-07-27 11:00:36 +10003579 sh->reconstruct_state = reconstruct_state_idle;
3580 clear_bit(STRIPE_EXPANDING, &sh->state);
3581 for (i = conf->raid_disks; i--; ) {
3582 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3583 set_bit(R5_LOCKED, &sh->dev[i].flags);
3584 s.locked++;
3585 }
3586 }
3587
3588 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3589 !sh->reconstruct_state) {
3590 /* Need to write out all blocks after computing parity */
3591 sh->disks = conf->raid_disks;
3592 stripe_set_idx(sh->sector, conf, 0, sh);
3593 schedule_reconstruction(sh, &s, 1, 1);
3594 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3595 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3596 atomic_dec(&conf->reshape_stripes);
3597 wake_up(&conf->wait_for_overlap);
3598 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3599 }
3600
3601 if (s.expanding && s.locked == 0 &&
3602 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3603 handle_stripe_expansion(conf, sh);
3604
3605finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003606 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10003607 if (unlikely(s.blocked_rdev)) {
3608 if (conf->mddev->external)
3609 md_wait_for_blocked_rdev(s.blocked_rdev,
3610 conf->mddev);
3611 else
3612 /* Internal metadata will immediately
3613 * be written by raid5d, so we don't
3614 * need to wait here.
3615 */
3616 rdev_dec_pending(s.blocked_rdev,
3617 conf->mddev);
3618 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003619
NeilBrownbc2607f2011-07-28 11:39:22 +10003620 if (s.handle_bad_blocks)
3621 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003622 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003623 struct r5dev *dev = &sh->dev[i];
3624 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3625 /* We own a safe reference to the rdev */
3626 rdev = conf->disks[i].rdev;
3627 if (!rdev_set_badblocks(rdev, sh->sector,
3628 STRIPE_SECTORS, 0))
3629 md_error(conf->mddev, rdev);
3630 rdev_dec_pending(rdev, conf->mddev);
3631 }
NeilBrownb84db562011-07-28 11:39:23 +10003632 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3633 rdev = conf->disks[i].rdev;
3634 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003635 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10003636 rdev_dec_pending(rdev, conf->mddev);
3637 }
NeilBrown977df362011-12-23 10:17:53 +11003638 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3639 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11003640 if (!rdev)
3641 /* rdev have been moved down */
3642 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11003643 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003644 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11003645 rdev_dec_pending(rdev, conf->mddev);
3646 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003647 }
3648
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003649 if (s.ops_request)
3650 raid_run_ops(sh, s.ops_request);
3651
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003652 ops_run_io(sh, &s);
3653
NeilBrownc5709ef2011-07-26 11:35:20 +10003654 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003655 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003656 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003657 * have actually been submitted.
3658 */
3659 atomic_dec(&conf->preread_active_stripes);
3660 if (atomic_read(&conf->preread_active_stripes) <
3661 IO_THRESHOLD)
3662 md_wakeup_thread(conf->mddev->thread);
3663 }
3664
NeilBrownc5709ef2011-07-26 11:35:20 +10003665 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003666
Dan Williams257a4b42011-11-08 16:22:06 +11003667 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003668}
3669
NeilBrownd1688a62011-10-11 16:49:52 +11003670static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671{
3672 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3673 while (!list_empty(&conf->delayed_list)) {
3674 struct list_head *l = conf->delayed_list.next;
3675 struct stripe_head *sh;
3676 sh = list_entry(l, struct stripe_head, lru);
3677 list_del_init(l);
3678 clear_bit(STRIPE_DELAYED, &sh->state);
3679 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3680 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003681 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 }
NeilBrown482c0832011-04-18 18:25:42 +10003683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684}
3685
NeilBrownd1688a62011-10-11 16:49:52 +11003686static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003687{
3688 /* device_lock is held */
3689 struct list_head head;
3690 list_add(&head, &conf->bitmap_list);
3691 list_del_init(&conf->bitmap_list);
3692 while (!list_empty(&head)) {
3693 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3694 list_del_init(&sh->lru);
3695 atomic_inc(&sh->count);
3696 __release_stripe(conf, sh);
3697 }
3698}
3699
NeilBrownfd01b882011-10-11 16:47:53 +11003700int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003701{
NeilBrownd1688a62011-10-11 16:49:52 +11003702 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003703
3704 /* No difference between reads and writes. Just check
3705 * how busy the stripe_cache is
3706 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003707
NeilBrownf022b2f2006-10-03 01:15:56 -07003708 if (conf->inactive_blocked)
3709 return 1;
3710 if (conf->quiesce)
3711 return 1;
3712 if (list_empty_careful(&conf->inactive_list))
3713 return 1;
3714
3715 return 0;
3716}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003717EXPORT_SYMBOL_GPL(md_raid5_congested);
3718
3719static int raid5_congested(void *data, int bits)
3720{
NeilBrownfd01b882011-10-11 16:47:53 +11003721 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003722
3723 return mddev_congested(mddev, bits) ||
3724 md_raid5_congested(mddev, bits);
3725}
NeilBrownf022b2f2006-10-03 01:15:56 -07003726
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003727/* We want read requests to align with chunks where possible,
3728 * but write requests don't need to.
3729 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003730static int raid5_mergeable_bvec(struct request_queue *q,
3731 struct bvec_merge_data *bvm,
3732 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003733{
NeilBrownfd01b882011-10-11 16:47:53 +11003734 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003735 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003736 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003737 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003738 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003739
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003740 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003741 return biovec->bv_len; /* always allow writes to be mergeable */
3742
Andre Noll664e7c42009-06-18 08:45:27 +10003743 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3744 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003745 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3746 if (max < 0) max = 0;
3747 if (max <= biovec->bv_len && bio_sectors == 0)
3748 return biovec->bv_len;
3749 else
3750 return max;
3751}
3752
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003753
NeilBrownfd01b882011-10-11 16:47:53 +11003754static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003755{
3756 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003757 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003758 unsigned int bio_sectors = bio->bi_size >> 9;
3759
Andre Noll664e7c42009-06-18 08:45:27 +10003760 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3761 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003762 return chunk_sectors >=
3763 ((sector & (chunk_sectors - 1)) + bio_sectors);
3764}
3765
3766/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003767 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3768 * later sampled by raid5d.
3769 */
NeilBrownd1688a62011-10-11 16:49:52 +11003770static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003771{
3772 unsigned long flags;
3773
3774 spin_lock_irqsave(&conf->device_lock, flags);
3775
3776 bi->bi_next = conf->retry_read_aligned_list;
3777 conf->retry_read_aligned_list = bi;
3778
3779 spin_unlock_irqrestore(&conf->device_lock, flags);
3780 md_wakeup_thread(conf->mddev->thread);
3781}
3782
3783
NeilBrownd1688a62011-10-11 16:49:52 +11003784static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003785{
3786 struct bio *bi;
3787
3788 bi = conf->retry_read_aligned;
3789 if (bi) {
3790 conf->retry_read_aligned = NULL;
3791 return bi;
3792 }
3793 bi = conf->retry_read_aligned_list;
3794 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003795 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003796 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003797 /*
3798 * this sets the active strip count to 1 and the processed
3799 * strip count to zero (upper 8 bits)
3800 */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003801 bi->bi_phys_segments = 1; /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003802 }
3803
3804 return bi;
3805}
3806
3807
3808/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003809 * The "raid5_align_endio" should check if the read succeeded and if it
3810 * did, call bio_endio on the original bio (having bio_put the new bio
3811 * first).
3812 * If the read failed..
3813 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003814static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003815{
3816 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003817 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003818 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003819 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003820 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003821
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003822 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003823
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003824 rdev = (void*)raid_bi->bi_next;
3825 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003826 mddev = rdev->mddev;
3827 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003828
3829 rdev_dec_pending(rdev, conf->mddev);
3830
3831 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003832 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003833 if (atomic_dec_and_test(&conf->active_aligned_reads))
3834 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003835 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003836 }
3837
3838
Dan Williams45b42332007-07-09 11:56:43 -07003839 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003840
3841 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003842}
3843
Neil Brown387bb172007-02-08 14:20:29 -08003844static int bio_fits_rdev(struct bio *bi)
3845{
Jens Axboe165125e2007-07-24 09:28:11 +02003846 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003847
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003848 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003849 return 0;
3850 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003851 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003852 return 0;
3853
3854 if (q->merge_bvec_fn)
3855 /* it's too hard to apply the merge_bvec_fn at this stage,
3856 * just just give up
3857 */
3858 return 0;
3859
3860 return 1;
3861}
3862
3863
NeilBrownfd01b882011-10-11 16:47:53 +11003864static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003865{
NeilBrownd1688a62011-10-11 16:49:52 +11003866 struct r5conf *conf = mddev->private;
NeilBrown8553fe72009-12-14 12:49:47 +11003867 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003868 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11003869 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11003870 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003871
3872 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003873 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003874 return 0;
3875 }
3876 /*
NeilBrowna167f662010-10-26 18:31:13 +11003877 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003878 */
NeilBrowna167f662010-10-26 18:31:13 +11003879 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003880 if (!align_bi)
3881 return 0;
3882 /*
3883 * set bi_end_io to a new function, and set bi_private to the
3884 * original bio.
3885 */
3886 align_bi->bi_end_io = raid5_align_endio;
3887 align_bi->bi_private = raid_bio;
3888 /*
3889 * compute position
3890 */
NeilBrown112bf892009-03-31 14:39:38 +11003891 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3892 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003893 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003894
NeilBrown671488c2011-12-23 10:17:52 +11003895 end_sector = align_bi->bi_sector + (align_bi->bi_size >> 9);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003896 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11003897 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
3898 if (!rdev || test_bit(Faulty, &rdev->flags) ||
3899 rdev->recovery_offset < end_sector) {
3900 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3901 if (rdev &&
3902 (test_bit(Faulty, &rdev->flags) ||
3903 !(test_bit(In_sync, &rdev->flags) ||
3904 rdev->recovery_offset >= end_sector)))
3905 rdev = NULL;
3906 }
3907 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10003908 sector_t first_bad;
3909 int bad_sectors;
3910
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003911 atomic_inc(&rdev->nr_pending);
3912 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003913 raid_bio->bi_next = (void*)rdev;
3914 align_bi->bi_bdev = rdev->bdev;
3915 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003916
NeilBrown31c176e2011-07-28 11:39:22 +10003917 if (!bio_fits_rdev(align_bi) ||
3918 is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
3919 &first_bad, &bad_sectors)) {
3920 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08003921 bio_put(align_bi);
3922 rdev_dec_pending(rdev, mddev);
3923 return 0;
3924 }
3925
majianpeng6c0544e2012-06-12 08:31:10 +08003926 /* No reshape active, so we can trust rdev->data_offset */
3927 align_bi->bi_sector += rdev->data_offset;
3928
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003929 spin_lock_irq(&conf->device_lock);
3930 wait_event_lock_irq(conf->wait_for_stripe,
3931 conf->quiesce == 0,
3932 conf->device_lock, /* nothing */);
3933 atomic_inc(&conf->active_aligned_reads);
3934 spin_unlock_irq(&conf->device_lock);
3935
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003936 generic_make_request(align_bi);
3937 return 1;
3938 } else {
3939 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003940 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003941 return 0;
3942 }
3943}
3944
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003945/* __get_priority_stripe - get the next stripe to process
3946 *
3947 * Full stripe writes are allowed to pass preread active stripes up until
3948 * the bypass_threshold is exceeded. In general the bypass_count
3949 * increments when the handle_list is handled before the hold_list; however, it
3950 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3951 * stripe with in flight i/o. The bypass_count will be reset when the
3952 * head of the hold_list has changed, i.e. the head was promoted to the
3953 * handle_list.
3954 */
NeilBrownd1688a62011-10-11 16:49:52 +11003955static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003956{
3957 struct stripe_head *sh;
3958
3959 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3960 __func__,
3961 list_empty(&conf->handle_list) ? "empty" : "busy",
3962 list_empty(&conf->hold_list) ? "empty" : "busy",
3963 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3964
3965 if (!list_empty(&conf->handle_list)) {
3966 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3967
3968 if (list_empty(&conf->hold_list))
3969 conf->bypass_count = 0;
3970 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3971 if (conf->hold_list.next == conf->last_hold)
3972 conf->bypass_count++;
3973 else {
3974 conf->last_hold = conf->hold_list.next;
3975 conf->bypass_count -= conf->bypass_threshold;
3976 if (conf->bypass_count < 0)
3977 conf->bypass_count = 0;
3978 }
3979 }
3980 } else if (!list_empty(&conf->hold_list) &&
3981 ((conf->bypass_threshold &&
3982 conf->bypass_count > conf->bypass_threshold) ||
3983 atomic_read(&conf->pending_full_writes) == 0)) {
3984 sh = list_entry(conf->hold_list.next,
3985 typeof(*sh), lru);
3986 conf->bypass_count -= conf->bypass_threshold;
3987 if (conf->bypass_count < 0)
3988 conf->bypass_count = 0;
3989 } else
3990 return NULL;
3991
3992 list_del_init(&sh->lru);
3993 atomic_inc(&sh->count);
3994 BUG_ON(atomic_read(&sh->count) != 1);
3995 return sh;
3996}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003997
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07003998static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999{
NeilBrownd1688a62011-10-11 16:49:52 +11004000 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11004001 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 sector_t new_sector;
4003 sector_t logical_sector, last_sector;
4004 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01004005 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11004006 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007
Tejun Heoe9c74692010-09-03 11:56:18 +02004008 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
4009 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004010 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07004011 }
4012
NeilBrown3d310eb2005-06-21 17:17:26 -07004013 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07004014
NeilBrown802ba062006-12-13 00:34:13 -08004015 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004016 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11004017 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004018 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004019
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4021 last_sector = bi->bi_sector + (bi->bi_size>>9);
4022 bi->bi_next = NULL;
4023 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07004024
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
4026 DEFINE_WAIT(w);
NeilBrownb5663ba2009-03-31 14:39:38 +11004027 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08004028
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004029 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11004030 previous = 0;
NeilBrownb578d552006-03-27 01:18:12 -08004031 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004032 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11004033 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08004034 * 64bit on a 32bit platform, and so it might be
4035 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02004036 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08004037 * the lock is dropped, so once we get a reference
4038 * to the stripe that we think it is, we will have
4039 * to check again.
4040 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004041 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004042 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004043 ? logical_sector < conf->reshape_progress
4044 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004045 previous = 1;
4046 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10004047 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004048 ? logical_sector < conf->reshape_safe
4049 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08004050 spin_unlock_irq(&conf->device_lock);
4051 schedule();
4052 goto retry;
4053 }
4054 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004055 spin_unlock_irq(&conf->device_lock);
4056 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004057
NeilBrown112bf892009-03-31 14:39:38 +11004058 new_sector = raid5_compute_sector(conf, logical_sector,
4059 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11004060 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10004061 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 (unsigned long long)new_sector,
4063 (unsigned long long)logical_sector);
4064
NeilBrownb5663ba2009-03-31 14:39:38 +11004065 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10004066 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004068 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004069 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08004070 * stripe, so we must do the range check again.
4071 * Expansion could still move past after this
4072 * test, but as we are holding a reference to
4073 * 'sh', we know that if that happens,
4074 * STRIPE_EXPANDING will get set and the expansion
4075 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004076 */
4077 int must_retry = 0;
4078 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004079 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11004080 ? logical_sector >= conf->reshape_progress
4081 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004082 /* mismatch, need to try again */
4083 must_retry = 1;
4084 spin_unlock_irq(&conf->device_lock);
4085 if (must_retry) {
4086 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004087 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004088 goto retry;
4089 }
4090 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004091
Namhyung Kimffd96e32011-07-18 17:38:51 +10004092 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004093 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004094 logical_sector < mddev->suspend_hi) {
4095 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004096 /* As the suspend_* range is controlled by
4097 * userspace, we want an interruptible
4098 * wait.
4099 */
4100 flush_signals(current);
4101 prepare_to_wait(&conf->wait_for_overlap,
4102 &w, TASK_INTERRUPTIBLE);
4103 if (logical_sector >= mddev->suspend_lo &&
4104 logical_sector < mddev->suspend_hi)
4105 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004106 goto retry;
4107 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004108
4109 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004110 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004111 /* Stripe is busy expanding or
4112 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 * and wait a while
4114 */
NeilBrown482c0832011-04-18 18:25:42 +10004115 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 release_stripe(sh);
4117 schedule();
4118 goto retry;
4119 }
4120 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004121 set_bit(STRIPE_HANDLE, &sh->state);
4122 clear_bit(STRIPE_DELAYED, &sh->state);
Tejun Heoe9c74692010-09-03 11:56:18 +02004123 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004124 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4125 atomic_inc(&conf->preread_active_stripes);
NeilBrownb357f042012-07-03 17:45:31 +10004126 mddev_check_plugged(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 } else {
4129 /* cannot get stripe for read-ahead, just give-up */
4130 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4131 finish_wait(&conf->wait_for_overlap, &w);
4132 break;
4133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004135
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004137 remaining = raid5_dec_bi_phys_segments(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004138 spin_unlock_irq(&conf->device_lock);
4139 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140
NeilBrown16a53ec2006-06-26 00:27:38 -07004141 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004143
Neil Brown0e13fe232008-06-28 08:31:20 +10004144 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146}
4147
NeilBrownfd01b882011-10-11 16:47:53 +11004148static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004149
NeilBrownfd01b882011-10-11 16:47:53 +11004150static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151{
NeilBrown52c03292006-06-26 00:27:43 -07004152 /* reshaping is quite different to recovery/resync so it is
4153 * handled quite separately ... here.
4154 *
4155 * On each call to sync_request, we gather one chunk worth of
4156 * destination stripes and flag them as expanding.
4157 * Then we find all the source stripes and request reads.
4158 * As the reads complete, handle_stripe will copy the data
4159 * into the destination stripe and release that stripe.
4160 */
NeilBrownd1688a62011-10-11 16:49:52 +11004161 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004163 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004164 int raid_disks = conf->previous_raid_disks;
4165 int data_disks = raid_disks - conf->max_degraded;
4166 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004167 int i;
4168 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004169 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004170 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004171 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004172 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004173
NeilBrownfef9c612009-03-31 15:16:46 +11004174 if (sector_nr == 0) {
4175 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10004176 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004177 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4178 sector_nr = raid5_size(mddev, 0, 0)
4179 - conf->reshape_progress;
NeilBrown2c810cd2012-05-21 09:27:00 +10004180 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004181 conf->reshape_progress > 0)
4182 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004183 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004184 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004185 mddev->curr_resync_completed = sector_nr;
4186 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004187 *skipped = 1;
4188 return sector_nr;
4189 }
NeilBrown52c03292006-06-26 00:27:43 -07004190 }
4191
NeilBrown7a661382009-03-31 15:21:40 +11004192 /* We need to process a full chunk at a time.
4193 * If old and new chunk sizes differ, we need to process the
4194 * largest of these
4195 */
Andre Noll664e7c42009-06-18 08:45:27 +10004196 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4197 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004198 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004199 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004200
NeilBrownb5254dd2012-05-21 09:27:01 +10004201 /* We update the metadata at least every 10 seconds, or when
4202 * the data about to be copied would over-write the source of
4203 * the data at the front of the range. i.e. one new_stripe
4204 * along from reshape_progress new_maps to after where
4205 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004206 */
NeilBrownfef9c612009-03-31 15:16:46 +11004207 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004208 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004209 readpos = conf->reshape_progress;
4210 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004211 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004212 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10004213 if (mddev->reshape_backwards) {
NeilBrowned37d832009-05-27 21:39:05 +10004214 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004215 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004216 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004217 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004218 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004219 readpos -= min_t(sector_t, reshape_sectors, readpos);
4220 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004221 }
NeilBrown52c03292006-06-26 00:27:43 -07004222
NeilBrownb5254dd2012-05-21 09:27:01 +10004223 /* Having calculated the 'writepos' possibly use it
4224 * to set 'stripe_addr' which is where we will write to.
4225 */
4226 if (mddev->reshape_backwards) {
4227 BUG_ON(conf->reshape_progress == 0);
4228 stripe_addr = writepos;
4229 BUG_ON((mddev->dev_sectors &
4230 ~((sector_t)reshape_sectors - 1))
4231 - reshape_sectors - stripe_addr
4232 != sector_nr);
4233 } else {
4234 BUG_ON(writepos != sector_nr + reshape_sectors);
4235 stripe_addr = sector_nr;
4236 }
4237
NeilBrownc8f517c2009-03-31 15:28:40 +11004238 /* 'writepos' is the most advanced device address we might write.
4239 * 'readpos' is the least advanced device address we might read.
4240 * 'safepos' is the least address recorded in the metadata as having
4241 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10004242 * If there is a min_offset_diff, these are adjusted either by
4243 * increasing the safepos/readpos if diff is negative, or
4244 * increasing writepos if diff is positive.
4245 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11004246 * ensure safety in the face of a crash - that must be done by userspace
4247 * making a backup of the data. So in that case there is no particular
4248 * rush to update metadata.
4249 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4250 * update the metadata to advance 'safepos' to match 'readpos' so that
4251 * we can be safe in the event of a crash.
4252 * So we insist on updating metadata if safepos is behind writepos and
4253 * readpos is beyond writepos.
4254 * In any case, update the metadata every 10 seconds.
4255 * Maybe that number should be configurable, but I'm not sure it is
4256 * worth it.... maybe it could be a multiple of safemode_delay???
4257 */
NeilBrownb5254dd2012-05-21 09:27:01 +10004258 if (conf->min_offset_diff < 0) {
4259 safepos += -conf->min_offset_diff;
4260 readpos += -conf->min_offset_diff;
4261 } else
4262 writepos += conf->min_offset_diff;
4263
NeilBrown2c810cd2012-05-21 09:27:00 +10004264 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11004265 ? (safepos > writepos && readpos < writepos)
4266 : (safepos < writepos && readpos > writepos)) ||
4267 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004268 /* Cannot proceed until we've updated the superblock... */
4269 wait_event(conf->wait_for_overlap,
4270 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004271 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004272 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004273 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07004274 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004275 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07004276 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004277 kthread_should_stop());
4278 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004279 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004280 spin_unlock_irq(&conf->device_lock);
4281 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004282 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004283 }
4284
NeilBrownab69ae12009-03-31 15:26:47 +11004285 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004286 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004287 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004288 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004289 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004290 set_bit(STRIPE_EXPANDING, &sh->state);
4291 atomic_inc(&conf->reshape_stripes);
4292 /* If any of this stripe is beyond the end of the old
4293 * array, then we need to zero those blocks
4294 */
4295 for (j=sh->disks; j--;) {
4296 sector_t s;
4297 if (j == sh->pd_idx)
4298 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004299 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004300 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004301 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004302 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004303 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004304 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004305 continue;
4306 }
4307 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4308 set_bit(R5_Expanded, &sh->dev[j].flags);
4309 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4310 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004311 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004312 set_bit(STRIPE_EXPAND_READY, &sh->state);
4313 set_bit(STRIPE_HANDLE, &sh->state);
4314 }
NeilBrownab69ae12009-03-31 15:26:47 +11004315 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004316 }
4317 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004318 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11004319 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004320 else
NeilBrown7a661382009-03-31 15:21:40 +11004321 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004322 spin_unlock_irq(&conf->device_lock);
4323 /* Ok, those stripe are ready. We can start scheduling
4324 * reads on the source stripes.
4325 * The source stripes are determined by mapping the first and last
4326 * block on the destination stripes.
4327 */
NeilBrown52c03292006-06-26 00:27:43 -07004328 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004329 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004330 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004331 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004332 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004333 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004334 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004335 if (last_sector >= mddev->dev_sectors)
4336 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004337 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004338 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004339 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4340 set_bit(STRIPE_HANDLE, &sh->state);
4341 release_stripe(sh);
4342 first_sector += STRIPE_SECTORS;
4343 }
NeilBrownab69ae12009-03-31 15:26:47 +11004344 /* Now that the sources are clearly marked, we can release
4345 * the destination stripes
4346 */
4347 while (!list_empty(&stripes)) {
4348 sh = list_entry(stripes.next, struct stripe_head, lru);
4349 list_del_init(&sh->lru);
4350 release_stripe(sh);
4351 }
NeilBrownc6207272008-02-06 01:39:52 -08004352 /* If this takes us to the resync_max point where we have to pause,
4353 * then we need to write out the superblock.
4354 */
NeilBrown7a661382009-03-31 15:21:40 +11004355 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004356 if ((sector_nr - mddev->curr_resync_completed) * 2
4357 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004358 /* Cannot proceed until we've updated the superblock... */
4359 wait_event(conf->wait_for_overlap,
4360 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004361 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004362 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004363 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004364 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4365 md_wakeup_thread(mddev->thread);
4366 wait_event(mddev->sb_wait,
4367 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4368 || kthread_should_stop());
4369 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004370 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004371 spin_unlock_irq(&conf->device_lock);
4372 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004373 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004374 }
NeilBrown7a661382009-03-31 15:21:40 +11004375 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004376}
4377
4378/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004379static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004380{
NeilBrownd1688a62011-10-11 16:49:52 +11004381 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004382 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004383 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004384 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004385 int still_degraded = 0;
4386 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387
NeilBrown72626682005-09-09 16:23:54 -07004388 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004390
NeilBrown29269552006-03-27 01:18:10 -08004391 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4392 end_reshape(conf);
4393 return 0;
4394 }
NeilBrown72626682005-09-09 16:23:54 -07004395
4396 if (mddev->curr_resync < max_sector) /* aborted */
4397 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4398 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004399 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004400 conf->fullsync = 0;
4401 bitmap_close_sync(mddev->bitmap);
4402
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 return 0;
4404 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004405
NeilBrown64bd6602009-08-03 10:59:58 +10004406 /* Allow raid5_quiesce to complete */
4407 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4408
NeilBrown52c03292006-06-26 00:27:43 -07004409 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4410 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004411
NeilBrownc6207272008-02-06 01:39:52 -08004412 /* No need to check resync_max as we never do more than one
4413 * stripe, and as resync_max will always be on a chunk boundary,
4414 * if the check in md_do_sync didn't fire, there is no chance
4415 * of overstepping resync_max here
4416 */
4417
NeilBrown16a53ec2006-06-26 00:27:38 -07004418 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 * to resync, then assert that we are finished, because there is
4420 * nothing we can do.
4421 */
NeilBrown3285edf2006-06-26 00:27:55 -07004422 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004423 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004424 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004425 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 return rv;
4427 }
NeilBrown72626682005-09-09 16:23:54 -07004428 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004429 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004430 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4431 /* we can skip this block, and probably more */
4432 sync_blocks /= STRIPE_SECTORS;
4433 *skipped = 1;
4434 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436
NeilBrownb47490c2008-02-06 01:39:50 -08004437 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4438
NeilBrowna8c906c2009-06-09 14:39:59 +10004439 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004441 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004443 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004445 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004447 /* Need to check if array will still be degraded after recovery/resync
4448 * We don't need to check the 'failed' flag as when that gets set,
4449 * recovery aborts.
4450 */
NeilBrownf001a702009-06-09 14:30:31 +10004451 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004452 if (conf->disks[i].rdev == NULL)
4453 still_degraded = 1;
4454
4455 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4456
NeilBrown83206d62011-07-26 11:19:49 +10004457 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458
NeilBrown14425772009-10-16 15:55:25 +11004459 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 release_stripe(sh);
4461
4462 return STRIPE_SECTORS;
4463}
4464
NeilBrownd1688a62011-10-11 16:49:52 +11004465static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004466{
4467 /* We may not be able to submit a whole bio at once as there
4468 * may not be enough stripe_heads available.
4469 * We cannot pre-allocate enough stripe_heads as we may need
4470 * more than exist in the cache (if we allow ever large chunks).
4471 * So we do one stripe head at a time and record in
4472 * ->bi_hw_segments how many have been done.
4473 *
4474 * We *know* that this entire raid_bio is in one chunk, so
4475 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4476 */
4477 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004478 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004479 sector_t sector, logical_sector, last_sector;
4480 int scnt = 0;
4481 int remaining;
4482 int handled = 0;
4483
4484 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004485 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004486 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004487 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4488
4489 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004490 logical_sector += STRIPE_SECTORS,
4491 sector += STRIPE_SECTORS,
4492 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004493
Jens Axboe960e7392008-08-15 10:41:18 +02004494 if (scnt < raid5_bi_hw_segments(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004495 /* already done this stripe */
4496 continue;
4497
NeilBrowna8c906c2009-06-09 14:39:59 +10004498 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004499
4500 if (!sh) {
4501 /* failed to get a stripe - must wait */
Jens Axboe960e7392008-08-15 10:41:18 +02004502 raid5_set_bi_hw_segments(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004503 conf->retry_read_aligned = raid_bio;
4504 return handled;
4505 }
4506
Neil Brown387bb172007-02-08 14:20:29 -08004507 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4508 release_stripe(sh);
Jens Axboe960e7392008-08-15 10:41:18 +02004509 raid5_set_bi_hw_segments(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004510 conf->retry_read_aligned = raid_bio;
4511 return handled;
4512 }
4513
Dan Williams36d1c642009-07-14 11:48:22 -07004514 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004515 release_stripe(sh);
4516 handled++;
4517 }
4518 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004519 remaining = raid5_dec_bi_phys_segments(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004520 spin_unlock_irq(&conf->device_lock);
Neil Brown0e13fe232008-06-28 08:31:20 +10004521 if (remaining == 0)
4522 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004523 if (atomic_dec_and_test(&conf->active_aligned_reads))
4524 wake_up(&conf->wait_for_stripe);
4525 return handled;
4526}
4527
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004528
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529/*
4530 * This is our raid5 kernel thread.
4531 *
4532 * We scan the hash table for stripes which can be handled now.
4533 * During the scan, completed stripes are saved for us by the interrupt
4534 * handler, so that they will not have to wait for our next wakeup.
4535 */
NeilBrownfd01b882011-10-11 16:47:53 +11004536static void raid5d(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537{
4538 struct stripe_head *sh;
NeilBrownd1688a62011-10-11 16:49:52 +11004539 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004541 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542
Dan Williams45b42332007-07-09 11:56:43 -07004543 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544
4545 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004547 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 handled = 0;
4549 spin_lock_irq(&conf->device_lock);
4550 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004551 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552
NeilBrown7c13edc2011-04-18 18:25:43 +10004553 if (atomic_read(&mddev->plug_cnt) == 0 &&
4554 !list_empty(&conf->bitmap_list)) {
4555 /* Now is a good time to flush some bitmap updates */
4556 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004557 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004558 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004559 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004560 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004561 activate_bit_delay(conf);
4562 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004563 if (atomic_read(&mddev->plug_cnt) == 0)
4564 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004565
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004566 while ((bio = remove_bio_from_retry(conf))) {
4567 int ok;
4568 spin_unlock_irq(&conf->device_lock);
4569 ok = retry_aligned_read(conf, bio);
4570 spin_lock_irq(&conf->device_lock);
4571 if (!ok)
4572 break;
4573 handled++;
4574 }
4575
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004576 sh = __get_priority_stripe(conf);
4577
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004578 if (!sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 spin_unlock_irq(&conf->device_lock);
4581
4582 handled++;
Dan Williams417b8d42009-10-16 16:25:22 +11004583 handle_stripe(sh);
4584 release_stripe(sh);
4585 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586
NeilBrownde393cd2011-07-28 11:31:48 +10004587 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
4588 md_check_recovery(mddev);
4589
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 spin_lock_irq(&conf->device_lock);
4591 }
Dan Williams45b42332007-07-09 11:56:43 -07004592 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593
4594 spin_unlock_irq(&conf->device_lock);
4595
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004596 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004597 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598
Dan Williams45b42332007-07-09 11:56:43 -07004599 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600}
4601
NeilBrown3f294f42005-11-08 21:39:25 -08004602static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004603raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004604{
NeilBrownd1688a62011-10-11 16:49:52 +11004605 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004606 if (conf)
4607 return sprintf(page, "%d\n", conf->max_nr_stripes);
4608 else
4609 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004610}
4611
NeilBrownc41d4ac2010-06-01 19:37:24 +10004612int
NeilBrownfd01b882011-10-11 16:47:53 +11004613raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004614{
NeilBrownd1688a62011-10-11 16:49:52 +11004615 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004616 int err;
4617
4618 if (size <= 16 || size > 32768)
4619 return -EINVAL;
4620 while (size < conf->max_nr_stripes) {
4621 if (drop_one_stripe(conf))
4622 conf->max_nr_stripes--;
4623 else
4624 break;
4625 }
4626 err = md_allow_write(mddev);
4627 if (err)
4628 return err;
4629 while (size > conf->max_nr_stripes) {
4630 if (grow_one_stripe(conf))
4631 conf->max_nr_stripes++;
4632 else break;
4633 }
4634 return 0;
4635}
4636EXPORT_SYMBOL(raid5_set_cache_size);
4637
NeilBrown3f294f42005-11-08 21:39:25 -08004638static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004639raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004640{
NeilBrownd1688a62011-10-11 16:49:52 +11004641 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004642 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004643 int err;
4644
NeilBrown3f294f42005-11-08 21:39:25 -08004645 if (len >= PAGE_SIZE)
4646 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004647 if (!conf)
4648 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004649
Dan Williams4ef197d82008-04-28 02:15:54 -07004650 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004651 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004652 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004653 if (err)
4654 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004655 return len;
4656}
NeilBrown007583c2005-11-08 21:39:30 -08004657
NeilBrown96de1e62005-11-08 21:39:39 -08004658static struct md_sysfs_entry
4659raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4660 raid5_show_stripe_cache_size,
4661 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004662
4663static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004664raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004665{
NeilBrownd1688a62011-10-11 16:49:52 +11004666 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004667 if (conf)
4668 return sprintf(page, "%d\n", conf->bypass_threshold);
4669 else
4670 return 0;
4671}
4672
4673static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004674raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004675{
NeilBrownd1688a62011-10-11 16:49:52 +11004676 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004677 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004678 if (len >= PAGE_SIZE)
4679 return -EINVAL;
4680 if (!conf)
4681 return -ENODEV;
4682
Dan Williams4ef197d82008-04-28 02:15:54 -07004683 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004684 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004685 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004686 return -EINVAL;
4687 conf->bypass_threshold = new;
4688 return len;
4689}
4690
4691static struct md_sysfs_entry
4692raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4693 S_IRUGO | S_IWUSR,
4694 raid5_show_preread_threshold,
4695 raid5_store_preread_threshold);
4696
4697static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004698stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004699{
NeilBrownd1688a62011-10-11 16:49:52 +11004700 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004701 if (conf)
4702 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4703 else
4704 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004705}
4706
NeilBrown96de1e62005-11-08 21:39:39 -08004707static struct md_sysfs_entry
4708raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004709
NeilBrown007583c2005-11-08 21:39:30 -08004710static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004711 &raid5_stripecache_size.attr,
4712 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004713 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004714 NULL,
4715};
NeilBrown007583c2005-11-08 21:39:30 -08004716static struct attribute_group raid5_attrs_group = {
4717 .name = NULL,
4718 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004719};
4720
Dan Williams80c3a6c2009-03-17 18:10:40 -07004721static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11004722raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07004723{
NeilBrownd1688a62011-10-11 16:49:52 +11004724 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004725
4726 if (!sectors)
4727 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004728 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11004729 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11004730 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004731
Andre Noll9d8f0362009-06-18 08:45:01 +10004732 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004733 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004734 return sectors * (raid_disks - conf->max_degraded);
4735}
4736
NeilBrownd1688a62011-10-11 16:49:52 +11004737static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004738{
4739 struct raid5_percpu *percpu;
4740 unsigned long cpu;
4741
4742 if (!conf->percpu)
4743 return;
4744
4745 get_online_cpus();
4746 for_each_possible_cpu(cpu) {
4747 percpu = per_cpu_ptr(conf->percpu, cpu);
4748 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004749 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004750 }
4751#ifdef CONFIG_HOTPLUG_CPU
4752 unregister_cpu_notifier(&conf->cpu_notify);
4753#endif
4754 put_online_cpus();
4755
4756 free_percpu(conf->percpu);
4757}
4758
NeilBrownd1688a62011-10-11 16:49:52 +11004759static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10004760{
4761 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004762 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10004763 kfree(conf->disks);
4764 kfree(conf->stripe_hashtbl);
4765 kfree(conf);
4766}
4767
Dan Williams36d1c642009-07-14 11:48:22 -07004768#ifdef CONFIG_HOTPLUG_CPU
4769static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4770 void *hcpu)
4771{
NeilBrownd1688a62011-10-11 16:49:52 +11004772 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07004773 long cpu = (long)hcpu;
4774 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4775
4776 switch (action) {
4777 case CPU_UP_PREPARE:
4778 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07004779 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07004780 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004781 if (!percpu->scribble)
4782 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4783
4784 if (!percpu->scribble ||
4785 (conf->level == 6 && !percpu->spare_page)) {
4786 safe_put_page(percpu->spare_page);
4787 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004788 pr_err("%s: failed memory allocation for cpu%ld\n",
4789 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07004790 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07004791 }
4792 break;
4793 case CPU_DEAD:
4794 case CPU_DEAD_FROZEN:
4795 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004796 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004797 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004798 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07004799 break;
4800 default:
4801 break;
4802 }
4803 return NOTIFY_OK;
4804}
4805#endif
4806
NeilBrownd1688a62011-10-11 16:49:52 +11004807static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004808{
4809 unsigned long cpu;
4810 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09004811 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004812 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004813 int err;
4814
Dan Williams36d1c642009-07-14 11:48:22 -07004815 allcpus = alloc_percpu(struct raid5_percpu);
4816 if (!allcpus)
4817 return -ENOMEM;
4818 conf->percpu = allcpus;
4819
4820 get_online_cpus();
4821 err = 0;
4822 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07004823 if (conf->level == 6) {
4824 spare_page = alloc_page(GFP_KERNEL);
4825 if (!spare_page) {
4826 err = -ENOMEM;
4827 break;
4828 }
4829 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
4830 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11004831 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004832 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07004833 err = -ENOMEM;
4834 break;
4835 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07004836 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004837 }
4838#ifdef CONFIG_HOTPLUG_CPU
4839 conf->cpu_notify.notifier_call = raid456_cpu_notify;
4840 conf->cpu_notify.priority = 0;
4841 if (err == 0)
4842 err = register_cpu_notifier(&conf->cpu_notify);
4843#endif
4844 put_online_cpus();
4845
4846 return err;
4847}
4848
NeilBrownd1688a62011-10-11 16:49:52 +11004849static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850{
NeilBrownd1688a62011-10-11 16:49:52 +11004851 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004852 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11004853 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10004855 char pers_name[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856
NeilBrown91adb562009-03-31 14:39:39 +11004857 if (mddev->new_level != 5
4858 && mddev->new_level != 4
4859 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10004860 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004861 mdname(mddev), mddev->new_level);
4862 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 }
NeilBrown91adb562009-03-31 14:39:39 +11004864 if ((mddev->new_level == 5
4865 && !algorithm_valid_raid5(mddev->new_layout)) ||
4866 (mddev->new_level == 6
4867 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004868 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11004869 mdname(mddev), mddev->new_layout);
4870 return ERR_PTR(-EIO);
4871 }
4872 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10004873 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004874 mdname(mddev), mddev->raid_disks);
4875 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11004876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877
Andre Noll664e7c42009-06-18 08:45:27 +10004878 if (!mddev->new_chunk_sectors ||
4879 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
4880 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10004881 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
4882 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11004883 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11004884 }
4885
NeilBrownd1688a62011-10-11 16:49:52 +11004886 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11004887 if (conf == NULL)
4888 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11004889 spin_lock_init(&conf->device_lock);
4890 init_waitqueue_head(&conf->wait_for_stripe);
4891 init_waitqueue_head(&conf->wait_for_overlap);
4892 INIT_LIST_HEAD(&conf->handle_list);
4893 INIT_LIST_HEAD(&conf->hold_list);
4894 INIT_LIST_HEAD(&conf->delayed_list);
4895 INIT_LIST_HEAD(&conf->bitmap_list);
4896 INIT_LIST_HEAD(&conf->inactive_list);
4897 atomic_set(&conf->active_stripes, 0);
4898 atomic_set(&conf->preread_active_stripes, 0);
4899 atomic_set(&conf->active_aligned_reads, 0);
4900 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11004901 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11004902
4903 conf->raid_disks = mddev->raid_disks;
4904 if (mddev->reshape_position == MaxSector)
4905 conf->previous_raid_disks = mddev->raid_disks;
4906 else
4907 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004908 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
4909 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11004910
NeilBrown5e5e3e72009-10-16 16:35:30 +11004911 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11004912 GFP_KERNEL);
4913 if (!conf->disks)
4914 goto abort;
4915
4916 conf->mddev = mddev;
4917
4918 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
4919 goto abort;
4920
Dan Williams36d1c642009-07-14 11:48:22 -07004921 conf->level = mddev->new_level;
4922 if (raid5_alloc_percpu(conf) != 0)
4923 goto abort;
4924
NeilBrown0c55e022010-05-03 14:09:02 +10004925 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11004926
NeilBrowndafb20f2012-03-19 12:46:39 +11004927 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11004928 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004929 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11004930 || raid_disk < 0)
4931 continue;
4932 disk = conf->disks + raid_disk;
4933
NeilBrown17045f52011-12-23 10:17:53 +11004934 if (test_bit(Replacement, &rdev->flags)) {
4935 if (disk->replacement)
4936 goto abort;
4937 disk->replacement = rdev;
4938 } else {
4939 if (disk->rdev)
4940 goto abort;
4941 disk->rdev = rdev;
4942 }
NeilBrown91adb562009-03-31 14:39:39 +11004943
4944 if (test_bit(In_sync, &rdev->flags)) {
4945 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10004946 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
4947 " disk %d\n",
4948 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05004949 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11004950 /* Cannot rely on bitmap to complete recovery */
4951 conf->fullsync = 1;
4952 }
4953
Andre Noll09c9e5f2009-06-18 08:45:55 +10004954 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11004955 conf->level = mddev->new_level;
4956 if (conf->level == 6)
4957 conf->max_degraded = 2;
4958 else
4959 conf->max_degraded = 1;
4960 conf->algorithm = mddev->new_layout;
4961 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11004962 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11004963 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10004964 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11004965 conf->prev_algo = mddev->layout;
4966 }
NeilBrown91adb562009-03-31 14:39:39 +11004967
4968 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11004969 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11004970 if (grow_stripes(conf, conf->max_nr_stripes)) {
4971 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004972 "md/raid:%s: couldn't allocate %dkB for buffers\n",
4973 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004974 goto abort;
4975 } else
NeilBrown0c55e022010-05-03 14:09:02 +10004976 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
4977 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004978
NeilBrown02326052012-07-03 15:56:52 +10004979 sprintf(pers_name, "raid%d", mddev->new_level);
4980 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11004981 if (!conf->thread) {
4982 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004983 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11004984 mdname(mddev));
4985 goto abort;
4986 }
4987
4988 return conf;
4989
4990 abort:
4991 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10004992 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11004993 return ERR_PTR(-EIO);
4994 } else
4995 return ERR_PTR(-ENOMEM);
4996}
4997
NeilBrownc148ffd2009-11-13 17:47:00 +11004998
4999static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
5000{
5001 switch (algo) {
5002 case ALGORITHM_PARITY_0:
5003 if (raid_disk < max_degraded)
5004 return 1;
5005 break;
5006 case ALGORITHM_PARITY_N:
5007 if (raid_disk >= raid_disks - max_degraded)
5008 return 1;
5009 break;
5010 case ALGORITHM_PARITY_0_6:
5011 if (raid_disk == 0 ||
5012 raid_disk == raid_disks - 1)
5013 return 1;
5014 break;
5015 case ALGORITHM_LEFT_ASYMMETRIC_6:
5016 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5017 case ALGORITHM_LEFT_SYMMETRIC_6:
5018 case ALGORITHM_RIGHT_SYMMETRIC_6:
5019 if (raid_disk == raid_disks - 1)
5020 return 1;
5021 }
5022 return 0;
5023}
5024
NeilBrownfd01b882011-10-11 16:47:53 +11005025static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11005026{
NeilBrownd1688a62011-10-11 16:49:52 +11005027 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10005028 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11005029 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11005030 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11005031 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11005032 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10005033 long long min_offset_diff = 0;
5034 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11005035
Andre Noll8c6ac862009-06-18 08:48:06 +10005036 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10005037 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10005038 " -- starting background reconstruction\n",
5039 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10005040
5041 rdev_for_each(rdev, mddev) {
5042 long long diff;
5043 if (rdev->raid_disk < 0)
5044 continue;
5045 diff = (rdev->new_data_offset - rdev->data_offset);
5046 if (first) {
5047 min_offset_diff = diff;
5048 first = 0;
5049 } else if (mddev->reshape_backwards &&
5050 diff < min_offset_diff)
5051 min_offset_diff = diff;
5052 else if (!mddev->reshape_backwards &&
5053 diff > min_offset_diff)
5054 min_offset_diff = diff;
5055 }
5056
NeilBrownf6705572006-03-27 01:18:11 -08005057 if (mddev->reshape_position != MaxSector) {
5058 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10005059 * Difficulties arise if the stripe we would write to
5060 * next is at or after the stripe we would read from next.
5061 * For a reshape that changes the number of devices, this
5062 * is only possible for a very short time, and mdadm makes
5063 * sure that time appears to have past before assembling
5064 * the array. So we fail if that time hasn't passed.
5065 * For a reshape that keeps the number of devices the same
5066 * mdadm must be monitoring the reshape can keeping the
5067 * critical areas read-only and backed up. It will start
5068 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08005069 */
5070 sector_t here_new, here_old;
5071 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11005072 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08005073
NeilBrown88ce4932009-03-31 15:24:23 +11005074 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10005075 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08005076 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08005077 mdname(mddev));
5078 return -EINVAL;
5079 }
NeilBrownf6705572006-03-27 01:18:11 -08005080 old_disks = mddev->raid_disks - mddev->delta_disks;
5081 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08005082 * further up in new geometry must map after here in old
5083 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08005084 */
5085 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10005086 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005087 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005088 printk(KERN_ERR "md/raid:%s: reshape_position not "
5089 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005090 return -EINVAL;
5091 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005092 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005093 /* here_new is the stripe we will write to */
5094 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10005095 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005096 (old_disks-max_degraded));
5097 /* here_old is the first stripe that we might need to read
5098 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10005099 if (mddev->delta_disks == 0) {
NeilBrownb5254dd2012-05-21 09:27:01 +10005100 if ((here_new * mddev->new_chunk_sectors !=
5101 here_old * mddev->chunk_sectors)) {
5102 printk(KERN_ERR "md/raid:%s: reshape position is"
5103 " confused - aborting\n", mdname(mddev));
5104 return -EINVAL;
5105 }
NeilBrown67ac6012009-08-13 10:06:24 +10005106 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10005107 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10005108 * and taking constant backups.
5109 * mdadm always starts a situation like this in
5110 * readonly mode so it can take control before
5111 * allowing any writes. So just check for that.
5112 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005113 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
5114 abs(min_offset_diff) >= mddev->new_chunk_sectors)
5115 /* not really in-place - so OK */;
5116 else if (mddev->ro == 0) {
5117 printk(KERN_ERR "md/raid:%s: in-place reshape "
5118 "must be started in read-only mode "
5119 "- aborting\n",
NeilBrown0c55e022010-05-03 14:09:02 +10005120 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005121 return -EINVAL;
5122 }
NeilBrown2c810cd2012-05-21 09:27:00 +10005123 } else if (mddev->reshape_backwards
NeilBrownb5254dd2012-05-21 09:27:01 +10005124 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
NeilBrown67ac6012009-08-13 10:06:24 +10005125 here_old * mddev->chunk_sectors)
5126 : (here_new * mddev->new_chunk_sectors >=
NeilBrownb5254dd2012-05-21 09:27:01 +10005127 here_old * mddev->chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08005128 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005129 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5130 "auto-recovery - aborting.\n",
5131 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005132 return -EINVAL;
5133 }
NeilBrown0c55e022010-05-03 14:09:02 +10005134 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5135 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005136 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005137 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005138 BUG_ON(mddev->level != mddev->new_level);
5139 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005140 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005141 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005142 }
5143
NeilBrown245f46c2009-03-31 14:39:39 +11005144 if (mddev->private == NULL)
5145 conf = setup_conf(mddev);
5146 else
5147 conf = mddev->private;
5148
NeilBrown91adb562009-03-31 14:39:39 +11005149 if (IS_ERR(conf))
5150 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005151
NeilBrownb5254dd2012-05-21 09:27:01 +10005152 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11005153 mddev->thread = conf->thread;
5154 conf->thread = NULL;
5155 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156
NeilBrown17045f52011-12-23 10:17:53 +11005157 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5158 i++) {
5159 rdev = conf->disks[i].rdev;
5160 if (!rdev && conf->disks[i].replacement) {
5161 /* The replacement is all we have yet */
5162 rdev = conf->disks[i].replacement;
5163 conf->disks[i].replacement = NULL;
5164 clear_bit(Replacement, &rdev->flags);
5165 conf->disks[i].rdev = rdev;
5166 }
5167 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11005168 continue;
NeilBrown17045f52011-12-23 10:17:53 +11005169 if (conf->disks[i].replacement &&
5170 conf->reshape_progress != MaxSector) {
5171 /* replacements and reshape simply do not mix. */
5172 printk(KERN_ERR "md: cannot handle concurrent "
5173 "replacement and reshape.\n");
5174 goto abort;
5175 }
NeilBrown2f115882010-06-17 17:41:03 +10005176 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005177 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005178 continue;
5179 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005180 /* This disc is not fully in-sync. However if it
5181 * just stored parity (beyond the recovery_offset),
5182 * when we don't need to be concerned about the
5183 * array being dirty.
5184 * When reshape goes 'backwards', we never have
5185 * partially completed devices, so we only need
5186 * to worry about reshape going forwards.
5187 */
5188 /* Hack because v0.91 doesn't store recovery_offset properly. */
5189 if (mddev->major_version == 0 &&
5190 mddev->minor_version > 90)
5191 rdev->recovery_offset = reshape_offset;
5192
NeilBrownc148ffd2009-11-13 17:47:00 +11005193 if (rdev->recovery_offset < reshape_offset) {
5194 /* We need to check old and new layout */
5195 if (!only_parity(rdev->raid_disk,
5196 conf->algorithm,
5197 conf->raid_disks,
5198 conf->max_degraded))
5199 continue;
5200 }
5201 if (!only_parity(rdev->raid_disk,
5202 conf->prev_algo,
5203 conf->previous_raid_disks,
5204 conf->max_degraded))
5205 continue;
5206 dirty_parity_disks++;
5207 }
NeilBrown91adb562009-03-31 14:39:39 +11005208
NeilBrown17045f52011-12-23 10:17:53 +11005209 /*
5210 * 0 for a fully functional array, 1 or 2 for a degraded array.
5211 */
NeilBrown908f4fb2011-12-23 10:17:50 +11005212 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213
NeilBrown674806d2010-06-16 17:17:53 +10005214 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005215 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07005217 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 goto abort;
5219 }
5220
NeilBrown91adb562009-03-31 14:39:39 +11005221 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10005222 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11005223 mddev->resync_max_sectors = mddev->dev_sectors;
5224
NeilBrownc148ffd2009-11-13 17:47:00 +11005225 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005227 if (mddev->ok_start_degraded)
5228 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10005229 "md/raid:%s: starting dirty degraded array"
5230 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005231 mdname(mddev));
5232 else {
5233 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005234 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005235 mdname(mddev));
5236 goto abort;
5237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238 }
5239
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10005241 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5242 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11005243 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5244 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 else
NeilBrown0c55e022010-05-03 14:09:02 +10005246 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5247 " out of %d devices, algorithm %d\n",
5248 mdname(mddev), conf->level,
5249 mddev->raid_disks - mddev->degraded,
5250 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251
5252 print_raid5_conf(conf);
5253
NeilBrownfef9c612009-03-31 15:16:46 +11005254 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11005255 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005256 atomic_set(&conf->reshape_stripes, 0);
5257 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5258 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5259 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5260 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5261 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005262 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005263 }
5264
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265
5266 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005267 if (mddev->to_remove == &raid5_attrs_group)
5268 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005269 else if (mddev->kobj.sd &&
5270 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005271 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10005272 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08005273 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10005274 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5275
5276 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005277 int chunk_size;
NeilBrown4a5add42010-06-01 19:37:28 +10005278 /* read-ahead size must cover two whole stripes, which
5279 * is 2 * (datadisks) * chunksize where 'n' is the
5280 * number of raid devices
5281 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5283 int stripe = data_disks *
5284 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5285 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5286 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10005287
5288 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005289
5290 mddev->queue->backing_dev_info.congested_data = mddev;
5291 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10005292
5293 chunk_size = mddev->chunk_sectors << 9;
5294 blk_queue_io_min(mddev->queue, chunk_size);
5295 blk_queue_io_opt(mddev->queue, chunk_size *
5296 (conf->raid_disks - conf->max_degraded));
5297
NeilBrown05616be2012-05-21 09:27:00 +10005298 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005299 disk_stack_limits(mddev->gendisk, rdev->bdev,
5300 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10005301 disk_stack_limits(mddev->gendisk, rdev->bdev,
5302 rdev->new_data_offset << 9);
5303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 }
5305
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306 return 0;
5307abort:
NeilBrown01f96c02011-09-21 15:30:20 +10005308 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11005309 print_raid5_conf(conf);
5310 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005312 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 return -EIO;
5314}
5315
NeilBrownfd01b882011-10-11 16:47:53 +11005316static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317{
NeilBrownd1688a62011-10-11 16:49:52 +11005318 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319
NeilBrown01f96c02011-09-21 15:30:20 +10005320 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005321 if (mddev->queue)
5322 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10005323 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005324 mddev->private = NULL;
5325 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326 return 0;
5327}
5328
NeilBrownfd01b882011-10-11 16:47:53 +11005329static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330{
NeilBrownd1688a62011-10-11 16:49:52 +11005331 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 int i;
5333
Andre Noll9d8f0362009-06-18 08:45:01 +10005334 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5335 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005336 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 for (i = 0; i < conf->raid_disks; i++)
5338 seq_printf (seq, "%s",
5339 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005340 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342}
5343
NeilBrownd1688a62011-10-11 16:49:52 +11005344static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345{
5346 int i;
5347 struct disk_info *tmp;
5348
NeilBrown0c55e022010-05-03 14:09:02 +10005349 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 if (!conf) {
5351 printk("(conf==NULL)\n");
5352 return;
5353 }
NeilBrown0c55e022010-05-03 14:09:02 +10005354 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5355 conf->raid_disks,
5356 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357
5358 for (i = 0; i < conf->raid_disks; i++) {
5359 char b[BDEVNAME_SIZE];
5360 tmp = conf->disks + i;
5361 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005362 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5363 i, !test_bit(Faulty, &tmp->rdev->flags),
5364 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 }
5366}
5367
NeilBrownfd01b882011-10-11 16:47:53 +11005368static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369{
5370 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005371 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005373 int count = 0;
5374 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375
5376 for (i = 0; i < conf->raid_disks; i++) {
5377 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11005378 if (tmp->replacement
5379 && tmp->replacement->recovery_offset == MaxSector
5380 && !test_bit(Faulty, &tmp->replacement->flags)
5381 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
5382 /* Replacement has just become active. */
5383 if (!tmp->rdev
5384 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
5385 count++;
5386 if (tmp->rdev) {
5387 /* Replaced device not technically faulty,
5388 * but we need to be sure it gets removed
5389 * and never re-added.
5390 */
5391 set_bit(Faulty, &tmp->rdev->flags);
5392 sysfs_notify_dirent_safe(
5393 tmp->rdev->sysfs_state);
5394 }
5395 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
5396 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005397 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005398 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005399 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005400 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005401 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 }
5403 }
NeilBrown6b965622010-08-18 11:56:59 +10005404 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005405 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005406 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005408 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409}
5410
NeilBrownb8321b62011-12-23 10:17:51 +11005411static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412{
NeilBrownd1688a62011-10-11 16:49:52 +11005413 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11005415 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11005416 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 struct disk_info *p = conf->disks + number;
5418
5419 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11005420 if (rdev == p->rdev)
5421 rdevp = &p->rdev;
5422 else if (rdev == p->replacement)
5423 rdevp = &p->replacement;
5424 else
5425 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11005426
NeilBrown657e3e42011-12-23 10:17:52 +11005427 if (number >= conf->raid_disks &&
5428 conf->reshape_progress == MaxSector)
5429 clear_bit(In_sync, &rdev->flags);
5430
5431 if (test_bit(In_sync, &rdev->flags) ||
5432 atomic_read(&rdev->nr_pending)) {
5433 err = -EBUSY;
5434 goto abort;
5435 }
5436 /* Only remove non-faulty devices if recovery
5437 * isn't possible.
5438 */
5439 if (!test_bit(Faulty, &rdev->flags) &&
5440 mddev->recovery_disabled != conf->recovery_disabled &&
5441 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11005442 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11005443 number < conf->raid_disks) {
5444 err = -EBUSY;
5445 goto abort;
5446 }
5447 *rdevp = NULL;
5448 synchronize_rcu();
5449 if (atomic_read(&rdev->nr_pending)) {
5450 /* lost the race, try later */
5451 err = -EBUSY;
5452 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11005453 } else if (p->replacement) {
5454 /* We must have just cleared 'rdev' */
5455 p->rdev = p->replacement;
5456 clear_bit(Replacement, &p->replacement->flags);
5457 smp_mb(); /* Make sure other CPUs may see both as identical
5458 * but will never see neither - if they are careful
5459 */
5460 p->replacement = NULL;
5461 clear_bit(WantReplacement, &rdev->flags);
5462 } else
5463 /* We might have just removed the Replacement as faulty-
5464 * clear the bit just in case
5465 */
5466 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467abort:
5468
5469 print_raid5_conf(conf);
5470 return err;
5471}
5472
NeilBrownfd01b882011-10-11 16:47:53 +11005473static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474{
NeilBrownd1688a62011-10-11 16:49:52 +11005475 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005476 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 int disk;
5478 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005479 int first = 0;
5480 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481
NeilBrown7f0da592011-07-28 11:39:22 +10005482 if (mddev->recovery_disabled == conf->recovery_disabled)
5483 return -EBUSY;
5484
NeilBrowndc10c642012-03-19 12:46:37 +11005485 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005487 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488
Neil Brown6c2fce22008-06-28 08:31:31 +10005489 if (rdev->raid_disk >= 0)
5490 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491
5492 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005493 * find the disk ... but prefer rdev->saved_raid_disk
5494 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005496 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005497 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005498 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10005499 first = rdev->saved_raid_disk;
5500
5501 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11005502 p = conf->disks + disk;
5503 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005504 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005506 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005507 if (rdev->saved_raid_disk != disk)
5508 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005509 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10005510 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005512 }
5513 for (disk = first; disk <= last; disk++) {
5514 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11005515 if (test_bit(WantReplacement, &p->rdev->flags) &&
5516 p->replacement == NULL) {
5517 clear_bit(In_sync, &rdev->flags);
5518 set_bit(Replacement, &rdev->flags);
5519 rdev->raid_disk = disk;
5520 err = 0;
5521 conf->fullsync = 1;
5522 rcu_assign_pointer(p->replacement, rdev);
5523 break;
5524 }
5525 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005526out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005528 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529}
5530
NeilBrownfd01b882011-10-11 16:47:53 +11005531static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532{
5533 /* no resync is happening, and there is enough space
5534 * on all devices, so we can resize.
5535 * We need to make sure resync covers any new space.
5536 * If the array is shrinking we should possibly wait until
5537 * any io in the removed space completes, but it hardly seems
5538 * worth it.
5539 */
NeilBrowna4a61252012-05-22 13:55:27 +10005540 sector_t newsize;
Andre Noll9d8f0362009-06-18 08:45:01 +10005541 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10005542 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
5543 if (mddev->external_size &&
5544 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11005545 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10005546 if (mddev->bitmap) {
5547 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
5548 if (ret)
5549 return ret;
5550 }
5551 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10005552 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005553 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005554 if (sectors > mddev->dev_sectors &&
5555 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005556 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5558 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005559 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005560 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 return 0;
5562}
5563
NeilBrownfd01b882011-10-11 16:47:53 +11005564static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005565{
5566 /* Can only proceed if there are plenty of stripe_heads.
5567 * We need a minimum of one full stripe,, and for sensible progress
5568 * it is best to have about 4 times that.
5569 * If we require 4 times, then the default 256 4K stripe_heads will
5570 * allow for chunk sizes up to 256K, which is probably OK.
5571 * If the chunk size is greater, user-space should request more
5572 * stripe_heads first.
5573 */
NeilBrownd1688a62011-10-11 16:49:52 +11005574 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005575 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5576 > conf->max_nr_stripes ||
5577 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5578 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005579 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5580 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005581 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5582 / STRIPE_SIZE)*4);
5583 return 0;
5584 }
5585 return 1;
5586}
5587
NeilBrownfd01b882011-10-11 16:47:53 +11005588static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005589{
NeilBrownd1688a62011-10-11 16:49:52 +11005590 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005591
NeilBrown88ce4932009-03-31 15:24:23 +11005592 if (mddev->delta_disks == 0 &&
5593 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005594 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005595 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10005596 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005597 return -EINVAL;
5598 if (mddev->delta_disks < 0) {
5599 /* We might be able to shrink, but the devices must
5600 * be made bigger first.
5601 * For raid6, 4 is the minimum size.
5602 * Otherwise 2 is the minimum
5603 */
5604 int min = 2;
5605 if (mddev->level == 6)
5606 min = 4;
5607 if (mddev->raid_disks + mddev->delta_disks < min)
5608 return -EINVAL;
5609 }
NeilBrown29269552006-03-27 01:18:10 -08005610
NeilBrown01ee22b2009-06-18 08:47:20 +10005611 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005612 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005613
NeilBrownec32a2b2009-03-31 15:17:38 +11005614 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
NeilBrown63c70c42006-03-27 01:18:13 -08005615}
5616
NeilBrownfd01b882011-10-11 16:47:53 +11005617static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005618{
NeilBrownd1688a62011-10-11 16:49:52 +11005619 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005620 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005621 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005622 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005623
NeilBrownf4168852007-02-28 20:11:53 -08005624 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005625 return -EBUSY;
5626
NeilBrown01ee22b2009-06-18 08:47:20 +10005627 if (!check_stripe_cache(mddev))
5628 return -ENOSPC;
5629
NeilBrown30b67642012-05-22 13:55:28 +10005630 if (has_failed(conf))
5631 return -EINVAL;
5632
NeilBrownc6563a82012-05-21 09:27:00 +10005633 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11005634 if (!test_bit(In_sync, &rdev->flags)
5635 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005636 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10005637 }
NeilBrown63c70c42006-03-27 01:18:13 -08005638
NeilBrownf4168852007-02-28 20:11:53 -08005639 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005640 /* Not enough devices even to make a degraded array
5641 * of that size
5642 */
5643 return -EINVAL;
5644
NeilBrownec32a2b2009-03-31 15:17:38 +11005645 /* Refuse to reduce size of the array. Any reductions in
5646 * array size must be through explicit setting of array_size
5647 * attribute.
5648 */
5649 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5650 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10005651 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11005652 "before number of disks\n", mdname(mddev));
5653 return -EINVAL;
5654 }
5655
NeilBrownf6705572006-03-27 01:18:11 -08005656 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005657 spin_lock_irq(&conf->device_lock);
5658 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005659 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005660 conf->prev_chunk_sectors = conf->chunk_sectors;
5661 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005662 conf->prev_algo = conf->algorithm;
5663 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10005664 conf->generation++;
5665 /* Code that selects data_offset needs to see the generation update
5666 * if reshape_progress has been set - so a memory barrier needed.
5667 */
5668 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10005669 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11005670 conf->reshape_progress = raid5_size(mddev, 0, 0);
5671 else
5672 conf->reshape_progress = 0;
5673 conf->reshape_safe = conf->reshape_progress;
NeilBrown29269552006-03-27 01:18:10 -08005674 spin_unlock_irq(&conf->device_lock);
5675
5676 /* Add some new drives, as many as will fit.
5677 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10005678 * Don't add devices if we are reducing the number of
5679 * devices in the array. This is because it is not possible
5680 * to correctly record the "partially reconstructed" state of
5681 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08005682 */
NeilBrown87a8dec2011-01-31 11:57:43 +11005683 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11005684 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11005685 if (rdev->raid_disk < 0 &&
5686 !test_bit(Faulty, &rdev->flags)) {
5687 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11005688 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11005689 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11005690 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11005691 else
NeilBrown87a8dec2011-01-31 11:57:43 +11005692 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10005693
5694 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11005695 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11005696 }
NeilBrown87a8dec2011-01-31 11:57:43 +11005697 } else if (rdev->raid_disk >= conf->previous_raid_disks
5698 && !test_bit(Faulty, &rdev->flags)) {
5699 /* This is a spare that was manually added */
5700 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11005701 }
NeilBrown29269552006-03-27 01:18:10 -08005702
NeilBrown87a8dec2011-01-31 11:57:43 +11005703 /* When a reshape changes the number of devices,
5704 * ->degraded is measured against the larger of the
5705 * pre and post number of devices.
5706 */
NeilBrownec32a2b2009-03-31 15:17:38 +11005707 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005708 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11005709 spin_unlock_irqrestore(&conf->device_lock, flags);
5710 }
NeilBrown63c70c42006-03-27 01:18:13 -08005711 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10005712 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07005713 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005714
NeilBrown29269552006-03-27 01:18:10 -08005715 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5716 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5717 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5718 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5719 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005720 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08005721 if (!mddev->sync_thread) {
5722 mddev->recovery = 0;
5723 spin_lock_irq(&conf->device_lock);
5724 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10005725 rdev_for_each(rdev, mddev)
5726 rdev->new_data_offset = rdev->data_offset;
5727 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11005728 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11005729 mddev->reshape_position = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005730 spin_unlock_irq(&conf->device_lock);
5731 return -EAGAIN;
5732 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005733 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005734 md_wakeup_thread(mddev->sync_thread);
5735 md_new_event(mddev);
5736 return 0;
5737}
NeilBrown29269552006-03-27 01:18:10 -08005738
NeilBrownec32a2b2009-03-31 15:17:38 +11005739/* This is called from the reshape thread and should make any
5740 * changes needed in 'conf'
5741 */
NeilBrownd1688a62011-10-11 16:49:52 +11005742static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08005743{
NeilBrown29269552006-03-27 01:18:10 -08005744
NeilBrownf6705572006-03-27 01:18:11 -08005745 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10005746 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07005747
NeilBrownf6705572006-03-27 01:18:11 -08005748 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11005749 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10005750 rdev_for_each(rdev, conf->mddev)
5751 rdev->data_offset = rdev->new_data_offset;
5752 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11005753 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08005754 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005755 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07005756
5757 /* read-ahead size must cover two whole stripes, which is
5758 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5759 */
NeilBrown4a5add42010-06-01 19:37:28 +10005760 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11005761 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005762 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11005763 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07005764 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5765 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5766 }
NeilBrown29269552006-03-27 01:18:10 -08005767 }
NeilBrown29269552006-03-27 01:18:10 -08005768}
5769
NeilBrownec32a2b2009-03-31 15:17:38 +11005770/* This is called from the raid5d thread with mddev_lock held.
5771 * It makes config changes to the device.
5772 */
NeilBrownfd01b882011-10-11 16:47:53 +11005773static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11005774{
NeilBrownd1688a62011-10-11 16:49:52 +11005775 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11005776
5777 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5778
NeilBrownec32a2b2009-03-31 15:17:38 +11005779 if (mddev->delta_disks > 0) {
5780 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5781 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005782 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11005783 } else {
5784 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11005785 spin_lock_irq(&conf->device_lock);
5786 mddev->degraded = calc_degraded(conf);
5787 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11005788 for (d = conf->raid_disks ;
5789 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10005790 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11005791 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10005792 if (rdev)
5793 clear_bit(In_sync, &rdev->flags);
5794 rdev = conf->disks[d].replacement;
5795 if (rdev)
5796 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10005797 }
NeilBrowncea9c222009-03-31 15:15:05 +11005798 }
NeilBrown88ce4932009-03-31 15:24:23 +11005799 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005800 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11005801 mddev->reshape_position = MaxSector;
5802 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005803 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11005804 }
5805}
5806
NeilBrownfd01b882011-10-11 16:47:53 +11005807static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07005808{
NeilBrownd1688a62011-10-11 16:49:52 +11005809 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07005810
5811 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08005812 case 2: /* resume for a suspend */
5813 wake_up(&conf->wait_for_overlap);
5814 break;
5815
NeilBrown72626682005-09-09 16:23:54 -07005816 case 1: /* stop all writes */
5817 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005818 /* '2' tells resync/reshape to pause so that all
5819 * active stripes can drain
5820 */
5821 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07005822 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005823 atomic_read(&conf->active_stripes) == 0 &&
5824 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07005825 conf->device_lock, /* nothing */);
NeilBrown64bd6602009-08-03 10:59:58 +10005826 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07005827 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005828 /* allow reshape to continue */
5829 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005830 break;
5831
5832 case 0: /* re-enable writes */
5833 spin_lock_irq(&conf->device_lock);
5834 conf->quiesce = 0;
5835 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08005836 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005837 spin_unlock_irq(&conf->device_lock);
5838 break;
5839 }
NeilBrown72626682005-09-09 16:23:54 -07005840}
NeilBrownb15c2e52006-01-06 00:20:16 -08005841
NeilBrownd562b0c2009-03-31 14:39:39 +11005842
NeilBrownfd01b882011-10-11 16:47:53 +11005843static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11005844{
NeilBrowne373ab12011-10-11 16:48:59 +11005845 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07005846 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11005847
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005848 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11005849 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10005850 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
5851 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005852 return ERR_PTR(-EINVAL);
5853 }
5854
NeilBrowne373ab12011-10-11 16:48:59 +11005855 sectors = raid0_conf->strip_zone[0].zone_end;
5856 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10005857 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005858 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11005859 mddev->new_layout = ALGORITHM_PARITY_N;
5860 mddev->new_chunk_sectors = mddev->chunk_sectors;
5861 mddev->raid_disks += 1;
5862 mddev->delta_disks = 1;
5863 /* make sure it will be not marked as dirty */
5864 mddev->recovery_cp = MaxSector;
5865
5866 return setup_conf(mddev);
5867}
5868
5869
NeilBrownfd01b882011-10-11 16:47:53 +11005870static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005871{
5872 int chunksect;
5873
5874 if (mddev->raid_disks != 2 ||
5875 mddev->degraded > 1)
5876 return ERR_PTR(-EINVAL);
5877
5878 /* Should check if there are write-behind devices? */
5879
5880 chunksect = 64*2; /* 64K by default */
5881
5882 /* The array must be an exact multiple of chunksize */
5883 while (chunksect && (mddev->array_sectors & (chunksect-1)))
5884 chunksect >>= 1;
5885
5886 if ((chunksect<<9) < STRIPE_SIZE)
5887 /* array size does not allow a suitable chunk size */
5888 return ERR_PTR(-EINVAL);
5889
5890 mddev->new_level = 5;
5891 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10005892 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11005893
5894 return setup_conf(mddev);
5895}
5896
NeilBrownfd01b882011-10-11 16:47:53 +11005897static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11005898{
5899 int new_layout;
5900
5901 switch (mddev->layout) {
5902 case ALGORITHM_LEFT_ASYMMETRIC_6:
5903 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
5904 break;
5905 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5906 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
5907 break;
5908 case ALGORITHM_LEFT_SYMMETRIC_6:
5909 new_layout = ALGORITHM_LEFT_SYMMETRIC;
5910 break;
5911 case ALGORITHM_RIGHT_SYMMETRIC_6:
5912 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
5913 break;
5914 case ALGORITHM_PARITY_0_6:
5915 new_layout = ALGORITHM_PARITY_0;
5916 break;
5917 case ALGORITHM_PARITY_N:
5918 new_layout = ALGORITHM_PARITY_N;
5919 break;
5920 default:
5921 return ERR_PTR(-EINVAL);
5922 }
5923 mddev->new_level = 5;
5924 mddev->new_layout = new_layout;
5925 mddev->delta_disks = -1;
5926 mddev->raid_disks -= 1;
5927 return setup_conf(mddev);
5928}
5929
NeilBrownd562b0c2009-03-31 14:39:39 +11005930
NeilBrownfd01b882011-10-11 16:47:53 +11005931static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11005932{
NeilBrown88ce4932009-03-31 15:24:23 +11005933 /* For a 2-drive array, the layout and chunk size can be changed
5934 * immediately as not restriping is needed.
5935 * For larger arrays we record the new value - after validation
5936 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11005937 */
NeilBrownd1688a62011-10-11 16:49:52 +11005938 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10005939 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11005940
NeilBrown597a7112009-06-18 08:47:42 +10005941 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11005942 return -EINVAL;
5943 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005944 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11005945 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005946 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11005947 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005948 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11005949 /* not factor of array size */
5950 return -EINVAL;
5951 }
5952
5953 /* They look valid */
5954
NeilBrown88ce4932009-03-31 15:24:23 +11005955 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10005956 /* can make the change immediately */
5957 if (mddev->new_layout >= 0) {
5958 conf->algorithm = mddev->new_layout;
5959 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11005960 }
5961 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10005962 conf->chunk_sectors = new_chunk ;
5963 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11005964 }
5965 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5966 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11005967 }
NeilBrown50ac1682009-06-18 08:47:55 +10005968 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11005969}
5970
NeilBrownfd01b882011-10-11 16:47:53 +11005971static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11005972{
NeilBrown597a7112009-06-18 08:47:42 +10005973 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10005974
NeilBrown597a7112009-06-18 08:47:42 +10005975 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11005976 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005977 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005978 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11005979 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005980 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11005981 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005982 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11005983 /* not factor of array size */
5984 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005985 }
NeilBrown88ce4932009-03-31 15:24:23 +11005986
5987 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10005988 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11005989}
5990
NeilBrownfd01b882011-10-11 16:47:53 +11005991static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005992{
5993 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005994 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005995 * raid1 - if there are two drives. We need to know the chunk size
5996 * raid4 - trivial - just use a raid4 layout.
5997 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005998 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005999 if (mddev->level == 0)
6000 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11006001 if (mddev->level == 1)
6002 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11006003 if (mddev->level == 4) {
6004 mddev->new_layout = ALGORITHM_PARITY_N;
6005 mddev->new_level = 5;
6006 return setup_conf(mddev);
6007 }
NeilBrownfc9739c2009-03-31 14:57:20 +11006008 if (mddev->level == 6)
6009 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11006010
6011 return ERR_PTR(-EINVAL);
6012}
6013
NeilBrownfd01b882011-10-11 16:47:53 +11006014static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11006015{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006016 /* raid4 can take over:
6017 * raid0 - if there is only one strip zone
6018 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11006019 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006020 if (mddev->level == 0)
6021 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11006022 if (mddev->level == 5 &&
6023 mddev->layout == ALGORITHM_PARITY_N) {
6024 mddev->new_layout = 0;
6025 mddev->new_level = 4;
6026 return setup_conf(mddev);
6027 }
6028 return ERR_PTR(-EINVAL);
6029}
NeilBrownd562b0c2009-03-31 14:39:39 +11006030
NeilBrown84fc4b52011-10-11 16:49:58 +11006031static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11006032
NeilBrownfd01b882011-10-11 16:47:53 +11006033static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11006034{
6035 /* Currently can only take over a raid5. We map the
6036 * personality to an equivalent raid6 personality
6037 * with the Q block at the end.
6038 */
6039 int new_layout;
6040
6041 if (mddev->pers != &raid5_personality)
6042 return ERR_PTR(-EINVAL);
6043 if (mddev->degraded > 1)
6044 return ERR_PTR(-EINVAL);
6045 if (mddev->raid_disks > 253)
6046 return ERR_PTR(-EINVAL);
6047 if (mddev->raid_disks < 3)
6048 return ERR_PTR(-EINVAL);
6049
6050 switch (mddev->layout) {
6051 case ALGORITHM_LEFT_ASYMMETRIC:
6052 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
6053 break;
6054 case ALGORITHM_RIGHT_ASYMMETRIC:
6055 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
6056 break;
6057 case ALGORITHM_LEFT_SYMMETRIC:
6058 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
6059 break;
6060 case ALGORITHM_RIGHT_SYMMETRIC:
6061 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
6062 break;
6063 case ALGORITHM_PARITY_0:
6064 new_layout = ALGORITHM_PARITY_0_6;
6065 break;
6066 case ALGORITHM_PARITY_N:
6067 new_layout = ALGORITHM_PARITY_N;
6068 break;
6069 default:
6070 return ERR_PTR(-EINVAL);
6071 }
6072 mddev->new_level = 6;
6073 mddev->new_layout = new_layout;
6074 mddev->delta_disks = 1;
6075 mddev->raid_disks += 1;
6076 return setup_conf(mddev);
6077}
6078
6079
NeilBrown84fc4b52011-10-11 16:49:58 +11006080static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07006081{
6082 .name = "raid6",
6083 .level = 6,
6084 .owner = THIS_MODULE,
6085 .make_request = make_request,
6086 .run = run,
6087 .stop = stop,
6088 .status = status,
6089 .error_handler = error,
6090 .hot_add_disk = raid5_add_disk,
6091 .hot_remove_disk= raid5_remove_disk,
6092 .spare_active = raid5_spare_active,
6093 .sync_request = sync_request,
6094 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006095 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10006096 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08006097 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006098 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07006099 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11006100 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07006101};
NeilBrown84fc4b52011-10-11 16:49:58 +11006102static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103{
6104 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08006105 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106 .owner = THIS_MODULE,
6107 .make_request = make_request,
6108 .run = run,
6109 .stop = stop,
6110 .status = status,
6111 .error_handler = error,
6112 .hot_add_disk = raid5_add_disk,
6113 .hot_remove_disk= raid5_remove_disk,
6114 .spare_active = raid5_spare_active,
6115 .sync_request = sync_request,
6116 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006117 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08006118 .check_reshape = raid5_check_reshape,
6119 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006120 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07006121 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11006122 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123};
6124
NeilBrown84fc4b52011-10-11 16:49:58 +11006125static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126{
NeilBrown2604b702006-01-06 00:20:36 -08006127 .name = "raid4",
6128 .level = 4,
6129 .owner = THIS_MODULE,
6130 .make_request = make_request,
6131 .run = run,
6132 .stop = stop,
6133 .status = status,
6134 .error_handler = error,
6135 .hot_add_disk = raid5_add_disk,
6136 .hot_remove_disk= raid5_remove_disk,
6137 .spare_active = raid5_spare_active,
6138 .sync_request = sync_request,
6139 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006140 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08006141 .check_reshape = raid5_check_reshape,
6142 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006143 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08006144 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11006145 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08006146};
6147
6148static int __init raid5_init(void)
6149{
NeilBrown16a53ec2006-06-26 00:27:38 -07006150 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006151 register_md_personality(&raid5_personality);
6152 register_md_personality(&raid4_personality);
6153 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154}
6155
NeilBrown2604b702006-01-06 00:20:36 -08006156static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157{
NeilBrown16a53ec2006-06-26 00:27:38 -07006158 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006159 unregister_md_personality(&raid5_personality);
6160 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161}
6162
6163module_init(raid5_init);
6164module_exit(raid5_exit);
6165MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11006166MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08006168MODULE_ALIAS("md-raid5");
6169MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08006170MODULE_ALIAS("md-level-5");
6171MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07006172MODULE_ALIAS("md-personality-8"); /* RAID6 */
6173MODULE_ALIAS("md-raid6");
6174MODULE_ALIAS("md-level-6");
6175
6176/* This used to be two separate modules, they were: */
6177MODULE_ALIAS("raid5");
6178MODULE_ALIAS("raid6");