blob: 8fc591877dc1efacf036a47ce02bac33453491ff [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
NeilBrownd1688a62011-10-11 16:49:52 +1100193static void __release_stripe(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 if (atomic_dec_and_test(&sh->count)) {
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200196 BUG_ON(!list_empty(&sh->lru));
197 BUG_ON(atomic_read(&conf->active_stripes)==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (test_bit(STRIPE_HANDLE, &sh->state)) {
NeilBrown482c0832011-04-18 18:25:42 +1000199 if (test_bit(STRIPE_DELAYED, &sh->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 list_add_tail(&sh->lru, &conf->delayed_list);
NeilBrown482c0832011-04-18 18:25:42 +1000201 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
202 sh->bm_seq - conf->seq_write > 0)
NeilBrown72626682005-09-09 16:23:54 -0700203 list_add_tail(&sh->lru, &conf->bitmap_list);
NeilBrown482c0832011-04-18 18:25:42 +1000204 else {
NeilBrown72626682005-09-09 16:23:54 -0700205 clear_bit(STRIPE_BIT_DELAY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 list_add_tail(&sh->lru, &conf->handle_list);
NeilBrown72626682005-09-09 16:23:54 -0700207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 md_wakeup_thread(conf->mddev->thread);
209 } else {
Dan Williams600aa102008-06-28 08:32:05 +1000210 BUG_ON(stripe_operations_active(sh));
majianpeng41fe75f2012-03-13 11:21:25 +1100211 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
212 if (atomic_dec_return(&conf->preread_active_stripes)
213 < IO_THRESHOLD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 md_wakeup_thread(conf->mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 atomic_dec(&conf->active_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800216 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
217 list_add_tail(&sh->lru, &conf->inactive_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 wake_up(&conf->wait_for_stripe);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -0800219 if (conf->retry_read_aligned)
220 md_wakeup_thread(conf->mddev->thread);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
223 }
224}
NeilBrownd0dabf72009-03-31 14:39:38 +1100225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226static void release_stripe(struct stripe_head *sh)
227{
NeilBrownd1688a62011-10-11 16:49:52 +1100228 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 unsigned long flags;
NeilBrown16a53ec2006-06-26 00:27:38 -0700230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 spin_lock_irqsave(&conf->device_lock, flags);
232 __release_stripe(conf, sh);
233 spin_unlock_irqrestore(&conf->device_lock, flags);
234}
235
NeilBrownfccddba2006-01-06 00:20:33 -0800236static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Dan Williams45b42332007-07-09 11:56:43 -0700238 pr_debug("remove_hash(), stripe %llu\n",
239 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
NeilBrownfccddba2006-01-06 00:20:33 -0800241 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
NeilBrownd1688a62011-10-11 16:49:52 +1100244static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
NeilBrownfccddba2006-01-06 00:20:33 -0800246 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Dan Williams45b42332007-07-09 11:56:43 -0700248 pr_debug("insert_hash(), stripe %llu\n",
249 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
NeilBrownfccddba2006-01-06 00:20:33 -0800251 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
254
255/* find an idle stripe, make sure it is unhashed, and return it. */
NeilBrownd1688a62011-10-11 16:49:52 +1100256static struct stripe_head *get_free_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 struct stripe_head *sh = NULL;
259 struct list_head *first;
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (list_empty(&conf->inactive_list))
262 goto out;
263 first = conf->inactive_list.next;
264 sh = list_entry(first, struct stripe_head, lru);
265 list_del_init(first);
266 remove_hash(sh);
267 atomic_inc(&conf->active_stripes);
268out:
269 return sh;
270}
271
NeilBrowne4e11e32010-06-16 16:45:16 +1000272static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 struct page *p;
275 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000276 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
NeilBrowne4e11e32010-06-16 16:45:16 +1000278 for (i = 0; i < num ; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 p = sh->dev[i].page;
280 if (!p)
281 continue;
282 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800283 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
285}
286
NeilBrowne4e11e32010-06-16 16:45:16 +1000287static int grow_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000290 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
NeilBrowne4e11e32010-06-16 16:45:16 +1000292 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 struct page *page;
294
295 if (!(page = alloc_page(GFP_KERNEL))) {
296 return 1;
297 }
298 sh->dev[i].page = page;
299 }
300 return 0;
301}
302
NeilBrown784052e2009-03-31 15:19:07 +1100303static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrownd1688a62011-10-11 16:49:52 +1100304static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100305 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
NeilBrownb5663ba2009-03-31 14:39:38 +1100307static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
NeilBrownd1688a62011-10-11 16:49:52 +1100309 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800310 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200312 BUG_ON(atomic_read(&sh->count) != 0);
313 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000314 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700315
Dan Williams45b42332007-07-09 11:56:43 -0700316 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 (unsigned long long)sh->sector);
318
319 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700320
NeilBrown86b42c72009-03-31 15:19:03 +1100321 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100322 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100324 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 sh->state = 0;
326
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800327
328 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 struct r5dev *dev = &sh->dev[i];
330
Dan Williamsd84e0f12007-01-02 13:52:30 -0700331 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700333 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700335 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000337 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100340 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342 insert_hash(conf, sh);
343}
344
NeilBrownd1688a62011-10-11 16:49:52 +1100345static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100346 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -0800349 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Dan Williams45b42332007-07-09 11:56:43 -0700351 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
NeilBrownfccddba2006-01-06 00:20:33 -0800352 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100353 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700355 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return NULL;
357}
358
NeilBrown674806d2010-06-16 17:17:53 +1000359/*
360 * Need to check if array has failed when deciding whether to:
361 * - start an array
362 * - remove non-faulty devices
363 * - add a spare
364 * - allow a reshape
365 * This determination is simple when no reshape is happening.
366 * However if there is a reshape, we need to carefully check
367 * both the before and after sections.
368 * This is because some failed devices may only affect one
369 * of the two sections, and some non-in_sync devices may
370 * be insync in the section most affected by failed devices.
371 */
NeilBrown908f4fb2011-12-23 10:17:50 +1100372static int calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000373{
NeilBrown908f4fb2011-12-23 10:17:50 +1100374 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000375 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000376
377 rcu_read_lock();
378 degraded = 0;
379 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100380 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown674806d2010-06-16 17:17:53 +1000381 if (!rdev || test_bit(Faulty, &rdev->flags))
382 degraded++;
383 else if (test_bit(In_sync, &rdev->flags))
384 ;
385 else
386 /* not in-sync or faulty.
387 * If the reshape increases the number of devices,
388 * this is being recovered by the reshape, so
389 * this 'previous' section is not in_sync.
390 * If the number of devices is being reduced however,
391 * the device can only be part of the array if
392 * we are reverting a reshape, so this section will
393 * be in-sync.
394 */
395 if (conf->raid_disks >= conf->previous_raid_disks)
396 degraded++;
397 }
398 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100399 if (conf->raid_disks == conf->previous_raid_disks)
400 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000401 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100402 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000403 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100404 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown674806d2010-06-16 17:17:53 +1000405 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100406 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000407 else if (test_bit(In_sync, &rdev->flags))
408 ;
409 else
410 /* not in-sync or faulty.
411 * If reshape increases the number of devices, this
412 * section has already been recovered, else it
413 * almost certainly hasn't.
414 */
415 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100416 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000417 }
418 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100419 if (degraded2 > degraded)
420 return degraded2;
421 return degraded;
422}
423
424static int has_failed(struct r5conf *conf)
425{
426 int degraded;
427
428 if (conf->mddev->reshape_position == MaxSector)
429 return conf->mddev->degraded > conf->max_degraded;
430
431 degraded = calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000432 if (degraded > conf->max_degraded)
433 return 1;
434 return 0;
435}
436
NeilBrownb5663ba2009-03-31 14:39:38 +1100437static struct stripe_head *
NeilBrownd1688a62011-10-11 16:49:52 +1100438get_active_stripe(struct r5conf *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000439 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
441 struct stripe_head *sh;
442
Dan Williams45b42332007-07-09 11:56:43 -0700443 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 spin_lock_irq(&conf->device_lock);
446
447 do {
NeilBrown72626682005-09-09 16:23:54 -0700448 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000449 conf->quiesce == 0 || noquiesce,
NeilBrown72626682005-09-09 16:23:54 -0700450 conf->device_lock, /* nothing */);
NeilBrown86b42c72009-03-31 15:19:03 +1100451 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (!sh) {
453 if (!conf->inactive_blocked)
454 sh = get_free_stripe(conf);
455 if (noblock && sh == NULL)
456 break;
457 if (!sh) {
458 conf->inactive_blocked = 1;
459 wait_event_lock_irq(conf->wait_for_stripe,
460 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800461 (atomic_read(&conf->active_stripes)
462 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 || !conf->inactive_blocked),
464 conf->device_lock,
NeilBrown7c13edc2011-04-18 18:25:43 +1000465 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 conf->inactive_blocked = 0;
467 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100468 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 } else {
470 if (atomic_read(&sh->count)) {
NeilBrownab69ae12009-03-31 15:26:47 +1100471 BUG_ON(!list_empty(&sh->lru)
472 && !test_bit(STRIPE_EXPANDING, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 } else {
474 if (!test_bit(STRIPE_HANDLE, &sh->state))
475 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700476 if (list_empty(&sh->lru) &&
477 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700478 BUG();
479 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
481 }
482 } while (sh == NULL);
483
484 if (sh)
485 atomic_inc(&sh->count);
486
487 spin_unlock_irq(&conf->device_lock);
488 return sh;
489}
490
NeilBrown6712ecf2007-09-27 12:47:43 +0200491static void
492raid5_end_read_request(struct bio *bi, int error);
493static void
494raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700495
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000496static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700497{
NeilBrownd1688a62011-10-11 16:49:52 +1100498 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700499 int i, disks = sh->disks;
500
501 might_sleep();
502
503 for (i = disks; i--; ) {
504 int rw;
NeilBrown9a3e1102011-12-23 10:17:53 +1100505 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100506 struct bio *bi, *rbi;
507 struct md_rdev *rdev, *rrdev = NULL;
Tejun Heoe9c74692010-09-03 11:56:18 +0200508 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
509 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
510 rw = WRITE_FUA;
511 else
512 rw = WRITE;
513 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700514 rw = READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100515 else if (test_and_clear_bit(R5_WantReplace,
516 &sh->dev[i].flags)) {
517 rw = WRITE;
518 replace_only = 1;
519 } else
Dan Williams91c00922007-01-02 13:52:30 -0700520 continue;
521
522 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100523 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700524
525 bi->bi_rw = rw;
NeilBrown977df362011-12-23 10:17:53 +1100526 rbi->bi_rw = rw;
527 if (rw & WRITE) {
Dan Williams91c00922007-01-02 13:52:30 -0700528 bi->bi_end_io = raid5_end_write_request;
NeilBrown977df362011-12-23 10:17:53 +1100529 rbi->bi_end_io = raid5_end_write_request;
530 } else
Dan Williams91c00922007-01-02 13:52:30 -0700531 bi->bi_end_io = raid5_end_read_request;
532
533 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100534 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100535 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
536 rdev = rcu_dereference(conf->disks[i].rdev);
537 if (!rdev) {
538 rdev = rrdev;
539 rrdev = NULL;
540 }
NeilBrown9a3e1102011-12-23 10:17:53 +1100541 if (rw & WRITE) {
542 if (replace_only)
543 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100544 if (rdev == rrdev)
545 /* We raced and saw duplicates */
546 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100547 } else {
NeilBrowndd054fc2011-12-23 10:17:53 +1100548 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100549 rdev = rrdev;
550 rrdev = NULL;
551 }
NeilBrown977df362011-12-23 10:17:53 +1100552
Dan Williams91c00922007-01-02 13:52:30 -0700553 if (rdev && test_bit(Faulty, &rdev->flags))
554 rdev = NULL;
555 if (rdev)
556 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100557 if (rrdev && test_bit(Faulty, &rrdev->flags))
558 rrdev = NULL;
559 if (rrdev)
560 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700561 rcu_read_unlock();
562
NeilBrown73e92e52011-07-28 11:39:22 +1000563 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100564 * need to check for writes. We never accept write errors
565 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000566 */
567 while ((rw & WRITE) && rdev &&
568 test_bit(WriteErrorSeen, &rdev->flags)) {
569 sector_t first_bad;
570 int bad_sectors;
571 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
572 &first_bad, &bad_sectors);
573 if (!bad)
574 break;
575
576 if (bad < 0) {
577 set_bit(BlockedBadBlocks, &rdev->flags);
578 if (!conf->mddev->external &&
579 conf->mddev->flags) {
580 /* It is very unlikely, but we might
581 * still need to write out the
582 * bad block log - better give it
583 * a chance*/
584 md_check_recovery(conf->mddev);
585 }
majianpeng8d936982012-07-03 12:11:54 +1000586 /*
587 * Because md_wait_for_blocked_rdev
588 * will dec nr_pending, we must
589 * increment it first.
590 */
591 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +1000592 md_wait_for_blocked_rdev(rdev, conf->mddev);
593 } else {
594 /* Acknowledged bad block - skip the write */
595 rdev_dec_pending(rdev, conf->mddev);
596 rdev = NULL;
597 }
598 }
599
Dan Williams91c00922007-01-02 13:52:30 -0700600 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100601 if (s->syncing || s->expanding || s->expanded
602 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700603 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
604
Dan Williams2b7497f2008-06-28 08:31:52 +1000605 set_bit(STRIPE_IO_STARTED, &sh->state);
606
Dan Williams91c00922007-01-02 13:52:30 -0700607 bi->bi_bdev = rdev->bdev;
608 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700609 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700610 bi->bi_rw, i);
611 atomic_inc(&sh->count);
612 bi->bi_sector = sh->sector + rdev->data_offset;
613 bi->bi_flags = 1 << BIO_UPTODATE;
Dan Williams91c00922007-01-02 13:52:30 -0700614 bi->bi_idx = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700615 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
616 bi->bi_io_vec[0].bv_offset = 0;
617 bi->bi_size = STRIPE_SIZE;
618 bi->bi_next = NULL;
NeilBrown977df362011-12-23 10:17:53 +1100619 if (rrdev)
620 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Dan Williams91c00922007-01-02 13:52:30 -0700621 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100622 }
623 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100624 if (s->syncing || s->expanding || s->expanded
625 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +1100626 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
627
628 set_bit(STRIPE_IO_STARTED, &sh->state);
629
630 rbi->bi_bdev = rrdev->bdev;
631 pr_debug("%s: for %llu schedule op %ld on "
632 "replacement disc %d\n",
633 __func__, (unsigned long long)sh->sector,
634 rbi->bi_rw, i);
635 atomic_inc(&sh->count);
636 rbi->bi_sector = sh->sector + rrdev->data_offset;
637 rbi->bi_flags = 1 << BIO_UPTODATE;
638 rbi->bi_idx = 0;
639 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
640 rbi->bi_io_vec[0].bv_offset = 0;
641 rbi->bi_size = STRIPE_SIZE;
642 rbi->bi_next = NULL;
643 generic_make_request(rbi);
644 }
645 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000646 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700647 set_bit(STRIPE_DEGRADED, &sh->state);
648 pr_debug("skip op %ld on disc %d for sector %llu\n",
649 bi->bi_rw, i, (unsigned long long)sh->sector);
650 clear_bit(R5_LOCKED, &sh->dev[i].flags);
651 set_bit(STRIPE_HANDLE, &sh->state);
652 }
653 }
654}
655
656static struct dma_async_tx_descriptor *
657async_copy_data(int frombio, struct bio *bio, struct page *page,
658 sector_t sector, struct dma_async_tx_descriptor *tx)
659{
660 struct bio_vec *bvl;
661 struct page *bio_page;
662 int i;
663 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700664 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700665 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700666
667 if (bio->bi_sector >= sector)
668 page_offset = (signed)(bio->bi_sector - sector) * 512;
669 else
670 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700671
Dan Williams0403e382009-09-08 17:42:50 -0700672 if (frombio)
673 flags |= ASYNC_TX_FENCE;
674 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
675
Dan Williams91c00922007-01-02 13:52:30 -0700676 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000677 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700678 int clen;
679 int b_offset = 0;
680
681 if (page_offset < 0) {
682 b_offset = -page_offset;
683 page_offset += b_offset;
684 len -= b_offset;
685 }
686
687 if (len > 0 && page_offset + len > STRIPE_SIZE)
688 clen = STRIPE_SIZE - page_offset;
689 else
690 clen = len;
691
692 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000693 b_offset += bvl->bv_offset;
694 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700695 if (frombio)
696 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700697 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700698 else
699 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700700 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700701 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700702 /* chain the operations */
703 submit.depend_tx = tx;
704
Dan Williams91c00922007-01-02 13:52:30 -0700705 if (clen < len) /* hit end of page */
706 break;
707 page_offset += len;
708 }
709
710 return tx;
711}
712
713static void ops_complete_biofill(void *stripe_head_ref)
714{
715 struct stripe_head *sh = stripe_head_ref;
716 struct bio *return_bi = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +1100717 struct r5conf *conf = sh->raid_conf;
Dan Williamse4d84902007-09-24 10:06:13 -0700718 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700719
Harvey Harrisone46b2722008-04-28 02:15:50 -0700720 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700721 (unsigned long long)sh->sector);
722
723 /* clear completed biofills */
Dan Williams83de75c2008-06-28 08:31:58 +1000724 spin_lock_irq(&conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700725 for (i = sh->disks; i--; ) {
726 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700727
728 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700729 /* and check if we need to reply to a read request,
730 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000731 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700732 */
733 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700734 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700735
Dan Williams91c00922007-01-02 13:52:30 -0700736 BUG_ON(!dev->read);
737 rbi = dev->read;
738 dev->read = NULL;
739 while (rbi && rbi->bi_sector <
740 dev->sector + STRIPE_SECTORS) {
741 rbi2 = r5_next_bio(rbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +0200742 if (!raid5_dec_bi_phys_segments(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700743 rbi->bi_next = return_bi;
744 return_bi = rbi;
745 }
Dan Williams91c00922007-01-02 13:52:30 -0700746 rbi = rbi2;
747 }
748 }
749 }
Dan Williams83de75c2008-06-28 08:31:58 +1000750 spin_unlock_irq(&conf->device_lock);
751 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700752
753 return_io(return_bi);
754
Dan Williamse4d84902007-09-24 10:06:13 -0700755 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700756 release_stripe(sh);
757}
758
759static void ops_run_biofill(struct stripe_head *sh)
760{
761 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +1100762 struct r5conf *conf = sh->raid_conf;
Dan Williamsa08abd82009-06-03 11:43:59 -0700763 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700764 int i;
765
Harvey Harrisone46b2722008-04-28 02:15:50 -0700766 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700767 (unsigned long long)sh->sector);
768
769 for (i = sh->disks; i--; ) {
770 struct r5dev *dev = &sh->dev[i];
771 if (test_bit(R5_Wantfill, &dev->flags)) {
772 struct bio *rbi;
773 spin_lock_irq(&conf->device_lock);
774 dev->read = rbi = dev->toread;
775 dev->toread = NULL;
776 spin_unlock_irq(&conf->device_lock);
777 while (rbi && rbi->bi_sector <
778 dev->sector + STRIPE_SECTORS) {
779 tx = async_copy_data(0, rbi, dev->page,
780 dev->sector, tx);
781 rbi = r5_next_bio(rbi, dev->sector);
782 }
783 }
784 }
785
786 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700787 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
788 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700789}
790
Dan Williams4e7d2c02009-08-29 19:13:11 -0700791static void mark_target_uptodate(struct stripe_head *sh, int target)
792{
793 struct r5dev *tgt;
794
795 if (target < 0)
796 return;
797
798 tgt = &sh->dev[target];
799 set_bit(R5_UPTODATE, &tgt->flags);
800 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
801 clear_bit(R5_Wantcompute, &tgt->flags);
802}
803
Dan Williamsac6b53b2009-07-14 13:40:19 -0700804static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700805{
806 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700807
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
Dan Williamsac6b53b2009-07-14 13:40:19 -0700811 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700812 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700813 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700814
Dan Williamsecc65c92008-06-28 08:31:57 +1000815 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
816 if (sh->check_state == check_state_compute_run)
817 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700818 set_bit(STRIPE_HANDLE, &sh->state);
819 release_stripe(sh);
820}
821
Dan Williamsd6f38f32009-07-14 11:50:52 -0700822/* return a pointer to the address conversion region of the scribble buffer */
823static addr_conv_t *to_addr_conv(struct stripe_head *sh,
824 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700825{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700826 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
827}
828
829static struct dma_async_tx_descriptor *
830ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
831{
Dan Williams91c00922007-01-02 13:52:30 -0700832 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700833 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700834 int target = sh->ops.target;
835 struct r5dev *tgt = &sh->dev[target];
836 struct page *xor_dest = tgt->page;
837 int count = 0;
838 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700839 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700840 int i;
841
842 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700843 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700844 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
845
846 for (i = disks; i--; )
847 if (i != target)
848 xor_srcs[count++] = sh->dev[i].page;
849
850 atomic_inc(&sh->count);
851
Dan Williams0403e382009-09-08 17:42:50 -0700852 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700853 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700854 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700855 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700856 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700857 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700858
Dan Williams91c00922007-01-02 13:52:30 -0700859 return tx;
860}
861
Dan Williamsac6b53b2009-07-14 13:40:19 -0700862/* set_syndrome_sources - populate source buffers for gen_syndrome
863 * @srcs - (struct page *) array of size sh->disks
864 * @sh - stripe_head to parse
865 *
866 * Populates srcs in proper layout order for the stripe and returns the
867 * 'count' of sources to be used in a call to async_gen_syndrome. The P
868 * destination buffer is recorded in srcs[count] and the Q destination
869 * is recorded in srcs[count+1]].
870 */
871static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
872{
873 int disks = sh->disks;
874 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
875 int d0_idx = raid6_d0(sh);
876 int count;
877 int i;
878
879 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100880 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700881
882 count = 0;
883 i = d0_idx;
884 do {
885 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
886
887 srcs[slot] = sh->dev[i].page;
888 i = raid6_next_disk(i, disks);
889 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700890
NeilBrowne4424fe2009-10-16 16:27:34 +1100891 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700892}
893
894static struct dma_async_tx_descriptor *
895ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
896{
897 int disks = sh->disks;
898 struct page **blocks = percpu->scribble;
899 int target;
900 int qd_idx = sh->qd_idx;
901 struct dma_async_tx_descriptor *tx;
902 struct async_submit_ctl submit;
903 struct r5dev *tgt;
904 struct page *dest;
905 int i;
906 int count;
907
908 if (sh->ops.target < 0)
909 target = sh->ops.target2;
910 else if (sh->ops.target2 < 0)
911 target = sh->ops.target;
912 else
913 /* we should only have one valid target */
914 BUG();
915 BUG_ON(target < 0);
916 pr_debug("%s: stripe %llu block: %d\n",
917 __func__, (unsigned long long)sh->sector, target);
918
919 tgt = &sh->dev[target];
920 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
921 dest = tgt->page;
922
923 atomic_inc(&sh->count);
924
925 if (target == qd_idx) {
926 count = set_syndrome_sources(blocks, sh);
927 blocks[count] = NULL; /* regenerating p is not necessary */
928 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700929 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
930 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700931 to_addr_conv(sh, percpu));
932 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
933 } else {
934 /* Compute any data- or p-drive using XOR */
935 count = 0;
936 for (i = disks; i-- ; ) {
937 if (i == target || i == qd_idx)
938 continue;
939 blocks[count++] = sh->dev[i].page;
940 }
941
Dan Williams0403e382009-09-08 17:42:50 -0700942 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
943 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700944 to_addr_conv(sh, percpu));
945 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
946 }
947
948 return tx;
949}
950
951static struct dma_async_tx_descriptor *
952ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
953{
954 int i, count, disks = sh->disks;
955 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
956 int d0_idx = raid6_d0(sh);
957 int faila = -1, failb = -1;
958 int target = sh->ops.target;
959 int target2 = sh->ops.target2;
960 struct r5dev *tgt = &sh->dev[target];
961 struct r5dev *tgt2 = &sh->dev[target2];
962 struct dma_async_tx_descriptor *tx;
963 struct page **blocks = percpu->scribble;
964 struct async_submit_ctl submit;
965
966 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
967 __func__, (unsigned long long)sh->sector, target, target2);
968 BUG_ON(target < 0 || target2 < 0);
969 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
970 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
971
Dan Williams6c910a72009-09-16 12:24:54 -0700972 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -0700973 * slot number conversion for 'faila' and 'failb'
974 */
975 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100976 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700977 count = 0;
978 i = d0_idx;
979 do {
980 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
981
982 blocks[slot] = sh->dev[i].page;
983
984 if (i == target)
985 faila = slot;
986 if (i == target2)
987 failb = slot;
988 i = raid6_next_disk(i, disks);
989 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700990
991 BUG_ON(faila == failb);
992 if (failb < faila)
993 swap(faila, failb);
994 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
995 __func__, (unsigned long long)sh->sector, faila, failb);
996
997 atomic_inc(&sh->count);
998
999 if (failb == syndrome_disks+1) {
1000 /* Q disk is one of the missing disks */
1001 if (faila == syndrome_disks) {
1002 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001003 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1004 ops_complete_compute, sh,
1005 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +11001006 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001007 STRIPE_SIZE, &submit);
1008 } else {
1009 struct page *dest;
1010 int data_target;
1011 int qd_idx = sh->qd_idx;
1012
1013 /* Missing D+Q: recompute D from P, then recompute Q */
1014 if (target == qd_idx)
1015 data_target = target2;
1016 else
1017 data_target = target;
1018
1019 count = 0;
1020 for (i = disks; i-- ; ) {
1021 if (i == data_target || i == qd_idx)
1022 continue;
1023 blocks[count++] = sh->dev[i].page;
1024 }
1025 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001026 init_async_submit(&submit,
1027 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1028 NULL, NULL, NULL,
1029 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001030 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1031 &submit);
1032
1033 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001034 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1035 ops_complete_compute, sh,
1036 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001037 return async_gen_syndrome(blocks, 0, count+2,
1038 STRIPE_SIZE, &submit);
1039 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001040 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001041 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1042 ops_complete_compute, sh,
1043 to_addr_conv(sh, percpu));
1044 if (failb == syndrome_disks) {
1045 /* We're missing D+P. */
1046 return async_raid6_datap_recov(syndrome_disks+2,
1047 STRIPE_SIZE, faila,
1048 blocks, &submit);
1049 } else {
1050 /* We're missing D+D. */
1051 return async_raid6_2data_recov(syndrome_disks+2,
1052 STRIPE_SIZE, faila, failb,
1053 blocks, &submit);
1054 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001055 }
1056}
1057
1058
Dan Williams91c00922007-01-02 13:52:30 -07001059static void ops_complete_prexor(void *stripe_head_ref)
1060{
1061 struct stripe_head *sh = stripe_head_ref;
1062
Harvey Harrisone46b2722008-04-28 02:15:50 -07001063 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001064 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001065}
1066
1067static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001068ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1069 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001070{
Dan Williams91c00922007-01-02 13:52:30 -07001071 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001072 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001073 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001074 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001075
1076 /* existing parity data subtracted */
1077 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1078
Harvey Harrisone46b2722008-04-28 02:15:50 -07001079 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001080 (unsigned long long)sh->sector);
1081
1082 for (i = disks; i--; ) {
1083 struct r5dev *dev = &sh->dev[i];
1084 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001085 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001086 xor_srcs[count++] = dev->page;
1087 }
1088
Dan Williams0403e382009-09-08 17:42:50 -07001089 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001090 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001091 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001092
1093 return tx;
1094}
1095
1096static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001097ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001098{
1099 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001100 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001101
Harvey Harrisone46b2722008-04-28 02:15:50 -07001102 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001103 (unsigned long long)sh->sector);
1104
1105 for (i = disks; i--; ) {
1106 struct r5dev *dev = &sh->dev[i];
1107 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001108
Dan Williamsd8ee0722008-06-28 08:32:06 +10001109 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001110 struct bio *wbi;
1111
NeilBrowncbe47ec2011-07-26 11:20:35 +10001112 spin_lock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001113 chosen = dev->towrite;
1114 dev->towrite = NULL;
1115 BUG_ON(dev->written);
1116 wbi = dev->written = chosen;
NeilBrowncbe47ec2011-07-26 11:20:35 +10001117 spin_unlock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001118
1119 while (wbi && wbi->bi_sector <
1120 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001121 if (wbi->bi_rw & REQ_FUA)
1122 set_bit(R5_WantFUA, &dev->flags);
Dan Williams91c00922007-01-02 13:52:30 -07001123 tx = async_copy_data(1, wbi, dev->page,
1124 dev->sector, tx);
1125 wbi = r5_next_bio(wbi, dev->sector);
1126 }
1127 }
1128 }
1129
1130 return tx;
1131}
1132
Dan Williamsac6b53b2009-07-14 13:40:19 -07001133static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001134{
1135 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001136 int disks = sh->disks;
1137 int pd_idx = sh->pd_idx;
1138 int qd_idx = sh->qd_idx;
1139 int i;
Tejun Heoe9c74692010-09-03 11:56:18 +02001140 bool fua = false;
Dan Williams91c00922007-01-02 13:52:30 -07001141
Harvey Harrisone46b2722008-04-28 02:15:50 -07001142 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001143 (unsigned long long)sh->sector);
1144
Tejun Heoe9c74692010-09-03 11:56:18 +02001145 for (i = disks; i--; )
1146 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
1147
Dan Williams91c00922007-01-02 13:52:30 -07001148 for (i = disks; i--; ) {
1149 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001150
Tejun Heoe9c74692010-09-03 11:56:18 +02001151 if (dev->written || i == pd_idx || i == qd_idx) {
Dan Williams91c00922007-01-02 13:52:30 -07001152 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001153 if (fua)
1154 set_bit(R5_WantFUA, &dev->flags);
1155 }
Dan Williams91c00922007-01-02 13:52:30 -07001156 }
1157
Dan Williamsd8ee0722008-06-28 08:32:06 +10001158 if (sh->reconstruct_state == reconstruct_state_drain_run)
1159 sh->reconstruct_state = reconstruct_state_drain_result;
1160 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1161 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1162 else {
1163 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1164 sh->reconstruct_state = reconstruct_state_result;
1165 }
Dan Williams91c00922007-01-02 13:52:30 -07001166
1167 set_bit(STRIPE_HANDLE, &sh->state);
1168 release_stripe(sh);
1169}
1170
1171static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001172ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1173 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001174{
Dan Williams91c00922007-01-02 13:52:30 -07001175 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001176 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001177 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001178 int count = 0, pd_idx = sh->pd_idx, i;
1179 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001180 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001181 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001182
Harvey Harrisone46b2722008-04-28 02:15:50 -07001183 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001184 (unsigned long long)sh->sector);
1185
1186 /* check if prexor is active which means only process blocks
1187 * that are part of a read-modify-write (written)
1188 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001189 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1190 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001191 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1192 for (i = disks; i--; ) {
1193 struct r5dev *dev = &sh->dev[i];
1194 if (dev->written)
1195 xor_srcs[count++] = dev->page;
1196 }
1197 } else {
1198 xor_dest = sh->dev[pd_idx].page;
1199 for (i = disks; i--; ) {
1200 struct r5dev *dev = &sh->dev[i];
1201 if (i != pd_idx)
1202 xor_srcs[count++] = dev->page;
1203 }
1204 }
1205
Dan Williams91c00922007-01-02 13:52:30 -07001206 /* 1/ if we prexor'd then the dest is reused as a source
1207 * 2/ if we did not prexor then we are redoing the parity
1208 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1209 * for the synchronous xor case
1210 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001211 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001212 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1213
1214 atomic_inc(&sh->count);
1215
Dan Williamsac6b53b2009-07-14 13:40:19 -07001216 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001217 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001218 if (unlikely(count == 1))
1219 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1220 else
1221 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001222}
1223
Dan Williamsac6b53b2009-07-14 13:40:19 -07001224static void
1225ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1226 struct dma_async_tx_descriptor *tx)
1227{
1228 struct async_submit_ctl submit;
1229 struct page **blocks = percpu->scribble;
1230 int count;
1231
1232 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1233
1234 count = set_syndrome_sources(blocks, sh);
1235
1236 atomic_inc(&sh->count);
1237
1238 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1239 sh, to_addr_conv(sh, percpu));
1240 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001241}
1242
1243static void ops_complete_check(void *stripe_head_ref)
1244{
1245 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001246
Harvey Harrisone46b2722008-04-28 02:15:50 -07001247 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001248 (unsigned long long)sh->sector);
1249
Dan Williamsecc65c92008-06-28 08:31:57 +10001250 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001251 set_bit(STRIPE_HANDLE, &sh->state);
1252 release_stripe(sh);
1253}
1254
Dan Williamsac6b53b2009-07-14 13:40:19 -07001255static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001256{
Dan Williams91c00922007-01-02 13:52:30 -07001257 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001258 int pd_idx = sh->pd_idx;
1259 int qd_idx = sh->qd_idx;
1260 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001261 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001262 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001263 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001264 int count;
1265 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001266
Harvey Harrisone46b2722008-04-28 02:15:50 -07001267 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001268 (unsigned long long)sh->sector);
1269
Dan Williamsac6b53b2009-07-14 13:40:19 -07001270 count = 0;
1271 xor_dest = sh->dev[pd_idx].page;
1272 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001273 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001274 if (i == pd_idx || i == qd_idx)
1275 continue;
1276 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001277 }
1278
Dan Williamsd6f38f32009-07-14 11:50:52 -07001279 init_async_submit(&submit, 0, NULL, NULL, NULL,
1280 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001281 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001282 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001283
Dan Williams91c00922007-01-02 13:52:30 -07001284 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001285 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1286 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001287}
1288
Dan Williamsac6b53b2009-07-14 13:40:19 -07001289static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1290{
1291 struct page **srcs = percpu->scribble;
1292 struct async_submit_ctl submit;
1293 int count;
1294
1295 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1296 (unsigned long long)sh->sector, checkp);
1297
1298 count = set_syndrome_sources(srcs, sh);
1299 if (!checkp)
1300 srcs[count] = NULL;
1301
1302 atomic_inc(&sh->count);
1303 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1304 sh, to_addr_conv(sh, percpu));
1305 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1306 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1307}
1308
Dan Williams417b8d42009-10-16 16:25:22 +11001309static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001310{
1311 int overlap_clear = 0, i, disks = sh->disks;
1312 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001313 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001314 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001315 struct raid5_percpu *percpu;
1316 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001317
Dan Williamsd6f38f32009-07-14 11:50:52 -07001318 cpu = get_cpu();
1319 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001320 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001321 ops_run_biofill(sh);
1322 overlap_clear++;
1323 }
1324
Dan Williams7b3a8712008-06-28 08:32:09 +10001325 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001326 if (level < 6)
1327 tx = ops_run_compute5(sh, percpu);
1328 else {
1329 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1330 tx = ops_run_compute6_1(sh, percpu);
1331 else
1332 tx = ops_run_compute6_2(sh, percpu);
1333 }
1334 /* terminate the chain if reconstruct is not set to be run */
1335 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001336 async_tx_ack(tx);
1337 }
Dan Williams91c00922007-01-02 13:52:30 -07001338
Dan Williams600aa102008-06-28 08:32:05 +10001339 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001340 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001341
Dan Williams600aa102008-06-28 08:32:05 +10001342 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001343 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001344 overlap_clear++;
1345 }
1346
Dan Williamsac6b53b2009-07-14 13:40:19 -07001347 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1348 if (level < 6)
1349 ops_run_reconstruct5(sh, percpu, tx);
1350 else
1351 ops_run_reconstruct6(sh, percpu, tx);
1352 }
Dan Williams91c00922007-01-02 13:52:30 -07001353
Dan Williamsac6b53b2009-07-14 13:40:19 -07001354 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1355 if (sh->check_state == check_state_run)
1356 ops_run_check_p(sh, percpu);
1357 else if (sh->check_state == check_state_run_q)
1358 ops_run_check_pq(sh, percpu, 0);
1359 else if (sh->check_state == check_state_run_pq)
1360 ops_run_check_pq(sh, percpu, 1);
1361 else
1362 BUG();
1363 }
Dan Williams91c00922007-01-02 13:52:30 -07001364
Dan Williams91c00922007-01-02 13:52:30 -07001365 if (overlap_clear)
1366 for (i = disks; i--; ) {
1367 struct r5dev *dev = &sh->dev[i];
1368 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1369 wake_up(&sh->raid_conf->wait_for_overlap);
1370 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001371 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001372}
1373
Dan Williams417b8d42009-10-16 16:25:22 +11001374#ifdef CONFIG_MULTICORE_RAID456
1375static void async_run_ops(void *param, async_cookie_t cookie)
1376{
1377 struct stripe_head *sh = param;
1378 unsigned long ops_request = sh->ops.request;
1379
1380 clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
1381 wake_up(&sh->ops.wait_for_ops);
1382
1383 __raid_run_ops(sh, ops_request);
1384 release_stripe(sh);
1385}
1386
1387static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1388{
1389 /* since handle_stripe can be called outside of raid5d context
1390 * we need to ensure sh->ops.request is de-staged before another
1391 * request arrives
1392 */
1393 wait_event(sh->ops.wait_for_ops,
1394 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
1395 sh->ops.request = ops_request;
1396
1397 atomic_inc(&sh->count);
1398 async_schedule(async_run_ops, sh);
1399}
1400#else
1401#define raid_run_ops __raid_run_ops
1402#endif
1403
NeilBrownd1688a62011-10-11 16:49:52 +11001404static int grow_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
1406 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001407 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001408 if (!sh)
1409 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001410
NeilBrown3f294f42005-11-08 21:39:25 -08001411 sh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001412 #ifdef CONFIG_MULTICORE_RAID456
1413 init_waitqueue_head(&sh->ops.wait_for_ops);
1414 #endif
NeilBrown3f294f42005-11-08 21:39:25 -08001415
NeilBrowne4e11e32010-06-16 16:45:16 +10001416 if (grow_buffers(sh)) {
1417 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001418 kmem_cache_free(conf->slab_cache, sh);
1419 return 0;
1420 }
1421 /* we just created an active stripe so... */
1422 atomic_set(&sh->count, 1);
1423 atomic_inc(&conf->active_stripes);
1424 INIT_LIST_HEAD(&sh->lru);
1425 release_stripe(sh);
1426 return 1;
1427}
1428
NeilBrownd1688a62011-10-11 16:49:52 +11001429static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001430{
Christoph Lametere18b8902006-12-06 20:33:20 -08001431 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001432 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
NeilBrownf4be6b42010-06-01 19:37:25 +10001434 if (conf->mddev->gendisk)
1435 sprintf(conf->cache_name[0],
1436 "raid%d-%s", conf->level, mdname(conf->mddev));
1437 else
1438 sprintf(conf->cache_name[0],
1439 "raid%d-%p", conf->level, conf->mddev);
1440 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1441
NeilBrownad01c9e2006-03-27 01:18:07 -08001442 conf->active_name = 0;
1443 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001445 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 if (!sc)
1447 return 1;
1448 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001449 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001450 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001451 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 return 0;
1454}
NeilBrown29269552006-03-27 01:18:10 -08001455
Dan Williamsd6f38f32009-07-14 11:50:52 -07001456/**
1457 * scribble_len - return the required size of the scribble region
1458 * @num - total number of disks in the array
1459 *
1460 * The size must be enough to contain:
1461 * 1/ a struct page pointer for each device in the array +2
1462 * 2/ room to convert each entry in (1) to its corresponding dma
1463 * (dma_map_page()) or page (page_address()) address.
1464 *
1465 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1466 * calculate over all devices (not just the data blocks), using zeros in place
1467 * of the P and Q blocks.
1468 */
1469static size_t scribble_len(int num)
1470{
1471 size_t len;
1472
1473 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1474
1475 return len;
1476}
1477
NeilBrownd1688a62011-10-11 16:49:52 +11001478static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001479{
1480 /* Make all the stripes able to hold 'newsize' devices.
1481 * New slots in each stripe get 'page' set to a new page.
1482 *
1483 * This happens in stages:
1484 * 1/ create a new kmem_cache and allocate the required number of
1485 * stripe_heads.
1486 * 2/ gather all the old stripe_heads and tranfer the pages across
1487 * to the new stripe_heads. This will have the side effect of
1488 * freezing the array as once all stripe_heads have been collected,
1489 * no IO will be possible. Old stripe heads are freed once their
1490 * pages have been transferred over, and the old kmem_cache is
1491 * freed when all stripes are done.
1492 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1493 * we simple return a failre status - no need to clean anything up.
1494 * 4/ allocate new pages for the new slots in the new stripe_heads.
1495 * If this fails, we don't bother trying the shrink the
1496 * stripe_heads down again, we just leave them as they are.
1497 * As each stripe_head is processed the new one is released into
1498 * active service.
1499 *
1500 * Once step2 is started, we cannot afford to wait for a write,
1501 * so we use GFP_NOIO allocations.
1502 */
1503 struct stripe_head *osh, *nsh;
1504 LIST_HEAD(newstripes);
1505 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001506 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001507 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001508 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001509 int i;
1510
1511 if (newsize <= conf->pool_size)
1512 return 0; /* never bother to shrink */
1513
Dan Williamsb5470dc2008-06-27 21:44:04 -07001514 err = md_allow_write(conf->mddev);
1515 if (err)
1516 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001517
NeilBrownad01c9e2006-03-27 01:18:07 -08001518 /* Step 1 */
1519 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1520 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001521 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001522 if (!sc)
1523 return -ENOMEM;
1524
1525 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001526 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001527 if (!nsh)
1528 break;
1529
NeilBrownad01c9e2006-03-27 01:18:07 -08001530 nsh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001531 #ifdef CONFIG_MULTICORE_RAID456
1532 init_waitqueue_head(&nsh->ops.wait_for_ops);
1533 #endif
NeilBrownad01c9e2006-03-27 01:18:07 -08001534
1535 list_add(&nsh->lru, &newstripes);
1536 }
1537 if (i) {
1538 /* didn't get enough, give up */
1539 while (!list_empty(&newstripes)) {
1540 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1541 list_del(&nsh->lru);
1542 kmem_cache_free(sc, nsh);
1543 }
1544 kmem_cache_destroy(sc);
1545 return -ENOMEM;
1546 }
1547 /* Step 2 - Must use GFP_NOIO now.
1548 * OK, we have enough stripes, start collecting inactive
1549 * stripes and copying them over
1550 */
1551 list_for_each_entry(nsh, &newstripes, lru) {
1552 spin_lock_irq(&conf->device_lock);
1553 wait_event_lock_irq(conf->wait_for_stripe,
1554 !list_empty(&conf->inactive_list),
1555 conf->device_lock,
NeilBrown482c0832011-04-18 18:25:42 +10001556 );
NeilBrownad01c9e2006-03-27 01:18:07 -08001557 osh = get_free_stripe(conf);
1558 spin_unlock_irq(&conf->device_lock);
1559 atomic_set(&nsh->count, 1);
1560 for(i=0; i<conf->pool_size; i++)
1561 nsh->dev[i].page = osh->dev[i].page;
1562 for( ; i<newsize; i++)
1563 nsh->dev[i].page = NULL;
1564 kmem_cache_free(conf->slab_cache, osh);
1565 }
1566 kmem_cache_destroy(conf->slab_cache);
1567
1568 /* Step 3.
1569 * At this point, we are holding all the stripes so the array
1570 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001571 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001572 */
1573 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1574 if (ndisks) {
1575 for (i=0; i<conf->raid_disks; i++)
1576 ndisks[i] = conf->disks[i];
1577 kfree(conf->disks);
1578 conf->disks = ndisks;
1579 } else
1580 err = -ENOMEM;
1581
Dan Williamsd6f38f32009-07-14 11:50:52 -07001582 get_online_cpus();
1583 conf->scribble_len = scribble_len(newsize);
1584 for_each_present_cpu(cpu) {
1585 struct raid5_percpu *percpu;
1586 void *scribble;
1587
1588 percpu = per_cpu_ptr(conf->percpu, cpu);
1589 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1590
1591 if (scribble) {
1592 kfree(percpu->scribble);
1593 percpu->scribble = scribble;
1594 } else {
1595 err = -ENOMEM;
1596 break;
1597 }
1598 }
1599 put_online_cpus();
1600
NeilBrownad01c9e2006-03-27 01:18:07 -08001601 /* Step 4, return new stripes to service */
1602 while(!list_empty(&newstripes)) {
1603 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1604 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001605
NeilBrownad01c9e2006-03-27 01:18:07 -08001606 for (i=conf->raid_disks; i < newsize; i++)
1607 if (nsh->dev[i].page == NULL) {
1608 struct page *p = alloc_page(GFP_NOIO);
1609 nsh->dev[i].page = p;
1610 if (!p)
1611 err = -ENOMEM;
1612 }
1613 release_stripe(nsh);
1614 }
1615 /* critical section pass, GFP_NOIO no longer needed */
1616
1617 conf->slab_cache = sc;
1618 conf->active_name = 1-conf->active_name;
1619 conf->pool_size = newsize;
1620 return err;
1621}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
NeilBrownd1688a62011-10-11 16:49:52 +11001623static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
1625 struct stripe_head *sh;
1626
NeilBrown3f294f42005-11-08 21:39:25 -08001627 spin_lock_irq(&conf->device_lock);
1628 sh = get_free_stripe(conf);
1629 spin_unlock_irq(&conf->device_lock);
1630 if (!sh)
1631 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001632 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001633 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001634 kmem_cache_free(conf->slab_cache, sh);
1635 atomic_dec(&conf->active_stripes);
1636 return 1;
1637}
1638
NeilBrownd1688a62011-10-11 16:49:52 +11001639static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001640{
1641 while (drop_one_stripe(conf))
1642 ;
1643
NeilBrown29fc7e32006-02-03 03:03:41 -08001644 if (conf->slab_cache)
1645 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 conf->slab_cache = NULL;
1647}
1648
NeilBrown6712ecf2007-09-27 12:47:43 +02001649static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
NeilBrown99c0fb52009-03-31 14:39:38 +11001651 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001652 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001653 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001655 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11001656 struct md_rdev *rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
1659 for (i=0 ; i<disks; i++)
1660 if (bi == &sh->dev[i].req)
1661 break;
1662
Dan Williams45b42332007-07-09 11:56:43 -07001663 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1664 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 uptodate);
1666 if (i == disks) {
1667 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001668 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 }
NeilBrown14a75d32011-12-23 10:17:52 +11001670 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11001671 /* If replacement finished while this request was outstanding,
1672 * 'replacement' might be NULL already.
1673 * In that case it moved down to 'rdev'.
1674 * rdev is not removed until all requests are finished.
1675 */
NeilBrown14a75d32011-12-23 10:17:52 +11001676 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001677 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11001678 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001682 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11001683 /* Note that this cannot happen on a
1684 * replacement device. We just fail those on
1685 * any error
1686 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001687 printk_ratelimited(
1688 KERN_INFO
1689 "md/raid:%s: read error corrected"
1690 " (%lu sectors at %llu on %s)\n",
1691 mdname(conf->mddev), STRIPE_SECTORS,
1692 (unsigned long long)(sh->sector
1693 + rdev->data_offset),
1694 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001695 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001696 clear_bit(R5_ReadError, &sh->dev[i].flags);
1697 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1698 }
NeilBrown14a75d32011-12-23 10:17:52 +11001699 if (atomic_read(&rdev->read_errors))
1700 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11001702 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001703 int retry = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001706 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11001707 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1708 printk_ratelimited(
1709 KERN_WARNING
1710 "md/raid:%s: read error on replacement device "
1711 "(sector %llu on %s).\n",
1712 mdname(conf->mddev),
1713 (unsigned long long)(sh->sector
1714 + rdev->data_offset),
1715 bdn);
1716 else if (conf->mddev->degraded >= conf->max_degraded)
Christian Dietrich8bda4702011-07-27 11:00:36 +10001717 printk_ratelimited(
1718 KERN_WARNING
1719 "md/raid:%s: read error not correctable "
1720 "(sector %llu on %s).\n",
1721 mdname(conf->mddev),
1722 (unsigned long long)(sh->sector
1723 + rdev->data_offset),
1724 bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001725 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
NeilBrown4e5314b2005-11-08 21:39:22 -08001726 /* Oh, no!!! */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001727 printk_ratelimited(
1728 KERN_WARNING
1729 "md/raid:%s: read error NOT corrected!! "
1730 "(sector %llu on %s).\n",
1731 mdname(conf->mddev),
1732 (unsigned long long)(sh->sector
1733 + rdev->data_offset),
1734 bdn);
NeilBrownd6950432006-07-10 04:44:20 -07001735 else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001736 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001737 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001738 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001739 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001740 else
1741 retry = 1;
1742 if (retry)
1743 set_bit(R5_ReadError, &sh->dev[i].flags);
1744 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001745 clear_bit(R5_ReadError, &sh->dev[i].flags);
1746 clear_bit(R5_ReWrite, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001747 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 }
NeilBrown14a75d32011-12-23 10:17:52 +11001750 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1752 set_bit(STRIPE_HANDLE, &sh->state);
1753 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754}
1755
NeilBrownd710e132008-10-13 11:55:12 +11001756static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757{
NeilBrown99c0fb52009-03-31 14:39:38 +11001758 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001759 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001760 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11001761 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001763 sector_t first_bad;
1764 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11001765 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
NeilBrown977df362011-12-23 10:17:53 +11001767 for (i = 0 ; i < disks; i++) {
1768 if (bi == &sh->dev[i].req) {
1769 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 break;
NeilBrown977df362011-12-23 10:17:53 +11001771 }
1772 if (bi == &sh->dev[i].rreq) {
1773 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001774 if (rdev)
1775 replacement = 1;
1776 else
1777 /* rdev was removed and 'replacement'
1778 * replaced it. rdev is not removed
1779 * until all requests are finished.
1780 */
1781 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11001782 break;
1783 }
1784 }
Dan Williams45b42332007-07-09 11:56:43 -07001785 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1787 uptodate);
1788 if (i == disks) {
1789 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001790 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 }
1792
NeilBrown977df362011-12-23 10:17:53 +11001793 if (replacement) {
1794 if (!uptodate)
1795 md_error(conf->mddev, rdev);
1796 else if (is_badblock(rdev, sh->sector,
1797 STRIPE_SECTORS,
1798 &first_bad, &bad_sectors))
1799 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1800 } else {
1801 if (!uptodate) {
1802 set_bit(WriteErrorSeen, &rdev->flags);
1803 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11001804 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1805 set_bit(MD_RECOVERY_NEEDED,
1806 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11001807 } else if (is_badblock(rdev, sh->sector,
1808 STRIPE_SECTORS,
1809 &first_bad, &bad_sectors))
1810 set_bit(R5_MadeGood, &sh->dev[i].flags);
1811 }
1812 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
NeilBrown977df362011-12-23 10:17:53 +11001814 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1815 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001817 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818}
1819
NeilBrown784052e2009-03-31 15:19:07 +11001820static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
NeilBrown784052e2009-03-31 15:19:07 +11001822static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
1824 struct r5dev *dev = &sh->dev[i];
1825
1826 bio_init(&dev->req);
1827 dev->req.bi_io_vec = &dev->vec;
1828 dev->req.bi_vcnt++;
1829 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11001831 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
NeilBrown977df362011-12-23 10:17:53 +11001833 bio_init(&dev->rreq);
1834 dev->rreq.bi_io_vec = &dev->rvec;
1835 dev->rreq.bi_vcnt++;
1836 dev->rreq.bi_max_vecs++;
1837 dev->rreq.bi_private = sh;
1838 dev->rvec.bv_page = dev->page;
1839
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001841 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842}
1843
NeilBrownfd01b882011-10-11 16:47:53 +11001844static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845{
1846 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11001847 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11001848 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10001849 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
NeilBrown908f4fb2011-12-23 10:17:50 +11001851 spin_lock_irqsave(&conf->device_lock, flags);
1852 clear_bit(In_sync, &rdev->flags);
1853 mddev->degraded = calc_degraded(conf);
1854 spin_unlock_irqrestore(&conf->device_lock, flags);
1855 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1856
NeilBrownde393cd2011-07-28 11:31:48 +10001857 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001858 set_bit(Faulty, &rdev->flags);
1859 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1860 printk(KERN_ALERT
1861 "md/raid:%s: Disk failure on %s, disabling device.\n"
1862 "md/raid:%s: Operation continuing on %d devices.\n",
1863 mdname(mddev),
1864 bdevname(rdev->bdev, b),
1865 mdname(mddev),
1866 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001867}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869/*
1870 * Input: a 'big' sector number,
1871 * Output: index of the data and parity disk, and the sector # in them.
1872 */
NeilBrownd1688a62011-10-11 16:49:52 +11001873static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001874 int previous, int *dd_idx,
1875 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001877 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001878 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001880 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001881 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001883 int algorithm = previous ? conf->prev_algo
1884 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001885 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1886 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001887 int raid_disks = previous ? conf->previous_raid_disks
1888 : conf->raid_disks;
1889 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
1891 /* First compute the information on this sector */
1892
1893 /*
1894 * Compute the chunk number and the sector offset inside the chunk
1895 */
1896 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1897 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
1899 /*
1900 * Compute the stripe number
1901 */
NeilBrown35f2a592010-04-20 14:13:34 +10001902 stripe = chunk_number;
1903 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10001904 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 /*
1906 * Select the parity disk based on the user selected algorithm.
1907 */
NeilBrown84789552011-07-27 11:00:36 +10001908 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07001909 switch(conf->level) {
1910 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001911 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001912 break;
1913 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001914 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001916 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001917 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 (*dd_idx)++;
1919 break;
1920 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001921 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001922 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 (*dd_idx)++;
1924 break;
1925 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001926 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001927 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 break;
1929 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001930 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001931 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001933 case ALGORITHM_PARITY_0:
1934 pd_idx = 0;
1935 (*dd_idx)++;
1936 break;
1937 case ALGORITHM_PARITY_N:
1938 pd_idx = data_disks;
1939 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11001941 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001942 }
1943 break;
1944 case 6:
1945
NeilBrowne183eae2009-03-31 15:20:22 +11001946 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001947 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001948 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001949 qd_idx = pd_idx + 1;
1950 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001951 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001952 qd_idx = 0;
1953 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001954 (*dd_idx) += 2; /* D D P Q D */
1955 break;
1956 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001957 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001958 qd_idx = pd_idx + 1;
1959 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001960 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001961 qd_idx = 0;
1962 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001963 (*dd_idx) += 2; /* D D P Q D */
1964 break;
1965 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001966 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001967 qd_idx = (pd_idx + 1) % raid_disks;
1968 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001969 break;
1970 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001971 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001972 qd_idx = (pd_idx + 1) % raid_disks;
1973 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001974 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001975
1976 case ALGORITHM_PARITY_0:
1977 pd_idx = 0;
1978 qd_idx = 1;
1979 (*dd_idx) += 2;
1980 break;
1981 case ALGORITHM_PARITY_N:
1982 pd_idx = data_disks;
1983 qd_idx = data_disks + 1;
1984 break;
1985
1986 case ALGORITHM_ROTATING_ZERO_RESTART:
1987 /* Exactly the same as RIGHT_ASYMMETRIC, but or
1988 * of blocks for computing Q is different.
1989 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001990 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11001991 qd_idx = pd_idx + 1;
1992 if (pd_idx == raid_disks-1) {
1993 (*dd_idx)++; /* Q D D D P */
1994 qd_idx = 0;
1995 } else if (*dd_idx >= pd_idx)
1996 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001997 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001998 break;
1999
2000 case ALGORITHM_ROTATING_N_RESTART:
2001 /* Same a left_asymmetric, by first stripe is
2002 * D D D P Q rather than
2003 * Q D D D P
2004 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002005 stripe2 += 1;
2006 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002007 qd_idx = pd_idx + 1;
2008 if (pd_idx == raid_disks-1) {
2009 (*dd_idx)++; /* Q D D D P */
2010 qd_idx = 0;
2011 } else if (*dd_idx >= pd_idx)
2012 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002013 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002014 break;
2015
2016 case ALGORITHM_ROTATING_N_CONTINUE:
2017 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002018 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002019 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2020 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002021 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002022 break;
2023
2024 case ALGORITHM_LEFT_ASYMMETRIC_6:
2025 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002026 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002027 if (*dd_idx >= pd_idx)
2028 (*dd_idx)++;
2029 qd_idx = raid_disks - 1;
2030 break;
2031
2032 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002033 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002034 if (*dd_idx >= pd_idx)
2035 (*dd_idx)++;
2036 qd_idx = raid_disks - 1;
2037 break;
2038
2039 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002040 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002041 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2042 qd_idx = raid_disks - 1;
2043 break;
2044
2045 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002046 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002047 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2048 qd_idx = raid_disks - 1;
2049 break;
2050
2051 case ALGORITHM_PARITY_0_6:
2052 pd_idx = 0;
2053 (*dd_idx)++;
2054 qd_idx = raid_disks - 1;
2055 break;
2056
NeilBrown16a53ec2006-06-26 00:27:38 -07002057 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002058 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002059 }
2060 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 }
2062
NeilBrown911d4ee2009-03-31 14:39:38 +11002063 if (sh) {
2064 sh->pd_idx = pd_idx;
2065 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002066 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 /*
2069 * Finally, compute the new sector number
2070 */
2071 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2072 return new_sector;
2073}
2074
2075
NeilBrown784052e2009-03-31 15:19:07 +11002076static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077{
NeilBrownd1688a62011-10-11 16:49:52 +11002078 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002079 int raid_disks = sh->disks;
2080 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002082 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2083 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002084 int algorithm = previous ? conf->prev_algo
2085 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 sector_t stripe;
2087 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002088 sector_t chunk_number;
2089 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002091 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
NeilBrown16a53ec2006-06-26 00:27:38 -07002093
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2095 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
NeilBrown16a53ec2006-06-26 00:27:38 -07002097 if (i == sh->pd_idx)
2098 return 0;
2099 switch(conf->level) {
2100 case 4: break;
2101 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002102 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 case ALGORITHM_LEFT_ASYMMETRIC:
2104 case ALGORITHM_RIGHT_ASYMMETRIC:
2105 if (i > sh->pd_idx)
2106 i--;
2107 break;
2108 case ALGORITHM_LEFT_SYMMETRIC:
2109 case ALGORITHM_RIGHT_SYMMETRIC:
2110 if (i < sh->pd_idx)
2111 i += raid_disks;
2112 i -= (sh->pd_idx + 1);
2113 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002114 case ALGORITHM_PARITY_0:
2115 i -= 1;
2116 break;
2117 case ALGORITHM_PARITY_N:
2118 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002120 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002121 }
2122 break;
2123 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002124 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002125 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002126 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002127 case ALGORITHM_LEFT_ASYMMETRIC:
2128 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002129 case ALGORITHM_ROTATING_ZERO_RESTART:
2130 case ALGORITHM_ROTATING_N_RESTART:
2131 if (sh->pd_idx == raid_disks-1)
2132 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002133 else if (i > sh->pd_idx)
2134 i -= 2; /* D D P Q D */
2135 break;
2136 case ALGORITHM_LEFT_SYMMETRIC:
2137 case ALGORITHM_RIGHT_SYMMETRIC:
2138 if (sh->pd_idx == raid_disks-1)
2139 i--; /* Q D D D P */
2140 else {
2141 /* D D P Q D */
2142 if (i < sh->pd_idx)
2143 i += raid_disks;
2144 i -= (sh->pd_idx + 2);
2145 }
2146 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002147 case ALGORITHM_PARITY_0:
2148 i -= 2;
2149 break;
2150 case ALGORITHM_PARITY_N:
2151 break;
2152 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002153 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002154 if (sh->pd_idx == 0)
2155 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002156 else {
2157 /* D D Q P D */
2158 if (i < sh->pd_idx)
2159 i += raid_disks;
2160 i -= (sh->pd_idx + 1);
2161 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002162 break;
2163 case ALGORITHM_LEFT_ASYMMETRIC_6:
2164 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2165 if (i > sh->pd_idx)
2166 i--;
2167 break;
2168 case ALGORITHM_LEFT_SYMMETRIC_6:
2169 case ALGORITHM_RIGHT_SYMMETRIC_6:
2170 if (i < sh->pd_idx)
2171 i += data_disks + 1;
2172 i -= (sh->pd_idx + 1);
2173 break;
2174 case ALGORITHM_PARITY_0_6:
2175 i -= 1;
2176 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002177 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002178 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002179 }
2180 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 }
2182
2183 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002184 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
NeilBrown112bf892009-03-31 14:39:38 +11002186 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002187 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002188 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2189 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002190 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2191 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 return 0;
2193 }
2194 return r_sector;
2195}
2196
2197
Dan Williams600aa102008-06-28 08:32:05 +10002198static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002199schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002200 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002201{
2202 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002203 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002204 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002205
Dan Williamse33129d2007-01-02 13:52:30 -07002206 if (rcw) {
2207 /* if we are not expanding this is a proper write request, and
2208 * there will be bios with new data to be drained into the
2209 * stripe cache
2210 */
2211 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10002212 sh->reconstruct_state = reconstruct_state_drain_run;
2213 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2214 } else
2215 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07002216
Dan Williamsac6b53b2009-07-14 13:40:19 -07002217 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002218
2219 for (i = disks; i--; ) {
2220 struct r5dev *dev = &sh->dev[i];
2221
2222 if (dev->towrite) {
2223 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002224 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002225 if (!expand)
2226 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002227 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002228 }
2229 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002230 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002231 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002232 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002233 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002234 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002235 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2236 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2237
Dan Williamsd8ee0722008-06-28 08:32:06 +10002238 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002239 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2240 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002241 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002242
2243 for (i = disks; i--; ) {
2244 struct r5dev *dev = &sh->dev[i];
2245 if (i == pd_idx)
2246 continue;
2247
Dan Williamse33129d2007-01-02 13:52:30 -07002248 if (dev->towrite &&
2249 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002250 test_bit(R5_Wantcompute, &dev->flags))) {
2251 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002252 set_bit(R5_LOCKED, &dev->flags);
2253 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002254 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002255 }
2256 }
2257 }
2258
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002259 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002260 * are in flight
2261 */
2262 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2263 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002264 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002265
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002266 if (level == 6) {
2267 int qd_idx = sh->qd_idx;
2268 struct r5dev *dev = &sh->dev[qd_idx];
2269
2270 set_bit(R5_LOCKED, &dev->flags);
2271 clear_bit(R5_UPTODATE, &dev->flags);
2272 s->locked++;
2273 }
2274
Dan Williams600aa102008-06-28 08:32:05 +10002275 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002276 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002277 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002278}
NeilBrown16a53ec2006-06-26 00:27:38 -07002279
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280/*
2281 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002282 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 * The bi_next chain must be in order.
2284 */
2285static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2286{
2287 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002288 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002289 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
NeilBrowncbe47ec2011-07-26 11:20:35 +10002291 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 (unsigned long long)bi->bi_sector,
2293 (unsigned long long)sh->sector);
2294
2295
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07002297 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -07002299 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
2300 firstwrite = 1;
2301 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 bip = &sh->dev[dd_idx].toread;
2303 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2304 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2305 goto overlap;
2306 bip = & (*bip)->bi_next;
2307 }
2308 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2309 goto overlap;
2310
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002311 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 if (*bip)
2313 bi->bi_next = *bip;
2314 *bip = bi;
Jens Axboe960e7392008-08-15 10:41:18 +02002315 bi->bi_phys_segments++;
NeilBrown72626682005-09-09 16:23:54 -07002316
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 if (forwrite) {
2318 /* check if page is covered */
2319 sector_t sector = sh->dev[dd_idx].sector;
2320 for (bi=sh->dev[dd_idx].towrite;
2321 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2322 bi && bi->bi_sector <= sector;
2323 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2324 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2325 sector = bi->bi_sector + (bi->bi_size>>9);
2326 }
2327 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2328 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2329 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002330 spin_unlock_irq(&conf->device_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002331
2332 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2333 (unsigned long long)(*bip)->bi_sector,
2334 (unsigned long long)sh->sector, dd_idx);
2335
2336 if (conf->mddev->bitmap && firstwrite) {
2337 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2338 STRIPE_SECTORS, 0);
2339 sh->bm_seq = conf->seq_flush+1;
2340 set_bit(STRIPE_BIT_DELAY, &sh->state);
2341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 return 1;
2343
2344 overlap:
2345 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
2346 spin_unlock_irq(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 return 0;
2348}
2349
NeilBrownd1688a62011-10-11 16:49:52 +11002350static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002351
NeilBrownd1688a62011-10-11 16:49:52 +11002352static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002353 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002354{
NeilBrown784052e2009-03-31 15:19:07 +11002355 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002356 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002357 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002358 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002359 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002360
NeilBrown112bf892009-03-31 14:39:38 +11002361 raid5_compute_sector(conf,
2362 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002363 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002364 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002365 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002366}
2367
Dan Williamsa4456852007-07-09 11:56:43 -07002368static void
NeilBrownd1688a62011-10-11 16:49:52 +11002369handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002370 struct stripe_head_state *s, int disks,
2371 struct bio **return_bi)
2372{
2373 int i;
2374 for (i = disks; i--; ) {
2375 struct bio *bi;
2376 int bitmap_end = 0;
2377
2378 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002379 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002380 rcu_read_lock();
2381 rdev = rcu_dereference(conf->disks[i].rdev);
2382 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002383 atomic_inc(&rdev->nr_pending);
2384 else
2385 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002386 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002387 if (rdev) {
2388 if (!rdev_set_badblocks(
2389 rdev,
2390 sh->sector,
2391 STRIPE_SECTORS, 0))
2392 md_error(conf->mddev, rdev);
2393 rdev_dec_pending(rdev, conf->mddev);
2394 }
Dan Williamsa4456852007-07-09 11:56:43 -07002395 }
2396 spin_lock_irq(&conf->device_lock);
2397 /* fail all writes first */
2398 bi = sh->dev[i].towrite;
2399 sh->dev[i].towrite = NULL;
2400 if (bi) {
2401 s->to_write--;
2402 bitmap_end = 1;
2403 }
2404
2405 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2406 wake_up(&conf->wait_for_overlap);
2407
2408 while (bi && bi->bi_sector <
2409 sh->dev[i].sector + STRIPE_SECTORS) {
2410 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2411 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002412 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002413 md_write_end(conf->mddev);
2414 bi->bi_next = *return_bi;
2415 *return_bi = bi;
2416 }
2417 bi = nextbi;
2418 }
2419 /* and fail all 'written' */
2420 bi = sh->dev[i].written;
2421 sh->dev[i].written = NULL;
2422 if (bi) bitmap_end = 1;
2423 while (bi && bi->bi_sector <
2424 sh->dev[i].sector + STRIPE_SECTORS) {
2425 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2426 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002427 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002428 md_write_end(conf->mddev);
2429 bi->bi_next = *return_bi;
2430 *return_bi = bi;
2431 }
2432 bi = bi2;
2433 }
2434
Dan Williamsb5e98d62007-01-02 13:52:31 -07002435 /* fail any reads if this device is non-operational and
2436 * the data has not reached the cache yet.
2437 */
2438 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2439 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2440 test_bit(R5_ReadError, &sh->dev[i].flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002441 bi = sh->dev[i].toread;
2442 sh->dev[i].toread = NULL;
2443 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2444 wake_up(&conf->wait_for_overlap);
2445 if (bi) s->to_read--;
2446 while (bi && bi->bi_sector <
2447 sh->dev[i].sector + STRIPE_SECTORS) {
2448 struct bio *nextbi =
2449 r5_next_bio(bi, sh->dev[i].sector);
2450 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002451 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002452 bi->bi_next = *return_bi;
2453 *return_bi = bi;
2454 }
2455 bi = nextbi;
2456 }
2457 }
2458 spin_unlock_irq(&conf->device_lock);
2459 if (bitmap_end)
2460 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2461 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002462 /* If we were in the middle of a write the parity block might
2463 * still be locked - so just clear all R5_LOCKED flags
2464 */
2465 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002466 }
2467
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002468 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2469 if (atomic_dec_and_test(&conf->pending_full_writes))
2470 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002471}
2472
NeilBrown7f0da592011-07-28 11:39:22 +10002473static void
NeilBrownd1688a62011-10-11 16:49:52 +11002474handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002475 struct stripe_head_state *s)
2476{
2477 int abort = 0;
2478 int i;
2479
NeilBrown7f0da592011-07-28 11:39:22 +10002480 clear_bit(STRIPE_SYNCING, &sh->state);
2481 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002482 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002483 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10002484 * Don't even need to abort as that is handled elsewhere
2485 * if needed, and not always wanted e.g. if there is a known
2486 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11002487 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002488 * non-sync devices, or abort the recovery
2489 */
NeilBrown18b98372012-04-01 23:48:38 +10002490 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2491 /* During recovery devices cannot be removed, so
2492 * locking and refcounting of rdevs is not needed
2493 */
2494 for (i = 0; i < conf->raid_disks; i++) {
2495 struct md_rdev *rdev = conf->disks[i].rdev;
2496 if (rdev
2497 && !test_bit(Faulty, &rdev->flags)
2498 && !test_bit(In_sync, &rdev->flags)
2499 && !rdev_set_badblocks(rdev, sh->sector,
2500 STRIPE_SECTORS, 0))
2501 abort = 1;
2502 rdev = conf->disks[i].replacement;
2503 if (rdev
2504 && !test_bit(Faulty, &rdev->flags)
2505 && !test_bit(In_sync, &rdev->flags)
2506 && !rdev_set_badblocks(rdev, sh->sector,
2507 STRIPE_SECTORS, 0))
2508 abort = 1;
2509 }
2510 if (abort)
2511 conf->recovery_disabled =
2512 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10002513 }
NeilBrown18b98372012-04-01 23:48:38 +10002514 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10002515}
2516
NeilBrown9a3e1102011-12-23 10:17:53 +11002517static int want_replace(struct stripe_head *sh, int disk_idx)
2518{
2519 struct md_rdev *rdev;
2520 int rv = 0;
2521 /* Doing recovery so rcu locking not required */
2522 rdev = sh->raid_conf->disks[disk_idx].replacement;
2523 if (rdev
2524 && !test_bit(Faulty, &rdev->flags)
2525 && !test_bit(In_sync, &rdev->flags)
2526 && (rdev->recovery_offset <= sh->sector
2527 || rdev->mddev->recovery_cp <= sh->sector))
2528 rv = 1;
2529
2530 return rv;
2531}
2532
NeilBrown93b3dbc2011-07-27 11:00:36 +10002533/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002534 * to be read or computed to satisfy a request.
2535 *
2536 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002537 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002538 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002539static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2540 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002541{
2542 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002543 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2544 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002545
Dan Williamsf38e1212007-01-02 13:52:30 -07002546 /* is the data in this block needed, and can we get it? */
2547 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002548 !test_bit(R5_UPTODATE, &dev->flags) &&
2549 (dev->toread ||
2550 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2551 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002552 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002553 (s->failed >= 1 && fdev[0]->toread) ||
2554 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002555 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2556 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2557 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002558 /* we would like to get this block, possibly by computing it,
2559 * otherwise read it if the backing disk is insync
2560 */
2561 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2562 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2563 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002564 (s->failed && (disk_idx == s->failed_num[0] ||
2565 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002566 /* have disk failed, and we're requested to fetch it;
2567 * do compute it
2568 */
2569 pr_debug("Computing stripe %llu block %d\n",
2570 (unsigned long long)sh->sector, disk_idx);
2571 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2572 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2573 set_bit(R5_Wantcompute, &dev->flags);
2574 sh->ops.target = disk_idx;
2575 sh->ops.target2 = -1; /* no 2nd target */
2576 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002577 /* Careful: from this point on 'uptodate' is in the eye
2578 * of raid_run_ops which services 'compute' operations
2579 * before writes. R5_Wantcompute flags a block that will
2580 * be R5_UPTODATE by the time it is needed for a
2581 * subsequent operation.
2582 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002583 s->uptodate++;
2584 return 1;
2585 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2586 /* Computing 2-failure is *very* expensive; only
2587 * do it if failed >= 2
2588 */
2589 int other;
2590 for (other = disks; other--; ) {
2591 if (other == disk_idx)
2592 continue;
2593 if (!test_bit(R5_UPTODATE,
2594 &sh->dev[other].flags))
2595 break;
2596 }
2597 BUG_ON(other < 0);
2598 pr_debug("Computing stripe %llu blocks %d,%d\n",
2599 (unsigned long long)sh->sector,
2600 disk_idx, other);
2601 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2602 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2603 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2604 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2605 sh->ops.target = disk_idx;
2606 sh->ops.target2 = other;
2607 s->uptodate += 2;
2608 s->req_compute = 1;
2609 return 1;
2610 } else if (test_bit(R5_Insync, &dev->flags)) {
2611 set_bit(R5_LOCKED, &dev->flags);
2612 set_bit(R5_Wantread, &dev->flags);
2613 s->locked++;
2614 pr_debug("Reading block %d (sync=%d)\n",
2615 disk_idx, s->syncing);
2616 }
2617 }
2618
2619 return 0;
2620}
2621
2622/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002623 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002624 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002625static void handle_stripe_fill(struct stripe_head *sh,
2626 struct stripe_head_state *s,
2627 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002628{
2629 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002630
2631 /* look for blocks to read/compute, skip this if a compute
2632 * is already in flight, or if the stripe contents are in the
2633 * midst of changing due to a write
2634 */
2635 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2636 !sh->reconstruct_state)
2637 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002638 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002639 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002640 set_bit(STRIPE_HANDLE, &sh->state);
2641}
2642
2643
Dan Williams1fe797e2008-06-28 09:16:30 +10002644/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002645 * any written block on an uptodate or failed drive can be returned.
2646 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2647 * never LOCKED, so we don't need to test 'failed' directly.
2648 */
NeilBrownd1688a62011-10-11 16:49:52 +11002649static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002650 struct stripe_head *sh, int disks, struct bio **return_bi)
2651{
2652 int i;
2653 struct r5dev *dev;
2654
2655 for (i = disks; i--; )
2656 if (sh->dev[i].written) {
2657 dev = &sh->dev[i];
2658 if (!test_bit(R5_LOCKED, &dev->flags) &&
2659 test_bit(R5_UPTODATE, &dev->flags)) {
2660 /* We can return any write requests */
2661 struct bio *wbi, *wbi2;
2662 int bitmap_end = 0;
Dan Williams45b42332007-07-09 11:56:43 -07002663 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002664 spin_lock_irq(&conf->device_lock);
2665 wbi = dev->written;
2666 dev->written = NULL;
2667 while (wbi && wbi->bi_sector <
2668 dev->sector + STRIPE_SECTORS) {
2669 wbi2 = r5_next_bio(wbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +02002670 if (!raid5_dec_bi_phys_segments(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002671 md_write_end(conf->mddev);
2672 wbi->bi_next = *return_bi;
2673 *return_bi = wbi;
2674 }
2675 wbi = wbi2;
2676 }
2677 if (dev->towrite == NULL)
2678 bitmap_end = 1;
2679 spin_unlock_irq(&conf->device_lock);
2680 if (bitmap_end)
2681 bitmap_endwrite(conf->mddev->bitmap,
2682 sh->sector,
2683 STRIPE_SECTORS,
2684 !test_bit(STRIPE_DEGRADED, &sh->state),
2685 0);
2686 }
2687 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002688
2689 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2690 if (atomic_dec_and_test(&conf->pending_full_writes))
2691 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002692}
2693
NeilBrownd1688a62011-10-11 16:49:52 +11002694static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002695 struct stripe_head *sh,
2696 struct stripe_head_state *s,
2697 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002698{
2699 int rmw = 0, rcw = 0, i;
NeilBrownc8ac1802011-07-27 11:00:36 +10002700 if (conf->max_degraded == 2) {
2701 /* RAID6 requires 'rcw' in current implementation
2702 * Calculate the real rcw later - for now fake it
2703 * look like rcw is cheaper
2704 */
2705 rcw = 1; rmw = 2;
2706 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002707 /* would I have to read this buffer for read_modify_write */
2708 struct r5dev *dev = &sh->dev[i];
2709 if ((dev->towrite || i == sh->pd_idx) &&
2710 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002711 !(test_bit(R5_UPTODATE, &dev->flags) ||
2712 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002713 if (test_bit(R5_Insync, &dev->flags))
2714 rmw++;
2715 else
2716 rmw += 2*disks; /* cannot read it */
2717 }
2718 /* Would I have to read this buffer for reconstruct_write */
2719 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2720 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002721 !(test_bit(R5_UPTODATE, &dev->flags) ||
2722 test_bit(R5_Wantcompute, &dev->flags))) {
2723 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002724 else
2725 rcw += 2*disks;
2726 }
2727 }
Dan Williams45b42332007-07-09 11:56:43 -07002728 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002729 (unsigned long long)sh->sector, rmw, rcw);
2730 set_bit(STRIPE_HANDLE, &sh->state);
2731 if (rmw < rcw && rmw > 0)
2732 /* prefer read-modify-write, but need to get some data */
2733 for (i = disks; i--; ) {
2734 struct r5dev *dev = &sh->dev[i];
2735 if ((dev->towrite || i == sh->pd_idx) &&
2736 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002737 !(test_bit(R5_UPTODATE, &dev->flags) ||
2738 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002739 test_bit(R5_Insync, &dev->flags)) {
2740 if (
2741 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002742 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002743 "%d for r-m-w\n", i);
2744 set_bit(R5_LOCKED, &dev->flags);
2745 set_bit(R5_Wantread, &dev->flags);
2746 s->locked++;
2747 } else {
2748 set_bit(STRIPE_DELAYED, &sh->state);
2749 set_bit(STRIPE_HANDLE, &sh->state);
2750 }
2751 }
2752 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002753 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002754 /* want reconstruct write, but need to get some data */
NeilBrownc8ac1802011-07-27 11:00:36 +10002755 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002756 for (i = disks; i--; ) {
2757 struct r5dev *dev = &sh->dev[i];
2758 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002759 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002760 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002761 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002762 test_bit(R5_Wantcompute, &dev->flags))) {
2763 rcw++;
2764 if (!test_bit(R5_Insync, &dev->flags))
2765 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002766 if (
2767 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002768 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002769 "%d for Reconstruct\n", i);
2770 set_bit(R5_LOCKED, &dev->flags);
2771 set_bit(R5_Wantread, &dev->flags);
2772 s->locked++;
2773 } else {
2774 set_bit(STRIPE_DELAYED, &sh->state);
2775 set_bit(STRIPE_HANDLE, &sh->state);
2776 }
2777 }
2778 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002779 }
Dan Williamsa4456852007-07-09 11:56:43 -07002780 /* now if nothing is locked, and if we have enough data,
2781 * we can start a write request
2782 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002783 /* since handle_stripe can be called at any time we need to handle the
2784 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002785 * subsequent call wants to start a write request. raid_run_ops only
2786 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002787 * simultaneously. If this is not the case then new writes need to be
2788 * held off until the compute completes.
2789 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002790 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2791 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2792 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002793 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002794}
2795
NeilBrownd1688a62011-10-11 16:49:52 +11002796static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002797 struct stripe_head_state *s, int disks)
2798{
Dan Williamsecc65c92008-06-28 08:31:57 +10002799 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002800
Dan Williamsbd2ab672008-04-10 21:29:27 -07002801 set_bit(STRIPE_HANDLE, &sh->state);
2802
Dan Williamsecc65c92008-06-28 08:31:57 +10002803 switch (sh->check_state) {
2804 case check_state_idle:
2805 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002806 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002807 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002808 sh->check_state = check_state_run;
2809 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002810 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002811 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002812 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002813 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002814 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002815 /* fall through */
2816 case check_state_compute_result:
2817 sh->check_state = check_state_idle;
2818 if (!dev)
2819 dev = &sh->dev[sh->pd_idx];
2820
2821 /* check that a write has not made the stripe insync */
2822 if (test_bit(STRIPE_INSYNC, &sh->state))
2823 break;
2824
2825 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002826 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2827 BUG_ON(s->uptodate != disks);
2828
2829 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002830 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002831 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002832
Dan Williamsa4456852007-07-09 11:56:43 -07002833 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002834 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002835 break;
2836 case check_state_run:
2837 break; /* we will be called again upon completion */
2838 case check_state_check_result:
2839 sh->check_state = check_state_idle;
2840
2841 /* if a failure occurred during the check operation, leave
2842 * STRIPE_INSYNC not set and let the stripe be handled again
2843 */
2844 if (s->failed)
2845 break;
2846
2847 /* handle a successful check operation, if parity is correct
2848 * we are done. Otherwise update the mismatch count and repair
2849 * parity if !MD_RECOVERY_CHECK
2850 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002851 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002852 /* parity is correct (on disc,
2853 * not in buffer any more)
2854 */
2855 set_bit(STRIPE_INSYNC, &sh->state);
2856 else {
2857 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2858 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2859 /* don't try to repair!! */
2860 set_bit(STRIPE_INSYNC, &sh->state);
2861 else {
2862 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002863 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002864 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2865 set_bit(R5_Wantcompute,
2866 &sh->dev[sh->pd_idx].flags);
2867 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002868 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10002869 s->uptodate++;
2870 }
2871 }
2872 break;
2873 case check_state_compute_run:
2874 break;
2875 default:
2876 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2877 __func__, sh->check_state,
2878 (unsigned long long) sh->sector);
2879 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002880 }
2881}
2882
2883
NeilBrownd1688a62011-10-11 16:49:52 +11002884static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07002885 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10002886 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002887{
Dan Williamsa4456852007-07-09 11:56:43 -07002888 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11002889 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07002890 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07002891
2892 set_bit(STRIPE_HANDLE, &sh->state);
2893
2894 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002895
Dan Williamsa4456852007-07-09 11:56:43 -07002896 /* Want to check and possibly repair P and Q.
2897 * However there could be one 'failed' device, in which
2898 * case we can only check one of them, possibly using the
2899 * other to generate missing data
2900 */
2901
Dan Williamsd82dfee2009-07-14 13:40:57 -07002902 switch (sh->check_state) {
2903 case check_state_idle:
2904 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10002905 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002906 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07002907 * makes sense to check P (If anything else were failed,
2908 * we would have used P to recreate it).
2909 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002910 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07002911 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002912 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002913 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07002914 * anything, so it makes sense to check it
2915 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002916 if (sh->check_state == check_state_run)
2917 sh->check_state = check_state_run_pq;
2918 else
2919 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07002920 }
Dan Williams36d1c642009-07-14 11:48:22 -07002921
Dan Williamsd82dfee2009-07-14 13:40:57 -07002922 /* discard potentially stale zero_sum_result */
2923 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07002924
Dan Williamsd82dfee2009-07-14 13:40:57 -07002925 if (sh->check_state == check_state_run) {
2926 /* async_xor_zero_sum destroys the contents of P */
2927 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
2928 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07002929 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002930 if (sh->check_state >= check_state_run &&
2931 sh->check_state <= check_state_run_pq) {
2932 /* async_syndrome_zero_sum preserves P and Q, so
2933 * no need to mark them !uptodate here
2934 */
2935 set_bit(STRIPE_OP_CHECK, &s->ops_request);
2936 break;
2937 }
Dan Williams36d1c642009-07-14 11:48:22 -07002938
Dan Williamsd82dfee2009-07-14 13:40:57 -07002939 /* we have 2-disk failure */
2940 BUG_ON(s->failed != 2);
2941 /* fall through */
2942 case check_state_compute_result:
2943 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07002944
Dan Williamsd82dfee2009-07-14 13:40:57 -07002945 /* check that a write has not made the stripe insync */
2946 if (test_bit(STRIPE_INSYNC, &sh->state))
2947 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002948
2949 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07002950 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07002951 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002952 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07002953 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10002954 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07002955 s->locked++;
2956 set_bit(R5_LOCKED, &dev->flags);
2957 set_bit(R5_Wantwrite, &dev->flags);
2958 }
2959 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10002960 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07002961 s->locked++;
2962 set_bit(R5_LOCKED, &dev->flags);
2963 set_bit(R5_Wantwrite, &dev->flags);
2964 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002965 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002966 dev = &sh->dev[pd_idx];
2967 s->locked++;
2968 set_bit(R5_LOCKED, &dev->flags);
2969 set_bit(R5_Wantwrite, &dev->flags);
2970 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002971 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002972 dev = &sh->dev[qd_idx];
2973 s->locked++;
2974 set_bit(R5_LOCKED, &dev->flags);
2975 set_bit(R5_Wantwrite, &dev->flags);
2976 }
2977 clear_bit(STRIPE_DEGRADED, &sh->state);
2978
2979 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002980 break;
2981 case check_state_run:
2982 case check_state_run_q:
2983 case check_state_run_pq:
2984 break; /* we will be called again upon completion */
2985 case check_state_check_result:
2986 sh->check_state = check_state_idle;
2987
2988 /* handle a successful check operation, if parity is correct
2989 * we are done. Otherwise update the mismatch count and repair
2990 * parity if !MD_RECOVERY_CHECK
2991 */
2992 if (sh->ops.zero_sum_result == 0) {
2993 /* both parities are correct */
2994 if (!s->failed)
2995 set_bit(STRIPE_INSYNC, &sh->state);
2996 else {
2997 /* in contrast to the raid5 case we can validate
2998 * parity, but still have a failure to write
2999 * back
3000 */
3001 sh->check_state = check_state_compute_result;
3002 /* Returning at this point means that we may go
3003 * off and bring p and/or q uptodate again so
3004 * we make sure to check zero_sum_result again
3005 * to verify if p or q need writeback
3006 */
3007 }
3008 } else {
3009 conf->mddev->resync_mismatches += STRIPE_SECTORS;
3010 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3011 /* don't try to repair!! */
3012 set_bit(STRIPE_INSYNC, &sh->state);
3013 else {
3014 int *target = &sh->ops.target;
3015
3016 sh->ops.target = -1;
3017 sh->ops.target2 = -1;
3018 sh->check_state = check_state_compute_run;
3019 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3020 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3021 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3022 set_bit(R5_Wantcompute,
3023 &sh->dev[pd_idx].flags);
3024 *target = pd_idx;
3025 target = &sh->ops.target2;
3026 s->uptodate++;
3027 }
3028 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3029 set_bit(R5_Wantcompute,
3030 &sh->dev[qd_idx].flags);
3031 *target = qd_idx;
3032 s->uptodate++;
3033 }
3034 }
3035 }
3036 break;
3037 case check_state_compute_run:
3038 break;
3039 default:
3040 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3041 __func__, sh->check_state,
3042 (unsigned long long) sh->sector);
3043 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003044 }
3045}
3046
NeilBrownd1688a62011-10-11 16:49:52 +11003047static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003048{
3049 int i;
3050
3051 /* We have read all the blocks in this stripe and now we need to
3052 * copy some of them into a target stripe for expand.
3053 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003054 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003055 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3056 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003057 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003058 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003059 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003060 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003061
NeilBrown784052e2009-03-31 15:19:07 +11003062 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003063 sector_t s = raid5_compute_sector(conf, bn, 0,
3064 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003065 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003066 if (sh2 == NULL)
3067 /* so far only the early blocks of this stripe
3068 * have been requested. When later blocks
3069 * get requested, we will try again
3070 */
3071 continue;
3072 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3073 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3074 /* must have already done this block */
3075 release_stripe(sh2);
3076 continue;
3077 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003078
3079 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003080 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003081 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003082 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003083 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003084
Dan Williamsa4456852007-07-09 11:56:43 -07003085 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3086 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3087 for (j = 0; j < conf->raid_disks; j++)
3088 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003089 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003090 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3091 break;
3092 if (j == conf->raid_disks) {
3093 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3094 set_bit(STRIPE_HANDLE, &sh2->state);
3095 }
3096 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003097
Dan Williamsa4456852007-07-09 11:56:43 -07003098 }
NeilBrowna2e08552007-09-11 15:23:36 -07003099 /* done submitting copies, wait for them to complete */
3100 if (tx) {
3101 async_tx_ack(tx);
3102 dma_wait_for_async_tx(tx);
3103 }
Dan Williamsa4456852007-07-09 11:56:43 -07003104}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105
3106/*
3107 * handle_stripe - do things to a stripe.
3108 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003109 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3110 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003112 * return some read requests which now have data
3113 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 * schedule a read on some buffers
3115 * schedule a write of some buffers
3116 * return confirmation of parity correctness
3117 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 */
Dan Williamsa4456852007-07-09 11:56:43 -07003119
NeilBrownacfe7262011-07-27 11:00:36 +10003120static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003121{
NeilBrownd1688a62011-10-11 16:49:52 +11003122 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003123 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003124 struct r5dev *dev;
3125 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003126 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003127
NeilBrownacfe7262011-07-27 11:00:36 +10003128 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003129
NeilBrownacfe7262011-07-27 11:00:36 +10003130 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3131 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3132 s->failed_num[0] = -1;
3133 s->failed_num[1] = -1;
3134
3135 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003136 rcu_read_lock();
NeilBrownc4c16632011-07-26 11:34:20 +10003137 spin_lock_irq(&conf->device_lock);
NeilBrown16a53ec2006-06-26 00:27:38 -07003138 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003139 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003140 sector_t first_bad;
3141 int bad_sectors;
3142 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003143
NeilBrown16a53ec2006-06-26 00:27:38 -07003144 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003145
Dan Williams45b42332007-07-09 11:56:43 -07003146 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003147 i, dev->flags,
3148 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003149 /* maybe we can reply to a read
3150 *
3151 * new wantfill requests are only permitted while
3152 * ops_complete_biofill is guaranteed to be inactive
3153 */
3154 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3155 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3156 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003157
3158 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003159 if (test_bit(R5_LOCKED, &dev->flags))
3160 s->locked++;
3161 if (test_bit(R5_UPTODATE, &dev->flags))
3162 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003163 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003164 s->compute++;
3165 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003166 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003167
NeilBrownacfe7262011-07-27 11:00:36 +10003168 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003169 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003170 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003171 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003172 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003173 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003174 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003175 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003176 }
Dan Williamsa4456852007-07-09 11:56:43 -07003177 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003178 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003179 /* Prefer to use the replacement for reads, but only
3180 * if it is recovered enough and has no bad blocks.
3181 */
3182 rdev = rcu_dereference(conf->disks[i].replacement);
3183 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3184 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3185 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3186 &first_bad, &bad_sectors))
3187 set_bit(R5_ReadRepl, &dev->flags);
3188 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003189 if (rdev)
3190 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003191 rdev = rcu_dereference(conf->disks[i].rdev);
3192 clear_bit(R5_ReadRepl, &dev->flags);
3193 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003194 if (rdev && test_bit(Faulty, &rdev->flags))
3195 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003196 if (rdev) {
3197 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3198 &first_bad, &bad_sectors);
3199 if (s->blocked_rdev == NULL
3200 && (test_bit(Blocked, &rdev->flags)
3201 || is_bad < 0)) {
3202 if (is_bad < 0)
3203 set_bit(BlockedBadBlocks,
3204 &rdev->flags);
3205 s->blocked_rdev = rdev;
3206 atomic_inc(&rdev->nr_pending);
3207 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003208 }
NeilBrown415e72d2010-06-17 17:25:21 +10003209 clear_bit(R5_Insync, &dev->flags);
3210 if (!rdev)
3211 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003212 else if (is_bad) {
3213 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10003214 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3215 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003216 /* treat as in-sync, but with a read error
3217 * which we can now try to correct
3218 */
3219 set_bit(R5_Insync, &dev->flags);
3220 set_bit(R5_ReadError, &dev->flags);
3221 }
3222 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003223 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003224 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003225 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003226 set_bit(R5_Insync, &dev->flags);
3227 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3228 test_bit(R5_Expanded, &dev->flags))
3229 /* If we've reshaped into here, we assume it is Insync.
3230 * We will shortly update recovery_offset to make
3231 * it official.
3232 */
3233 set_bit(R5_Insync, &dev->flags);
3234
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003235 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003236 /* This flag does not apply to '.replacement'
3237 * only to .rdev, so make sure to check that*/
3238 struct md_rdev *rdev2 = rcu_dereference(
3239 conf->disks[i].rdev);
3240 if (rdev2 == rdev)
3241 clear_bit(R5_Insync, &dev->flags);
3242 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003243 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003244 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003245 } else
3246 clear_bit(R5_WriteError, &dev->flags);
3247 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003248 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003249 /* This flag does not apply to '.replacement'
3250 * only to .rdev, so make sure to check that*/
3251 struct md_rdev *rdev2 = rcu_dereference(
3252 conf->disks[i].rdev);
3253 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003254 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003255 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003256 } else
3257 clear_bit(R5_MadeGood, &dev->flags);
3258 }
NeilBrown977df362011-12-23 10:17:53 +11003259 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3260 struct md_rdev *rdev2 = rcu_dereference(
3261 conf->disks[i].replacement);
3262 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3263 s->handle_bad_blocks = 1;
3264 atomic_inc(&rdev2->nr_pending);
3265 } else
3266 clear_bit(R5_MadeGoodRepl, &dev->flags);
3267 }
NeilBrown415e72d2010-06-17 17:25:21 +10003268 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003269 /* The ReadError flag will just be confusing now */
3270 clear_bit(R5_ReadError, &dev->flags);
3271 clear_bit(R5_ReWrite, &dev->flags);
3272 }
NeilBrown415e72d2010-06-17 17:25:21 +10003273 if (test_bit(R5_ReadError, &dev->flags))
3274 clear_bit(R5_Insync, &dev->flags);
3275 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003276 if (s->failed < 2)
3277 s->failed_num[s->failed] = i;
3278 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003279 if (rdev && !test_bit(Faulty, &rdev->flags))
3280 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003281 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003282 }
NeilBrownc4c16632011-07-26 11:34:20 +10003283 spin_unlock_irq(&conf->device_lock);
NeilBrown9a3e1102011-12-23 10:17:53 +11003284 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3285 /* If there is a failed device being replaced,
3286 * we must be recovering.
3287 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10003288 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11003289 * else we can only be replacing
3290 * sync and recovery both need to read all devices, and so
3291 * use the same flag.
3292 */
3293 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10003294 sh->sector >= conf->mddev->recovery_cp ||
3295 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11003296 s->syncing = 1;
3297 else
3298 s->replacing = 1;
3299 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003300 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003301}
NeilBrownf4168852007-02-28 20:11:53 -08003302
NeilBrowncc940152011-07-26 11:35:35 +10003303static void handle_stripe(struct stripe_head *sh)
3304{
3305 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003306 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003307 int i;
NeilBrown84789552011-07-27 11:00:36 +10003308 int prexor;
3309 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003310 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003311
3312 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003313 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003314 /* already being handled, ensure it gets handled
3315 * again when current action finishes */
3316 set_bit(STRIPE_HANDLE, &sh->state);
3317 return;
3318 }
3319
3320 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3321 set_bit(STRIPE_SYNCING, &sh->state);
3322 clear_bit(STRIPE_INSYNC, &sh->state);
3323 }
3324 clear_bit(STRIPE_DELAYED, &sh->state);
3325
3326 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3327 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3328 (unsigned long long)sh->sector, sh->state,
3329 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3330 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003331
NeilBrownacfe7262011-07-27 11:00:36 +10003332 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003333
NeilBrownbc2607f2011-07-28 11:39:22 +10003334 if (s.handle_bad_blocks) {
3335 set_bit(STRIPE_HANDLE, &sh->state);
3336 goto finish;
3337 }
3338
NeilBrown474af965fe2011-07-27 11:00:36 +10003339 if (unlikely(s.blocked_rdev)) {
3340 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003341 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003342 set_bit(STRIPE_HANDLE, &sh->state);
3343 goto finish;
3344 }
3345 /* There is nothing for the blocked_rdev to block */
3346 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3347 s.blocked_rdev = NULL;
3348 }
3349
3350 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3351 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3352 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3353 }
3354
3355 pr_debug("locked=%d uptodate=%d to_read=%d"
3356 " to_write=%d failed=%d failed_num=%d,%d\n",
3357 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3358 s.failed_num[0], s.failed_num[1]);
3359 /* check if the array has lost more than max_degraded devices and,
3360 * if so, some requests might need to be failed.
3361 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003362 if (s.failed > conf->max_degraded) {
3363 sh->check_state = 0;
3364 sh->reconstruct_state = 0;
3365 if (s.to_read+s.to_write+s.written)
3366 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003367 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003368 handle_failed_sync(conf, sh, &s);
3369 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003370
3371 /*
3372 * might be able to return some write requests if the parity blocks
3373 * are safe, or on a failed drive
3374 */
3375 pdev = &sh->dev[sh->pd_idx];
3376 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3377 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3378 qdev = &sh->dev[sh->qd_idx];
3379 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3380 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3381 || conf->level < 6;
3382
3383 if (s.written &&
3384 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3385 && !test_bit(R5_LOCKED, &pdev->flags)
3386 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3387 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3388 && !test_bit(R5_LOCKED, &qdev->flags)
3389 && test_bit(R5_UPTODATE, &qdev->flags)))))
3390 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3391
3392 /* Now we might consider reading some blocks, either to check/generate
3393 * parity, or to satisfy requests
3394 * or to load a block that is being partially written.
3395 */
3396 if (s.to_read || s.non_overwrite
3397 || (conf->level == 6 && s.to_write && s.failed)
NeilBrown9a3e1102011-12-23 10:17:53 +11003398 || (s.syncing && (s.uptodate + s.compute < disks))
3399 || s.replacing
3400 || s.expanding)
NeilBrown474af965fe2011-07-27 11:00:36 +10003401 handle_stripe_fill(sh, &s, disks);
3402
NeilBrown84789552011-07-27 11:00:36 +10003403 /* Now we check to see if any write operations have recently
3404 * completed
3405 */
3406 prexor = 0;
3407 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3408 prexor = 1;
3409 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3410 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3411 sh->reconstruct_state = reconstruct_state_idle;
3412
3413 /* All the 'written' buffers and the parity block are ready to
3414 * be written back to disk
3415 */
3416 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3417 BUG_ON(sh->qd_idx >= 0 &&
3418 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags));
3419 for (i = disks; i--; ) {
3420 struct r5dev *dev = &sh->dev[i];
3421 if (test_bit(R5_LOCKED, &dev->flags) &&
3422 (i == sh->pd_idx || i == sh->qd_idx ||
3423 dev->written)) {
3424 pr_debug("Writing block %d\n", i);
3425 set_bit(R5_Wantwrite, &dev->flags);
3426 if (prexor)
3427 continue;
3428 if (!test_bit(R5_Insync, &dev->flags) ||
3429 ((i == sh->pd_idx || i == sh->qd_idx) &&
3430 s.failed == 0))
3431 set_bit(STRIPE_INSYNC, &sh->state);
3432 }
3433 }
3434 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3435 s.dec_preread_active = 1;
3436 }
3437
3438 /* Now to consider new write requests and what else, if anything
3439 * should be read. We do not handle new writes when:
3440 * 1/ A 'write' operation (copy+xor) is already in flight.
3441 * 2/ A 'check' operation is in flight, as it may clobber the parity
3442 * block.
3443 */
3444 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3445 handle_stripe_dirtying(conf, sh, &s, disks);
3446
3447 /* maybe we need to check and possibly fix the parity for this stripe
3448 * Any reads will already have been scheduled, so we just see if enough
3449 * data is available. The parity check is held off while parity
3450 * dependent operations are in flight.
3451 */
3452 if (sh->check_state ||
3453 (s.syncing && s.locked == 0 &&
3454 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3455 !test_bit(STRIPE_INSYNC, &sh->state))) {
3456 if (conf->level == 6)
3457 handle_parity_checks6(conf, sh, &s, disks);
3458 else
3459 handle_parity_checks5(conf, sh, &s, disks);
3460 }
NeilBrownc5a31002011-07-27 11:00:36 +10003461
NeilBrown9a3e1102011-12-23 10:17:53 +11003462 if (s.replacing && s.locked == 0
3463 && !test_bit(STRIPE_INSYNC, &sh->state)) {
3464 /* Write out to replacement devices where possible */
3465 for (i = 0; i < conf->raid_disks; i++)
3466 if (test_bit(R5_UPTODATE, &sh->dev[i].flags) &&
3467 test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3468 set_bit(R5_WantReplace, &sh->dev[i].flags);
3469 set_bit(R5_LOCKED, &sh->dev[i].flags);
3470 s.locked++;
3471 }
3472 set_bit(STRIPE_INSYNC, &sh->state);
3473 }
3474 if ((s.syncing || s.replacing) && s.locked == 0 &&
3475 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003476 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3477 clear_bit(STRIPE_SYNCING, &sh->state);
3478 }
3479
3480 /* If the failed drives are just a ReadError, then we might need
3481 * to progress the repair/check process
3482 */
3483 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3484 for (i = 0; i < s.failed; i++) {
3485 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3486 if (test_bit(R5_ReadError, &dev->flags)
3487 && !test_bit(R5_LOCKED, &dev->flags)
3488 && test_bit(R5_UPTODATE, &dev->flags)
3489 ) {
3490 if (!test_bit(R5_ReWrite, &dev->flags)) {
3491 set_bit(R5_Wantwrite, &dev->flags);
3492 set_bit(R5_ReWrite, &dev->flags);
3493 set_bit(R5_LOCKED, &dev->flags);
3494 s.locked++;
3495 } else {
3496 /* let's read it back */
3497 set_bit(R5_Wantread, &dev->flags);
3498 set_bit(R5_LOCKED, &dev->flags);
3499 s.locked++;
3500 }
3501 }
3502 }
3503
3504
NeilBrown3687c062011-07-27 11:00:36 +10003505 /* Finish reconstruct operations initiated by the expansion process */
3506 if (sh->reconstruct_state == reconstruct_state_result) {
3507 struct stripe_head *sh_src
3508 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3509 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3510 /* sh cannot be written until sh_src has been read.
3511 * so arrange for sh to be delayed a little
3512 */
3513 set_bit(STRIPE_DELAYED, &sh->state);
3514 set_bit(STRIPE_HANDLE, &sh->state);
3515 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3516 &sh_src->state))
3517 atomic_inc(&conf->preread_active_stripes);
3518 release_stripe(sh_src);
3519 goto finish;
3520 }
3521 if (sh_src)
3522 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003523
NeilBrown3687c062011-07-27 11:00:36 +10003524 sh->reconstruct_state = reconstruct_state_idle;
3525 clear_bit(STRIPE_EXPANDING, &sh->state);
3526 for (i = conf->raid_disks; i--; ) {
3527 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3528 set_bit(R5_LOCKED, &sh->dev[i].flags);
3529 s.locked++;
3530 }
3531 }
3532
3533 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3534 !sh->reconstruct_state) {
3535 /* Need to write out all blocks after computing parity */
3536 sh->disks = conf->raid_disks;
3537 stripe_set_idx(sh->sector, conf, 0, sh);
3538 schedule_reconstruction(sh, &s, 1, 1);
3539 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3540 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3541 atomic_dec(&conf->reshape_stripes);
3542 wake_up(&conf->wait_for_overlap);
3543 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3544 }
3545
3546 if (s.expanding && s.locked == 0 &&
3547 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3548 handle_stripe_expansion(conf, sh);
3549
3550finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003551 /* wait for this device to become unblocked */
NeilBrown43220aa2011-08-31 12:49:14 +10003552 if (conf->mddev->external && unlikely(s.blocked_rdev))
NeilBrownc5709ef2011-07-26 11:35:20 +10003553 md_wait_for_blocked_rdev(s.blocked_rdev, conf->mddev);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003554
NeilBrownbc2607f2011-07-28 11:39:22 +10003555 if (s.handle_bad_blocks)
3556 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003557 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003558 struct r5dev *dev = &sh->dev[i];
3559 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3560 /* We own a safe reference to the rdev */
3561 rdev = conf->disks[i].rdev;
3562 if (!rdev_set_badblocks(rdev, sh->sector,
3563 STRIPE_SECTORS, 0))
3564 md_error(conf->mddev, rdev);
3565 rdev_dec_pending(rdev, conf->mddev);
3566 }
NeilBrownb84db562011-07-28 11:39:23 +10003567 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3568 rdev = conf->disks[i].rdev;
3569 rdev_clear_badblocks(rdev, sh->sector,
3570 STRIPE_SECTORS);
3571 rdev_dec_pending(rdev, conf->mddev);
3572 }
NeilBrown977df362011-12-23 10:17:53 +11003573 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3574 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11003575 if (!rdev)
3576 /* rdev have been moved down */
3577 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11003578 rdev_clear_badblocks(rdev, sh->sector,
3579 STRIPE_SECTORS);
3580 rdev_dec_pending(rdev, conf->mddev);
3581 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003582 }
3583
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003584 if (s.ops_request)
3585 raid_run_ops(sh, s.ops_request);
3586
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003587 ops_run_io(sh, &s);
3588
NeilBrownc5709ef2011-07-26 11:35:20 +10003589 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003590 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003591 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003592 * have actually been submitted.
3593 */
3594 atomic_dec(&conf->preread_active_stripes);
3595 if (atomic_read(&conf->preread_active_stripes) <
3596 IO_THRESHOLD)
3597 md_wakeup_thread(conf->mddev->thread);
3598 }
3599
NeilBrownc5709ef2011-07-26 11:35:20 +10003600 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003601
Dan Williams257a4b42011-11-08 16:22:06 +11003602 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003603}
3604
NeilBrownd1688a62011-10-11 16:49:52 +11003605static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606{
3607 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3608 while (!list_empty(&conf->delayed_list)) {
3609 struct list_head *l = conf->delayed_list.next;
3610 struct stripe_head *sh;
3611 sh = list_entry(l, struct stripe_head, lru);
3612 list_del_init(l);
3613 clear_bit(STRIPE_DELAYED, &sh->state);
3614 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3615 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003616 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 }
NeilBrown482c0832011-04-18 18:25:42 +10003618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619}
3620
NeilBrownd1688a62011-10-11 16:49:52 +11003621static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003622{
3623 /* device_lock is held */
3624 struct list_head head;
3625 list_add(&head, &conf->bitmap_list);
3626 list_del_init(&conf->bitmap_list);
3627 while (!list_empty(&head)) {
3628 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3629 list_del_init(&sh->lru);
3630 atomic_inc(&sh->count);
3631 __release_stripe(conf, sh);
3632 }
3633}
3634
NeilBrownfd01b882011-10-11 16:47:53 +11003635int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003636{
NeilBrownd1688a62011-10-11 16:49:52 +11003637 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003638
3639 /* No difference between reads and writes. Just check
3640 * how busy the stripe_cache is
3641 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003642
NeilBrownf022b2f2006-10-03 01:15:56 -07003643 if (conf->inactive_blocked)
3644 return 1;
3645 if (conf->quiesce)
3646 return 1;
3647 if (list_empty_careful(&conf->inactive_list))
3648 return 1;
3649
3650 return 0;
3651}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003652EXPORT_SYMBOL_GPL(md_raid5_congested);
3653
3654static int raid5_congested(void *data, int bits)
3655{
NeilBrownfd01b882011-10-11 16:47:53 +11003656 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003657
3658 return mddev_congested(mddev, bits) ||
3659 md_raid5_congested(mddev, bits);
3660}
NeilBrownf022b2f2006-10-03 01:15:56 -07003661
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003662/* We want read requests to align with chunks where possible,
3663 * but write requests don't need to.
3664 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003665static int raid5_mergeable_bvec(struct request_queue *q,
3666 struct bvec_merge_data *bvm,
3667 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003668{
NeilBrownfd01b882011-10-11 16:47:53 +11003669 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003670 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003671 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003672 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003673 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003674
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003675 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003676 return biovec->bv_len; /* always allow writes to be mergeable */
3677
Andre Noll664e7c42009-06-18 08:45:27 +10003678 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3679 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003680 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3681 if (max < 0) max = 0;
3682 if (max <= biovec->bv_len && bio_sectors == 0)
3683 return biovec->bv_len;
3684 else
3685 return max;
3686}
3687
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003688
NeilBrownfd01b882011-10-11 16:47:53 +11003689static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003690{
3691 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003692 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003693 unsigned int bio_sectors = bio->bi_size >> 9;
3694
Andre Noll664e7c42009-06-18 08:45:27 +10003695 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3696 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003697 return chunk_sectors >=
3698 ((sector & (chunk_sectors - 1)) + bio_sectors);
3699}
3700
3701/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003702 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3703 * later sampled by raid5d.
3704 */
NeilBrownd1688a62011-10-11 16:49:52 +11003705static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003706{
3707 unsigned long flags;
3708
3709 spin_lock_irqsave(&conf->device_lock, flags);
3710
3711 bi->bi_next = conf->retry_read_aligned_list;
3712 conf->retry_read_aligned_list = bi;
3713
3714 spin_unlock_irqrestore(&conf->device_lock, flags);
3715 md_wakeup_thread(conf->mddev->thread);
3716}
3717
3718
NeilBrownd1688a62011-10-11 16:49:52 +11003719static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003720{
3721 struct bio *bi;
3722
3723 bi = conf->retry_read_aligned;
3724 if (bi) {
3725 conf->retry_read_aligned = NULL;
3726 return bi;
3727 }
3728 bi = conf->retry_read_aligned_list;
3729 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003730 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003731 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003732 /*
3733 * this sets the active strip count to 1 and the processed
3734 * strip count to zero (upper 8 bits)
3735 */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003736 bi->bi_phys_segments = 1; /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003737 }
3738
3739 return bi;
3740}
3741
3742
3743/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003744 * The "raid5_align_endio" should check if the read succeeded and if it
3745 * did, call bio_endio on the original bio (having bio_put the new bio
3746 * first).
3747 * If the read failed..
3748 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003749static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003750{
3751 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003752 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003753 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003754 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003755 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003756
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003757 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003758
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003759 rdev = (void*)raid_bi->bi_next;
3760 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003761 mddev = rdev->mddev;
3762 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003763
3764 rdev_dec_pending(rdev, conf->mddev);
3765
3766 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003767 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003768 if (atomic_dec_and_test(&conf->active_aligned_reads))
3769 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003770 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003771 }
3772
3773
Dan Williams45b42332007-07-09 11:56:43 -07003774 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003775
3776 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003777}
3778
Neil Brown387bb172007-02-08 14:20:29 -08003779static int bio_fits_rdev(struct bio *bi)
3780{
Jens Axboe165125e2007-07-24 09:28:11 +02003781 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003782
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003783 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003784 return 0;
3785 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003786 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003787 return 0;
3788
3789 if (q->merge_bvec_fn)
3790 /* it's too hard to apply the merge_bvec_fn at this stage,
3791 * just just give up
3792 */
3793 return 0;
3794
3795 return 1;
3796}
3797
3798
NeilBrownfd01b882011-10-11 16:47:53 +11003799static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003800{
NeilBrownd1688a62011-10-11 16:49:52 +11003801 struct r5conf *conf = mddev->private;
NeilBrown8553fe72009-12-14 12:49:47 +11003802 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003803 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11003804 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11003805 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003806
3807 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003808 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003809 return 0;
3810 }
3811 /*
NeilBrowna167f662010-10-26 18:31:13 +11003812 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003813 */
NeilBrowna167f662010-10-26 18:31:13 +11003814 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003815 if (!align_bi)
3816 return 0;
3817 /*
3818 * set bi_end_io to a new function, and set bi_private to the
3819 * original bio.
3820 */
3821 align_bi->bi_end_io = raid5_align_endio;
3822 align_bi->bi_private = raid_bio;
3823 /*
3824 * compute position
3825 */
NeilBrown112bf892009-03-31 14:39:38 +11003826 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3827 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003828 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003829
NeilBrown671488c2011-12-23 10:17:52 +11003830 end_sector = align_bi->bi_sector + (align_bi->bi_size >> 9);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003831 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11003832 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
3833 if (!rdev || test_bit(Faulty, &rdev->flags) ||
3834 rdev->recovery_offset < end_sector) {
3835 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3836 if (rdev &&
3837 (test_bit(Faulty, &rdev->flags) ||
3838 !(test_bit(In_sync, &rdev->flags) ||
3839 rdev->recovery_offset >= end_sector)))
3840 rdev = NULL;
3841 }
3842 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10003843 sector_t first_bad;
3844 int bad_sectors;
3845
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003846 atomic_inc(&rdev->nr_pending);
3847 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003848 raid_bio->bi_next = (void*)rdev;
3849 align_bi->bi_bdev = rdev->bdev;
3850 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
3851 align_bi->bi_sector += rdev->data_offset;
3852
NeilBrown31c176e2011-07-28 11:39:22 +10003853 if (!bio_fits_rdev(align_bi) ||
3854 is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
3855 &first_bad, &bad_sectors)) {
3856 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08003857 bio_put(align_bi);
3858 rdev_dec_pending(rdev, mddev);
3859 return 0;
3860 }
3861
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003862 spin_lock_irq(&conf->device_lock);
3863 wait_event_lock_irq(conf->wait_for_stripe,
3864 conf->quiesce == 0,
3865 conf->device_lock, /* nothing */);
3866 atomic_inc(&conf->active_aligned_reads);
3867 spin_unlock_irq(&conf->device_lock);
3868
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003869 generic_make_request(align_bi);
3870 return 1;
3871 } else {
3872 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003873 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003874 return 0;
3875 }
3876}
3877
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003878/* __get_priority_stripe - get the next stripe to process
3879 *
3880 * Full stripe writes are allowed to pass preread active stripes up until
3881 * the bypass_threshold is exceeded. In general the bypass_count
3882 * increments when the handle_list is handled before the hold_list; however, it
3883 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3884 * stripe with in flight i/o. The bypass_count will be reset when the
3885 * head of the hold_list has changed, i.e. the head was promoted to the
3886 * handle_list.
3887 */
NeilBrownd1688a62011-10-11 16:49:52 +11003888static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003889{
3890 struct stripe_head *sh;
3891
3892 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3893 __func__,
3894 list_empty(&conf->handle_list) ? "empty" : "busy",
3895 list_empty(&conf->hold_list) ? "empty" : "busy",
3896 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3897
3898 if (!list_empty(&conf->handle_list)) {
3899 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3900
3901 if (list_empty(&conf->hold_list))
3902 conf->bypass_count = 0;
3903 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3904 if (conf->hold_list.next == conf->last_hold)
3905 conf->bypass_count++;
3906 else {
3907 conf->last_hold = conf->hold_list.next;
3908 conf->bypass_count -= conf->bypass_threshold;
3909 if (conf->bypass_count < 0)
3910 conf->bypass_count = 0;
3911 }
3912 }
3913 } else if (!list_empty(&conf->hold_list) &&
3914 ((conf->bypass_threshold &&
3915 conf->bypass_count > conf->bypass_threshold) ||
3916 atomic_read(&conf->pending_full_writes) == 0)) {
3917 sh = list_entry(conf->hold_list.next,
3918 typeof(*sh), lru);
3919 conf->bypass_count -= conf->bypass_threshold;
3920 if (conf->bypass_count < 0)
3921 conf->bypass_count = 0;
3922 } else
3923 return NULL;
3924
3925 list_del_init(&sh->lru);
3926 atomic_inc(&sh->count);
3927 BUG_ON(atomic_read(&sh->count) != 1);
3928 return sh;
3929}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003930
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07003931static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932{
NeilBrownd1688a62011-10-11 16:49:52 +11003933 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11003934 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 sector_t new_sector;
3936 sector_t logical_sector, last_sector;
3937 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01003938 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11003939 int remaining;
NeilBrown7c13edc2011-04-18 18:25:43 +10003940 int plugged;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941
Tejun Heoe9c74692010-09-03 11:56:18 +02003942 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
3943 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02003944 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07003945 }
3946
NeilBrown3d310eb2005-06-21 17:17:26 -07003947 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07003948
NeilBrown802ba062006-12-13 00:34:13 -08003949 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003950 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11003951 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02003952 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003953
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
3955 last_sector = bi->bi_sector + (bi->bi_size>>9);
3956 bi->bi_next = NULL;
3957 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07003958
NeilBrown7c13edc2011-04-18 18:25:43 +10003959 plugged = mddev_check_plugged(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
3961 DEFINE_WAIT(w);
NeilBrown16a53ec2006-06-26 00:27:38 -07003962 int disks, data_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003963 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08003964
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003965 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11003966 previous = 0;
NeilBrownb0f9ec02009-03-31 15:27:18 +11003967 disks = conf->raid_disks;
NeilBrownb578d552006-03-27 01:18:12 -08003968 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11003969 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11003970 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08003971 * 64bit on a 32bit platform, and so it might be
3972 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02003973 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08003974 * the lock is dropped, so once we get a reference
3975 * to the stripe that we think it is, we will have
3976 * to check again.
3977 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003978 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11003979 if (mddev->delta_disks < 0
3980 ? logical_sector < conf->reshape_progress
3981 : logical_sector >= conf->reshape_progress) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003982 disks = conf->previous_raid_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003983 previous = 1;
3984 } else {
NeilBrownfef9c612009-03-31 15:16:46 +11003985 if (mddev->delta_disks < 0
3986 ? logical_sector < conf->reshape_safe
3987 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08003988 spin_unlock_irq(&conf->device_lock);
3989 schedule();
3990 goto retry;
3991 }
3992 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003993 spin_unlock_irq(&conf->device_lock);
3994 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003995 data_disks = disks - conf->max_degraded;
3996
NeilBrown112bf892009-03-31 14:39:38 +11003997 new_sector = raid5_compute_sector(conf, logical_sector,
3998 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003999 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10004000 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 (unsigned long long)new_sector,
4002 (unsigned long long)logical_sector);
4003
NeilBrownb5663ba2009-03-31 14:39:38 +11004004 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10004005 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004007 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004008 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08004009 * stripe, so we must do the range check again.
4010 * Expansion could still move past after this
4011 * test, but as we are holding a reference to
4012 * 'sh', we know that if that happens,
4013 * STRIPE_EXPANDING will get set and the expansion
4014 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004015 */
4016 int must_retry = 0;
4017 spin_lock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004018 if (mddev->delta_disks < 0
4019 ? logical_sector >= conf->reshape_progress
4020 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004021 /* mismatch, need to try again */
4022 must_retry = 1;
4023 spin_unlock_irq(&conf->device_lock);
4024 if (must_retry) {
4025 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004026 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004027 goto retry;
4028 }
4029 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004030
Namhyung Kimffd96e32011-07-18 17:38:51 +10004031 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004032 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004033 logical_sector < mddev->suspend_hi) {
4034 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004035 /* As the suspend_* range is controlled by
4036 * userspace, we want an interruptible
4037 * wait.
4038 */
4039 flush_signals(current);
4040 prepare_to_wait(&conf->wait_for_overlap,
4041 &w, TASK_INTERRUPTIBLE);
4042 if (logical_sector >= mddev->suspend_lo &&
4043 logical_sector < mddev->suspend_hi)
4044 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004045 goto retry;
4046 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004047
4048 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004049 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004050 /* Stripe is busy expanding or
4051 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 * and wait a while
4053 */
NeilBrown482c0832011-04-18 18:25:42 +10004054 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 release_stripe(sh);
4056 schedule();
4057 goto retry;
4058 }
4059 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004060 set_bit(STRIPE_HANDLE, &sh->state);
4061 clear_bit(STRIPE_DELAYED, &sh->state);
Tejun Heoe9c74692010-09-03 11:56:18 +02004062 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004063 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4064 atomic_inc(&conf->preread_active_stripes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 } else {
4067 /* cannot get stripe for read-ahead, just give-up */
4068 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4069 finish_wait(&conf->wait_for_overlap, &w);
4070 break;
4071 }
4072
4073 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004074 if (!plugged)
4075 md_wakeup_thread(mddev->thread);
4076
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004078 remaining = raid5_dec_bi_phys_segments(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004079 spin_unlock_irq(&conf->device_lock);
4080 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081
NeilBrown16a53ec2006-06-26 00:27:38 -07004082 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004084
Neil Brown0e13fe232008-06-28 08:31:20 +10004085 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087}
4088
NeilBrownfd01b882011-10-11 16:47:53 +11004089static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004090
NeilBrownfd01b882011-10-11 16:47:53 +11004091static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092{
NeilBrown52c03292006-06-26 00:27:43 -07004093 /* reshaping is quite different to recovery/resync so it is
4094 * handled quite separately ... here.
4095 *
4096 * On each call to sync_request, we gather one chunk worth of
4097 * destination stripes and flag them as expanding.
4098 * Then we find all the source stripes and request reads.
4099 * As the reads complete, handle_stripe will copy the data
4100 * into the destination stripe and release that stripe.
4101 */
NeilBrownd1688a62011-10-11 16:49:52 +11004102 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004104 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004105 int raid_disks = conf->previous_raid_disks;
4106 int data_disks = raid_disks - conf->max_degraded;
4107 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004108 int i;
4109 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004110 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004111 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004112 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004113 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004114
NeilBrownfef9c612009-03-31 15:16:46 +11004115 if (sector_nr == 0) {
4116 /* If restarting in the middle, skip the initial sectors */
4117 if (mddev->delta_disks < 0 &&
4118 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4119 sector_nr = raid5_size(mddev, 0, 0)
4120 - conf->reshape_progress;
NeilBrowna6397552009-08-13 10:13:00 +10004121 } else if (mddev->delta_disks >= 0 &&
NeilBrownfef9c612009-03-31 15:16:46 +11004122 conf->reshape_progress > 0)
4123 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004124 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004125 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004126 mddev->curr_resync_completed = sector_nr;
4127 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004128 *skipped = 1;
4129 return sector_nr;
4130 }
NeilBrown52c03292006-06-26 00:27:43 -07004131 }
4132
NeilBrown7a661382009-03-31 15:21:40 +11004133 /* We need to process a full chunk at a time.
4134 * If old and new chunk sizes differ, we need to process the
4135 * largest of these
4136 */
Andre Noll664e7c42009-06-18 08:45:27 +10004137 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4138 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004139 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004140 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004141
NeilBrown52c03292006-06-26 00:27:43 -07004142 /* we update the metadata when there is more than 3Meg
4143 * in the block range (that is rather arbitrary, should
4144 * probably be time based) or when the data about to be
4145 * copied would over-write the source of the data at
4146 * the front of the range.
NeilBrownfef9c612009-03-31 15:16:46 +11004147 * i.e. one new_stripe along from reshape_progress new_maps
4148 * to after where reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004149 */
NeilBrownfef9c612009-03-31 15:16:46 +11004150 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004151 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004152 readpos = conf->reshape_progress;
4153 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004154 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004155 sector_div(safepos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004156 if (mddev->delta_disks < 0) {
NeilBrowned37d832009-05-27 21:39:05 +10004157 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004158 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004159 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004160 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004161 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004162 readpos -= min_t(sector_t, reshape_sectors, readpos);
4163 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004164 }
NeilBrown52c03292006-06-26 00:27:43 -07004165
NeilBrownc8f517c2009-03-31 15:28:40 +11004166 /* 'writepos' is the most advanced device address we might write.
4167 * 'readpos' is the least advanced device address we might read.
4168 * 'safepos' is the least address recorded in the metadata as having
4169 * been reshaped.
4170 * If 'readpos' is behind 'writepos', then there is no way that we can
4171 * ensure safety in the face of a crash - that must be done by userspace
4172 * making a backup of the data. So in that case there is no particular
4173 * rush to update metadata.
4174 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4175 * update the metadata to advance 'safepos' to match 'readpos' so that
4176 * we can be safe in the event of a crash.
4177 * So we insist on updating metadata if safepos is behind writepos and
4178 * readpos is beyond writepos.
4179 * In any case, update the metadata every 10 seconds.
4180 * Maybe that number should be configurable, but I'm not sure it is
4181 * worth it.... maybe it could be a multiple of safemode_delay???
4182 */
NeilBrownfef9c612009-03-31 15:16:46 +11004183 if ((mddev->delta_disks < 0
NeilBrownc8f517c2009-03-31 15:28:40 +11004184 ? (safepos > writepos && readpos < writepos)
4185 : (safepos < writepos && readpos > writepos)) ||
4186 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004187 /* Cannot proceed until we've updated the superblock... */
4188 wait_event(conf->wait_for_overlap,
4189 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004190 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004191 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004192 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07004193 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004194 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07004195 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004196 kthread_should_stop());
4197 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004198 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004199 spin_unlock_irq(&conf->device_lock);
4200 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004201 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004202 }
4203
NeilBrownec32a2b2009-03-31 15:17:38 +11004204 if (mddev->delta_disks < 0) {
4205 BUG_ON(conf->reshape_progress == 0);
4206 stripe_addr = writepos;
4207 BUG_ON((mddev->dev_sectors &
NeilBrown7a661382009-03-31 15:21:40 +11004208 ~((sector_t)reshape_sectors - 1))
4209 - reshape_sectors - stripe_addr
NeilBrownec32a2b2009-03-31 15:17:38 +11004210 != sector_nr);
4211 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004212 BUG_ON(writepos != sector_nr + reshape_sectors);
NeilBrownec32a2b2009-03-31 15:17:38 +11004213 stripe_addr = sector_nr;
4214 }
NeilBrownab69ae12009-03-31 15:26:47 +11004215 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004216 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004217 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004218 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004219 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004220 set_bit(STRIPE_EXPANDING, &sh->state);
4221 atomic_inc(&conf->reshape_stripes);
4222 /* If any of this stripe is beyond the end of the old
4223 * array, then we need to zero those blocks
4224 */
4225 for (j=sh->disks; j--;) {
4226 sector_t s;
4227 if (j == sh->pd_idx)
4228 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004229 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004230 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004231 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004232 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004233 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004234 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004235 continue;
4236 }
4237 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4238 set_bit(R5_Expanded, &sh->dev[j].flags);
4239 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4240 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004241 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004242 set_bit(STRIPE_EXPAND_READY, &sh->state);
4243 set_bit(STRIPE_HANDLE, &sh->state);
4244 }
NeilBrownab69ae12009-03-31 15:26:47 +11004245 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004246 }
4247 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004248 if (mddev->delta_disks < 0)
NeilBrown7a661382009-03-31 15:21:40 +11004249 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004250 else
NeilBrown7a661382009-03-31 15:21:40 +11004251 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004252 spin_unlock_irq(&conf->device_lock);
4253 /* Ok, those stripe are ready. We can start scheduling
4254 * reads on the source stripes.
4255 * The source stripes are determined by mapping the first and last
4256 * block on the destination stripes.
4257 */
NeilBrown52c03292006-06-26 00:27:43 -07004258 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004259 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004260 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004261 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004262 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004263 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004264 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004265 if (last_sector >= mddev->dev_sectors)
4266 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004267 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004268 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004269 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4270 set_bit(STRIPE_HANDLE, &sh->state);
4271 release_stripe(sh);
4272 first_sector += STRIPE_SECTORS;
4273 }
NeilBrownab69ae12009-03-31 15:26:47 +11004274 /* Now that the sources are clearly marked, we can release
4275 * the destination stripes
4276 */
4277 while (!list_empty(&stripes)) {
4278 sh = list_entry(stripes.next, struct stripe_head, lru);
4279 list_del_init(&sh->lru);
4280 release_stripe(sh);
4281 }
NeilBrownc6207272008-02-06 01:39:52 -08004282 /* If this takes us to the resync_max point where we have to pause,
4283 * then we need to write out the superblock.
4284 */
NeilBrown7a661382009-03-31 15:21:40 +11004285 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004286 if ((sector_nr - mddev->curr_resync_completed) * 2
4287 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004288 /* Cannot proceed until we've updated the superblock... */
4289 wait_event(conf->wait_for_overlap,
4290 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004291 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004292 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004293 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004294 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4295 md_wakeup_thread(mddev->thread);
4296 wait_event(mddev->sb_wait,
4297 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4298 || kthread_should_stop());
4299 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004300 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004301 spin_unlock_irq(&conf->device_lock);
4302 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004303 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004304 }
NeilBrown7a661382009-03-31 15:21:40 +11004305 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004306}
4307
4308/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004309static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004310{
NeilBrownd1688a62011-10-11 16:49:52 +11004311 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004312 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004313 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004314 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004315 int still_degraded = 0;
4316 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317
NeilBrown72626682005-09-09 16:23:54 -07004318 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004320
NeilBrown29269552006-03-27 01:18:10 -08004321 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4322 end_reshape(conf);
4323 return 0;
4324 }
NeilBrown72626682005-09-09 16:23:54 -07004325
4326 if (mddev->curr_resync < max_sector) /* aborted */
4327 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4328 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004329 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004330 conf->fullsync = 0;
4331 bitmap_close_sync(mddev->bitmap);
4332
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 return 0;
4334 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004335
NeilBrown64bd6602009-08-03 10:59:58 +10004336 /* Allow raid5_quiesce to complete */
4337 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4338
NeilBrown52c03292006-06-26 00:27:43 -07004339 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4340 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004341
NeilBrownc6207272008-02-06 01:39:52 -08004342 /* No need to check resync_max as we never do more than one
4343 * stripe, and as resync_max will always be on a chunk boundary,
4344 * if the check in md_do_sync didn't fire, there is no chance
4345 * of overstepping resync_max here
4346 */
4347
NeilBrown16a53ec2006-06-26 00:27:38 -07004348 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 * to resync, then assert that we are finished, because there is
4350 * nothing we can do.
4351 */
NeilBrown3285edf2006-06-26 00:27:55 -07004352 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004353 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004354 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004355 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 return rv;
4357 }
NeilBrown72626682005-09-09 16:23:54 -07004358 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004359 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004360 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4361 /* we can skip this block, and probably more */
4362 sync_blocks /= STRIPE_SECTORS;
4363 *skipped = 1;
4364 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366
NeilBrownb47490c2008-02-06 01:39:50 -08004367 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4368
NeilBrowna8c906c2009-06-09 14:39:59 +10004369 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004371 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004373 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004375 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004377 /* Need to check if array will still be degraded after recovery/resync
4378 * We don't need to check the 'failed' flag as when that gets set,
4379 * recovery aborts.
4380 */
NeilBrownf001a702009-06-09 14:30:31 +10004381 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004382 if (conf->disks[i].rdev == NULL)
4383 still_degraded = 1;
4384
4385 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4386
NeilBrown83206d62011-07-26 11:19:49 +10004387 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388
NeilBrown14425772009-10-16 15:55:25 +11004389 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 release_stripe(sh);
4391
4392 return STRIPE_SECTORS;
4393}
4394
NeilBrownd1688a62011-10-11 16:49:52 +11004395static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004396{
4397 /* We may not be able to submit a whole bio at once as there
4398 * may not be enough stripe_heads available.
4399 * We cannot pre-allocate enough stripe_heads as we may need
4400 * more than exist in the cache (if we allow ever large chunks).
4401 * So we do one stripe head at a time and record in
4402 * ->bi_hw_segments how many have been done.
4403 *
4404 * We *know* that this entire raid_bio is in one chunk, so
4405 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4406 */
4407 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004408 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004409 sector_t sector, logical_sector, last_sector;
4410 int scnt = 0;
4411 int remaining;
4412 int handled = 0;
4413
4414 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004415 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004416 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004417 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4418
4419 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004420 logical_sector += STRIPE_SECTORS,
4421 sector += STRIPE_SECTORS,
4422 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004423
Jens Axboe960e7392008-08-15 10:41:18 +02004424 if (scnt < raid5_bi_hw_segments(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004425 /* already done this stripe */
4426 continue;
4427
NeilBrowna8c906c2009-06-09 14:39:59 +10004428 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004429
4430 if (!sh) {
4431 /* failed to get a stripe - must wait */
Jens Axboe960e7392008-08-15 10:41:18 +02004432 raid5_set_bi_hw_segments(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004433 conf->retry_read_aligned = raid_bio;
4434 return handled;
4435 }
4436
Neil Brown387bb172007-02-08 14:20:29 -08004437 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4438 release_stripe(sh);
Jens Axboe960e7392008-08-15 10:41:18 +02004439 raid5_set_bi_hw_segments(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004440 conf->retry_read_aligned = raid_bio;
4441 return handled;
4442 }
4443
Dan Williams36d1c642009-07-14 11:48:22 -07004444 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004445 release_stripe(sh);
4446 handled++;
4447 }
4448 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004449 remaining = raid5_dec_bi_phys_segments(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004450 spin_unlock_irq(&conf->device_lock);
Neil Brown0e13fe232008-06-28 08:31:20 +10004451 if (remaining == 0)
4452 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004453 if (atomic_dec_and_test(&conf->active_aligned_reads))
4454 wake_up(&conf->wait_for_stripe);
4455 return handled;
4456}
4457
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004458
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459/*
4460 * This is our raid5 kernel thread.
4461 *
4462 * We scan the hash table for stripes which can be handled now.
4463 * During the scan, completed stripes are saved for us by the interrupt
4464 * handler, so that they will not have to wait for our next wakeup.
4465 */
NeilBrownfd01b882011-10-11 16:47:53 +11004466static void raid5d(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467{
4468 struct stripe_head *sh;
NeilBrownd1688a62011-10-11 16:49:52 +11004469 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004471 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472
Dan Williams45b42332007-07-09 11:56:43 -07004473 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474
4475 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004477 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 handled = 0;
4479 spin_lock_irq(&conf->device_lock);
4480 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004481 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482
NeilBrown7c13edc2011-04-18 18:25:43 +10004483 if (atomic_read(&mddev->plug_cnt) == 0 &&
4484 !list_empty(&conf->bitmap_list)) {
4485 /* Now is a good time to flush some bitmap updates */
4486 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004487 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004488 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004489 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004490 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004491 activate_bit_delay(conf);
4492 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004493 if (atomic_read(&mddev->plug_cnt) == 0)
4494 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004495
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004496 while ((bio = remove_bio_from_retry(conf))) {
4497 int ok;
4498 spin_unlock_irq(&conf->device_lock);
4499 ok = retry_aligned_read(conf, bio);
4500 spin_lock_irq(&conf->device_lock);
4501 if (!ok)
4502 break;
4503 handled++;
4504 }
4505
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004506 sh = __get_priority_stripe(conf);
4507
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004508 if (!sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 spin_unlock_irq(&conf->device_lock);
4511
4512 handled++;
Dan Williams417b8d42009-10-16 16:25:22 +11004513 handle_stripe(sh);
4514 release_stripe(sh);
4515 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516
NeilBrownde393cd2011-07-28 11:31:48 +10004517 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
4518 md_check_recovery(mddev);
4519
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 spin_lock_irq(&conf->device_lock);
4521 }
Dan Williams45b42332007-07-09 11:56:43 -07004522 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523
4524 spin_unlock_irq(&conf->device_lock);
4525
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004526 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004527 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528
Dan Williams45b42332007-07-09 11:56:43 -07004529 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530}
4531
NeilBrown3f294f42005-11-08 21:39:25 -08004532static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004533raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004534{
NeilBrownd1688a62011-10-11 16:49:52 +11004535 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004536 if (conf)
4537 return sprintf(page, "%d\n", conf->max_nr_stripes);
4538 else
4539 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004540}
4541
NeilBrownc41d4ac2010-06-01 19:37:24 +10004542int
NeilBrownfd01b882011-10-11 16:47:53 +11004543raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004544{
NeilBrownd1688a62011-10-11 16:49:52 +11004545 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004546 int err;
4547
4548 if (size <= 16 || size > 32768)
4549 return -EINVAL;
4550 while (size < conf->max_nr_stripes) {
4551 if (drop_one_stripe(conf))
4552 conf->max_nr_stripes--;
4553 else
4554 break;
4555 }
4556 err = md_allow_write(mddev);
4557 if (err)
4558 return err;
4559 while (size > conf->max_nr_stripes) {
4560 if (grow_one_stripe(conf))
4561 conf->max_nr_stripes++;
4562 else break;
4563 }
4564 return 0;
4565}
4566EXPORT_SYMBOL(raid5_set_cache_size);
4567
NeilBrown3f294f42005-11-08 21:39:25 -08004568static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004569raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004570{
NeilBrownd1688a62011-10-11 16:49:52 +11004571 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004572 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004573 int err;
4574
NeilBrown3f294f42005-11-08 21:39:25 -08004575 if (len >= PAGE_SIZE)
4576 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004577 if (!conf)
4578 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004579
Dan Williams4ef197d82008-04-28 02:15:54 -07004580 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004581 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004582 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004583 if (err)
4584 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004585 return len;
4586}
NeilBrown007583c2005-11-08 21:39:30 -08004587
NeilBrown96de1e62005-11-08 21:39:39 -08004588static struct md_sysfs_entry
4589raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4590 raid5_show_stripe_cache_size,
4591 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004592
4593static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004594raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004595{
NeilBrownd1688a62011-10-11 16:49:52 +11004596 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004597 if (conf)
4598 return sprintf(page, "%d\n", conf->bypass_threshold);
4599 else
4600 return 0;
4601}
4602
4603static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004604raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004605{
NeilBrownd1688a62011-10-11 16:49:52 +11004606 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004607 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004608 if (len >= PAGE_SIZE)
4609 return -EINVAL;
4610 if (!conf)
4611 return -ENODEV;
4612
Dan Williams4ef197d82008-04-28 02:15:54 -07004613 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004614 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004615 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004616 return -EINVAL;
4617 conf->bypass_threshold = new;
4618 return len;
4619}
4620
4621static struct md_sysfs_entry
4622raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4623 S_IRUGO | S_IWUSR,
4624 raid5_show_preread_threshold,
4625 raid5_store_preread_threshold);
4626
4627static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004628stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004629{
NeilBrownd1688a62011-10-11 16:49:52 +11004630 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004631 if (conf)
4632 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4633 else
4634 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004635}
4636
NeilBrown96de1e62005-11-08 21:39:39 -08004637static struct md_sysfs_entry
4638raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004639
NeilBrown007583c2005-11-08 21:39:30 -08004640static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004641 &raid5_stripecache_size.attr,
4642 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004643 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004644 NULL,
4645};
NeilBrown007583c2005-11-08 21:39:30 -08004646static struct attribute_group raid5_attrs_group = {
4647 .name = NULL,
4648 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004649};
4650
Dan Williams80c3a6c2009-03-17 18:10:40 -07004651static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11004652raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07004653{
NeilBrownd1688a62011-10-11 16:49:52 +11004654 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004655
4656 if (!sectors)
4657 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004658 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11004659 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11004660 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004661
Andre Noll9d8f0362009-06-18 08:45:01 +10004662 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004663 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004664 return sectors * (raid_disks - conf->max_degraded);
4665}
4666
NeilBrownd1688a62011-10-11 16:49:52 +11004667static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004668{
4669 struct raid5_percpu *percpu;
4670 unsigned long cpu;
4671
4672 if (!conf->percpu)
4673 return;
4674
4675 get_online_cpus();
4676 for_each_possible_cpu(cpu) {
4677 percpu = per_cpu_ptr(conf->percpu, cpu);
4678 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004679 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004680 }
4681#ifdef CONFIG_HOTPLUG_CPU
4682 unregister_cpu_notifier(&conf->cpu_notify);
4683#endif
4684 put_online_cpus();
4685
4686 free_percpu(conf->percpu);
4687}
4688
NeilBrownd1688a62011-10-11 16:49:52 +11004689static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10004690{
4691 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004692 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10004693 kfree(conf->disks);
4694 kfree(conf->stripe_hashtbl);
4695 kfree(conf);
4696}
4697
Dan Williams36d1c642009-07-14 11:48:22 -07004698#ifdef CONFIG_HOTPLUG_CPU
4699static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4700 void *hcpu)
4701{
NeilBrownd1688a62011-10-11 16:49:52 +11004702 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07004703 long cpu = (long)hcpu;
4704 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4705
4706 switch (action) {
4707 case CPU_UP_PREPARE:
4708 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07004709 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07004710 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004711 if (!percpu->scribble)
4712 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4713
4714 if (!percpu->scribble ||
4715 (conf->level == 6 && !percpu->spare_page)) {
4716 safe_put_page(percpu->spare_page);
4717 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004718 pr_err("%s: failed memory allocation for cpu%ld\n",
4719 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07004720 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07004721 }
4722 break;
4723 case CPU_DEAD:
4724 case CPU_DEAD_FROZEN:
4725 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004726 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004727 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004728 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07004729 break;
4730 default:
4731 break;
4732 }
4733 return NOTIFY_OK;
4734}
4735#endif
4736
NeilBrownd1688a62011-10-11 16:49:52 +11004737static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004738{
4739 unsigned long cpu;
4740 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09004741 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004742 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004743 int err;
4744
Dan Williams36d1c642009-07-14 11:48:22 -07004745 allcpus = alloc_percpu(struct raid5_percpu);
4746 if (!allcpus)
4747 return -ENOMEM;
4748 conf->percpu = allcpus;
4749
4750 get_online_cpus();
4751 err = 0;
4752 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07004753 if (conf->level == 6) {
4754 spare_page = alloc_page(GFP_KERNEL);
4755 if (!spare_page) {
4756 err = -ENOMEM;
4757 break;
4758 }
4759 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
4760 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11004761 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004762 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07004763 err = -ENOMEM;
4764 break;
4765 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07004766 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004767 }
4768#ifdef CONFIG_HOTPLUG_CPU
4769 conf->cpu_notify.notifier_call = raid456_cpu_notify;
4770 conf->cpu_notify.priority = 0;
4771 if (err == 0)
4772 err = register_cpu_notifier(&conf->cpu_notify);
4773#endif
4774 put_online_cpus();
4775
4776 return err;
4777}
4778
NeilBrownd1688a62011-10-11 16:49:52 +11004779static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780{
NeilBrownd1688a62011-10-11 16:49:52 +11004781 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004782 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11004783 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 struct disk_info *disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785
NeilBrown91adb562009-03-31 14:39:39 +11004786 if (mddev->new_level != 5
4787 && mddev->new_level != 4
4788 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10004789 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004790 mdname(mddev), mddev->new_level);
4791 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 }
NeilBrown91adb562009-03-31 14:39:39 +11004793 if ((mddev->new_level == 5
4794 && !algorithm_valid_raid5(mddev->new_layout)) ||
4795 (mddev->new_level == 6
4796 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004797 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11004798 mdname(mddev), mddev->new_layout);
4799 return ERR_PTR(-EIO);
4800 }
4801 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10004802 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004803 mdname(mddev), mddev->raid_disks);
4804 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11004805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806
Andre Noll664e7c42009-06-18 08:45:27 +10004807 if (!mddev->new_chunk_sectors ||
4808 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
4809 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10004810 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
4811 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11004812 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11004813 }
4814
NeilBrownd1688a62011-10-11 16:49:52 +11004815 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11004816 if (conf == NULL)
4817 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11004818 spin_lock_init(&conf->device_lock);
4819 init_waitqueue_head(&conf->wait_for_stripe);
4820 init_waitqueue_head(&conf->wait_for_overlap);
4821 INIT_LIST_HEAD(&conf->handle_list);
4822 INIT_LIST_HEAD(&conf->hold_list);
4823 INIT_LIST_HEAD(&conf->delayed_list);
4824 INIT_LIST_HEAD(&conf->bitmap_list);
4825 INIT_LIST_HEAD(&conf->inactive_list);
4826 atomic_set(&conf->active_stripes, 0);
4827 atomic_set(&conf->preread_active_stripes, 0);
4828 atomic_set(&conf->active_aligned_reads, 0);
4829 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11004830 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11004831
4832 conf->raid_disks = mddev->raid_disks;
4833 if (mddev->reshape_position == MaxSector)
4834 conf->previous_raid_disks = mddev->raid_disks;
4835 else
4836 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004837 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
4838 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11004839
NeilBrown5e5e3e72009-10-16 16:35:30 +11004840 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11004841 GFP_KERNEL);
4842 if (!conf->disks)
4843 goto abort;
4844
4845 conf->mddev = mddev;
4846
4847 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
4848 goto abort;
4849
Dan Williams36d1c642009-07-14 11:48:22 -07004850 conf->level = mddev->new_level;
4851 if (raid5_alloc_percpu(conf) != 0)
4852 goto abort;
4853
NeilBrown0c55e022010-05-03 14:09:02 +10004854 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11004855
NeilBrowndafb20f2012-03-19 12:46:39 +11004856 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11004857 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004858 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11004859 || raid_disk < 0)
4860 continue;
4861 disk = conf->disks + raid_disk;
4862
NeilBrown17045f52011-12-23 10:17:53 +11004863 if (test_bit(Replacement, &rdev->flags)) {
4864 if (disk->replacement)
4865 goto abort;
4866 disk->replacement = rdev;
4867 } else {
4868 if (disk->rdev)
4869 goto abort;
4870 disk->rdev = rdev;
4871 }
NeilBrown91adb562009-03-31 14:39:39 +11004872
4873 if (test_bit(In_sync, &rdev->flags)) {
4874 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10004875 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
4876 " disk %d\n",
4877 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05004878 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11004879 /* Cannot rely on bitmap to complete recovery */
4880 conf->fullsync = 1;
4881 }
4882
Andre Noll09c9e5f2009-06-18 08:45:55 +10004883 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11004884 conf->level = mddev->new_level;
4885 if (conf->level == 6)
4886 conf->max_degraded = 2;
4887 else
4888 conf->max_degraded = 1;
4889 conf->algorithm = mddev->new_layout;
4890 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11004891 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11004892 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10004893 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11004894 conf->prev_algo = mddev->layout;
4895 }
NeilBrown91adb562009-03-31 14:39:39 +11004896
4897 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11004898 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11004899 if (grow_stripes(conf, conf->max_nr_stripes)) {
4900 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004901 "md/raid:%s: couldn't allocate %dkB for buffers\n",
4902 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004903 goto abort;
4904 } else
NeilBrown0c55e022010-05-03 14:09:02 +10004905 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
4906 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004907
NeilBrown0da3c612009-09-23 18:09:45 +10004908 conf->thread = md_register_thread(raid5d, mddev, NULL);
NeilBrown91adb562009-03-31 14:39:39 +11004909 if (!conf->thread) {
4910 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004911 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11004912 mdname(mddev));
4913 goto abort;
4914 }
4915
4916 return conf;
4917
4918 abort:
4919 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10004920 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11004921 return ERR_PTR(-EIO);
4922 } else
4923 return ERR_PTR(-ENOMEM);
4924}
4925
NeilBrownc148ffd2009-11-13 17:47:00 +11004926
4927static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
4928{
4929 switch (algo) {
4930 case ALGORITHM_PARITY_0:
4931 if (raid_disk < max_degraded)
4932 return 1;
4933 break;
4934 case ALGORITHM_PARITY_N:
4935 if (raid_disk >= raid_disks - max_degraded)
4936 return 1;
4937 break;
4938 case ALGORITHM_PARITY_0_6:
4939 if (raid_disk == 0 ||
4940 raid_disk == raid_disks - 1)
4941 return 1;
4942 break;
4943 case ALGORITHM_LEFT_ASYMMETRIC_6:
4944 case ALGORITHM_RIGHT_ASYMMETRIC_6:
4945 case ALGORITHM_LEFT_SYMMETRIC_6:
4946 case ALGORITHM_RIGHT_SYMMETRIC_6:
4947 if (raid_disk == raid_disks - 1)
4948 return 1;
4949 }
4950 return 0;
4951}
4952
NeilBrownfd01b882011-10-11 16:47:53 +11004953static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11004954{
NeilBrownd1688a62011-10-11 16:49:52 +11004955 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10004956 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11004957 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11004958 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11004959 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11004960 int i;
NeilBrown91adb562009-03-31 14:39:39 +11004961
Andre Noll8c6ac862009-06-18 08:48:06 +10004962 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10004963 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10004964 " -- starting background reconstruction\n",
4965 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004966 if (mddev->reshape_position != MaxSector) {
4967 /* Check that we can continue the reshape.
4968 * Currently only disks can change, it must
4969 * increase, and we must be past the point where
4970 * a stripe over-writes itself
4971 */
4972 sector_t here_new, here_old;
4973 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11004974 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08004975
NeilBrown88ce4932009-03-31 15:24:23 +11004976 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10004977 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08004978 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08004979 mdname(mddev));
4980 return -EINVAL;
4981 }
NeilBrownf6705572006-03-27 01:18:11 -08004982 old_disks = mddev->raid_disks - mddev->delta_disks;
4983 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08004984 * further up in new geometry must map after here in old
4985 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08004986 */
4987 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10004988 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08004989 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004990 printk(KERN_ERR "md/raid:%s: reshape_position not "
4991 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004992 return -EINVAL;
4993 }
NeilBrownc148ffd2009-11-13 17:47:00 +11004994 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08004995 /* here_new is the stripe we will write to */
4996 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10004997 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08004998 (old_disks-max_degraded));
4999 /* here_old is the first stripe that we might need to read
5000 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10005001 if (mddev->delta_disks == 0) {
5002 /* We cannot be sure it is safe to start an in-place
5003 * reshape. It is only safe if user-space if monitoring
5004 * and taking constant backups.
5005 * mdadm always starts a situation like this in
5006 * readonly mode so it can take control before
5007 * allowing any writes. So just check for that.
5008 */
5009 if ((here_new * mddev->new_chunk_sectors !=
5010 here_old * mddev->chunk_sectors) ||
5011 mddev->ro == 0) {
NeilBrown0c55e022010-05-03 14:09:02 +10005012 printk(KERN_ERR "md/raid:%s: in-place reshape must be started"
5013 " in read-only mode - aborting\n",
5014 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005015 return -EINVAL;
5016 }
5017 } else if (mddev->delta_disks < 0
5018 ? (here_new * mddev->new_chunk_sectors <=
5019 here_old * mddev->chunk_sectors)
5020 : (here_new * mddev->new_chunk_sectors >=
5021 here_old * mddev->chunk_sectors)) {
NeilBrownf6705572006-03-27 01:18:11 -08005022 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005023 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5024 "auto-recovery - aborting.\n",
5025 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005026 return -EINVAL;
5027 }
NeilBrown0c55e022010-05-03 14:09:02 +10005028 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5029 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005030 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005031 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005032 BUG_ON(mddev->level != mddev->new_level);
5033 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005034 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005035 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005036 }
5037
NeilBrown245f46c2009-03-31 14:39:39 +11005038 if (mddev->private == NULL)
5039 conf = setup_conf(mddev);
5040 else
5041 conf = mddev->private;
5042
NeilBrown91adb562009-03-31 14:39:39 +11005043 if (IS_ERR(conf))
5044 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005045
NeilBrown91adb562009-03-31 14:39:39 +11005046 mddev->thread = conf->thread;
5047 conf->thread = NULL;
5048 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049
NeilBrown17045f52011-12-23 10:17:53 +11005050 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5051 i++) {
5052 rdev = conf->disks[i].rdev;
5053 if (!rdev && conf->disks[i].replacement) {
5054 /* The replacement is all we have yet */
5055 rdev = conf->disks[i].replacement;
5056 conf->disks[i].replacement = NULL;
5057 clear_bit(Replacement, &rdev->flags);
5058 conf->disks[i].rdev = rdev;
5059 }
5060 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11005061 continue;
NeilBrown17045f52011-12-23 10:17:53 +11005062 if (conf->disks[i].replacement &&
5063 conf->reshape_progress != MaxSector) {
5064 /* replacements and reshape simply do not mix. */
5065 printk(KERN_ERR "md: cannot handle concurrent "
5066 "replacement and reshape.\n");
5067 goto abort;
5068 }
NeilBrown2f115882010-06-17 17:41:03 +10005069 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005070 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005071 continue;
5072 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005073 /* This disc is not fully in-sync. However if it
5074 * just stored parity (beyond the recovery_offset),
5075 * when we don't need to be concerned about the
5076 * array being dirty.
5077 * When reshape goes 'backwards', we never have
5078 * partially completed devices, so we only need
5079 * to worry about reshape going forwards.
5080 */
5081 /* Hack because v0.91 doesn't store recovery_offset properly. */
5082 if (mddev->major_version == 0 &&
5083 mddev->minor_version > 90)
5084 rdev->recovery_offset = reshape_offset;
5085
NeilBrownc148ffd2009-11-13 17:47:00 +11005086 if (rdev->recovery_offset < reshape_offset) {
5087 /* We need to check old and new layout */
5088 if (!only_parity(rdev->raid_disk,
5089 conf->algorithm,
5090 conf->raid_disks,
5091 conf->max_degraded))
5092 continue;
5093 }
5094 if (!only_parity(rdev->raid_disk,
5095 conf->prev_algo,
5096 conf->previous_raid_disks,
5097 conf->max_degraded))
5098 continue;
5099 dirty_parity_disks++;
5100 }
NeilBrown91adb562009-03-31 14:39:39 +11005101
NeilBrown17045f52011-12-23 10:17:53 +11005102 /*
5103 * 0 for a fully functional array, 1 or 2 for a degraded array.
5104 */
NeilBrown908f4fb2011-12-23 10:17:50 +11005105 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106
NeilBrown674806d2010-06-16 17:17:53 +10005107 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005108 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07005110 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 goto abort;
5112 }
5113
NeilBrown91adb562009-03-31 14:39:39 +11005114 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10005115 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11005116 mddev->resync_max_sectors = mddev->dev_sectors;
5117
NeilBrownc148ffd2009-11-13 17:47:00 +11005118 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8e2006-01-06 00:20:15 -08005120 if (mddev->ok_start_degraded)
5121 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10005122 "md/raid:%s: starting dirty degraded array"
5123 " - data corruption possible.\n",
NeilBrown6ff8d8e2006-01-06 00:20:15 -08005124 mdname(mddev));
5125 else {
5126 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005127 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8e2006-01-06 00:20:15 -08005128 mdname(mddev));
5129 goto abort;
5130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 }
5132
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10005134 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5135 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11005136 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5137 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 else
NeilBrown0c55e022010-05-03 14:09:02 +10005139 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5140 " out of %d devices, algorithm %d\n",
5141 mdname(mddev), conf->level,
5142 mddev->raid_disks - mddev->degraded,
5143 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144
5145 print_raid5_conf(conf);
5146
NeilBrownfef9c612009-03-31 15:16:46 +11005147 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11005148 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005149 atomic_set(&conf->reshape_stripes, 0);
5150 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5151 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5152 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5153 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5154 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005155 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005156 }
5157
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158
5159 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005160 if (mddev->to_remove == &raid5_attrs_group)
5161 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005162 else if (mddev->kobj.sd &&
5163 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005164 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10005165 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08005166 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10005167 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5168
5169 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005170 int chunk_size;
NeilBrown4a5add42010-06-01 19:37:28 +10005171 /* read-ahead size must cover two whole stripes, which
5172 * is 2 * (datadisks) * chunksize where 'n' is the
5173 * number of raid devices
5174 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5176 int stripe = data_disks *
5177 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5178 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5179 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10005180
5181 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005182
5183 mddev->queue->backing_dev_info.congested_data = mddev;
5184 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10005185
5186 chunk_size = mddev->chunk_sectors << 9;
5187 blk_queue_io_min(mddev->queue, chunk_size);
5188 blk_queue_io_opt(mddev->queue, chunk_size *
5189 (conf->raid_disks - conf->max_degraded));
5190
NeilBrowndafb20f2012-03-19 12:46:39 +11005191 rdev_for_each(rdev, mddev)
NeilBrown9f7c2222010-07-26 12:04:13 +10005192 disk_stack_limits(mddev->gendisk, rdev->bdev,
5193 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 }
5195
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 return 0;
5197abort:
NeilBrown01f96c02011-09-21 15:30:20 +10005198 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11005199 print_raid5_conf(conf);
5200 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005202 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 return -EIO;
5204}
5205
NeilBrownfd01b882011-10-11 16:47:53 +11005206static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207{
NeilBrownd1688a62011-10-11 16:49:52 +11005208 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209
NeilBrown01f96c02011-09-21 15:30:20 +10005210 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005211 if (mddev->queue)
5212 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10005213 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005214 mddev->private = NULL;
5215 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 return 0;
5217}
5218
NeilBrownfd01b882011-10-11 16:47:53 +11005219static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220{
NeilBrownd1688a62011-10-11 16:49:52 +11005221 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 int i;
5223
Andre Noll9d8f0362009-06-18 08:45:01 +10005224 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5225 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005226 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 for (i = 0; i < conf->raid_disks; i++)
5228 seq_printf (seq, "%s",
5229 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005230 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232}
5233
NeilBrownd1688a62011-10-11 16:49:52 +11005234static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235{
5236 int i;
5237 struct disk_info *tmp;
5238
NeilBrown0c55e022010-05-03 14:09:02 +10005239 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240 if (!conf) {
5241 printk("(conf==NULL)\n");
5242 return;
5243 }
NeilBrown0c55e022010-05-03 14:09:02 +10005244 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5245 conf->raid_disks,
5246 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247
5248 for (i = 0; i < conf->raid_disks; i++) {
5249 char b[BDEVNAME_SIZE];
5250 tmp = conf->disks + i;
5251 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005252 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5253 i, !test_bit(Faulty, &tmp->rdev->flags),
5254 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 }
5256}
5257
NeilBrownfd01b882011-10-11 16:47:53 +11005258static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259{
5260 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005261 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005263 int count = 0;
5264 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265
5266 for (i = 0; i < conf->raid_disks; i++) {
5267 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11005268 if (tmp->replacement
5269 && tmp->replacement->recovery_offset == MaxSector
5270 && !test_bit(Faulty, &tmp->replacement->flags)
5271 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
5272 /* Replacement has just become active. */
5273 if (!tmp->rdev
5274 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
5275 count++;
5276 if (tmp->rdev) {
5277 /* Replaced device not technically faulty,
5278 * but we need to be sure it gets removed
5279 * and never re-added.
5280 */
5281 set_bit(Faulty, &tmp->rdev->flags);
5282 sysfs_notify_dirent_safe(
5283 tmp->rdev->sysfs_state);
5284 }
5285 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
5286 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005287 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005288 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005289 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005290 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005291 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 }
5293 }
NeilBrown6b965622010-08-18 11:56:59 +10005294 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005295 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005296 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005298 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299}
5300
NeilBrownb8321b62011-12-23 10:17:51 +11005301static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302{
NeilBrownd1688a62011-10-11 16:49:52 +11005303 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11005305 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11005306 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 struct disk_info *p = conf->disks + number;
5308
5309 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11005310 if (rdev == p->rdev)
5311 rdevp = &p->rdev;
5312 else if (rdev == p->replacement)
5313 rdevp = &p->replacement;
5314 else
5315 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11005316
NeilBrown657e3e42011-12-23 10:17:52 +11005317 if (number >= conf->raid_disks &&
5318 conf->reshape_progress == MaxSector)
5319 clear_bit(In_sync, &rdev->flags);
5320
5321 if (test_bit(In_sync, &rdev->flags) ||
5322 atomic_read(&rdev->nr_pending)) {
5323 err = -EBUSY;
5324 goto abort;
5325 }
5326 /* Only remove non-faulty devices if recovery
5327 * isn't possible.
5328 */
5329 if (!test_bit(Faulty, &rdev->flags) &&
5330 mddev->recovery_disabled != conf->recovery_disabled &&
5331 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11005332 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11005333 number < conf->raid_disks) {
5334 err = -EBUSY;
5335 goto abort;
5336 }
5337 *rdevp = NULL;
5338 synchronize_rcu();
5339 if (atomic_read(&rdev->nr_pending)) {
5340 /* lost the race, try later */
5341 err = -EBUSY;
5342 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11005343 } else if (p->replacement) {
5344 /* We must have just cleared 'rdev' */
5345 p->rdev = p->replacement;
5346 clear_bit(Replacement, &p->replacement->flags);
5347 smp_mb(); /* Make sure other CPUs may see both as identical
5348 * but will never see neither - if they are careful
5349 */
5350 p->replacement = NULL;
5351 clear_bit(WantReplacement, &rdev->flags);
5352 } else
5353 /* We might have just removed the Replacement as faulty-
5354 * clear the bit just in case
5355 */
5356 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357abort:
5358
5359 print_raid5_conf(conf);
5360 return err;
5361}
5362
NeilBrownfd01b882011-10-11 16:47:53 +11005363static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364{
NeilBrownd1688a62011-10-11 16:49:52 +11005365 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005366 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367 int disk;
5368 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005369 int first = 0;
5370 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371
NeilBrown7f0da592011-07-28 11:39:22 +10005372 if (mddev->recovery_disabled == conf->recovery_disabled)
5373 return -EBUSY;
5374
NeilBrowndc10c642012-03-19 12:46:37 +11005375 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005377 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378
Neil Brown6c2fce22008-06-28 08:31:31 +10005379 if (rdev->raid_disk >= 0)
5380 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381
5382 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005383 * find the disk ... but prefer rdev->saved_raid_disk
5384 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005386 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005387 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005388 conf->disks[rdev->saved_raid_disk].rdev == NULL)
5389 disk = rdev->saved_raid_disk;
5390 else
Neil Brown6c2fce22008-06-28 08:31:31 +10005391 disk = first;
NeilBrown7bfec5f2011-12-23 10:17:53 +11005392 for ( ; disk <= last ; disk++) {
5393 p = conf->disks + disk;
5394 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005395 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005397 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005398 if (rdev->saved_raid_disk != disk)
5399 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005400 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 break;
5402 }
NeilBrown7bfec5f2011-12-23 10:17:53 +11005403 if (test_bit(WantReplacement, &p->rdev->flags) &&
5404 p->replacement == NULL) {
5405 clear_bit(In_sync, &rdev->flags);
5406 set_bit(Replacement, &rdev->flags);
5407 rdev->raid_disk = disk;
5408 err = 0;
5409 conf->fullsync = 1;
5410 rcu_assign_pointer(p->replacement, rdev);
5411 break;
5412 }
5413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005415 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416}
5417
NeilBrownfd01b882011-10-11 16:47:53 +11005418static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419{
5420 /* no resync is happening, and there is enough space
5421 * on all devices, so we can resize.
5422 * We need to make sure resync covers any new space.
5423 * If the array is shrinking we should possibly wait until
5424 * any io in the removed space completes, but it hardly seems
5425 * worth it.
5426 */
Andre Noll9d8f0362009-06-18 08:45:01 +10005427 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Dan Williams1f403622009-03-31 14:59:03 +11005428 md_set_array_sectors(mddev, raid5_size(mddev, sectors,
5429 mddev->raid_disks));
Dan Williamsb522adc2009-03-31 15:00:31 +11005430 if (mddev->array_sectors >
5431 raid5_size(mddev, sectors, mddev->raid_disks))
5432 return -EINVAL;
Andre Nollf233ea52008-07-21 17:05:22 +10005433 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005434 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005435 if (sectors > mddev->dev_sectors &&
5436 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005437 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5439 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005440 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005441 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 return 0;
5443}
5444
NeilBrownfd01b882011-10-11 16:47:53 +11005445static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005446{
5447 /* Can only proceed if there are plenty of stripe_heads.
5448 * We need a minimum of one full stripe,, and for sensible progress
5449 * it is best to have about 4 times that.
5450 * If we require 4 times, then the default 256 4K stripe_heads will
5451 * allow for chunk sizes up to 256K, which is probably OK.
5452 * If the chunk size is greater, user-space should request more
5453 * stripe_heads first.
5454 */
NeilBrownd1688a62011-10-11 16:49:52 +11005455 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005456 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5457 > conf->max_nr_stripes ||
5458 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5459 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005460 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5461 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005462 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5463 / STRIPE_SIZE)*4);
5464 return 0;
5465 }
5466 return 1;
5467}
5468
NeilBrownfd01b882011-10-11 16:47:53 +11005469static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005470{
NeilBrownd1688a62011-10-11 16:49:52 +11005471 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005472
NeilBrown88ce4932009-03-31 15:24:23 +11005473 if (mddev->delta_disks == 0 &&
5474 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005475 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005476 return 0; /* nothing to do */
NeilBrowndba034e2008-08-05 15:54:13 +10005477 if (mddev->bitmap)
5478 /* Cannot grow a bitmap yet */
5479 return -EBUSY;
NeilBrown674806d2010-06-16 17:17:53 +10005480 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005481 return -EINVAL;
5482 if (mddev->delta_disks < 0) {
5483 /* We might be able to shrink, but the devices must
5484 * be made bigger first.
5485 * For raid6, 4 is the minimum size.
5486 * Otherwise 2 is the minimum
5487 */
5488 int min = 2;
5489 if (mddev->level == 6)
5490 min = 4;
5491 if (mddev->raid_disks + mddev->delta_disks < min)
5492 return -EINVAL;
5493 }
NeilBrown29269552006-03-27 01:18:10 -08005494
NeilBrown01ee22b2009-06-18 08:47:20 +10005495 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005496 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005497
NeilBrownec32a2b2009-03-31 15:17:38 +11005498 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
NeilBrown63c70c42006-03-27 01:18:13 -08005499}
5500
NeilBrownfd01b882011-10-11 16:47:53 +11005501static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005502{
NeilBrownd1688a62011-10-11 16:49:52 +11005503 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005504 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005505 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005506 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005507
NeilBrownf4168852007-02-28 20:11:53 -08005508 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005509 return -EBUSY;
5510
NeilBrown01ee22b2009-06-18 08:47:20 +10005511 if (!check_stripe_cache(mddev))
5512 return -ENOSPC;
5513
NeilBrowndafb20f2012-03-19 12:46:39 +11005514 rdev_for_each(rdev, mddev)
NeilBrown469518a2011-01-31 11:57:43 +11005515 if (!test_bit(In_sync, &rdev->flags)
5516 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005517 spares++;
NeilBrown63c70c42006-03-27 01:18:13 -08005518
NeilBrownf4168852007-02-28 20:11:53 -08005519 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005520 /* Not enough devices even to make a degraded array
5521 * of that size
5522 */
5523 return -EINVAL;
5524
NeilBrownec32a2b2009-03-31 15:17:38 +11005525 /* Refuse to reduce size of the array. Any reductions in
5526 * array size must be through explicit setting of array_size
5527 * attribute.
5528 */
5529 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5530 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10005531 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11005532 "before number of disks\n", mdname(mddev));
5533 return -EINVAL;
5534 }
5535
NeilBrownf6705572006-03-27 01:18:11 -08005536 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005537 spin_lock_irq(&conf->device_lock);
5538 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005539 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005540 conf->prev_chunk_sectors = conf->chunk_sectors;
5541 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005542 conf->prev_algo = conf->algorithm;
5543 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11005544 if (mddev->delta_disks < 0)
5545 conf->reshape_progress = raid5_size(mddev, 0, 0);
5546 else
5547 conf->reshape_progress = 0;
5548 conf->reshape_safe = conf->reshape_progress;
NeilBrown86b42c72009-03-31 15:19:03 +11005549 conf->generation++;
NeilBrown29269552006-03-27 01:18:10 -08005550 spin_unlock_irq(&conf->device_lock);
5551
5552 /* Add some new drives, as many as will fit.
5553 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10005554 * Don't add devices if we are reducing the number of
5555 * devices in the array. This is because it is not possible
5556 * to correctly record the "partially reconstructed" state of
5557 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08005558 */
NeilBrown87a8dec2011-01-31 11:57:43 +11005559 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11005560 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11005561 if (rdev->raid_disk < 0 &&
5562 !test_bit(Faulty, &rdev->flags)) {
5563 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11005564 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11005565 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11005566 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11005567 else
NeilBrown87a8dec2011-01-31 11:57:43 +11005568 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10005569
5570 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11005571 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11005572 }
NeilBrown87a8dec2011-01-31 11:57:43 +11005573 } else if (rdev->raid_disk >= conf->previous_raid_disks
5574 && !test_bit(Faulty, &rdev->flags)) {
5575 /* This is a spare that was manually added */
5576 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11005577 }
NeilBrown29269552006-03-27 01:18:10 -08005578
NeilBrown87a8dec2011-01-31 11:57:43 +11005579 /* When a reshape changes the number of devices,
5580 * ->degraded is measured against the larger of the
5581 * pre and post number of devices.
5582 */
NeilBrownec32a2b2009-03-31 15:17:38 +11005583 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005584 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11005585 spin_unlock_irqrestore(&conf->device_lock, flags);
5586 }
NeilBrown63c70c42006-03-27 01:18:13 -08005587 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10005588 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07005589 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005590
NeilBrown29269552006-03-27 01:18:10 -08005591 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5592 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5593 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5594 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5595 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005596 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08005597 if (!mddev->sync_thread) {
5598 mddev->recovery = 0;
5599 spin_lock_irq(&conf->device_lock);
5600 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005601 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11005602 mddev->reshape_position = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005603 spin_unlock_irq(&conf->device_lock);
5604 return -EAGAIN;
5605 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005606 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005607 md_wakeup_thread(mddev->sync_thread);
5608 md_new_event(mddev);
5609 return 0;
5610}
NeilBrown29269552006-03-27 01:18:10 -08005611
NeilBrownec32a2b2009-03-31 15:17:38 +11005612/* This is called from the reshape thread and should make any
5613 * changes needed in 'conf'
5614 */
NeilBrownd1688a62011-10-11 16:49:52 +11005615static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08005616{
NeilBrown29269552006-03-27 01:18:10 -08005617
NeilBrownf6705572006-03-27 01:18:11 -08005618 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
Dan Williams80c3a6c2009-03-17 18:10:40 -07005619
NeilBrownf6705572006-03-27 01:18:11 -08005620 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11005621 conf->previous_raid_disks = conf->raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005622 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08005623 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005624 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07005625
5626 /* read-ahead size must cover two whole stripes, which is
5627 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5628 */
NeilBrown4a5add42010-06-01 19:37:28 +10005629 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11005630 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005631 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11005632 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07005633 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5634 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5635 }
NeilBrown29269552006-03-27 01:18:10 -08005636 }
NeilBrown29269552006-03-27 01:18:10 -08005637}
5638
NeilBrownec32a2b2009-03-31 15:17:38 +11005639/* This is called from the raid5d thread with mddev_lock held.
5640 * It makes config changes to the device.
5641 */
NeilBrownfd01b882011-10-11 16:47:53 +11005642static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11005643{
NeilBrownd1688a62011-10-11 16:49:52 +11005644 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11005645
5646 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5647
NeilBrownec32a2b2009-03-31 15:17:38 +11005648 if (mddev->delta_disks > 0) {
5649 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5650 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005651 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11005652 } else {
5653 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11005654 spin_lock_irq(&conf->device_lock);
5655 mddev->degraded = calc_degraded(conf);
5656 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11005657 for (d = conf->raid_disks ;
5658 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10005659 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11005660 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownb8321b62011-12-23 10:17:51 +11005661 if (rdev &&
5662 raid5_remove_disk(mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10005663 sysfs_unlink_rdev(mddev, rdev);
NeilBrown1a67dde2009-08-13 10:41:49 +10005664 rdev->raid_disk = -1;
5665 }
5666 }
NeilBrowncea9c222009-03-31 15:15:05 +11005667 }
NeilBrown88ce4932009-03-31 15:24:23 +11005668 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005669 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11005670 mddev->reshape_position = MaxSector;
5671 mddev->delta_disks = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11005672 }
5673}
5674
NeilBrownfd01b882011-10-11 16:47:53 +11005675static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07005676{
NeilBrownd1688a62011-10-11 16:49:52 +11005677 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07005678
5679 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08005680 case 2: /* resume for a suspend */
5681 wake_up(&conf->wait_for_overlap);
5682 break;
5683
NeilBrown72626682005-09-09 16:23:54 -07005684 case 1: /* stop all writes */
5685 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005686 /* '2' tells resync/reshape to pause so that all
5687 * active stripes can drain
5688 */
5689 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07005690 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005691 atomic_read(&conf->active_stripes) == 0 &&
5692 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07005693 conf->device_lock, /* nothing */);
NeilBrown64bd6602009-08-03 10:59:58 +10005694 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07005695 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005696 /* allow reshape to continue */
5697 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005698 break;
5699
5700 case 0: /* re-enable writes */
5701 spin_lock_irq(&conf->device_lock);
5702 conf->quiesce = 0;
5703 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08005704 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005705 spin_unlock_irq(&conf->device_lock);
5706 break;
5707 }
NeilBrown72626682005-09-09 16:23:54 -07005708}
NeilBrownb15c2e52006-01-06 00:20:16 -08005709
NeilBrownd562b0c2009-03-31 14:39:39 +11005710
NeilBrownfd01b882011-10-11 16:47:53 +11005711static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11005712{
NeilBrowne373ab12011-10-11 16:48:59 +11005713 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07005714 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11005715
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005716 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11005717 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10005718 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
5719 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005720 return ERR_PTR(-EINVAL);
5721 }
5722
NeilBrowne373ab12011-10-11 16:48:59 +11005723 sectors = raid0_conf->strip_zone[0].zone_end;
5724 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10005725 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005726 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11005727 mddev->new_layout = ALGORITHM_PARITY_N;
5728 mddev->new_chunk_sectors = mddev->chunk_sectors;
5729 mddev->raid_disks += 1;
5730 mddev->delta_disks = 1;
5731 /* make sure it will be not marked as dirty */
5732 mddev->recovery_cp = MaxSector;
5733
5734 return setup_conf(mddev);
5735}
5736
5737
NeilBrownfd01b882011-10-11 16:47:53 +11005738static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005739{
5740 int chunksect;
5741
5742 if (mddev->raid_disks != 2 ||
5743 mddev->degraded > 1)
5744 return ERR_PTR(-EINVAL);
5745
5746 /* Should check if there are write-behind devices? */
5747
5748 chunksect = 64*2; /* 64K by default */
5749
5750 /* The array must be an exact multiple of chunksize */
5751 while (chunksect && (mddev->array_sectors & (chunksect-1)))
5752 chunksect >>= 1;
5753
5754 if ((chunksect<<9) < STRIPE_SIZE)
5755 /* array size does not allow a suitable chunk size */
5756 return ERR_PTR(-EINVAL);
5757
5758 mddev->new_level = 5;
5759 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10005760 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11005761
5762 return setup_conf(mddev);
5763}
5764
NeilBrownfd01b882011-10-11 16:47:53 +11005765static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11005766{
5767 int new_layout;
5768
5769 switch (mddev->layout) {
5770 case ALGORITHM_LEFT_ASYMMETRIC_6:
5771 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
5772 break;
5773 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5774 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
5775 break;
5776 case ALGORITHM_LEFT_SYMMETRIC_6:
5777 new_layout = ALGORITHM_LEFT_SYMMETRIC;
5778 break;
5779 case ALGORITHM_RIGHT_SYMMETRIC_6:
5780 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
5781 break;
5782 case ALGORITHM_PARITY_0_6:
5783 new_layout = ALGORITHM_PARITY_0;
5784 break;
5785 case ALGORITHM_PARITY_N:
5786 new_layout = ALGORITHM_PARITY_N;
5787 break;
5788 default:
5789 return ERR_PTR(-EINVAL);
5790 }
5791 mddev->new_level = 5;
5792 mddev->new_layout = new_layout;
5793 mddev->delta_disks = -1;
5794 mddev->raid_disks -= 1;
5795 return setup_conf(mddev);
5796}
5797
NeilBrownd562b0c2009-03-31 14:39:39 +11005798
NeilBrownfd01b882011-10-11 16:47:53 +11005799static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11005800{
NeilBrown88ce4932009-03-31 15:24:23 +11005801 /* For a 2-drive array, the layout and chunk size can be changed
5802 * immediately as not restriping is needed.
5803 * For larger arrays we record the new value - after validation
5804 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11005805 */
NeilBrownd1688a62011-10-11 16:49:52 +11005806 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10005807 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11005808
NeilBrown597a7112009-06-18 08:47:42 +10005809 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11005810 return -EINVAL;
5811 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005812 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11005813 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005814 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11005815 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005816 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11005817 /* not factor of array size */
5818 return -EINVAL;
5819 }
5820
5821 /* They look valid */
5822
NeilBrown88ce4932009-03-31 15:24:23 +11005823 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10005824 /* can make the change immediately */
5825 if (mddev->new_layout >= 0) {
5826 conf->algorithm = mddev->new_layout;
5827 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11005828 }
5829 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10005830 conf->chunk_sectors = new_chunk ;
5831 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11005832 }
5833 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5834 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11005835 }
NeilBrown50ac1682009-06-18 08:47:55 +10005836 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11005837}
5838
NeilBrownfd01b882011-10-11 16:47:53 +11005839static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11005840{
NeilBrown597a7112009-06-18 08:47:42 +10005841 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10005842
NeilBrown597a7112009-06-18 08:47:42 +10005843 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11005844 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005845 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005846 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11005847 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005848 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11005849 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005850 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11005851 /* not factor of array size */
5852 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005853 }
NeilBrown88ce4932009-03-31 15:24:23 +11005854
5855 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10005856 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11005857}
5858
NeilBrownfd01b882011-10-11 16:47:53 +11005859static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005860{
5861 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005862 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005863 * raid1 - if there are two drives. We need to know the chunk size
5864 * raid4 - trivial - just use a raid4 layout.
5865 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005866 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005867 if (mddev->level == 0)
5868 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11005869 if (mddev->level == 1)
5870 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11005871 if (mddev->level == 4) {
5872 mddev->new_layout = ALGORITHM_PARITY_N;
5873 mddev->new_level = 5;
5874 return setup_conf(mddev);
5875 }
NeilBrownfc9739c2009-03-31 14:57:20 +11005876 if (mddev->level == 6)
5877 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11005878
5879 return ERR_PTR(-EINVAL);
5880}
5881
NeilBrownfd01b882011-10-11 16:47:53 +11005882static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11005883{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005884 /* raid4 can take over:
5885 * raid0 - if there is only one strip zone
5886 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11005887 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005888 if (mddev->level == 0)
5889 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11005890 if (mddev->level == 5 &&
5891 mddev->layout == ALGORITHM_PARITY_N) {
5892 mddev->new_layout = 0;
5893 mddev->new_level = 4;
5894 return setup_conf(mddev);
5895 }
5896 return ERR_PTR(-EINVAL);
5897}
NeilBrownd562b0c2009-03-31 14:39:39 +11005898
NeilBrown84fc4b52011-10-11 16:49:58 +11005899static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11005900
NeilBrownfd01b882011-10-11 16:47:53 +11005901static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11005902{
5903 /* Currently can only take over a raid5. We map the
5904 * personality to an equivalent raid6 personality
5905 * with the Q block at the end.
5906 */
5907 int new_layout;
5908
5909 if (mddev->pers != &raid5_personality)
5910 return ERR_PTR(-EINVAL);
5911 if (mddev->degraded > 1)
5912 return ERR_PTR(-EINVAL);
5913 if (mddev->raid_disks > 253)
5914 return ERR_PTR(-EINVAL);
5915 if (mddev->raid_disks < 3)
5916 return ERR_PTR(-EINVAL);
5917
5918 switch (mddev->layout) {
5919 case ALGORITHM_LEFT_ASYMMETRIC:
5920 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
5921 break;
5922 case ALGORITHM_RIGHT_ASYMMETRIC:
5923 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
5924 break;
5925 case ALGORITHM_LEFT_SYMMETRIC:
5926 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
5927 break;
5928 case ALGORITHM_RIGHT_SYMMETRIC:
5929 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
5930 break;
5931 case ALGORITHM_PARITY_0:
5932 new_layout = ALGORITHM_PARITY_0_6;
5933 break;
5934 case ALGORITHM_PARITY_N:
5935 new_layout = ALGORITHM_PARITY_N;
5936 break;
5937 default:
5938 return ERR_PTR(-EINVAL);
5939 }
5940 mddev->new_level = 6;
5941 mddev->new_layout = new_layout;
5942 mddev->delta_disks = 1;
5943 mddev->raid_disks += 1;
5944 return setup_conf(mddev);
5945}
5946
5947
NeilBrown84fc4b52011-10-11 16:49:58 +11005948static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07005949{
5950 .name = "raid6",
5951 .level = 6,
5952 .owner = THIS_MODULE,
5953 .make_request = make_request,
5954 .run = run,
5955 .stop = stop,
5956 .status = status,
5957 .error_handler = error,
5958 .hot_add_disk = raid5_add_disk,
5959 .hot_remove_disk= raid5_remove_disk,
5960 .spare_active = raid5_spare_active,
5961 .sync_request = sync_request,
5962 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005963 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10005964 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08005965 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005966 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07005967 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11005968 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07005969};
NeilBrown84fc4b52011-10-11 16:49:58 +11005970static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971{
5972 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08005973 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974 .owner = THIS_MODULE,
5975 .make_request = make_request,
5976 .run = run,
5977 .stop = stop,
5978 .status = status,
5979 .error_handler = error,
5980 .hot_add_disk = raid5_add_disk,
5981 .hot_remove_disk= raid5_remove_disk,
5982 .spare_active = raid5_spare_active,
5983 .sync_request = sync_request,
5984 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005985 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08005986 .check_reshape = raid5_check_reshape,
5987 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005988 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07005989 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11005990 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991};
5992
NeilBrown84fc4b52011-10-11 16:49:58 +11005993static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994{
NeilBrown2604b702006-01-06 00:20:36 -08005995 .name = "raid4",
5996 .level = 4,
5997 .owner = THIS_MODULE,
5998 .make_request = make_request,
5999 .run = run,
6000 .stop = stop,
6001 .status = status,
6002 .error_handler = error,
6003 .hot_add_disk = raid5_add_disk,
6004 .hot_remove_disk= raid5_remove_disk,
6005 .spare_active = raid5_spare_active,
6006 .sync_request = sync_request,
6007 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006008 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08006009 .check_reshape = raid5_check_reshape,
6010 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006011 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08006012 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11006013 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08006014};
6015
6016static int __init raid5_init(void)
6017{
NeilBrown16a53ec2006-06-26 00:27:38 -07006018 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006019 register_md_personality(&raid5_personality);
6020 register_md_personality(&raid4_personality);
6021 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022}
6023
NeilBrown2604b702006-01-06 00:20:36 -08006024static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025{
NeilBrown16a53ec2006-06-26 00:27:38 -07006026 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006027 unregister_md_personality(&raid5_personality);
6028 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029}
6030
6031module_init(raid5_init);
6032module_exit(raid5_exit);
6033MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11006034MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08006036MODULE_ALIAS("md-raid5");
6037MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08006038MODULE_ALIAS("md-level-5");
6039MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07006040MODULE_ALIAS("md-personality-8"); /* RAID6 */
6041MODULE_ALIAS("md-raid6");
6042MODULE_ALIAS("md-level-6");
6043
6044/* This used to be two separate modules, they were: */
6045MODULE_ALIAS("raid5");
6046MODULE_ALIAS("raid6");