blob: 9799be80bf3143fef6420e1c8f456fa46fb786f1 [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 }
586 md_wait_for_blocked_rdev(rdev, conf->mddev);
587 } else {
588 /* Acknowledged bad block - skip the write */
589 rdev_dec_pending(rdev, conf->mddev);
590 rdev = NULL;
591 }
592 }
593
Dan Williams91c00922007-01-02 13:52:30 -0700594 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100595 if (s->syncing || s->expanding || s->expanded
596 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700597 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
598
Dan Williams2b7497f2008-06-28 08:31:52 +1000599 set_bit(STRIPE_IO_STARTED, &sh->state);
600
Dan Williams91c00922007-01-02 13:52:30 -0700601 bi->bi_bdev = rdev->bdev;
602 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700603 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700604 bi->bi_rw, i);
605 atomic_inc(&sh->count);
606 bi->bi_sector = sh->sector + rdev->data_offset;
607 bi->bi_flags = 1 << BIO_UPTODATE;
Dan Williams91c00922007-01-02 13:52:30 -0700608 bi->bi_idx = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700609 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
610 bi->bi_io_vec[0].bv_offset = 0;
611 bi->bi_size = STRIPE_SIZE;
612 bi->bi_next = NULL;
NeilBrown977df362011-12-23 10:17:53 +1100613 if (rrdev)
614 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Dan Williams91c00922007-01-02 13:52:30 -0700615 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100616 }
617 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100618 if (s->syncing || s->expanding || s->expanded
619 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +1100620 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
621
622 set_bit(STRIPE_IO_STARTED, &sh->state);
623
624 rbi->bi_bdev = rrdev->bdev;
625 pr_debug("%s: for %llu schedule op %ld on "
626 "replacement disc %d\n",
627 __func__, (unsigned long long)sh->sector,
628 rbi->bi_rw, i);
629 atomic_inc(&sh->count);
630 rbi->bi_sector = sh->sector + rrdev->data_offset;
631 rbi->bi_flags = 1 << BIO_UPTODATE;
632 rbi->bi_idx = 0;
633 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
634 rbi->bi_io_vec[0].bv_offset = 0;
635 rbi->bi_size = STRIPE_SIZE;
636 rbi->bi_next = NULL;
637 generic_make_request(rbi);
638 }
639 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000640 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700641 set_bit(STRIPE_DEGRADED, &sh->state);
642 pr_debug("skip op %ld on disc %d for sector %llu\n",
643 bi->bi_rw, i, (unsigned long long)sh->sector);
644 clear_bit(R5_LOCKED, &sh->dev[i].flags);
645 set_bit(STRIPE_HANDLE, &sh->state);
646 }
647 }
648}
649
650static struct dma_async_tx_descriptor *
651async_copy_data(int frombio, struct bio *bio, struct page *page,
652 sector_t sector, struct dma_async_tx_descriptor *tx)
653{
654 struct bio_vec *bvl;
655 struct page *bio_page;
656 int i;
657 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700658 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700659 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700660
661 if (bio->bi_sector >= sector)
662 page_offset = (signed)(bio->bi_sector - sector) * 512;
663 else
664 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700665
Dan Williams0403e382009-09-08 17:42:50 -0700666 if (frombio)
667 flags |= ASYNC_TX_FENCE;
668 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
669
Dan Williams91c00922007-01-02 13:52:30 -0700670 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000671 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700672 int clen;
673 int b_offset = 0;
674
675 if (page_offset < 0) {
676 b_offset = -page_offset;
677 page_offset += b_offset;
678 len -= b_offset;
679 }
680
681 if (len > 0 && page_offset + len > STRIPE_SIZE)
682 clen = STRIPE_SIZE - page_offset;
683 else
684 clen = len;
685
686 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000687 b_offset += bvl->bv_offset;
688 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700689 if (frombio)
690 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700691 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700692 else
693 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700694 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700695 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700696 /* chain the operations */
697 submit.depend_tx = tx;
698
Dan Williams91c00922007-01-02 13:52:30 -0700699 if (clen < len) /* hit end of page */
700 break;
701 page_offset += len;
702 }
703
704 return tx;
705}
706
707static void ops_complete_biofill(void *stripe_head_ref)
708{
709 struct stripe_head *sh = stripe_head_ref;
710 struct bio *return_bi = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +1100711 struct r5conf *conf = sh->raid_conf;
Dan Williamse4d84902007-09-24 10:06:13 -0700712 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700713
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700714 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700715 (unsigned long long)sh->sector);
716
717 /* clear completed biofills */
Dan Williams83de75c2008-06-28 08:31:58 +1000718 spin_lock_irq(&conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700719 for (i = sh->disks; i--; ) {
720 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700721
722 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700723 /* and check if we need to reply to a read request,
724 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000725 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700726 */
727 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700728 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700729
Dan Williams91c00922007-01-02 13:52:30 -0700730 BUG_ON(!dev->read);
731 rbi = dev->read;
732 dev->read = NULL;
733 while (rbi && rbi->bi_sector <
734 dev->sector + STRIPE_SECTORS) {
735 rbi2 = r5_next_bio(rbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +0200736 if (!raid5_dec_bi_phys_segments(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700737 rbi->bi_next = return_bi;
738 return_bi = rbi;
739 }
Dan Williams91c00922007-01-02 13:52:30 -0700740 rbi = rbi2;
741 }
742 }
743 }
Dan Williams83de75c2008-06-28 08:31:58 +1000744 spin_unlock_irq(&conf->device_lock);
745 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700746
747 return_io(return_bi);
748
Dan Williamse4d84902007-09-24 10:06:13 -0700749 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700750 release_stripe(sh);
751}
752
753static void ops_run_biofill(struct stripe_head *sh)
754{
755 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +1100756 struct r5conf *conf = sh->raid_conf;
Dan Williamsa08abd82009-06-03 11:43:59 -0700757 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700758 int i;
759
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700760 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700761 (unsigned long long)sh->sector);
762
763 for (i = sh->disks; i--; ) {
764 struct r5dev *dev = &sh->dev[i];
765 if (test_bit(R5_Wantfill, &dev->flags)) {
766 struct bio *rbi;
767 spin_lock_irq(&conf->device_lock);
768 dev->read = rbi = dev->toread;
769 dev->toread = NULL;
770 spin_unlock_irq(&conf->device_lock);
771 while (rbi && rbi->bi_sector <
772 dev->sector + STRIPE_SECTORS) {
773 tx = async_copy_data(0, rbi, dev->page,
774 dev->sector, tx);
775 rbi = r5_next_bio(rbi, dev->sector);
776 }
777 }
778 }
779
780 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700781 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
782 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700783}
784
Dan Williams4e7d2c02009-08-29 19:13:11 -0700785static void mark_target_uptodate(struct stripe_head *sh, int target)
786{
787 struct r5dev *tgt;
788
789 if (target < 0)
790 return;
791
792 tgt = &sh->dev[target];
793 set_bit(R5_UPTODATE, &tgt->flags);
794 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
795 clear_bit(R5_Wantcompute, &tgt->flags);
796}
797
Dan Williamsac6b53b2009-07-14 13:40:19 -0700798static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700799{
800 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700801
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700802 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700803 (unsigned long long)sh->sector);
804
Dan Williamsac6b53b2009-07-14 13:40:19 -0700805 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700806 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700807 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700808
Dan Williamsecc65c92008-06-28 08:31:57 +1000809 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
810 if (sh->check_state == check_state_compute_run)
811 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700812 set_bit(STRIPE_HANDLE, &sh->state);
813 release_stripe(sh);
814}
815
Dan Williamsd6f38f32009-07-14 11:50:52 -0700816/* return a pointer to the address conversion region of the scribble buffer */
817static addr_conv_t *to_addr_conv(struct stripe_head *sh,
818 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700819{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700820 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
821}
822
823static struct dma_async_tx_descriptor *
824ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
825{
Dan Williams91c00922007-01-02 13:52:30 -0700826 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700827 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700828 int target = sh->ops.target;
829 struct r5dev *tgt = &sh->dev[target];
830 struct page *xor_dest = tgt->page;
831 int count = 0;
832 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700833 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700834 int i;
835
836 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700837 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700838 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
839
840 for (i = disks; i--; )
841 if (i != target)
842 xor_srcs[count++] = sh->dev[i].page;
843
844 atomic_inc(&sh->count);
845
Dan Williams0403e382009-09-08 17:42:50 -0700846 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700847 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700848 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700849 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700850 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700851 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700852
Dan Williams91c00922007-01-02 13:52:30 -0700853 return tx;
854}
855
Dan Williamsac6b53b2009-07-14 13:40:19 -0700856/* set_syndrome_sources - populate source buffers for gen_syndrome
857 * @srcs - (struct page *) array of size sh->disks
858 * @sh - stripe_head to parse
859 *
860 * Populates srcs in proper layout order for the stripe and returns the
861 * 'count' of sources to be used in a call to async_gen_syndrome. The P
862 * destination buffer is recorded in srcs[count] and the Q destination
863 * is recorded in srcs[count+1]].
864 */
865static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
866{
867 int disks = sh->disks;
868 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
869 int d0_idx = raid6_d0(sh);
870 int count;
871 int i;
872
873 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100874 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700875
876 count = 0;
877 i = d0_idx;
878 do {
879 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
880
881 srcs[slot] = sh->dev[i].page;
882 i = raid6_next_disk(i, disks);
883 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700884
NeilBrowne4424fe2009-10-16 16:27:34 +1100885 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700886}
887
888static struct dma_async_tx_descriptor *
889ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
890{
891 int disks = sh->disks;
892 struct page **blocks = percpu->scribble;
893 int target;
894 int qd_idx = sh->qd_idx;
895 struct dma_async_tx_descriptor *tx;
896 struct async_submit_ctl submit;
897 struct r5dev *tgt;
898 struct page *dest;
899 int i;
900 int count;
901
902 if (sh->ops.target < 0)
903 target = sh->ops.target2;
904 else if (sh->ops.target2 < 0)
905 target = sh->ops.target;
906 else
907 /* we should only have one valid target */
908 BUG();
909 BUG_ON(target < 0);
910 pr_debug("%s: stripe %llu block: %d\n",
911 __func__, (unsigned long long)sh->sector, target);
912
913 tgt = &sh->dev[target];
914 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
915 dest = tgt->page;
916
917 atomic_inc(&sh->count);
918
919 if (target == qd_idx) {
920 count = set_syndrome_sources(blocks, sh);
921 blocks[count] = NULL; /* regenerating p is not necessary */
922 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700923 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
924 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700925 to_addr_conv(sh, percpu));
926 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
927 } else {
928 /* Compute any data- or p-drive using XOR */
929 count = 0;
930 for (i = disks; i-- ; ) {
931 if (i == target || i == qd_idx)
932 continue;
933 blocks[count++] = sh->dev[i].page;
934 }
935
Dan Williams0403e382009-09-08 17:42:50 -0700936 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
937 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700938 to_addr_conv(sh, percpu));
939 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
940 }
941
942 return tx;
943}
944
945static struct dma_async_tx_descriptor *
946ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
947{
948 int i, count, disks = sh->disks;
949 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
950 int d0_idx = raid6_d0(sh);
951 int faila = -1, failb = -1;
952 int target = sh->ops.target;
953 int target2 = sh->ops.target2;
954 struct r5dev *tgt = &sh->dev[target];
955 struct r5dev *tgt2 = &sh->dev[target2];
956 struct dma_async_tx_descriptor *tx;
957 struct page **blocks = percpu->scribble;
958 struct async_submit_ctl submit;
959
960 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
961 __func__, (unsigned long long)sh->sector, target, target2);
962 BUG_ON(target < 0 || target2 < 0);
963 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
964 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
965
Dan Williams6c910a72009-09-16 12:24:54 -0700966 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -0700967 * slot number conversion for 'faila' and 'failb'
968 */
969 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100970 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700971 count = 0;
972 i = d0_idx;
973 do {
974 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
975
976 blocks[slot] = sh->dev[i].page;
977
978 if (i == target)
979 faila = slot;
980 if (i == target2)
981 failb = slot;
982 i = raid6_next_disk(i, disks);
983 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700984
985 BUG_ON(faila == failb);
986 if (failb < faila)
987 swap(faila, failb);
988 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
989 __func__, (unsigned long long)sh->sector, faila, failb);
990
991 atomic_inc(&sh->count);
992
993 if (failb == syndrome_disks+1) {
994 /* Q disk is one of the missing disks */
995 if (faila == syndrome_disks) {
996 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -0700997 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
998 ops_complete_compute, sh,
999 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +11001000 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001001 STRIPE_SIZE, &submit);
1002 } else {
1003 struct page *dest;
1004 int data_target;
1005 int qd_idx = sh->qd_idx;
1006
1007 /* Missing D+Q: recompute D from P, then recompute Q */
1008 if (target == qd_idx)
1009 data_target = target2;
1010 else
1011 data_target = target;
1012
1013 count = 0;
1014 for (i = disks; i-- ; ) {
1015 if (i == data_target || i == qd_idx)
1016 continue;
1017 blocks[count++] = sh->dev[i].page;
1018 }
1019 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001020 init_async_submit(&submit,
1021 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1022 NULL, NULL, NULL,
1023 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001024 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1025 &submit);
1026
1027 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001028 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1029 ops_complete_compute, sh,
1030 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001031 return async_gen_syndrome(blocks, 0, count+2,
1032 STRIPE_SIZE, &submit);
1033 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001034 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001035 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1036 ops_complete_compute, sh,
1037 to_addr_conv(sh, percpu));
1038 if (failb == syndrome_disks) {
1039 /* We're missing D+P. */
1040 return async_raid6_datap_recov(syndrome_disks+2,
1041 STRIPE_SIZE, faila,
1042 blocks, &submit);
1043 } else {
1044 /* We're missing D+D. */
1045 return async_raid6_2data_recov(syndrome_disks+2,
1046 STRIPE_SIZE, faila, failb,
1047 blocks, &submit);
1048 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001049 }
1050}
1051
1052
Dan Williams91c00922007-01-02 13:52:30 -07001053static void ops_complete_prexor(void *stripe_head_ref)
1054{
1055 struct stripe_head *sh = stripe_head_ref;
1056
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001057 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001058 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001059}
1060
1061static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001062ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1063 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001064{
Dan Williams91c00922007-01-02 13:52:30 -07001065 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001066 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001067 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001068 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001069
1070 /* existing parity data subtracted */
1071 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1072
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001073 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001074 (unsigned long long)sh->sector);
1075
1076 for (i = disks; i--; ) {
1077 struct r5dev *dev = &sh->dev[i];
1078 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001079 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001080 xor_srcs[count++] = dev->page;
1081 }
1082
Dan Williams0403e382009-09-08 17:42:50 -07001083 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001084 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001085 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001086
1087 return tx;
1088}
1089
1090static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001091ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001092{
1093 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001094 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001095
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001096 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001097 (unsigned long long)sh->sector);
1098
1099 for (i = disks; i--; ) {
1100 struct r5dev *dev = &sh->dev[i];
1101 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001102
Dan Williamsd8ee0722008-06-28 08:32:06 +10001103 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001104 struct bio *wbi;
1105
NeilBrowncbe47ec2011-07-26 11:20:35 +10001106 spin_lock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001107 chosen = dev->towrite;
1108 dev->towrite = NULL;
1109 BUG_ON(dev->written);
1110 wbi = dev->written = chosen;
NeilBrowncbe47ec2011-07-26 11:20:35 +10001111 spin_unlock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001112
1113 while (wbi && wbi->bi_sector <
1114 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001115 if (wbi->bi_rw & REQ_FUA)
1116 set_bit(R5_WantFUA, &dev->flags);
Dan Williams91c00922007-01-02 13:52:30 -07001117 tx = async_copy_data(1, wbi, dev->page,
1118 dev->sector, tx);
1119 wbi = r5_next_bio(wbi, dev->sector);
1120 }
1121 }
1122 }
1123
1124 return tx;
1125}
1126
Dan Williamsac6b53b2009-07-14 13:40:19 -07001127static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001128{
1129 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001130 int disks = sh->disks;
1131 int pd_idx = sh->pd_idx;
1132 int qd_idx = sh->qd_idx;
1133 int i;
Tejun Heoe9c74692010-09-03 11:56:18 +02001134 bool fua = false;
Dan Williams91c00922007-01-02 13:52:30 -07001135
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001136 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001137 (unsigned long long)sh->sector);
1138
Tejun Heoe9c74692010-09-03 11:56:18 +02001139 for (i = disks; i--; )
1140 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
1141
Dan Williams91c00922007-01-02 13:52:30 -07001142 for (i = disks; i--; ) {
1143 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001144
Tejun Heoe9c74692010-09-03 11:56:18 +02001145 if (dev->written || i == pd_idx || i == qd_idx) {
Dan Williams91c00922007-01-02 13:52:30 -07001146 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001147 if (fua)
1148 set_bit(R5_WantFUA, &dev->flags);
1149 }
Dan Williams91c00922007-01-02 13:52:30 -07001150 }
1151
Dan Williamsd8ee0722008-06-28 08:32:06 +10001152 if (sh->reconstruct_state == reconstruct_state_drain_run)
1153 sh->reconstruct_state = reconstruct_state_drain_result;
1154 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1155 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1156 else {
1157 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1158 sh->reconstruct_state = reconstruct_state_result;
1159 }
Dan Williams91c00922007-01-02 13:52:30 -07001160
1161 set_bit(STRIPE_HANDLE, &sh->state);
1162 release_stripe(sh);
1163}
1164
1165static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001166ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1167 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001168{
Dan Williams91c00922007-01-02 13:52:30 -07001169 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001170 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001171 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001172 int count = 0, pd_idx = sh->pd_idx, i;
1173 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001174 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001175 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001176
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001177 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001178 (unsigned long long)sh->sector);
1179
1180 /* check if prexor is active which means only process blocks
1181 * that are part of a read-modify-write (written)
1182 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001183 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1184 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001185 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1186 for (i = disks; i--; ) {
1187 struct r5dev *dev = &sh->dev[i];
1188 if (dev->written)
1189 xor_srcs[count++] = dev->page;
1190 }
1191 } else {
1192 xor_dest = sh->dev[pd_idx].page;
1193 for (i = disks; i--; ) {
1194 struct r5dev *dev = &sh->dev[i];
1195 if (i != pd_idx)
1196 xor_srcs[count++] = dev->page;
1197 }
1198 }
1199
Dan Williams91c00922007-01-02 13:52:30 -07001200 /* 1/ if we prexor'd then the dest is reused as a source
1201 * 2/ if we did not prexor then we are redoing the parity
1202 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1203 * for the synchronous xor case
1204 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001205 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001206 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1207
1208 atomic_inc(&sh->count);
1209
Dan Williamsac6b53b2009-07-14 13:40:19 -07001210 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001211 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001212 if (unlikely(count == 1))
1213 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1214 else
1215 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001216}
1217
Dan Williamsac6b53b2009-07-14 13:40:19 -07001218static void
1219ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1220 struct dma_async_tx_descriptor *tx)
1221{
1222 struct async_submit_ctl submit;
1223 struct page **blocks = percpu->scribble;
1224 int count;
1225
1226 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1227
1228 count = set_syndrome_sources(blocks, sh);
1229
1230 atomic_inc(&sh->count);
1231
1232 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1233 sh, to_addr_conv(sh, percpu));
1234 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001235}
1236
1237static void ops_complete_check(void *stripe_head_ref)
1238{
1239 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001240
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001241 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001242 (unsigned long long)sh->sector);
1243
Dan Williamsecc65c92008-06-28 08:31:57 +10001244 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001245 set_bit(STRIPE_HANDLE, &sh->state);
1246 release_stripe(sh);
1247}
1248
Dan Williamsac6b53b2009-07-14 13:40:19 -07001249static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001250{
Dan Williams91c00922007-01-02 13:52:30 -07001251 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001252 int pd_idx = sh->pd_idx;
1253 int qd_idx = sh->qd_idx;
1254 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001255 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001256 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001257 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001258 int count;
1259 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001260
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001261 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001262 (unsigned long long)sh->sector);
1263
Dan Williamsac6b53b2009-07-14 13:40:19 -07001264 count = 0;
1265 xor_dest = sh->dev[pd_idx].page;
1266 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001267 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001268 if (i == pd_idx || i == qd_idx)
1269 continue;
1270 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001271 }
1272
Dan Williamsd6f38f32009-07-14 11:50:52 -07001273 init_async_submit(&submit, 0, NULL, NULL, NULL,
1274 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001275 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001276 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001277
Dan Williams91c00922007-01-02 13:52:30 -07001278 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001279 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1280 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001281}
1282
Dan Williamsac6b53b2009-07-14 13:40:19 -07001283static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1284{
1285 struct page **srcs = percpu->scribble;
1286 struct async_submit_ctl submit;
1287 int count;
1288
1289 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1290 (unsigned long long)sh->sector, checkp);
1291
1292 count = set_syndrome_sources(srcs, sh);
1293 if (!checkp)
1294 srcs[count] = NULL;
1295
1296 atomic_inc(&sh->count);
1297 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1298 sh, to_addr_conv(sh, percpu));
1299 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1300 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1301}
1302
Dan Williams417b8d42009-10-16 16:25:22 +11001303static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001304{
1305 int overlap_clear = 0, i, disks = sh->disks;
1306 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001307 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001308 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001309 struct raid5_percpu *percpu;
1310 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001311
Dan Williamsd6f38f32009-07-14 11:50:52 -07001312 cpu = get_cpu();
1313 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001314 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001315 ops_run_biofill(sh);
1316 overlap_clear++;
1317 }
1318
Dan Williams7b3a8712008-06-28 08:32:09 +10001319 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001320 if (level < 6)
1321 tx = ops_run_compute5(sh, percpu);
1322 else {
1323 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1324 tx = ops_run_compute6_1(sh, percpu);
1325 else
1326 tx = ops_run_compute6_2(sh, percpu);
1327 }
1328 /* terminate the chain if reconstruct is not set to be run */
1329 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001330 async_tx_ack(tx);
1331 }
Dan Williams91c00922007-01-02 13:52:30 -07001332
Dan Williams600aa102008-06-28 08:32:05 +10001333 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001334 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001335
Dan Williams600aa102008-06-28 08:32:05 +10001336 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001337 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001338 overlap_clear++;
1339 }
1340
Dan Williamsac6b53b2009-07-14 13:40:19 -07001341 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1342 if (level < 6)
1343 ops_run_reconstruct5(sh, percpu, tx);
1344 else
1345 ops_run_reconstruct6(sh, percpu, tx);
1346 }
Dan Williams91c00922007-01-02 13:52:30 -07001347
Dan Williamsac6b53b2009-07-14 13:40:19 -07001348 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1349 if (sh->check_state == check_state_run)
1350 ops_run_check_p(sh, percpu);
1351 else if (sh->check_state == check_state_run_q)
1352 ops_run_check_pq(sh, percpu, 0);
1353 else if (sh->check_state == check_state_run_pq)
1354 ops_run_check_pq(sh, percpu, 1);
1355 else
1356 BUG();
1357 }
Dan Williams91c00922007-01-02 13:52:30 -07001358
Dan Williams91c00922007-01-02 13:52:30 -07001359 if (overlap_clear)
1360 for (i = disks; i--; ) {
1361 struct r5dev *dev = &sh->dev[i];
1362 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1363 wake_up(&sh->raid_conf->wait_for_overlap);
1364 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001365 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001366}
1367
Dan Williams417b8d42009-10-16 16:25:22 +11001368#ifdef CONFIG_MULTICORE_RAID456
1369static void async_run_ops(void *param, async_cookie_t cookie)
1370{
1371 struct stripe_head *sh = param;
1372 unsigned long ops_request = sh->ops.request;
1373
1374 clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
1375 wake_up(&sh->ops.wait_for_ops);
1376
1377 __raid_run_ops(sh, ops_request);
1378 release_stripe(sh);
1379}
1380
1381static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1382{
1383 /* since handle_stripe can be called outside of raid5d context
1384 * we need to ensure sh->ops.request is de-staged before another
1385 * request arrives
1386 */
1387 wait_event(sh->ops.wait_for_ops,
1388 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
1389 sh->ops.request = ops_request;
1390
1391 atomic_inc(&sh->count);
1392 async_schedule(async_run_ops, sh);
1393}
1394#else
1395#define raid_run_ops __raid_run_ops
1396#endif
1397
NeilBrownd1688a62011-10-11 16:49:52 +11001398static int grow_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
1400 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001401 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001402 if (!sh)
1403 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001404
NeilBrown3f294f42005-11-08 21:39:25 -08001405 sh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001406 #ifdef CONFIG_MULTICORE_RAID456
1407 init_waitqueue_head(&sh->ops.wait_for_ops);
1408 #endif
NeilBrown3f294f42005-11-08 21:39:25 -08001409
NeilBrowne4e11e32010-06-16 16:45:16 +10001410 if (grow_buffers(sh)) {
1411 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001412 kmem_cache_free(conf->slab_cache, sh);
1413 return 0;
1414 }
1415 /* we just created an active stripe so... */
1416 atomic_set(&sh->count, 1);
1417 atomic_inc(&conf->active_stripes);
1418 INIT_LIST_HEAD(&sh->lru);
1419 release_stripe(sh);
1420 return 1;
1421}
1422
NeilBrownd1688a62011-10-11 16:49:52 +11001423static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001424{
Christoph Lametere18b8902006-12-06 20:33:20 -08001425 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001426 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
NeilBrownf4be6b42010-06-01 19:37:25 +10001428 if (conf->mddev->gendisk)
1429 sprintf(conf->cache_name[0],
1430 "raid%d-%s", conf->level, mdname(conf->mddev));
1431 else
1432 sprintf(conf->cache_name[0],
1433 "raid%d-%p", conf->level, conf->mddev);
1434 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1435
NeilBrownad01c9e2006-03-27 01:18:07 -08001436 conf->active_name = 0;
1437 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001439 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 if (!sc)
1441 return 1;
1442 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001443 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001444 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001445 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 return 0;
1448}
NeilBrown29269552006-03-27 01:18:10 -08001449
Dan Williamsd6f38f32009-07-14 11:50:52 -07001450/**
1451 * scribble_len - return the required size of the scribble region
1452 * @num - total number of disks in the array
1453 *
1454 * The size must be enough to contain:
1455 * 1/ a struct page pointer for each device in the array +2
1456 * 2/ room to convert each entry in (1) to its corresponding dma
1457 * (dma_map_page()) or page (page_address()) address.
1458 *
1459 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1460 * calculate over all devices (not just the data blocks), using zeros in place
1461 * of the P and Q blocks.
1462 */
1463static size_t scribble_len(int num)
1464{
1465 size_t len;
1466
1467 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1468
1469 return len;
1470}
1471
NeilBrownd1688a62011-10-11 16:49:52 +11001472static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001473{
1474 /* Make all the stripes able to hold 'newsize' devices.
1475 * New slots in each stripe get 'page' set to a new page.
1476 *
1477 * This happens in stages:
1478 * 1/ create a new kmem_cache and allocate the required number of
1479 * stripe_heads.
1480 * 2/ gather all the old stripe_heads and tranfer the pages across
1481 * to the new stripe_heads. This will have the side effect of
1482 * freezing the array as once all stripe_heads have been collected,
1483 * no IO will be possible. Old stripe heads are freed once their
1484 * pages have been transferred over, and the old kmem_cache is
1485 * freed when all stripes are done.
1486 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1487 * we simple return a failre status - no need to clean anything up.
1488 * 4/ allocate new pages for the new slots in the new stripe_heads.
1489 * If this fails, we don't bother trying the shrink the
1490 * stripe_heads down again, we just leave them as they are.
1491 * As each stripe_head is processed the new one is released into
1492 * active service.
1493 *
1494 * Once step2 is started, we cannot afford to wait for a write,
1495 * so we use GFP_NOIO allocations.
1496 */
1497 struct stripe_head *osh, *nsh;
1498 LIST_HEAD(newstripes);
1499 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001500 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001501 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001502 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001503 int i;
1504
1505 if (newsize <= conf->pool_size)
1506 return 0; /* never bother to shrink */
1507
Dan Williamsb5470dc2008-06-27 21:44:04 -07001508 err = md_allow_write(conf->mddev);
1509 if (err)
1510 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001511
NeilBrownad01c9e2006-03-27 01:18:07 -08001512 /* Step 1 */
1513 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1514 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001515 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001516 if (!sc)
1517 return -ENOMEM;
1518
1519 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001520 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001521 if (!nsh)
1522 break;
1523
NeilBrownad01c9e2006-03-27 01:18:07 -08001524 nsh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001525 #ifdef CONFIG_MULTICORE_RAID456
1526 init_waitqueue_head(&nsh->ops.wait_for_ops);
1527 #endif
NeilBrownad01c9e2006-03-27 01:18:07 -08001528
1529 list_add(&nsh->lru, &newstripes);
1530 }
1531 if (i) {
1532 /* didn't get enough, give up */
1533 while (!list_empty(&newstripes)) {
1534 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1535 list_del(&nsh->lru);
1536 kmem_cache_free(sc, nsh);
1537 }
1538 kmem_cache_destroy(sc);
1539 return -ENOMEM;
1540 }
1541 /* Step 2 - Must use GFP_NOIO now.
1542 * OK, we have enough stripes, start collecting inactive
1543 * stripes and copying them over
1544 */
1545 list_for_each_entry(nsh, &newstripes, lru) {
1546 spin_lock_irq(&conf->device_lock);
1547 wait_event_lock_irq(conf->wait_for_stripe,
1548 !list_empty(&conf->inactive_list),
1549 conf->device_lock,
NeilBrown482c0832011-04-18 18:25:42 +10001550 );
NeilBrownad01c9e2006-03-27 01:18:07 -08001551 osh = get_free_stripe(conf);
1552 spin_unlock_irq(&conf->device_lock);
1553 atomic_set(&nsh->count, 1);
1554 for(i=0; i<conf->pool_size; i++)
1555 nsh->dev[i].page = osh->dev[i].page;
1556 for( ; i<newsize; i++)
1557 nsh->dev[i].page = NULL;
1558 kmem_cache_free(conf->slab_cache, osh);
1559 }
1560 kmem_cache_destroy(conf->slab_cache);
1561
1562 /* Step 3.
1563 * At this point, we are holding all the stripes so the array
1564 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001565 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001566 */
1567 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1568 if (ndisks) {
1569 for (i=0; i<conf->raid_disks; i++)
1570 ndisks[i] = conf->disks[i];
1571 kfree(conf->disks);
1572 conf->disks = ndisks;
1573 } else
1574 err = -ENOMEM;
1575
Dan Williamsd6f38f32009-07-14 11:50:52 -07001576 get_online_cpus();
1577 conf->scribble_len = scribble_len(newsize);
1578 for_each_present_cpu(cpu) {
1579 struct raid5_percpu *percpu;
1580 void *scribble;
1581
1582 percpu = per_cpu_ptr(conf->percpu, cpu);
1583 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1584
1585 if (scribble) {
1586 kfree(percpu->scribble);
1587 percpu->scribble = scribble;
1588 } else {
1589 err = -ENOMEM;
1590 break;
1591 }
1592 }
1593 put_online_cpus();
1594
NeilBrownad01c9e2006-03-27 01:18:07 -08001595 /* Step 4, return new stripes to service */
1596 while(!list_empty(&newstripes)) {
1597 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1598 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001599
NeilBrownad01c9e2006-03-27 01:18:07 -08001600 for (i=conf->raid_disks; i < newsize; i++)
1601 if (nsh->dev[i].page == NULL) {
1602 struct page *p = alloc_page(GFP_NOIO);
1603 nsh->dev[i].page = p;
1604 if (!p)
1605 err = -ENOMEM;
1606 }
1607 release_stripe(nsh);
1608 }
1609 /* critical section pass, GFP_NOIO no longer needed */
1610
1611 conf->slab_cache = sc;
1612 conf->active_name = 1-conf->active_name;
1613 conf->pool_size = newsize;
1614 return err;
1615}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
NeilBrownd1688a62011-10-11 16:49:52 +11001617static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
1619 struct stripe_head *sh;
1620
NeilBrown3f294f42005-11-08 21:39:25 -08001621 spin_lock_irq(&conf->device_lock);
1622 sh = get_free_stripe(conf);
1623 spin_unlock_irq(&conf->device_lock);
1624 if (!sh)
1625 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001626 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001627 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001628 kmem_cache_free(conf->slab_cache, sh);
1629 atomic_dec(&conf->active_stripes);
1630 return 1;
1631}
1632
NeilBrownd1688a62011-10-11 16:49:52 +11001633static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001634{
1635 while (drop_one_stripe(conf))
1636 ;
1637
NeilBrown29fc7e32006-02-03 03:03:41 -08001638 if (conf->slab_cache)
1639 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 conf->slab_cache = NULL;
1641}
1642
NeilBrown6712ecf2007-09-27 12:47:43 +02001643static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
NeilBrown99c0fb52009-03-31 14:39:38 +11001645 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001646 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001647 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001649 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11001650 struct md_rdev *rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
1653 for (i=0 ; i<disks; i++)
1654 if (bi == &sh->dev[i].req)
1655 break;
1656
Dan Williams45b42332007-07-09 11:56:43 -07001657 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1658 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 uptodate);
1660 if (i == disks) {
1661 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001662 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 }
NeilBrown14a75d32011-12-23 10:17:52 +11001664 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11001665 /* If replacement finished while this request was outstanding,
1666 * 'replacement' might be NULL already.
1667 * In that case it moved down to 'rdev'.
1668 * rdev is not removed until all requests are finished.
1669 */
NeilBrown14a75d32011-12-23 10:17:52 +11001670 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001671 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11001672 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
1674 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001676 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11001677 /* Note that this cannot happen on a
1678 * replacement device. We just fail those on
1679 * any error
1680 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001681 printk_ratelimited(
1682 KERN_INFO
1683 "md/raid:%s: read error corrected"
1684 " (%lu sectors at %llu on %s)\n",
1685 mdname(conf->mddev), STRIPE_SECTORS,
1686 (unsigned long long)(sh->sector
1687 + rdev->data_offset),
1688 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001689 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001690 clear_bit(R5_ReadError, &sh->dev[i].flags);
1691 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1692 }
NeilBrown14a75d32011-12-23 10:17:52 +11001693 if (atomic_read(&rdev->read_errors))
1694 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11001696 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001697 int retry = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001698
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001700 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11001701 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1702 printk_ratelimited(
1703 KERN_WARNING
1704 "md/raid:%s: read error on replacement device "
1705 "(sector %llu on %s).\n",
1706 mdname(conf->mddev),
1707 (unsigned long long)(sh->sector
1708 + rdev->data_offset),
1709 bdn);
1710 else if (conf->mddev->degraded >= conf->max_degraded)
Christian Dietrich8bda4702011-07-27 11:00:36 +10001711 printk_ratelimited(
1712 KERN_WARNING
1713 "md/raid:%s: read error not correctable "
1714 "(sector %llu on %s).\n",
1715 mdname(conf->mddev),
1716 (unsigned long long)(sh->sector
1717 + rdev->data_offset),
1718 bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001719 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
NeilBrown4e5314b2005-11-08 21:39:22 -08001720 /* Oh, no!!! */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001721 printk_ratelimited(
1722 KERN_WARNING
1723 "md/raid:%s: read error NOT corrected!! "
1724 "(sector %llu on %s).\n",
1725 mdname(conf->mddev),
1726 (unsigned long long)(sh->sector
1727 + rdev->data_offset),
1728 bdn);
NeilBrownd6950432006-07-10 04:44:20 -07001729 else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001730 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001731 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001732 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001733 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001734 else
1735 retry = 1;
1736 if (retry)
1737 set_bit(R5_ReadError, &sh->dev[i].flags);
1738 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001739 clear_bit(R5_ReadError, &sh->dev[i].flags);
1740 clear_bit(R5_ReWrite, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001741 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 }
NeilBrown14a75d32011-12-23 10:17:52 +11001744 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1746 set_bit(STRIPE_HANDLE, &sh->state);
1747 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748}
1749
NeilBrownd710e132008-10-13 11:55:12 +11001750static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751{
NeilBrown99c0fb52009-03-31 14:39:38 +11001752 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001753 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001754 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11001755 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001757 sector_t first_bad;
1758 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11001759 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
NeilBrown977df362011-12-23 10:17:53 +11001761 for (i = 0 ; i < disks; i++) {
1762 if (bi == &sh->dev[i].req) {
1763 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 break;
NeilBrown977df362011-12-23 10:17:53 +11001765 }
1766 if (bi == &sh->dev[i].rreq) {
1767 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001768 if (rdev)
1769 replacement = 1;
1770 else
1771 /* rdev was removed and 'replacement'
1772 * replaced it. rdev is not removed
1773 * until all requests are finished.
1774 */
1775 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11001776 break;
1777 }
1778 }
Dan Williams45b42332007-07-09 11:56:43 -07001779 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1781 uptodate);
1782 if (i == disks) {
1783 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001784 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 }
1786
NeilBrown977df362011-12-23 10:17:53 +11001787 if (replacement) {
1788 if (!uptodate)
1789 md_error(conf->mddev, rdev);
1790 else if (is_badblock(rdev, sh->sector,
1791 STRIPE_SECTORS,
1792 &first_bad, &bad_sectors))
1793 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1794 } else {
1795 if (!uptodate) {
1796 set_bit(WriteErrorSeen, &rdev->flags);
1797 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11001798 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1799 set_bit(MD_RECOVERY_NEEDED,
1800 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11001801 } else if (is_badblock(rdev, sh->sector,
1802 STRIPE_SECTORS,
1803 &first_bad, &bad_sectors))
1804 set_bit(R5_MadeGood, &sh->dev[i].flags);
1805 }
1806 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
NeilBrown977df362011-12-23 10:17:53 +11001808 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1809 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001811 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812}
1813
NeilBrown784052e2009-03-31 15:19:07 +11001814static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
NeilBrown784052e2009-03-31 15:19:07 +11001816static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
1818 struct r5dev *dev = &sh->dev[i];
1819
1820 bio_init(&dev->req);
1821 dev->req.bi_io_vec = &dev->vec;
1822 dev->req.bi_vcnt++;
1823 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11001825 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
NeilBrown977df362011-12-23 10:17:53 +11001827 bio_init(&dev->rreq);
1828 dev->rreq.bi_io_vec = &dev->rvec;
1829 dev->rreq.bi_vcnt++;
1830 dev->rreq.bi_max_vecs++;
1831 dev->rreq.bi_private = sh;
1832 dev->rvec.bv_page = dev->page;
1833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001835 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836}
1837
NeilBrownfd01b882011-10-11 16:47:53 +11001838static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839{
1840 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11001841 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11001842 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10001843 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
NeilBrown908f4fb2011-12-23 10:17:50 +11001845 spin_lock_irqsave(&conf->device_lock, flags);
1846 clear_bit(In_sync, &rdev->flags);
1847 mddev->degraded = calc_degraded(conf);
1848 spin_unlock_irqrestore(&conf->device_lock, flags);
1849 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1850
NeilBrownde393cd2011-07-28 11:31:48 +10001851 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001852 set_bit(Faulty, &rdev->flags);
1853 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1854 printk(KERN_ALERT
1855 "md/raid:%s: Disk failure on %s, disabling device.\n"
1856 "md/raid:%s: Operation continuing on %d devices.\n",
1857 mdname(mddev),
1858 bdevname(rdev->bdev, b),
1859 mdname(mddev),
1860 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001861}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863/*
1864 * Input: a 'big' sector number,
1865 * Output: index of the data and parity disk, and the sector # in them.
1866 */
NeilBrownd1688a62011-10-11 16:49:52 +11001867static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001868 int previous, int *dd_idx,
1869 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001871 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001872 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001874 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001875 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001877 int algorithm = previous ? conf->prev_algo
1878 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001879 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1880 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001881 int raid_disks = previous ? conf->previous_raid_disks
1882 : conf->raid_disks;
1883 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
1885 /* First compute the information on this sector */
1886
1887 /*
1888 * Compute the chunk number and the sector offset inside the chunk
1889 */
1890 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1891 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
1893 /*
1894 * Compute the stripe number
1895 */
NeilBrown35f2a592010-04-20 14:13:34 +10001896 stripe = chunk_number;
1897 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10001898 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 /*
1900 * Select the parity disk based on the user selected algorithm.
1901 */
NeilBrown84789552011-07-27 11:00:36 +10001902 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07001903 switch(conf->level) {
1904 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001905 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001906 break;
1907 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001908 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001910 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001911 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 (*dd_idx)++;
1913 break;
1914 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001915 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001916 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 (*dd_idx)++;
1918 break;
1919 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001920 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001921 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 break;
1923 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001924 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001925 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001927 case ALGORITHM_PARITY_0:
1928 pd_idx = 0;
1929 (*dd_idx)++;
1930 break;
1931 case ALGORITHM_PARITY_N:
1932 pd_idx = data_disks;
1933 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11001935 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001936 }
1937 break;
1938 case 6:
1939
NeilBrowne183eae2009-03-31 15:20:22 +11001940 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001941 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001942 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001943 qd_idx = pd_idx + 1;
1944 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001945 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001946 qd_idx = 0;
1947 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001948 (*dd_idx) += 2; /* D D P Q D */
1949 break;
1950 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001951 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001952 qd_idx = pd_idx + 1;
1953 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001954 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001955 qd_idx = 0;
1956 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001957 (*dd_idx) += 2; /* D D P Q D */
1958 break;
1959 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001960 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001961 qd_idx = (pd_idx + 1) % raid_disks;
1962 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001963 break;
1964 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001965 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001966 qd_idx = (pd_idx + 1) % raid_disks;
1967 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001968 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001969
1970 case ALGORITHM_PARITY_0:
1971 pd_idx = 0;
1972 qd_idx = 1;
1973 (*dd_idx) += 2;
1974 break;
1975 case ALGORITHM_PARITY_N:
1976 pd_idx = data_disks;
1977 qd_idx = data_disks + 1;
1978 break;
1979
1980 case ALGORITHM_ROTATING_ZERO_RESTART:
1981 /* Exactly the same as RIGHT_ASYMMETRIC, but or
1982 * of blocks for computing Q is different.
1983 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001984 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11001985 qd_idx = pd_idx + 1;
1986 if (pd_idx == raid_disks-1) {
1987 (*dd_idx)++; /* Q D D D P */
1988 qd_idx = 0;
1989 } else if (*dd_idx >= pd_idx)
1990 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001991 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001992 break;
1993
1994 case ALGORITHM_ROTATING_N_RESTART:
1995 /* Same a left_asymmetric, by first stripe is
1996 * D D D P Q rather than
1997 * Q D D D P
1998 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001999 stripe2 += 1;
2000 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002001 qd_idx = pd_idx + 1;
2002 if (pd_idx == raid_disks-1) {
2003 (*dd_idx)++; /* Q D D D P */
2004 qd_idx = 0;
2005 } else if (*dd_idx >= pd_idx)
2006 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002007 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002008 break;
2009
2010 case ALGORITHM_ROTATING_N_CONTINUE:
2011 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002012 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002013 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2014 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002015 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002016 break;
2017
2018 case ALGORITHM_LEFT_ASYMMETRIC_6:
2019 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002020 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002021 if (*dd_idx >= pd_idx)
2022 (*dd_idx)++;
2023 qd_idx = raid_disks - 1;
2024 break;
2025
2026 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002027 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002028 if (*dd_idx >= pd_idx)
2029 (*dd_idx)++;
2030 qd_idx = raid_disks - 1;
2031 break;
2032
2033 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002034 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002035 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2036 qd_idx = raid_disks - 1;
2037 break;
2038
2039 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002040 pd_idx = 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_PARITY_0_6:
2046 pd_idx = 0;
2047 (*dd_idx)++;
2048 qd_idx = raid_disks - 1;
2049 break;
2050
NeilBrown16a53ec2006-06-26 00:27:38 -07002051 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002052 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002053 }
2054 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 }
2056
NeilBrown911d4ee2009-03-31 14:39:38 +11002057 if (sh) {
2058 sh->pd_idx = pd_idx;
2059 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002060 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 /*
2063 * Finally, compute the new sector number
2064 */
2065 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2066 return new_sector;
2067}
2068
2069
NeilBrown784052e2009-03-31 15:19:07 +11002070static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071{
NeilBrownd1688a62011-10-11 16:49:52 +11002072 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002073 int raid_disks = sh->disks;
2074 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002076 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2077 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002078 int algorithm = previous ? conf->prev_algo
2079 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 sector_t stripe;
2081 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002082 sector_t chunk_number;
2083 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002085 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
NeilBrown16a53ec2006-06-26 00:27:38 -07002087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2089 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
NeilBrown16a53ec2006-06-26 00:27:38 -07002091 if (i == sh->pd_idx)
2092 return 0;
2093 switch(conf->level) {
2094 case 4: break;
2095 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002096 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 case ALGORITHM_LEFT_ASYMMETRIC:
2098 case ALGORITHM_RIGHT_ASYMMETRIC:
2099 if (i > sh->pd_idx)
2100 i--;
2101 break;
2102 case ALGORITHM_LEFT_SYMMETRIC:
2103 case ALGORITHM_RIGHT_SYMMETRIC:
2104 if (i < sh->pd_idx)
2105 i += raid_disks;
2106 i -= (sh->pd_idx + 1);
2107 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002108 case ALGORITHM_PARITY_0:
2109 i -= 1;
2110 break;
2111 case ALGORITHM_PARITY_N:
2112 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002114 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002115 }
2116 break;
2117 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002118 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002119 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002120 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002121 case ALGORITHM_LEFT_ASYMMETRIC:
2122 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002123 case ALGORITHM_ROTATING_ZERO_RESTART:
2124 case ALGORITHM_ROTATING_N_RESTART:
2125 if (sh->pd_idx == raid_disks-1)
2126 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002127 else if (i > sh->pd_idx)
2128 i -= 2; /* D D P Q D */
2129 break;
2130 case ALGORITHM_LEFT_SYMMETRIC:
2131 case ALGORITHM_RIGHT_SYMMETRIC:
2132 if (sh->pd_idx == raid_disks-1)
2133 i--; /* Q D D D P */
2134 else {
2135 /* D D P Q D */
2136 if (i < sh->pd_idx)
2137 i += raid_disks;
2138 i -= (sh->pd_idx + 2);
2139 }
2140 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002141 case ALGORITHM_PARITY_0:
2142 i -= 2;
2143 break;
2144 case ALGORITHM_PARITY_N:
2145 break;
2146 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002147 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002148 if (sh->pd_idx == 0)
2149 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002150 else {
2151 /* D D Q P D */
2152 if (i < sh->pd_idx)
2153 i += raid_disks;
2154 i -= (sh->pd_idx + 1);
2155 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002156 break;
2157 case ALGORITHM_LEFT_ASYMMETRIC_6:
2158 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2159 if (i > sh->pd_idx)
2160 i--;
2161 break;
2162 case ALGORITHM_LEFT_SYMMETRIC_6:
2163 case ALGORITHM_RIGHT_SYMMETRIC_6:
2164 if (i < sh->pd_idx)
2165 i += data_disks + 1;
2166 i -= (sh->pd_idx + 1);
2167 break;
2168 case ALGORITHM_PARITY_0_6:
2169 i -= 1;
2170 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002171 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002172 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002173 }
2174 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 }
2176
2177 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002178 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
NeilBrown112bf892009-03-31 14:39:38 +11002180 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002181 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002182 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2183 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002184 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2185 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 return 0;
2187 }
2188 return r_sector;
2189}
2190
2191
Dan Williams600aa102008-06-28 08:32:05 +10002192static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002193schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002194 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002195{
2196 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002197 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002198 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002199
Dan Williamse33129d2007-01-02 13:52:30 -07002200 if (rcw) {
2201 /* if we are not expanding this is a proper write request, and
2202 * there will be bios with new data to be drained into the
2203 * stripe cache
2204 */
2205 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10002206 sh->reconstruct_state = reconstruct_state_drain_run;
2207 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2208 } else
2209 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07002210
Dan Williamsac6b53b2009-07-14 13:40:19 -07002211 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002212
2213 for (i = disks; i--; ) {
2214 struct r5dev *dev = &sh->dev[i];
2215
2216 if (dev->towrite) {
2217 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002218 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002219 if (!expand)
2220 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002221 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002222 }
2223 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002224 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002225 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002226 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002227 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002228 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002229 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2230 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2231
Dan Williamsd8ee0722008-06-28 08:32:06 +10002232 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002233 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2234 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002235 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002236
2237 for (i = disks; i--; ) {
2238 struct r5dev *dev = &sh->dev[i];
2239 if (i == pd_idx)
2240 continue;
2241
Dan Williamse33129d2007-01-02 13:52:30 -07002242 if (dev->towrite &&
2243 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002244 test_bit(R5_Wantcompute, &dev->flags))) {
2245 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002246 set_bit(R5_LOCKED, &dev->flags);
2247 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002248 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002249 }
2250 }
2251 }
2252
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002253 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002254 * are in flight
2255 */
2256 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2257 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002258 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002259
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002260 if (level == 6) {
2261 int qd_idx = sh->qd_idx;
2262 struct r5dev *dev = &sh->dev[qd_idx];
2263
2264 set_bit(R5_LOCKED, &dev->flags);
2265 clear_bit(R5_UPTODATE, &dev->flags);
2266 s->locked++;
2267 }
2268
Dan Williams600aa102008-06-28 08:32:05 +10002269 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07002270 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002271 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002272}
NeilBrown16a53ec2006-06-26 00:27:38 -07002273
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274/*
2275 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002276 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 * The bi_next chain must be in order.
2278 */
2279static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2280{
2281 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002282 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002283 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
NeilBrowncbe47ec2011-07-26 11:20:35 +10002285 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 (unsigned long long)bi->bi_sector,
2287 (unsigned long long)sh->sector);
2288
2289
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07002291 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -07002293 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
2294 firstwrite = 1;
2295 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 bip = &sh->dev[dd_idx].toread;
2297 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2298 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2299 goto overlap;
2300 bip = & (*bip)->bi_next;
2301 }
2302 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2303 goto overlap;
2304
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002305 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 if (*bip)
2307 bi->bi_next = *bip;
2308 *bip = bi;
Jens Axboe960e7392008-08-15 10:41:18 +02002309 bi->bi_phys_segments++;
NeilBrown72626682005-09-09 16:23:54 -07002310
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 if (forwrite) {
2312 /* check if page is covered */
2313 sector_t sector = sh->dev[dd_idx].sector;
2314 for (bi=sh->dev[dd_idx].towrite;
2315 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2316 bi && bi->bi_sector <= sector;
2317 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2318 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2319 sector = bi->bi_sector + (bi->bi_size>>9);
2320 }
2321 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2322 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2323 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002324 spin_unlock_irq(&conf->device_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002325
2326 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2327 (unsigned long long)(*bip)->bi_sector,
2328 (unsigned long long)sh->sector, dd_idx);
2329
2330 if (conf->mddev->bitmap && firstwrite) {
2331 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2332 STRIPE_SECTORS, 0);
2333 sh->bm_seq = conf->seq_flush+1;
2334 set_bit(STRIPE_BIT_DELAY, &sh->state);
2335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 return 1;
2337
2338 overlap:
2339 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
2340 spin_unlock_irq(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 return 0;
2342}
2343
NeilBrownd1688a62011-10-11 16:49:52 +11002344static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002345
NeilBrownd1688a62011-10-11 16:49:52 +11002346static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002347 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002348{
NeilBrown784052e2009-03-31 15:19:07 +11002349 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002350 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002351 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002352 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002353 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002354
NeilBrown112bf892009-03-31 14:39:38 +11002355 raid5_compute_sector(conf,
2356 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002357 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002358 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002359 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002360}
2361
Dan Williamsa4456852007-07-09 11:56:43 -07002362static void
NeilBrownd1688a62011-10-11 16:49:52 +11002363handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002364 struct stripe_head_state *s, int disks,
2365 struct bio **return_bi)
2366{
2367 int i;
2368 for (i = disks; i--; ) {
2369 struct bio *bi;
2370 int bitmap_end = 0;
2371
2372 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002373 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002374 rcu_read_lock();
2375 rdev = rcu_dereference(conf->disks[i].rdev);
2376 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002377 atomic_inc(&rdev->nr_pending);
2378 else
2379 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002380 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002381 if (rdev) {
2382 if (!rdev_set_badblocks(
2383 rdev,
2384 sh->sector,
2385 STRIPE_SECTORS, 0))
2386 md_error(conf->mddev, rdev);
2387 rdev_dec_pending(rdev, conf->mddev);
2388 }
Dan Williamsa4456852007-07-09 11:56:43 -07002389 }
2390 spin_lock_irq(&conf->device_lock);
2391 /* fail all writes first */
2392 bi = sh->dev[i].towrite;
2393 sh->dev[i].towrite = NULL;
2394 if (bi) {
2395 s->to_write--;
2396 bitmap_end = 1;
2397 }
2398
2399 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2400 wake_up(&conf->wait_for_overlap);
2401
2402 while (bi && bi->bi_sector <
2403 sh->dev[i].sector + STRIPE_SECTORS) {
2404 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2405 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002406 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002407 md_write_end(conf->mddev);
2408 bi->bi_next = *return_bi;
2409 *return_bi = bi;
2410 }
2411 bi = nextbi;
2412 }
2413 /* and fail all 'written' */
2414 bi = sh->dev[i].written;
2415 sh->dev[i].written = NULL;
2416 if (bi) bitmap_end = 1;
2417 while (bi && bi->bi_sector <
2418 sh->dev[i].sector + STRIPE_SECTORS) {
2419 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2420 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002421 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002422 md_write_end(conf->mddev);
2423 bi->bi_next = *return_bi;
2424 *return_bi = bi;
2425 }
2426 bi = bi2;
2427 }
2428
Dan Williamsb5e98d62007-01-02 13:52:31 -07002429 /* fail any reads if this device is non-operational and
2430 * the data has not reached the cache yet.
2431 */
2432 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2433 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2434 test_bit(R5_ReadError, &sh->dev[i].flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002435 bi = sh->dev[i].toread;
2436 sh->dev[i].toread = NULL;
2437 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2438 wake_up(&conf->wait_for_overlap);
2439 if (bi) s->to_read--;
2440 while (bi && bi->bi_sector <
2441 sh->dev[i].sector + STRIPE_SECTORS) {
2442 struct bio *nextbi =
2443 r5_next_bio(bi, sh->dev[i].sector);
2444 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002445 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002446 bi->bi_next = *return_bi;
2447 *return_bi = bi;
2448 }
2449 bi = nextbi;
2450 }
2451 }
2452 spin_unlock_irq(&conf->device_lock);
2453 if (bitmap_end)
2454 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2455 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002456 /* If we were in the middle of a write the parity block might
2457 * still be locked - so just clear all R5_LOCKED flags
2458 */
2459 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002460 }
2461
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002462 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2463 if (atomic_dec_and_test(&conf->pending_full_writes))
2464 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002465}
2466
NeilBrown7f0da592011-07-28 11:39:22 +10002467static void
NeilBrownd1688a62011-10-11 16:49:52 +11002468handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002469 struct stripe_head_state *s)
2470{
2471 int abort = 0;
2472 int i;
2473
NeilBrown7f0da592011-07-28 11:39:22 +10002474 clear_bit(STRIPE_SYNCING, &sh->state);
2475 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002476 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002477 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10002478 * Don't even need to abort as that is handled elsewhere
2479 * if needed, and not always wanted e.g. if there is a known
2480 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11002481 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002482 * non-sync devices, or abort the recovery
2483 */
NeilBrown18b98372012-04-01 23:48:38 +10002484 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2485 /* During recovery devices cannot be removed, so
2486 * locking and refcounting of rdevs is not needed
2487 */
2488 for (i = 0; i < conf->raid_disks; i++) {
2489 struct md_rdev *rdev = conf->disks[i].rdev;
2490 if (rdev
2491 && !test_bit(Faulty, &rdev->flags)
2492 && !test_bit(In_sync, &rdev->flags)
2493 && !rdev_set_badblocks(rdev, sh->sector,
2494 STRIPE_SECTORS, 0))
2495 abort = 1;
2496 rdev = conf->disks[i].replacement;
2497 if (rdev
2498 && !test_bit(Faulty, &rdev->flags)
2499 && !test_bit(In_sync, &rdev->flags)
2500 && !rdev_set_badblocks(rdev, sh->sector,
2501 STRIPE_SECTORS, 0))
2502 abort = 1;
2503 }
2504 if (abort)
2505 conf->recovery_disabled =
2506 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10002507 }
NeilBrown18b98372012-04-01 23:48:38 +10002508 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10002509}
2510
NeilBrown9a3e1102011-12-23 10:17:53 +11002511static int want_replace(struct stripe_head *sh, int disk_idx)
2512{
2513 struct md_rdev *rdev;
2514 int rv = 0;
2515 /* Doing recovery so rcu locking not required */
2516 rdev = sh->raid_conf->disks[disk_idx].replacement;
2517 if (rdev
2518 && !test_bit(Faulty, &rdev->flags)
2519 && !test_bit(In_sync, &rdev->flags)
2520 && (rdev->recovery_offset <= sh->sector
2521 || rdev->mddev->recovery_cp <= sh->sector))
2522 rv = 1;
2523
2524 return rv;
2525}
2526
NeilBrown93b3dbc2011-07-27 11:00:36 +10002527/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002528 * to be read or computed to satisfy a request.
2529 *
2530 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002531 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002532 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002533static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2534 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002535{
2536 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002537 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2538 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002539
Dan Williamsf38e1212007-01-02 13:52:30 -07002540 /* is the data in this block needed, and can we get it? */
2541 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002542 !test_bit(R5_UPTODATE, &dev->flags) &&
2543 (dev->toread ||
2544 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2545 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002546 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002547 (s->failed >= 1 && fdev[0]->toread) ||
2548 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002549 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2550 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2551 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002552 /* we would like to get this block, possibly by computing it,
2553 * otherwise read it if the backing disk is insync
2554 */
2555 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2556 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2557 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002558 (s->failed && (disk_idx == s->failed_num[0] ||
2559 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002560 /* have disk failed, and we're requested to fetch it;
2561 * do compute it
2562 */
2563 pr_debug("Computing stripe %llu block %d\n",
2564 (unsigned long long)sh->sector, disk_idx);
2565 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2566 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2567 set_bit(R5_Wantcompute, &dev->flags);
2568 sh->ops.target = disk_idx;
2569 sh->ops.target2 = -1; /* no 2nd target */
2570 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002571 /* Careful: from this point on 'uptodate' is in the eye
2572 * of raid_run_ops which services 'compute' operations
2573 * before writes. R5_Wantcompute flags a block that will
2574 * be R5_UPTODATE by the time it is needed for a
2575 * subsequent operation.
2576 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002577 s->uptodate++;
2578 return 1;
2579 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2580 /* Computing 2-failure is *very* expensive; only
2581 * do it if failed >= 2
2582 */
2583 int other;
2584 for (other = disks; other--; ) {
2585 if (other == disk_idx)
2586 continue;
2587 if (!test_bit(R5_UPTODATE,
2588 &sh->dev[other].flags))
2589 break;
2590 }
2591 BUG_ON(other < 0);
2592 pr_debug("Computing stripe %llu blocks %d,%d\n",
2593 (unsigned long long)sh->sector,
2594 disk_idx, other);
2595 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2596 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2597 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2598 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2599 sh->ops.target = disk_idx;
2600 sh->ops.target2 = other;
2601 s->uptodate += 2;
2602 s->req_compute = 1;
2603 return 1;
2604 } else if (test_bit(R5_Insync, &dev->flags)) {
2605 set_bit(R5_LOCKED, &dev->flags);
2606 set_bit(R5_Wantread, &dev->flags);
2607 s->locked++;
2608 pr_debug("Reading block %d (sync=%d)\n",
2609 disk_idx, s->syncing);
2610 }
2611 }
2612
2613 return 0;
2614}
2615
2616/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002617 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002618 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002619static void handle_stripe_fill(struct stripe_head *sh,
2620 struct stripe_head_state *s,
2621 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002622{
2623 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002624
2625 /* look for blocks to read/compute, skip this if a compute
2626 * is already in flight, or if the stripe contents are in the
2627 * midst of changing due to a write
2628 */
2629 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2630 !sh->reconstruct_state)
2631 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002632 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002633 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002634 set_bit(STRIPE_HANDLE, &sh->state);
2635}
2636
2637
Dan Williams1fe797e2008-06-28 09:16:30 +10002638/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002639 * any written block on an uptodate or failed drive can be returned.
2640 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2641 * never LOCKED, so we don't need to test 'failed' directly.
2642 */
NeilBrownd1688a62011-10-11 16:49:52 +11002643static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002644 struct stripe_head *sh, int disks, struct bio **return_bi)
2645{
2646 int i;
2647 struct r5dev *dev;
2648
2649 for (i = disks; i--; )
2650 if (sh->dev[i].written) {
2651 dev = &sh->dev[i];
2652 if (!test_bit(R5_LOCKED, &dev->flags) &&
2653 test_bit(R5_UPTODATE, &dev->flags)) {
2654 /* We can return any write requests */
2655 struct bio *wbi, *wbi2;
2656 int bitmap_end = 0;
Dan Williams45b42332007-07-09 11:56:43 -07002657 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002658 spin_lock_irq(&conf->device_lock);
2659 wbi = dev->written;
2660 dev->written = NULL;
2661 while (wbi && wbi->bi_sector <
2662 dev->sector + STRIPE_SECTORS) {
2663 wbi2 = r5_next_bio(wbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +02002664 if (!raid5_dec_bi_phys_segments(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002665 md_write_end(conf->mddev);
2666 wbi->bi_next = *return_bi;
2667 *return_bi = wbi;
2668 }
2669 wbi = wbi2;
2670 }
2671 if (dev->towrite == NULL)
2672 bitmap_end = 1;
2673 spin_unlock_irq(&conf->device_lock);
2674 if (bitmap_end)
2675 bitmap_endwrite(conf->mddev->bitmap,
2676 sh->sector,
2677 STRIPE_SECTORS,
2678 !test_bit(STRIPE_DEGRADED, &sh->state),
2679 0);
2680 }
2681 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002682
2683 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2684 if (atomic_dec_and_test(&conf->pending_full_writes))
2685 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002686}
2687
NeilBrownd1688a62011-10-11 16:49:52 +11002688static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002689 struct stripe_head *sh,
2690 struct stripe_head_state *s,
2691 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002692{
2693 int rmw = 0, rcw = 0, i;
NeilBrownc8ac1802011-07-27 11:00:36 +10002694 if (conf->max_degraded == 2) {
2695 /* RAID6 requires 'rcw' in current implementation
2696 * Calculate the real rcw later - for now fake it
2697 * look like rcw is cheaper
2698 */
2699 rcw = 1; rmw = 2;
2700 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002701 /* would I have to read this buffer for read_modify_write */
2702 struct r5dev *dev = &sh->dev[i];
2703 if ((dev->towrite || i == sh->pd_idx) &&
2704 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002705 !(test_bit(R5_UPTODATE, &dev->flags) ||
2706 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002707 if (test_bit(R5_Insync, &dev->flags))
2708 rmw++;
2709 else
2710 rmw += 2*disks; /* cannot read it */
2711 }
2712 /* Would I have to read this buffer for reconstruct_write */
2713 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2714 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002715 !(test_bit(R5_UPTODATE, &dev->flags) ||
2716 test_bit(R5_Wantcompute, &dev->flags))) {
2717 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002718 else
2719 rcw += 2*disks;
2720 }
2721 }
Dan Williams45b42332007-07-09 11:56:43 -07002722 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002723 (unsigned long long)sh->sector, rmw, rcw);
2724 set_bit(STRIPE_HANDLE, &sh->state);
2725 if (rmw < rcw && rmw > 0)
2726 /* prefer read-modify-write, but need to get some data */
2727 for (i = disks; i--; ) {
2728 struct r5dev *dev = &sh->dev[i];
2729 if ((dev->towrite || i == sh->pd_idx) &&
2730 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002731 !(test_bit(R5_UPTODATE, &dev->flags) ||
2732 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002733 test_bit(R5_Insync, &dev->flags)) {
2734 if (
2735 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002736 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002737 "%d for r-m-w\n", i);
2738 set_bit(R5_LOCKED, &dev->flags);
2739 set_bit(R5_Wantread, &dev->flags);
2740 s->locked++;
2741 } else {
2742 set_bit(STRIPE_DELAYED, &sh->state);
2743 set_bit(STRIPE_HANDLE, &sh->state);
2744 }
2745 }
2746 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002747 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002748 /* want reconstruct write, but need to get some data */
NeilBrownc8ac1802011-07-27 11:00:36 +10002749 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002750 for (i = disks; i--; ) {
2751 struct r5dev *dev = &sh->dev[i];
2752 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002753 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002754 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002755 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002756 test_bit(R5_Wantcompute, &dev->flags))) {
2757 rcw++;
2758 if (!test_bit(R5_Insync, &dev->flags))
2759 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002760 if (
2761 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002762 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002763 "%d for Reconstruct\n", i);
2764 set_bit(R5_LOCKED, &dev->flags);
2765 set_bit(R5_Wantread, &dev->flags);
2766 s->locked++;
2767 } else {
2768 set_bit(STRIPE_DELAYED, &sh->state);
2769 set_bit(STRIPE_HANDLE, &sh->state);
2770 }
2771 }
2772 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002773 }
Dan Williamsa4456852007-07-09 11:56:43 -07002774 /* now if nothing is locked, and if we have enough data,
2775 * we can start a write request
2776 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002777 /* since handle_stripe can be called at any time we need to handle the
2778 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002779 * subsequent call wants to start a write request. raid_run_ops only
2780 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002781 * simultaneously. If this is not the case then new writes need to be
2782 * held off until the compute completes.
2783 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002784 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2785 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2786 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002787 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002788}
2789
NeilBrownd1688a62011-10-11 16:49:52 +11002790static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002791 struct stripe_head_state *s, int disks)
2792{
Dan Williamsecc65c92008-06-28 08:31:57 +10002793 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002794
Dan Williamsbd2ab672008-04-10 21:29:27 -07002795 set_bit(STRIPE_HANDLE, &sh->state);
2796
Dan Williamsecc65c92008-06-28 08:31:57 +10002797 switch (sh->check_state) {
2798 case check_state_idle:
2799 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002800 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002801 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002802 sh->check_state = check_state_run;
2803 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002804 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002805 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002806 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002807 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002808 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002809 /* fall through */
2810 case check_state_compute_result:
2811 sh->check_state = check_state_idle;
2812 if (!dev)
2813 dev = &sh->dev[sh->pd_idx];
2814
2815 /* check that a write has not made the stripe insync */
2816 if (test_bit(STRIPE_INSYNC, &sh->state))
2817 break;
2818
2819 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002820 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2821 BUG_ON(s->uptodate != disks);
2822
2823 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002824 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002825 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002826
Dan Williamsa4456852007-07-09 11:56:43 -07002827 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002828 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002829 break;
2830 case check_state_run:
2831 break; /* we will be called again upon completion */
2832 case check_state_check_result:
2833 sh->check_state = check_state_idle;
2834
2835 /* if a failure occurred during the check operation, leave
2836 * STRIPE_INSYNC not set and let the stripe be handled again
2837 */
2838 if (s->failed)
2839 break;
2840
2841 /* handle a successful check operation, if parity is correct
2842 * we are done. Otherwise update the mismatch count and repair
2843 * parity if !MD_RECOVERY_CHECK
2844 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002845 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002846 /* parity is correct (on disc,
2847 * not in buffer any more)
2848 */
2849 set_bit(STRIPE_INSYNC, &sh->state);
2850 else {
2851 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2852 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2853 /* don't try to repair!! */
2854 set_bit(STRIPE_INSYNC, &sh->state);
2855 else {
2856 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002857 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002858 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2859 set_bit(R5_Wantcompute,
2860 &sh->dev[sh->pd_idx].flags);
2861 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002862 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10002863 s->uptodate++;
2864 }
2865 }
2866 break;
2867 case check_state_compute_run:
2868 break;
2869 default:
2870 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2871 __func__, sh->check_state,
2872 (unsigned long long) sh->sector);
2873 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002874 }
2875}
2876
2877
NeilBrownd1688a62011-10-11 16:49:52 +11002878static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07002879 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10002880 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002881{
Dan Williamsa4456852007-07-09 11:56:43 -07002882 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11002883 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07002884 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07002885
2886 set_bit(STRIPE_HANDLE, &sh->state);
2887
2888 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002889
Dan Williamsa4456852007-07-09 11:56:43 -07002890 /* Want to check and possibly repair P and Q.
2891 * However there could be one 'failed' device, in which
2892 * case we can only check one of them, possibly using the
2893 * other to generate missing data
2894 */
2895
Dan Williamsd82dfee2009-07-14 13:40:57 -07002896 switch (sh->check_state) {
2897 case check_state_idle:
2898 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10002899 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002900 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07002901 * makes sense to check P (If anything else were failed,
2902 * we would have used P to recreate it).
2903 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002904 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07002905 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002906 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002907 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07002908 * anything, so it makes sense to check it
2909 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002910 if (sh->check_state == check_state_run)
2911 sh->check_state = check_state_run_pq;
2912 else
2913 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07002914 }
Dan Williams36d1c642009-07-14 11:48:22 -07002915
Dan Williamsd82dfee2009-07-14 13:40:57 -07002916 /* discard potentially stale zero_sum_result */
2917 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07002918
Dan Williamsd82dfee2009-07-14 13:40:57 -07002919 if (sh->check_state == check_state_run) {
2920 /* async_xor_zero_sum destroys the contents of P */
2921 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
2922 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07002923 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002924 if (sh->check_state >= check_state_run &&
2925 sh->check_state <= check_state_run_pq) {
2926 /* async_syndrome_zero_sum preserves P and Q, so
2927 * no need to mark them !uptodate here
2928 */
2929 set_bit(STRIPE_OP_CHECK, &s->ops_request);
2930 break;
2931 }
Dan Williams36d1c642009-07-14 11:48:22 -07002932
Dan Williamsd82dfee2009-07-14 13:40:57 -07002933 /* we have 2-disk failure */
2934 BUG_ON(s->failed != 2);
2935 /* fall through */
2936 case check_state_compute_result:
2937 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07002938
Dan Williamsd82dfee2009-07-14 13:40:57 -07002939 /* check that a write has not made the stripe insync */
2940 if (test_bit(STRIPE_INSYNC, &sh->state))
2941 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002942
2943 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07002944 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07002945 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002946 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07002947 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10002948 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07002949 s->locked++;
2950 set_bit(R5_LOCKED, &dev->flags);
2951 set_bit(R5_Wantwrite, &dev->flags);
2952 }
2953 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10002954 dev = &sh->dev[s->failed_num[0]];
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 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002959 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002960 dev = &sh->dev[pd_idx];
2961 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_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002966 dev = &sh->dev[qd_idx];
2967 s->locked++;
2968 set_bit(R5_LOCKED, &dev->flags);
2969 set_bit(R5_Wantwrite, &dev->flags);
2970 }
2971 clear_bit(STRIPE_DEGRADED, &sh->state);
2972
2973 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002974 break;
2975 case check_state_run:
2976 case check_state_run_q:
2977 case check_state_run_pq:
2978 break; /* we will be called again upon completion */
2979 case check_state_check_result:
2980 sh->check_state = check_state_idle;
2981
2982 /* handle a successful check operation, if parity is correct
2983 * we are done. Otherwise update the mismatch count and repair
2984 * parity if !MD_RECOVERY_CHECK
2985 */
2986 if (sh->ops.zero_sum_result == 0) {
2987 /* both parities are correct */
2988 if (!s->failed)
2989 set_bit(STRIPE_INSYNC, &sh->state);
2990 else {
2991 /* in contrast to the raid5 case we can validate
2992 * parity, but still have a failure to write
2993 * back
2994 */
2995 sh->check_state = check_state_compute_result;
2996 /* Returning at this point means that we may go
2997 * off and bring p and/or q uptodate again so
2998 * we make sure to check zero_sum_result again
2999 * to verify if p or q need writeback
3000 */
3001 }
3002 } else {
3003 conf->mddev->resync_mismatches += STRIPE_SECTORS;
3004 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3005 /* don't try to repair!! */
3006 set_bit(STRIPE_INSYNC, &sh->state);
3007 else {
3008 int *target = &sh->ops.target;
3009
3010 sh->ops.target = -1;
3011 sh->ops.target2 = -1;
3012 sh->check_state = check_state_compute_run;
3013 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3014 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3015 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3016 set_bit(R5_Wantcompute,
3017 &sh->dev[pd_idx].flags);
3018 *target = pd_idx;
3019 target = &sh->ops.target2;
3020 s->uptodate++;
3021 }
3022 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3023 set_bit(R5_Wantcompute,
3024 &sh->dev[qd_idx].flags);
3025 *target = qd_idx;
3026 s->uptodate++;
3027 }
3028 }
3029 }
3030 break;
3031 case check_state_compute_run:
3032 break;
3033 default:
3034 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3035 __func__, sh->check_state,
3036 (unsigned long long) sh->sector);
3037 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003038 }
3039}
3040
NeilBrownd1688a62011-10-11 16:49:52 +11003041static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003042{
3043 int i;
3044
3045 /* We have read all the blocks in this stripe and now we need to
3046 * copy some of them into a target stripe for expand.
3047 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003048 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003049 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3050 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003051 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003052 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003053 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003054 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003055
NeilBrown784052e2009-03-31 15:19:07 +11003056 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003057 sector_t s = raid5_compute_sector(conf, bn, 0,
3058 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003059 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003060 if (sh2 == NULL)
3061 /* so far only the early blocks of this stripe
3062 * have been requested. When later blocks
3063 * get requested, we will try again
3064 */
3065 continue;
3066 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3067 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3068 /* must have already done this block */
3069 release_stripe(sh2);
3070 continue;
3071 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003072
3073 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003074 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003075 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003076 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003077 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003078
Dan Williamsa4456852007-07-09 11:56:43 -07003079 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3080 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3081 for (j = 0; j < conf->raid_disks; j++)
3082 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003083 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003084 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3085 break;
3086 if (j == conf->raid_disks) {
3087 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3088 set_bit(STRIPE_HANDLE, &sh2->state);
3089 }
3090 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003091
Dan Williamsa4456852007-07-09 11:56:43 -07003092 }
NeilBrowna2e08552007-09-11 15:23:36 -07003093 /* done submitting copies, wait for them to complete */
3094 if (tx) {
3095 async_tx_ack(tx);
3096 dma_wait_for_async_tx(tx);
3097 }
Dan Williamsa4456852007-07-09 11:56:43 -07003098}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
3100/*
3101 * handle_stripe - do things to a stripe.
3102 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003103 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3104 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003106 * return some read requests which now have data
3107 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 * schedule a read on some buffers
3109 * schedule a write of some buffers
3110 * return confirmation of parity correctness
3111 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 */
Dan Williamsa4456852007-07-09 11:56:43 -07003113
NeilBrownacfe7262011-07-27 11:00:36 +10003114static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003115{
NeilBrownd1688a62011-10-11 16:49:52 +11003116 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003117 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003118 struct r5dev *dev;
3119 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003120 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003121
NeilBrownacfe7262011-07-27 11:00:36 +10003122 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003123
NeilBrownacfe7262011-07-27 11:00:36 +10003124 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3125 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3126 s->failed_num[0] = -1;
3127 s->failed_num[1] = -1;
3128
3129 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003130 rcu_read_lock();
NeilBrownc4c16632011-07-26 11:34:20 +10003131 spin_lock_irq(&conf->device_lock);
NeilBrown16a53ec2006-06-26 00:27:38 -07003132 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003133 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003134 sector_t first_bad;
3135 int bad_sectors;
3136 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003137
NeilBrown16a53ec2006-06-26 00:27:38 -07003138 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003139
Dan Williams45b42332007-07-09 11:56:43 -07003140 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003141 i, dev->flags,
3142 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003143 /* maybe we can reply to a read
3144 *
3145 * new wantfill requests are only permitted while
3146 * ops_complete_biofill is guaranteed to be inactive
3147 */
3148 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3149 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3150 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003151
3152 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003153 if (test_bit(R5_LOCKED, &dev->flags))
3154 s->locked++;
3155 if (test_bit(R5_UPTODATE, &dev->flags))
3156 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003157 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003158 s->compute++;
3159 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003160 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003161
NeilBrownacfe7262011-07-27 11:00:36 +10003162 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003163 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003164 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003165 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003166 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003167 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003168 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003169 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003170 }
Dan Williamsa4456852007-07-09 11:56:43 -07003171 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003172 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003173 /* Prefer to use the replacement for reads, but only
3174 * if it is recovered enough and has no bad blocks.
3175 */
3176 rdev = rcu_dereference(conf->disks[i].replacement);
3177 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3178 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3179 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3180 &first_bad, &bad_sectors))
3181 set_bit(R5_ReadRepl, &dev->flags);
3182 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003183 if (rdev)
3184 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003185 rdev = rcu_dereference(conf->disks[i].rdev);
3186 clear_bit(R5_ReadRepl, &dev->flags);
3187 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003188 if (rdev && test_bit(Faulty, &rdev->flags))
3189 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003190 if (rdev) {
3191 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3192 &first_bad, &bad_sectors);
3193 if (s->blocked_rdev == NULL
3194 && (test_bit(Blocked, &rdev->flags)
3195 || is_bad < 0)) {
3196 if (is_bad < 0)
3197 set_bit(BlockedBadBlocks,
3198 &rdev->flags);
3199 s->blocked_rdev = rdev;
3200 atomic_inc(&rdev->nr_pending);
3201 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003202 }
NeilBrown415e72d2010-06-17 17:25:21 +10003203 clear_bit(R5_Insync, &dev->flags);
3204 if (!rdev)
3205 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003206 else if (is_bad) {
3207 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10003208 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3209 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003210 /* treat as in-sync, but with a read error
3211 * which we can now try to correct
3212 */
3213 set_bit(R5_Insync, &dev->flags);
3214 set_bit(R5_ReadError, &dev->flags);
3215 }
3216 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003217 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003218 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003219 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003220 set_bit(R5_Insync, &dev->flags);
3221 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3222 test_bit(R5_Expanded, &dev->flags))
3223 /* If we've reshaped into here, we assume it is Insync.
3224 * We will shortly update recovery_offset to make
3225 * it official.
3226 */
3227 set_bit(R5_Insync, &dev->flags);
3228
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003229 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003230 /* This flag does not apply to '.replacement'
3231 * only to .rdev, so make sure to check that*/
3232 struct md_rdev *rdev2 = rcu_dereference(
3233 conf->disks[i].rdev);
3234 if (rdev2 == rdev)
3235 clear_bit(R5_Insync, &dev->flags);
3236 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003237 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003238 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003239 } else
3240 clear_bit(R5_WriteError, &dev->flags);
3241 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003242 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003243 /* This flag does not apply to '.replacement'
3244 * only to .rdev, so make sure to check that*/
3245 struct md_rdev *rdev2 = rcu_dereference(
3246 conf->disks[i].rdev);
3247 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003248 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003249 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003250 } else
3251 clear_bit(R5_MadeGood, &dev->flags);
3252 }
NeilBrown977df362011-12-23 10:17:53 +11003253 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3254 struct md_rdev *rdev2 = rcu_dereference(
3255 conf->disks[i].replacement);
3256 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3257 s->handle_bad_blocks = 1;
3258 atomic_inc(&rdev2->nr_pending);
3259 } else
3260 clear_bit(R5_MadeGoodRepl, &dev->flags);
3261 }
NeilBrown415e72d2010-06-17 17:25:21 +10003262 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003263 /* The ReadError flag will just be confusing now */
3264 clear_bit(R5_ReadError, &dev->flags);
3265 clear_bit(R5_ReWrite, &dev->flags);
3266 }
NeilBrown415e72d2010-06-17 17:25:21 +10003267 if (test_bit(R5_ReadError, &dev->flags))
3268 clear_bit(R5_Insync, &dev->flags);
3269 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003270 if (s->failed < 2)
3271 s->failed_num[s->failed] = i;
3272 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003273 if (rdev && !test_bit(Faulty, &rdev->flags))
3274 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003275 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003276 }
NeilBrownc4c16632011-07-26 11:34:20 +10003277 spin_unlock_irq(&conf->device_lock);
NeilBrown9a3e1102011-12-23 10:17:53 +11003278 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3279 /* If there is a failed device being replaced,
3280 * we must be recovering.
3281 * else if we are after recovery_cp, we must be syncing
3282 * else we can only be replacing
3283 * sync and recovery both need to read all devices, and so
3284 * use the same flag.
3285 */
3286 if (do_recovery ||
3287 sh->sector >= conf->mddev->recovery_cp)
3288 s->syncing = 1;
3289 else
3290 s->replacing = 1;
3291 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003292 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003293}
NeilBrownf4168852007-02-28 20:11:53 -08003294
NeilBrowncc940152011-07-26 11:35:35 +10003295static void handle_stripe(struct stripe_head *sh)
3296{
3297 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003298 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003299 int i;
NeilBrown84789552011-07-27 11:00:36 +10003300 int prexor;
3301 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003302 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003303
3304 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003305 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003306 /* already being handled, ensure it gets handled
3307 * again when current action finishes */
3308 set_bit(STRIPE_HANDLE, &sh->state);
3309 return;
3310 }
3311
3312 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3313 set_bit(STRIPE_SYNCING, &sh->state);
3314 clear_bit(STRIPE_INSYNC, &sh->state);
3315 }
3316 clear_bit(STRIPE_DELAYED, &sh->state);
3317
3318 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3319 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3320 (unsigned long long)sh->sector, sh->state,
3321 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3322 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003323
NeilBrownacfe7262011-07-27 11:00:36 +10003324 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003325
NeilBrownbc2607f2011-07-28 11:39:22 +10003326 if (s.handle_bad_blocks) {
3327 set_bit(STRIPE_HANDLE, &sh->state);
3328 goto finish;
3329 }
3330
NeilBrown474af965fe2011-07-27 11:00:36 +10003331 if (unlikely(s.blocked_rdev)) {
3332 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003333 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003334 set_bit(STRIPE_HANDLE, &sh->state);
3335 goto finish;
3336 }
3337 /* There is nothing for the blocked_rdev to block */
3338 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3339 s.blocked_rdev = NULL;
3340 }
3341
3342 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3343 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3344 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3345 }
3346
3347 pr_debug("locked=%d uptodate=%d to_read=%d"
3348 " to_write=%d failed=%d failed_num=%d,%d\n",
3349 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3350 s.failed_num[0], s.failed_num[1]);
3351 /* check if the array has lost more than max_degraded devices and,
3352 * if so, some requests might need to be failed.
3353 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003354 if (s.failed > conf->max_degraded) {
3355 sh->check_state = 0;
3356 sh->reconstruct_state = 0;
3357 if (s.to_read+s.to_write+s.written)
3358 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003359 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003360 handle_failed_sync(conf, sh, &s);
3361 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003362
3363 /*
3364 * might be able to return some write requests if the parity blocks
3365 * are safe, or on a failed drive
3366 */
3367 pdev = &sh->dev[sh->pd_idx];
3368 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3369 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3370 qdev = &sh->dev[sh->qd_idx];
3371 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3372 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3373 || conf->level < 6;
3374
3375 if (s.written &&
3376 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3377 && !test_bit(R5_LOCKED, &pdev->flags)
3378 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3379 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3380 && !test_bit(R5_LOCKED, &qdev->flags)
3381 && test_bit(R5_UPTODATE, &qdev->flags)))))
3382 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3383
3384 /* Now we might consider reading some blocks, either to check/generate
3385 * parity, or to satisfy requests
3386 * or to load a block that is being partially written.
3387 */
3388 if (s.to_read || s.non_overwrite
3389 || (conf->level == 6 && s.to_write && s.failed)
NeilBrown9a3e1102011-12-23 10:17:53 +11003390 || (s.syncing && (s.uptodate + s.compute < disks))
3391 || s.replacing
3392 || s.expanding)
NeilBrown474af965fe2011-07-27 11:00:36 +10003393 handle_stripe_fill(sh, &s, disks);
3394
NeilBrown84789552011-07-27 11:00:36 +10003395 /* Now we check to see if any write operations have recently
3396 * completed
3397 */
3398 prexor = 0;
3399 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3400 prexor = 1;
3401 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3402 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3403 sh->reconstruct_state = reconstruct_state_idle;
3404
3405 /* All the 'written' buffers and the parity block are ready to
3406 * be written back to disk
3407 */
3408 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3409 BUG_ON(sh->qd_idx >= 0 &&
3410 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags));
3411 for (i = disks; i--; ) {
3412 struct r5dev *dev = &sh->dev[i];
3413 if (test_bit(R5_LOCKED, &dev->flags) &&
3414 (i == sh->pd_idx || i == sh->qd_idx ||
3415 dev->written)) {
3416 pr_debug("Writing block %d\n", i);
3417 set_bit(R5_Wantwrite, &dev->flags);
3418 if (prexor)
3419 continue;
3420 if (!test_bit(R5_Insync, &dev->flags) ||
3421 ((i == sh->pd_idx || i == sh->qd_idx) &&
3422 s.failed == 0))
3423 set_bit(STRIPE_INSYNC, &sh->state);
3424 }
3425 }
3426 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3427 s.dec_preread_active = 1;
3428 }
3429
3430 /* Now to consider new write requests and what else, if anything
3431 * should be read. We do not handle new writes when:
3432 * 1/ A 'write' operation (copy+xor) is already in flight.
3433 * 2/ A 'check' operation is in flight, as it may clobber the parity
3434 * block.
3435 */
3436 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3437 handle_stripe_dirtying(conf, sh, &s, disks);
3438
3439 /* maybe we need to check and possibly fix the parity for this stripe
3440 * Any reads will already have been scheduled, so we just see if enough
3441 * data is available. The parity check is held off while parity
3442 * dependent operations are in flight.
3443 */
3444 if (sh->check_state ||
3445 (s.syncing && s.locked == 0 &&
3446 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3447 !test_bit(STRIPE_INSYNC, &sh->state))) {
3448 if (conf->level == 6)
3449 handle_parity_checks6(conf, sh, &s, disks);
3450 else
3451 handle_parity_checks5(conf, sh, &s, disks);
3452 }
NeilBrownc5a31002011-07-27 11:00:36 +10003453
NeilBrown9a3e1102011-12-23 10:17:53 +11003454 if (s.replacing && s.locked == 0
3455 && !test_bit(STRIPE_INSYNC, &sh->state)) {
3456 /* Write out to replacement devices where possible */
3457 for (i = 0; i < conf->raid_disks; i++)
3458 if (test_bit(R5_UPTODATE, &sh->dev[i].flags) &&
3459 test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3460 set_bit(R5_WantReplace, &sh->dev[i].flags);
3461 set_bit(R5_LOCKED, &sh->dev[i].flags);
3462 s.locked++;
3463 }
3464 set_bit(STRIPE_INSYNC, &sh->state);
3465 }
3466 if ((s.syncing || s.replacing) && s.locked == 0 &&
3467 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003468 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3469 clear_bit(STRIPE_SYNCING, &sh->state);
3470 }
3471
3472 /* If the failed drives are just a ReadError, then we might need
3473 * to progress the repair/check process
3474 */
3475 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3476 for (i = 0; i < s.failed; i++) {
3477 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3478 if (test_bit(R5_ReadError, &dev->flags)
3479 && !test_bit(R5_LOCKED, &dev->flags)
3480 && test_bit(R5_UPTODATE, &dev->flags)
3481 ) {
3482 if (!test_bit(R5_ReWrite, &dev->flags)) {
3483 set_bit(R5_Wantwrite, &dev->flags);
3484 set_bit(R5_ReWrite, &dev->flags);
3485 set_bit(R5_LOCKED, &dev->flags);
3486 s.locked++;
3487 } else {
3488 /* let's read it back */
3489 set_bit(R5_Wantread, &dev->flags);
3490 set_bit(R5_LOCKED, &dev->flags);
3491 s.locked++;
3492 }
3493 }
3494 }
3495
3496
NeilBrown3687c062011-07-27 11:00:36 +10003497 /* Finish reconstruct operations initiated by the expansion process */
3498 if (sh->reconstruct_state == reconstruct_state_result) {
3499 struct stripe_head *sh_src
3500 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3501 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3502 /* sh cannot be written until sh_src has been read.
3503 * so arrange for sh to be delayed a little
3504 */
3505 set_bit(STRIPE_DELAYED, &sh->state);
3506 set_bit(STRIPE_HANDLE, &sh->state);
3507 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3508 &sh_src->state))
3509 atomic_inc(&conf->preread_active_stripes);
3510 release_stripe(sh_src);
3511 goto finish;
3512 }
3513 if (sh_src)
3514 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003515
NeilBrown3687c062011-07-27 11:00:36 +10003516 sh->reconstruct_state = reconstruct_state_idle;
3517 clear_bit(STRIPE_EXPANDING, &sh->state);
3518 for (i = conf->raid_disks; i--; ) {
3519 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3520 set_bit(R5_LOCKED, &sh->dev[i].flags);
3521 s.locked++;
3522 }
3523 }
3524
3525 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3526 !sh->reconstruct_state) {
3527 /* Need to write out all blocks after computing parity */
3528 sh->disks = conf->raid_disks;
3529 stripe_set_idx(sh->sector, conf, 0, sh);
3530 schedule_reconstruction(sh, &s, 1, 1);
3531 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3532 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3533 atomic_dec(&conf->reshape_stripes);
3534 wake_up(&conf->wait_for_overlap);
3535 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3536 }
3537
3538 if (s.expanding && s.locked == 0 &&
3539 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3540 handle_stripe_expansion(conf, sh);
3541
3542finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003543 /* wait for this device to become unblocked */
NeilBrown43220aa2011-08-31 12:49:14 +10003544 if (conf->mddev->external && unlikely(s.blocked_rdev))
NeilBrownc5709ef2011-07-26 11:35:20 +10003545 md_wait_for_blocked_rdev(s.blocked_rdev, conf->mddev);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003546
NeilBrownbc2607f2011-07-28 11:39:22 +10003547 if (s.handle_bad_blocks)
3548 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003549 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003550 struct r5dev *dev = &sh->dev[i];
3551 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3552 /* We own a safe reference to the rdev */
3553 rdev = conf->disks[i].rdev;
3554 if (!rdev_set_badblocks(rdev, sh->sector,
3555 STRIPE_SECTORS, 0))
3556 md_error(conf->mddev, rdev);
3557 rdev_dec_pending(rdev, conf->mddev);
3558 }
NeilBrownb84db562011-07-28 11:39:23 +10003559 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3560 rdev = conf->disks[i].rdev;
3561 rdev_clear_badblocks(rdev, sh->sector,
3562 STRIPE_SECTORS);
3563 rdev_dec_pending(rdev, conf->mddev);
3564 }
NeilBrown977df362011-12-23 10:17:53 +11003565 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3566 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11003567 if (!rdev)
3568 /* rdev have been moved down */
3569 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11003570 rdev_clear_badblocks(rdev, sh->sector,
3571 STRIPE_SECTORS);
3572 rdev_dec_pending(rdev, conf->mddev);
3573 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003574 }
3575
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003576 if (s.ops_request)
3577 raid_run_ops(sh, s.ops_request);
3578
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003579 ops_run_io(sh, &s);
3580
NeilBrownc5709ef2011-07-26 11:35:20 +10003581 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003582 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003583 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003584 * have actually been submitted.
3585 */
3586 atomic_dec(&conf->preread_active_stripes);
3587 if (atomic_read(&conf->preread_active_stripes) <
3588 IO_THRESHOLD)
3589 md_wakeup_thread(conf->mddev->thread);
3590 }
3591
NeilBrownc5709ef2011-07-26 11:35:20 +10003592 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003593
Dan Williams257a4b42011-11-08 16:22:06 +11003594 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003595}
3596
NeilBrownd1688a62011-10-11 16:49:52 +11003597static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598{
3599 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3600 while (!list_empty(&conf->delayed_list)) {
3601 struct list_head *l = conf->delayed_list.next;
3602 struct stripe_head *sh;
3603 sh = list_entry(l, struct stripe_head, lru);
3604 list_del_init(l);
3605 clear_bit(STRIPE_DELAYED, &sh->state);
3606 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3607 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003608 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 }
NeilBrown482c0832011-04-18 18:25:42 +10003610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611}
3612
NeilBrownd1688a62011-10-11 16:49:52 +11003613static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003614{
3615 /* device_lock is held */
3616 struct list_head head;
3617 list_add(&head, &conf->bitmap_list);
3618 list_del_init(&conf->bitmap_list);
3619 while (!list_empty(&head)) {
3620 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3621 list_del_init(&sh->lru);
3622 atomic_inc(&sh->count);
3623 __release_stripe(conf, sh);
3624 }
3625}
3626
NeilBrownfd01b882011-10-11 16:47:53 +11003627int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003628{
NeilBrownd1688a62011-10-11 16:49:52 +11003629 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003630
3631 /* No difference between reads and writes. Just check
3632 * how busy the stripe_cache is
3633 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003634
NeilBrownf022b2f2006-10-03 01:15:56 -07003635 if (conf->inactive_blocked)
3636 return 1;
3637 if (conf->quiesce)
3638 return 1;
3639 if (list_empty_careful(&conf->inactive_list))
3640 return 1;
3641
3642 return 0;
3643}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003644EXPORT_SYMBOL_GPL(md_raid5_congested);
3645
3646static int raid5_congested(void *data, int bits)
3647{
NeilBrownfd01b882011-10-11 16:47:53 +11003648 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003649
3650 return mddev_congested(mddev, bits) ||
3651 md_raid5_congested(mddev, bits);
3652}
NeilBrownf022b2f2006-10-03 01:15:56 -07003653
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003654/* We want read requests to align with chunks where possible,
3655 * but write requests don't need to.
3656 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003657static int raid5_mergeable_bvec(struct request_queue *q,
3658 struct bvec_merge_data *bvm,
3659 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003660{
NeilBrownfd01b882011-10-11 16:47:53 +11003661 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003662 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003663 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003664 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003665 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003666
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003667 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003668 return biovec->bv_len; /* always allow writes to be mergeable */
3669
Andre Noll664e7c42009-06-18 08:45:27 +10003670 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3671 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003672 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3673 if (max < 0) max = 0;
3674 if (max <= biovec->bv_len && bio_sectors == 0)
3675 return biovec->bv_len;
3676 else
3677 return max;
3678}
3679
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003680
NeilBrownfd01b882011-10-11 16:47:53 +11003681static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003682{
3683 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003684 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003685 unsigned int bio_sectors = bio->bi_size >> 9;
3686
Andre Noll664e7c42009-06-18 08:45:27 +10003687 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3688 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003689 return chunk_sectors >=
3690 ((sector & (chunk_sectors - 1)) + bio_sectors);
3691}
3692
3693/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003694 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3695 * later sampled by raid5d.
3696 */
NeilBrownd1688a62011-10-11 16:49:52 +11003697static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003698{
3699 unsigned long flags;
3700
3701 spin_lock_irqsave(&conf->device_lock, flags);
3702
3703 bi->bi_next = conf->retry_read_aligned_list;
3704 conf->retry_read_aligned_list = bi;
3705
3706 spin_unlock_irqrestore(&conf->device_lock, flags);
3707 md_wakeup_thread(conf->mddev->thread);
3708}
3709
3710
NeilBrownd1688a62011-10-11 16:49:52 +11003711static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003712{
3713 struct bio *bi;
3714
3715 bi = conf->retry_read_aligned;
3716 if (bi) {
3717 conf->retry_read_aligned = NULL;
3718 return bi;
3719 }
3720 bi = conf->retry_read_aligned_list;
3721 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003722 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003723 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003724 /*
3725 * this sets the active strip count to 1 and the processed
3726 * strip count to zero (upper 8 bits)
3727 */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003728 bi->bi_phys_segments = 1; /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003729 }
3730
3731 return bi;
3732}
3733
3734
3735/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003736 * The "raid5_align_endio" should check if the read succeeded and if it
3737 * did, call bio_endio on the original bio (having bio_put the new bio
3738 * first).
3739 * If the read failed..
3740 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003741static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003742{
3743 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003744 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003745 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003746 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003747 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003748
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003749 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003750
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003751 rdev = (void*)raid_bi->bi_next;
3752 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003753 mddev = rdev->mddev;
3754 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003755
3756 rdev_dec_pending(rdev, conf->mddev);
3757
3758 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003759 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003760 if (atomic_dec_and_test(&conf->active_aligned_reads))
3761 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003762 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003763 }
3764
3765
Dan Williams45b42332007-07-09 11:56:43 -07003766 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003767
3768 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003769}
3770
Neil Brown387bb172007-02-08 14:20:29 -08003771static int bio_fits_rdev(struct bio *bi)
3772{
Jens Axboe165125e2007-07-24 09:28:11 +02003773 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003774
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003775 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003776 return 0;
3777 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003778 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003779 return 0;
3780
3781 if (q->merge_bvec_fn)
3782 /* it's too hard to apply the merge_bvec_fn at this stage,
3783 * just just give up
3784 */
3785 return 0;
3786
3787 return 1;
3788}
3789
3790
NeilBrownfd01b882011-10-11 16:47:53 +11003791static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003792{
NeilBrownd1688a62011-10-11 16:49:52 +11003793 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11003794 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003795 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11003796 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11003797 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003798
3799 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003800 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003801 return 0;
3802 }
3803 /*
NeilBrowna167f662010-10-26 18:31:13 +11003804 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003805 */
NeilBrowna167f662010-10-26 18:31:13 +11003806 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003807 if (!align_bi)
3808 return 0;
3809 /*
3810 * set bi_end_io to a new function, and set bi_private to the
3811 * original bio.
3812 */
3813 align_bi->bi_end_io = raid5_align_endio;
3814 align_bi->bi_private = raid_bio;
3815 /*
3816 * compute position
3817 */
NeilBrown112bf892009-03-31 14:39:38 +11003818 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3819 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003820 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003821
NeilBrown671488c2011-12-23 10:17:52 +11003822 end_sector = align_bi->bi_sector + (align_bi->bi_size >> 9);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003823 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11003824 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
3825 if (!rdev || test_bit(Faulty, &rdev->flags) ||
3826 rdev->recovery_offset < end_sector) {
3827 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3828 if (rdev &&
3829 (test_bit(Faulty, &rdev->flags) ||
3830 !(test_bit(In_sync, &rdev->flags) ||
3831 rdev->recovery_offset >= end_sector)))
3832 rdev = NULL;
3833 }
3834 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10003835 sector_t first_bad;
3836 int bad_sectors;
3837
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003838 atomic_inc(&rdev->nr_pending);
3839 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003840 raid_bio->bi_next = (void*)rdev;
3841 align_bi->bi_bdev = rdev->bdev;
3842 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
3843 align_bi->bi_sector += rdev->data_offset;
3844
NeilBrown31c176e2011-07-28 11:39:22 +10003845 if (!bio_fits_rdev(align_bi) ||
3846 is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
3847 &first_bad, &bad_sectors)) {
3848 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08003849 bio_put(align_bi);
3850 rdev_dec_pending(rdev, mddev);
3851 return 0;
3852 }
3853
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003854 spin_lock_irq(&conf->device_lock);
3855 wait_event_lock_irq(conf->wait_for_stripe,
3856 conf->quiesce == 0,
3857 conf->device_lock, /* nothing */);
3858 atomic_inc(&conf->active_aligned_reads);
3859 spin_unlock_irq(&conf->device_lock);
3860
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003861 generic_make_request(align_bi);
3862 return 1;
3863 } else {
3864 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003865 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003866 return 0;
3867 }
3868}
3869
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003870/* __get_priority_stripe - get the next stripe to process
3871 *
3872 * Full stripe writes are allowed to pass preread active stripes up until
3873 * the bypass_threshold is exceeded. In general the bypass_count
3874 * increments when the handle_list is handled before the hold_list; however, it
3875 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3876 * stripe with in flight i/o. The bypass_count will be reset when the
3877 * head of the hold_list has changed, i.e. the head was promoted to the
3878 * handle_list.
3879 */
NeilBrownd1688a62011-10-11 16:49:52 +11003880static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003881{
3882 struct stripe_head *sh;
3883
3884 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3885 __func__,
3886 list_empty(&conf->handle_list) ? "empty" : "busy",
3887 list_empty(&conf->hold_list) ? "empty" : "busy",
3888 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3889
3890 if (!list_empty(&conf->handle_list)) {
3891 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3892
3893 if (list_empty(&conf->hold_list))
3894 conf->bypass_count = 0;
3895 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3896 if (conf->hold_list.next == conf->last_hold)
3897 conf->bypass_count++;
3898 else {
3899 conf->last_hold = conf->hold_list.next;
3900 conf->bypass_count -= conf->bypass_threshold;
3901 if (conf->bypass_count < 0)
3902 conf->bypass_count = 0;
3903 }
3904 }
3905 } else if (!list_empty(&conf->hold_list) &&
3906 ((conf->bypass_threshold &&
3907 conf->bypass_count > conf->bypass_threshold) ||
3908 atomic_read(&conf->pending_full_writes) == 0)) {
3909 sh = list_entry(conf->hold_list.next,
3910 typeof(*sh), lru);
3911 conf->bypass_count -= conf->bypass_threshold;
3912 if (conf->bypass_count < 0)
3913 conf->bypass_count = 0;
3914 } else
3915 return NULL;
3916
3917 list_del_init(&sh->lru);
3918 atomic_inc(&sh->count);
3919 BUG_ON(atomic_read(&sh->count) != 1);
3920 return sh;
3921}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003922
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07003923static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924{
NeilBrownd1688a62011-10-11 16:49:52 +11003925 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11003926 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 sector_t new_sector;
3928 sector_t logical_sector, last_sector;
3929 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01003930 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11003931 int remaining;
NeilBrown7c13edc2011-04-18 18:25:43 +10003932 int plugged;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933
Tejun Heoe9c74692010-09-03 11:56:18 +02003934 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
3935 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02003936 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07003937 }
3938
NeilBrown3d310eb2005-06-21 17:17:26 -07003939 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07003940
NeilBrown802ba062006-12-13 00:34:13 -08003941 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003942 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11003943 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02003944 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003945
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
3947 last_sector = bi->bi_sector + (bi->bi_size>>9);
3948 bi->bi_next = NULL;
3949 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07003950
NeilBrown7c13edc2011-04-18 18:25:43 +10003951 plugged = mddev_check_plugged(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
3953 DEFINE_WAIT(w);
NeilBrown16a53ec2006-06-26 00:27:38 -07003954 int disks, data_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003955 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08003956
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003957 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11003958 previous = 0;
NeilBrownb0f9ec02009-03-31 15:27:18 +11003959 disks = conf->raid_disks;
NeilBrownb578d552006-03-27 01:18:12 -08003960 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11003961 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11003962 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f762006-03-27 01:18:15 -08003963 * 64bit on a 32bit platform, and so it might be
3964 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02003965 * Of course reshape_progress could change after
NeilBrowndf8e7f762006-03-27 01:18:15 -08003966 * the lock is dropped, so once we get a reference
3967 * to the stripe that we think it is, we will have
3968 * to check again.
3969 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003970 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11003971 if (mddev->delta_disks < 0
3972 ? logical_sector < conf->reshape_progress
3973 : logical_sector >= conf->reshape_progress) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003974 disks = conf->previous_raid_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003975 previous = 1;
3976 } else {
NeilBrownfef9c612009-03-31 15:16:46 +11003977 if (mddev->delta_disks < 0
3978 ? logical_sector < conf->reshape_safe
3979 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08003980 spin_unlock_irq(&conf->device_lock);
3981 schedule();
3982 goto retry;
3983 }
3984 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003985 spin_unlock_irq(&conf->device_lock);
3986 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003987 data_disks = disks - conf->max_degraded;
3988
NeilBrown112bf892009-03-31 14:39:38 +11003989 new_sector = raid5_compute_sector(conf, logical_sector,
3990 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003991 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10003992 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 (unsigned long long)new_sector,
3994 (unsigned long long)logical_sector);
3995
NeilBrownb5663ba2009-03-31 14:39:38 +11003996 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10003997 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11003999 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004000 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f762006-03-27 01:18:15 -08004001 * stripe, so we must do the range check again.
4002 * Expansion could still move past after this
4003 * test, but as we are holding a reference to
4004 * 'sh', we know that if that happens,
4005 * STRIPE_EXPANDING will get set and the expansion
4006 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004007 */
4008 int must_retry = 0;
4009 spin_lock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004010 if (mddev->delta_disks < 0
4011 ? logical_sector >= conf->reshape_progress
4012 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004013 /* mismatch, need to try again */
4014 must_retry = 1;
4015 spin_unlock_irq(&conf->device_lock);
4016 if (must_retry) {
4017 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004018 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004019 goto retry;
4020 }
4021 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004022
Namhyung Kimffd96e32011-07-18 17:38:51 +10004023 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004024 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004025 logical_sector < mddev->suspend_hi) {
4026 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004027 /* As the suspend_* range is controlled by
4028 * userspace, we want an interruptible
4029 * wait.
4030 */
4031 flush_signals(current);
4032 prepare_to_wait(&conf->wait_for_overlap,
4033 &w, TASK_INTERRUPTIBLE);
4034 if (logical_sector >= mddev->suspend_lo &&
4035 logical_sector < mddev->suspend_hi)
4036 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004037 goto retry;
4038 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004039
4040 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004041 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004042 /* Stripe is busy expanding or
4043 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 * and wait a while
4045 */
NeilBrown482c0832011-04-18 18:25:42 +10004046 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 release_stripe(sh);
4048 schedule();
4049 goto retry;
4050 }
4051 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004052 set_bit(STRIPE_HANDLE, &sh->state);
4053 clear_bit(STRIPE_DELAYED, &sh->state);
Tejun Heoe9c74692010-09-03 11:56:18 +02004054 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004055 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4056 atomic_inc(&conf->preread_active_stripes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 } else {
4059 /* cannot get stripe for read-ahead, just give-up */
4060 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4061 finish_wait(&conf->wait_for_overlap, &w);
4062 break;
4063 }
4064
4065 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004066 if (!plugged)
4067 md_wakeup_thread(mddev->thread);
4068
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004070 remaining = raid5_dec_bi_phys_segments(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004071 spin_unlock_irq(&conf->device_lock);
4072 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073
NeilBrown16a53ec2006-06-26 00:27:38 -07004074 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004076
Neil Brown0e13fe232008-06-28 08:31:20 +10004077 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079}
4080
NeilBrownfd01b882011-10-11 16:47:53 +11004081static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004082
NeilBrownfd01b882011-10-11 16:47:53 +11004083static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084{
NeilBrown52c03292006-06-26 00:27:43 -07004085 /* reshaping is quite different to recovery/resync so it is
4086 * handled quite separately ... here.
4087 *
4088 * On each call to sync_request, we gather one chunk worth of
4089 * destination stripes and flag them as expanding.
4090 * Then we find all the source stripes and request reads.
4091 * As the reads complete, handle_stripe will copy the data
4092 * into the destination stripe and release that stripe.
4093 */
NeilBrownd1688a62011-10-11 16:49:52 +11004094 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004096 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004097 int raid_disks = conf->previous_raid_disks;
4098 int data_disks = raid_disks - conf->max_degraded;
4099 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004100 int i;
4101 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004102 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004103 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004104 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004105 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004106
NeilBrownfef9c612009-03-31 15:16:46 +11004107 if (sector_nr == 0) {
4108 /* If restarting in the middle, skip the initial sectors */
4109 if (mddev->delta_disks < 0 &&
4110 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4111 sector_nr = raid5_size(mddev, 0, 0)
4112 - conf->reshape_progress;
NeilBrowna6397552009-08-13 10:13:00 +10004113 } else if (mddev->delta_disks >= 0 &&
NeilBrownfef9c612009-03-31 15:16:46 +11004114 conf->reshape_progress > 0)
4115 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004116 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004117 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004118 mddev->curr_resync_completed = sector_nr;
4119 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004120 *skipped = 1;
4121 return sector_nr;
4122 }
NeilBrown52c03292006-06-26 00:27:43 -07004123 }
4124
NeilBrown7a661382009-03-31 15:21:40 +11004125 /* We need to process a full chunk at a time.
4126 * If old and new chunk sizes differ, we need to process the
4127 * largest of these
4128 */
Andre Noll664e7c42009-06-18 08:45:27 +10004129 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4130 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004131 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004132 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004133
NeilBrown52c03292006-06-26 00:27:43 -07004134 /* we update the metadata when there is more than 3Meg
4135 * in the block range (that is rather arbitrary, should
4136 * probably be time based) or when the data about to be
4137 * copied would over-write the source of the data at
4138 * the front of the range.
NeilBrownfef9c612009-03-31 15:16:46 +11004139 * i.e. one new_stripe along from reshape_progress new_maps
4140 * to after where reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004141 */
NeilBrownfef9c612009-03-31 15:16:46 +11004142 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004143 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004144 readpos = conf->reshape_progress;
4145 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004146 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004147 sector_div(safepos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004148 if (mddev->delta_disks < 0) {
NeilBrowned37d832009-05-27 21:39:05 +10004149 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004150 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004151 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004152 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004153 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004154 readpos -= min_t(sector_t, reshape_sectors, readpos);
4155 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004156 }
NeilBrown52c03292006-06-26 00:27:43 -07004157
NeilBrownc8f517c2009-03-31 15:28:40 +11004158 /* 'writepos' is the most advanced device address we might write.
4159 * 'readpos' is the least advanced device address we might read.
4160 * 'safepos' is the least address recorded in the metadata as having
4161 * been reshaped.
4162 * If 'readpos' is behind 'writepos', then there is no way that we can
4163 * ensure safety in the face of a crash - that must be done by userspace
4164 * making a backup of the data. So in that case there is no particular
4165 * rush to update metadata.
4166 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4167 * update the metadata to advance 'safepos' to match 'readpos' so that
4168 * we can be safe in the event of a crash.
4169 * So we insist on updating metadata if safepos is behind writepos and
4170 * readpos is beyond writepos.
4171 * In any case, update the metadata every 10 seconds.
4172 * Maybe that number should be configurable, but I'm not sure it is
4173 * worth it.... maybe it could be a multiple of safemode_delay???
4174 */
NeilBrownfef9c612009-03-31 15:16:46 +11004175 if ((mddev->delta_disks < 0
NeilBrownc8f517c2009-03-31 15:28:40 +11004176 ? (safepos > writepos && readpos < writepos)
4177 : (safepos < writepos && readpos > writepos)) ||
4178 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004179 /* Cannot proceed until we've updated the superblock... */
4180 wait_event(conf->wait_for_overlap,
4181 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004182 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004183 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004184 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b422006-10-03 01:15:46 -07004185 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004186 md_wakeup_thread(mddev->thread);
NeilBrown850b2b422006-10-03 01:15:46 -07004187 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004188 kthread_should_stop());
4189 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004190 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004191 spin_unlock_irq(&conf->device_lock);
4192 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004193 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004194 }
4195
NeilBrownec32a2b2009-03-31 15:17:38 +11004196 if (mddev->delta_disks < 0) {
4197 BUG_ON(conf->reshape_progress == 0);
4198 stripe_addr = writepos;
4199 BUG_ON((mddev->dev_sectors &
NeilBrown7a661382009-03-31 15:21:40 +11004200 ~((sector_t)reshape_sectors - 1))
4201 - reshape_sectors - stripe_addr
NeilBrownec32a2b2009-03-31 15:17:38 +11004202 != sector_nr);
4203 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004204 BUG_ON(writepos != sector_nr + reshape_sectors);
NeilBrownec32a2b2009-03-31 15:17:38 +11004205 stripe_addr = sector_nr;
4206 }
NeilBrownab69ae12009-03-31 15:26:47 +11004207 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004208 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004209 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004210 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004211 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004212 set_bit(STRIPE_EXPANDING, &sh->state);
4213 atomic_inc(&conf->reshape_stripes);
4214 /* If any of this stripe is beyond the end of the old
4215 * array, then we need to zero those blocks
4216 */
4217 for (j=sh->disks; j--;) {
4218 sector_t s;
4219 if (j == sh->pd_idx)
4220 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004221 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004222 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004223 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004224 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004225 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004226 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004227 continue;
4228 }
4229 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4230 set_bit(R5_Expanded, &sh->dev[j].flags);
4231 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4232 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004233 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004234 set_bit(STRIPE_EXPAND_READY, &sh->state);
4235 set_bit(STRIPE_HANDLE, &sh->state);
4236 }
NeilBrownab69ae12009-03-31 15:26:47 +11004237 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004238 }
4239 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004240 if (mddev->delta_disks < 0)
NeilBrown7a661382009-03-31 15:21:40 +11004241 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004242 else
NeilBrown7a661382009-03-31 15:21:40 +11004243 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004244 spin_unlock_irq(&conf->device_lock);
4245 /* Ok, those stripe are ready. We can start scheduling
4246 * reads on the source stripes.
4247 * The source stripes are determined by mapping the first and last
4248 * block on the destination stripes.
4249 */
NeilBrown52c03292006-06-26 00:27:43 -07004250 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004251 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004252 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004253 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004254 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004255 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004256 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004257 if (last_sector >= mddev->dev_sectors)
4258 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004259 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004260 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004261 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4262 set_bit(STRIPE_HANDLE, &sh->state);
4263 release_stripe(sh);
4264 first_sector += STRIPE_SECTORS;
4265 }
NeilBrownab69ae12009-03-31 15:26:47 +11004266 /* Now that the sources are clearly marked, we can release
4267 * the destination stripes
4268 */
4269 while (!list_empty(&stripes)) {
4270 sh = list_entry(stripes.next, struct stripe_head, lru);
4271 list_del_init(&sh->lru);
4272 release_stripe(sh);
4273 }
NeilBrownc6207272008-02-06 01:39:52 -08004274 /* If this takes us to the resync_max point where we have to pause,
4275 * then we need to write out the superblock.
4276 */
NeilBrown7a661382009-03-31 15:21:40 +11004277 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004278 if ((sector_nr - mddev->curr_resync_completed) * 2
4279 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004280 /* Cannot proceed until we've updated the superblock... */
4281 wait_event(conf->wait_for_overlap,
4282 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004283 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004284 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004285 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004286 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4287 md_wakeup_thread(mddev->thread);
4288 wait_event(mddev->sb_wait,
4289 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4290 || kthread_should_stop());
4291 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004292 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004293 spin_unlock_irq(&conf->device_lock);
4294 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004295 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004296 }
NeilBrown7a661382009-03-31 15:21:40 +11004297 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004298}
4299
4300/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004301static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004302{
NeilBrownd1688a62011-10-11 16:49:52 +11004303 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004304 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004305 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004306 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004307 int still_degraded = 0;
4308 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309
NeilBrown72626682005-09-09 16:23:54 -07004310 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004312
NeilBrown29269552006-03-27 01:18:10 -08004313 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4314 end_reshape(conf);
4315 return 0;
4316 }
NeilBrown72626682005-09-09 16:23:54 -07004317
4318 if (mddev->curr_resync < max_sector) /* aborted */
4319 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4320 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004321 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004322 conf->fullsync = 0;
4323 bitmap_close_sync(mddev->bitmap);
4324
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 return 0;
4326 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004327
NeilBrown64bd6602009-08-03 10:59:58 +10004328 /* Allow raid5_quiesce to complete */
4329 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4330
NeilBrown52c03292006-06-26 00:27:43 -07004331 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4332 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004333
NeilBrownc6207272008-02-06 01:39:52 -08004334 /* No need to check resync_max as we never do more than one
4335 * stripe, and as resync_max will always be on a chunk boundary,
4336 * if the check in md_do_sync didn't fire, there is no chance
4337 * of overstepping resync_max here
4338 */
4339
NeilBrown16a53ec2006-06-26 00:27:38 -07004340 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 * to resync, then assert that we are finished, because there is
4342 * nothing we can do.
4343 */
NeilBrown3285edf2006-06-26 00:27:55 -07004344 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004345 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004346 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004347 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 return rv;
4349 }
NeilBrown72626682005-09-09 16:23:54 -07004350 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004351 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004352 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4353 /* we can skip this block, and probably more */
4354 sync_blocks /= STRIPE_SECTORS;
4355 *skipped = 1;
4356 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358
NeilBrownb47490c2008-02-06 01:39:50 -08004359 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4360
NeilBrowna8c906c2009-06-09 14:39:59 +10004361 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004363 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004365 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004367 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004369 /* Need to check if array will still be degraded after recovery/resync
4370 * We don't need to check the 'failed' flag as when that gets set,
4371 * recovery aborts.
4372 */
NeilBrownf001a702009-06-09 14:30:31 +10004373 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004374 if (conf->disks[i].rdev == NULL)
4375 still_degraded = 1;
4376
4377 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4378
NeilBrown83206d62011-07-26 11:19:49 +10004379 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380
NeilBrown14425772009-10-16 15:55:25 +11004381 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 release_stripe(sh);
4383
4384 return STRIPE_SECTORS;
4385}
4386
NeilBrownd1688a62011-10-11 16:49:52 +11004387static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004388{
4389 /* We may not be able to submit a whole bio at once as there
4390 * may not be enough stripe_heads available.
4391 * We cannot pre-allocate enough stripe_heads as we may need
4392 * more than exist in the cache (if we allow ever large chunks).
4393 * So we do one stripe head at a time and record in
4394 * ->bi_hw_segments how many have been done.
4395 *
4396 * We *know* that this entire raid_bio is in one chunk, so
4397 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4398 */
4399 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004400 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004401 sector_t sector, logical_sector, last_sector;
4402 int scnt = 0;
4403 int remaining;
4404 int handled = 0;
4405
4406 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004407 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004408 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004409 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4410
4411 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004412 logical_sector += STRIPE_SECTORS,
4413 sector += STRIPE_SECTORS,
4414 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004415
Jens Axboe960e7392008-08-15 10:41:18 +02004416 if (scnt < raid5_bi_hw_segments(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004417 /* already done this stripe */
4418 continue;
4419
NeilBrowna8c906c2009-06-09 14:39:59 +10004420 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004421
4422 if (!sh) {
4423 /* failed to get a stripe - must wait */
Jens Axboe960e7392008-08-15 10:41:18 +02004424 raid5_set_bi_hw_segments(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004425 conf->retry_read_aligned = raid_bio;
4426 return handled;
4427 }
4428
Neil Brown387bb172007-02-08 14:20:29 -08004429 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4430 release_stripe(sh);
Jens Axboe960e7392008-08-15 10:41:18 +02004431 raid5_set_bi_hw_segments(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004432 conf->retry_read_aligned = raid_bio;
4433 return handled;
4434 }
4435
Dan Williams36d1c642009-07-14 11:48:22 -07004436 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004437 release_stripe(sh);
4438 handled++;
4439 }
4440 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004441 remaining = raid5_dec_bi_phys_segments(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004442 spin_unlock_irq(&conf->device_lock);
Neil Brown0e13fe232008-06-28 08:31:20 +10004443 if (remaining == 0)
4444 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004445 if (atomic_dec_and_test(&conf->active_aligned_reads))
4446 wake_up(&conf->wait_for_stripe);
4447 return handled;
4448}
4449
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004450
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451/*
4452 * This is our raid5 kernel thread.
4453 *
4454 * We scan the hash table for stripes which can be handled now.
4455 * During the scan, completed stripes are saved for us by the interrupt
4456 * handler, so that they will not have to wait for our next wakeup.
4457 */
NeilBrownfd01b882011-10-11 16:47:53 +11004458static void raid5d(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459{
4460 struct stripe_head *sh;
NeilBrownd1688a62011-10-11 16:49:52 +11004461 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004463 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464
Dan Williams45b42332007-07-09 11:56:43 -07004465 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466
4467 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004469 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 handled = 0;
4471 spin_lock_irq(&conf->device_lock);
4472 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004473 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474
NeilBrown7c13edc2011-04-18 18:25:43 +10004475 if (atomic_read(&mddev->plug_cnt) == 0 &&
4476 !list_empty(&conf->bitmap_list)) {
4477 /* Now is a good time to flush some bitmap updates */
4478 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004479 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004480 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004481 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004482 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004483 activate_bit_delay(conf);
4484 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004485 if (atomic_read(&mddev->plug_cnt) == 0)
4486 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004487
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004488 while ((bio = remove_bio_from_retry(conf))) {
4489 int ok;
4490 spin_unlock_irq(&conf->device_lock);
4491 ok = retry_aligned_read(conf, bio);
4492 spin_lock_irq(&conf->device_lock);
4493 if (!ok)
4494 break;
4495 handled++;
4496 }
4497
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004498 sh = __get_priority_stripe(conf);
4499
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004500 if (!sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 spin_unlock_irq(&conf->device_lock);
4503
4504 handled++;
Dan Williams417b8d42009-10-16 16:25:22 +11004505 handle_stripe(sh);
4506 release_stripe(sh);
4507 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508
NeilBrownde393cd2011-07-28 11:31:48 +10004509 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
4510 md_check_recovery(mddev);
4511
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 spin_lock_irq(&conf->device_lock);
4513 }
Dan Williams45b42332007-07-09 11:56:43 -07004514 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515
4516 spin_unlock_irq(&conf->device_lock);
4517
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004518 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004519 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520
Dan Williams45b42332007-07-09 11:56:43 -07004521 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522}
4523
NeilBrown3f294f42005-11-08 21:39:25 -08004524static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004525raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004526{
NeilBrownd1688a62011-10-11 16:49:52 +11004527 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004528 if (conf)
4529 return sprintf(page, "%d\n", conf->max_nr_stripes);
4530 else
4531 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004532}
4533
NeilBrownc41d4ac2010-06-01 19:37:24 +10004534int
NeilBrownfd01b882011-10-11 16:47:53 +11004535raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004536{
NeilBrownd1688a62011-10-11 16:49:52 +11004537 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004538 int err;
4539
4540 if (size <= 16 || size > 32768)
4541 return -EINVAL;
4542 while (size < conf->max_nr_stripes) {
4543 if (drop_one_stripe(conf))
4544 conf->max_nr_stripes--;
4545 else
4546 break;
4547 }
4548 err = md_allow_write(mddev);
4549 if (err)
4550 return err;
4551 while (size > conf->max_nr_stripes) {
4552 if (grow_one_stripe(conf))
4553 conf->max_nr_stripes++;
4554 else break;
4555 }
4556 return 0;
4557}
4558EXPORT_SYMBOL(raid5_set_cache_size);
4559
NeilBrown3f294f42005-11-08 21:39:25 -08004560static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004561raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004562{
NeilBrownd1688a62011-10-11 16:49:52 +11004563 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004564 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004565 int err;
4566
NeilBrown3f294f42005-11-08 21:39:25 -08004567 if (len >= PAGE_SIZE)
4568 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004569 if (!conf)
4570 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004571
Dan Williams4ef197d82008-04-28 02:15:54 -07004572 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004573 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004574 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004575 if (err)
4576 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004577 return len;
4578}
NeilBrown007583c2005-11-08 21:39:30 -08004579
NeilBrown96de1e62005-11-08 21:39:39 -08004580static struct md_sysfs_entry
4581raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4582 raid5_show_stripe_cache_size,
4583 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004584
4585static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004586raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004587{
NeilBrownd1688a62011-10-11 16:49:52 +11004588 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004589 if (conf)
4590 return sprintf(page, "%d\n", conf->bypass_threshold);
4591 else
4592 return 0;
4593}
4594
4595static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004596raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004597{
NeilBrownd1688a62011-10-11 16:49:52 +11004598 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004599 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004600 if (len >= PAGE_SIZE)
4601 return -EINVAL;
4602 if (!conf)
4603 return -ENODEV;
4604
Dan Williams4ef197d82008-04-28 02:15:54 -07004605 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004606 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004607 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004608 return -EINVAL;
4609 conf->bypass_threshold = new;
4610 return len;
4611}
4612
4613static struct md_sysfs_entry
4614raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4615 S_IRUGO | S_IWUSR,
4616 raid5_show_preread_threshold,
4617 raid5_store_preread_threshold);
4618
4619static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004620stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004621{
NeilBrownd1688a62011-10-11 16:49:52 +11004622 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004623 if (conf)
4624 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4625 else
4626 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004627}
4628
NeilBrown96de1e62005-11-08 21:39:39 -08004629static struct md_sysfs_entry
4630raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004631
NeilBrown007583c2005-11-08 21:39:30 -08004632static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004633 &raid5_stripecache_size.attr,
4634 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004635 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004636 NULL,
4637};
NeilBrown007583c2005-11-08 21:39:30 -08004638static struct attribute_group raid5_attrs_group = {
4639 .name = NULL,
4640 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004641};
4642
Dan Williams80c3a6c2009-03-17 18:10:40 -07004643static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11004644raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07004645{
NeilBrownd1688a62011-10-11 16:49:52 +11004646 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004647
4648 if (!sectors)
4649 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004650 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11004651 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11004652 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004653
Andre Noll9d8f0362009-06-18 08:45:01 +10004654 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004655 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004656 return sectors * (raid_disks - conf->max_degraded);
4657}
4658
NeilBrownd1688a62011-10-11 16:49:52 +11004659static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004660{
4661 struct raid5_percpu *percpu;
4662 unsigned long cpu;
4663
4664 if (!conf->percpu)
4665 return;
4666
4667 get_online_cpus();
4668 for_each_possible_cpu(cpu) {
4669 percpu = per_cpu_ptr(conf->percpu, cpu);
4670 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004671 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004672 }
4673#ifdef CONFIG_HOTPLUG_CPU
4674 unregister_cpu_notifier(&conf->cpu_notify);
4675#endif
4676 put_online_cpus();
4677
4678 free_percpu(conf->percpu);
4679}
4680
NeilBrownd1688a62011-10-11 16:49:52 +11004681static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10004682{
4683 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004684 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10004685 kfree(conf->disks);
4686 kfree(conf->stripe_hashtbl);
4687 kfree(conf);
4688}
4689
Dan Williams36d1c642009-07-14 11:48:22 -07004690#ifdef CONFIG_HOTPLUG_CPU
4691static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4692 void *hcpu)
4693{
NeilBrownd1688a62011-10-11 16:49:52 +11004694 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07004695 long cpu = (long)hcpu;
4696 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4697
4698 switch (action) {
4699 case CPU_UP_PREPARE:
4700 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07004701 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07004702 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004703 if (!percpu->scribble)
4704 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4705
4706 if (!percpu->scribble ||
4707 (conf->level == 6 && !percpu->spare_page)) {
4708 safe_put_page(percpu->spare_page);
4709 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004710 pr_err("%s: failed memory allocation for cpu%ld\n",
4711 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07004712 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07004713 }
4714 break;
4715 case CPU_DEAD:
4716 case CPU_DEAD_FROZEN:
4717 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004718 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004719 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004720 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07004721 break;
4722 default:
4723 break;
4724 }
4725 return NOTIFY_OK;
4726}
4727#endif
4728
NeilBrownd1688a62011-10-11 16:49:52 +11004729static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004730{
4731 unsigned long cpu;
4732 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09004733 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004734 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004735 int err;
4736
Dan Williams36d1c642009-07-14 11:48:22 -07004737 allcpus = alloc_percpu(struct raid5_percpu);
4738 if (!allcpus)
4739 return -ENOMEM;
4740 conf->percpu = allcpus;
4741
4742 get_online_cpus();
4743 err = 0;
4744 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07004745 if (conf->level == 6) {
4746 spare_page = alloc_page(GFP_KERNEL);
4747 if (!spare_page) {
4748 err = -ENOMEM;
4749 break;
4750 }
4751 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
4752 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11004753 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004754 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07004755 err = -ENOMEM;
4756 break;
4757 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07004758 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004759 }
4760#ifdef CONFIG_HOTPLUG_CPU
4761 conf->cpu_notify.notifier_call = raid456_cpu_notify;
4762 conf->cpu_notify.priority = 0;
4763 if (err == 0)
4764 err = register_cpu_notifier(&conf->cpu_notify);
4765#endif
4766 put_online_cpus();
4767
4768 return err;
4769}
4770
NeilBrownd1688a62011-10-11 16:49:52 +11004771static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772{
NeilBrownd1688a62011-10-11 16:49:52 +11004773 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004774 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11004775 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 struct disk_info *disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777
NeilBrown91adb562009-03-31 14:39:39 +11004778 if (mddev->new_level != 5
4779 && mddev->new_level != 4
4780 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10004781 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004782 mdname(mddev), mddev->new_level);
4783 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 }
NeilBrown91adb562009-03-31 14:39:39 +11004785 if ((mddev->new_level == 5
4786 && !algorithm_valid_raid5(mddev->new_layout)) ||
4787 (mddev->new_level == 6
4788 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004789 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11004790 mdname(mddev), mddev->new_layout);
4791 return ERR_PTR(-EIO);
4792 }
4793 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10004794 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004795 mdname(mddev), mddev->raid_disks);
4796 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11004797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798
Andre Noll664e7c42009-06-18 08:45:27 +10004799 if (!mddev->new_chunk_sectors ||
4800 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
4801 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10004802 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
4803 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11004804 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11004805 }
4806
NeilBrownd1688a62011-10-11 16:49:52 +11004807 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11004808 if (conf == NULL)
4809 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11004810 spin_lock_init(&conf->device_lock);
4811 init_waitqueue_head(&conf->wait_for_stripe);
4812 init_waitqueue_head(&conf->wait_for_overlap);
4813 INIT_LIST_HEAD(&conf->handle_list);
4814 INIT_LIST_HEAD(&conf->hold_list);
4815 INIT_LIST_HEAD(&conf->delayed_list);
4816 INIT_LIST_HEAD(&conf->bitmap_list);
4817 INIT_LIST_HEAD(&conf->inactive_list);
4818 atomic_set(&conf->active_stripes, 0);
4819 atomic_set(&conf->preread_active_stripes, 0);
4820 atomic_set(&conf->active_aligned_reads, 0);
4821 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11004822 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11004823
4824 conf->raid_disks = mddev->raid_disks;
4825 if (mddev->reshape_position == MaxSector)
4826 conf->previous_raid_disks = mddev->raid_disks;
4827 else
4828 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004829 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
4830 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11004831
NeilBrown5e5e3e72009-10-16 16:35:30 +11004832 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11004833 GFP_KERNEL);
4834 if (!conf->disks)
4835 goto abort;
4836
4837 conf->mddev = mddev;
4838
4839 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
4840 goto abort;
4841
Dan Williams36d1c642009-07-14 11:48:22 -07004842 conf->level = mddev->new_level;
4843 if (raid5_alloc_percpu(conf) != 0)
4844 goto abort;
4845
NeilBrown0c55e022010-05-03 14:09:02 +10004846 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11004847
NeilBrowndafb20f2012-03-19 12:46:39 +11004848 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11004849 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004850 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11004851 || raid_disk < 0)
4852 continue;
4853 disk = conf->disks + raid_disk;
4854
NeilBrown17045f52011-12-23 10:17:53 +11004855 if (test_bit(Replacement, &rdev->flags)) {
4856 if (disk->replacement)
4857 goto abort;
4858 disk->replacement = rdev;
4859 } else {
4860 if (disk->rdev)
4861 goto abort;
4862 disk->rdev = rdev;
4863 }
NeilBrown91adb562009-03-31 14:39:39 +11004864
4865 if (test_bit(In_sync, &rdev->flags)) {
4866 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10004867 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
4868 " disk %d\n",
4869 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05004870 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11004871 /* Cannot rely on bitmap to complete recovery */
4872 conf->fullsync = 1;
4873 }
4874
Andre Noll09c9e5f2009-06-18 08:45:55 +10004875 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11004876 conf->level = mddev->new_level;
4877 if (conf->level == 6)
4878 conf->max_degraded = 2;
4879 else
4880 conf->max_degraded = 1;
4881 conf->algorithm = mddev->new_layout;
4882 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11004883 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11004884 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10004885 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11004886 conf->prev_algo = mddev->layout;
4887 }
NeilBrown91adb562009-03-31 14:39:39 +11004888
4889 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11004890 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11004891 if (grow_stripes(conf, conf->max_nr_stripes)) {
4892 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004893 "md/raid:%s: couldn't allocate %dkB for buffers\n",
4894 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004895 goto abort;
4896 } else
NeilBrown0c55e022010-05-03 14:09:02 +10004897 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
4898 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004899
NeilBrown0da3c612009-09-23 18:09:45 +10004900 conf->thread = md_register_thread(raid5d, mddev, NULL);
NeilBrown91adb562009-03-31 14:39:39 +11004901 if (!conf->thread) {
4902 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004903 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11004904 mdname(mddev));
4905 goto abort;
4906 }
4907
4908 return conf;
4909
4910 abort:
4911 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10004912 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11004913 return ERR_PTR(-EIO);
4914 } else
4915 return ERR_PTR(-ENOMEM);
4916}
4917
NeilBrownc148ffd2009-11-13 17:47:00 +11004918
4919static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
4920{
4921 switch (algo) {
4922 case ALGORITHM_PARITY_0:
4923 if (raid_disk < max_degraded)
4924 return 1;
4925 break;
4926 case ALGORITHM_PARITY_N:
4927 if (raid_disk >= raid_disks - max_degraded)
4928 return 1;
4929 break;
4930 case ALGORITHM_PARITY_0_6:
4931 if (raid_disk == 0 ||
4932 raid_disk == raid_disks - 1)
4933 return 1;
4934 break;
4935 case ALGORITHM_LEFT_ASYMMETRIC_6:
4936 case ALGORITHM_RIGHT_ASYMMETRIC_6:
4937 case ALGORITHM_LEFT_SYMMETRIC_6:
4938 case ALGORITHM_RIGHT_SYMMETRIC_6:
4939 if (raid_disk == raid_disks - 1)
4940 return 1;
4941 }
4942 return 0;
4943}
4944
NeilBrownfd01b882011-10-11 16:47:53 +11004945static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11004946{
NeilBrownd1688a62011-10-11 16:49:52 +11004947 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10004948 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11004949 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11004950 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11004951 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11004952 int i;
NeilBrown91adb562009-03-31 14:39:39 +11004953
Andre Noll8c6ac8682009-06-18 08:48:06 +10004954 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10004955 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac8682009-06-18 08:48:06 +10004956 " -- starting background reconstruction\n",
4957 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004958 if (mddev->reshape_position != MaxSector) {
4959 /* Check that we can continue the reshape.
4960 * Currently only disks can change, it must
4961 * increase, and we must be past the point where
4962 * a stripe over-writes itself
4963 */
4964 sector_t here_new, here_old;
4965 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11004966 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08004967
NeilBrown88ce4932009-03-31 15:24:23 +11004968 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10004969 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08004970 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08004971 mdname(mddev));
4972 return -EINVAL;
4973 }
NeilBrownf6705572006-03-27 01:18:11 -08004974 old_disks = mddev->raid_disks - mddev->delta_disks;
4975 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08004976 * further up in new geometry must map after here in old
4977 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08004978 */
4979 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10004980 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08004981 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004982 printk(KERN_ERR "md/raid:%s: reshape_position not "
4983 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004984 return -EINVAL;
4985 }
NeilBrownc148ffd2009-11-13 17:47:00 +11004986 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08004987 /* here_new is the stripe we will write to */
4988 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10004989 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08004990 (old_disks-max_degraded));
4991 /* here_old is the first stripe that we might need to read
4992 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10004993 if (mddev->delta_disks == 0) {
4994 /* We cannot be sure it is safe to start an in-place
4995 * reshape. It is only safe if user-space if monitoring
4996 * and taking constant backups.
4997 * mdadm always starts a situation like this in
4998 * readonly mode so it can take control before
4999 * allowing any writes. So just check for that.
5000 */
5001 if ((here_new * mddev->new_chunk_sectors !=
5002 here_old * mddev->chunk_sectors) ||
5003 mddev->ro == 0) {
NeilBrown0c55e022010-05-03 14:09:02 +10005004 printk(KERN_ERR "md/raid:%s: in-place reshape must be started"
5005 " in read-only mode - aborting\n",
5006 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005007 return -EINVAL;
5008 }
5009 } else if (mddev->delta_disks < 0
5010 ? (here_new * mddev->new_chunk_sectors <=
5011 here_old * mddev->chunk_sectors)
5012 : (here_new * mddev->new_chunk_sectors >=
5013 here_old * mddev->chunk_sectors)) {
NeilBrownf6705572006-03-27 01:18:11 -08005014 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005015 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5016 "auto-recovery - aborting.\n",
5017 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005018 return -EINVAL;
5019 }
NeilBrown0c55e022010-05-03 14:09:02 +10005020 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5021 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005022 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005023 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005024 BUG_ON(mddev->level != mddev->new_level);
5025 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005026 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005027 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005028 }
5029
NeilBrown245f46c2009-03-31 14:39:39 +11005030 if (mddev->private == NULL)
5031 conf = setup_conf(mddev);
5032 else
5033 conf = mddev->private;
5034
NeilBrown91adb562009-03-31 14:39:39 +11005035 if (IS_ERR(conf))
5036 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005037
NeilBrown91adb562009-03-31 14:39:39 +11005038 mddev->thread = conf->thread;
5039 conf->thread = NULL;
5040 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041
NeilBrown17045f52011-12-23 10:17:53 +11005042 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5043 i++) {
5044 rdev = conf->disks[i].rdev;
5045 if (!rdev && conf->disks[i].replacement) {
5046 /* The replacement is all we have yet */
5047 rdev = conf->disks[i].replacement;
5048 conf->disks[i].replacement = NULL;
5049 clear_bit(Replacement, &rdev->flags);
5050 conf->disks[i].rdev = rdev;
5051 }
5052 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11005053 continue;
NeilBrown17045f52011-12-23 10:17:53 +11005054 if (conf->disks[i].replacement &&
5055 conf->reshape_progress != MaxSector) {
5056 /* replacements and reshape simply do not mix. */
5057 printk(KERN_ERR "md: cannot handle concurrent "
5058 "replacement and reshape.\n");
5059 goto abort;
5060 }
NeilBrown2f115882010-06-17 17:41:03 +10005061 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005062 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005063 continue;
5064 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005065 /* This disc is not fully in-sync. However if it
5066 * just stored parity (beyond the recovery_offset),
5067 * when we don't need to be concerned about the
5068 * array being dirty.
5069 * When reshape goes 'backwards', we never have
5070 * partially completed devices, so we only need
5071 * to worry about reshape going forwards.
5072 */
5073 /* Hack because v0.91 doesn't store recovery_offset properly. */
5074 if (mddev->major_version == 0 &&
5075 mddev->minor_version > 90)
5076 rdev->recovery_offset = reshape_offset;
5077
NeilBrownc148ffd2009-11-13 17:47:00 +11005078 if (rdev->recovery_offset < reshape_offset) {
5079 /* We need to check old and new layout */
5080 if (!only_parity(rdev->raid_disk,
5081 conf->algorithm,
5082 conf->raid_disks,
5083 conf->max_degraded))
5084 continue;
5085 }
5086 if (!only_parity(rdev->raid_disk,
5087 conf->prev_algo,
5088 conf->previous_raid_disks,
5089 conf->max_degraded))
5090 continue;
5091 dirty_parity_disks++;
5092 }
NeilBrown91adb562009-03-31 14:39:39 +11005093
NeilBrown17045f52011-12-23 10:17:53 +11005094 /*
5095 * 0 for a fully functional array, 1 or 2 for a degraded array.
5096 */
NeilBrown908f4fb2011-12-23 10:17:50 +11005097 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098
NeilBrown674806d2010-06-16 17:17:53 +10005099 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005100 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07005102 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 goto abort;
5104 }
5105
NeilBrown91adb562009-03-31 14:39:39 +11005106 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10005107 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11005108 mddev->resync_max_sectors = mddev->dev_sectors;
5109
NeilBrownc148ffd2009-11-13 17:47:00 +11005110 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005112 if (mddev->ok_start_degraded)
5113 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10005114 "md/raid:%s: starting dirty degraded array"
5115 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005116 mdname(mddev));
5117 else {
5118 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005119 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005120 mdname(mddev));
5121 goto abort;
5122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 }
5124
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10005126 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5127 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11005128 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5129 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 else
NeilBrown0c55e022010-05-03 14:09:02 +10005131 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5132 " out of %d devices, algorithm %d\n",
5133 mdname(mddev), conf->level,
5134 mddev->raid_disks - mddev->degraded,
5135 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136
5137 print_raid5_conf(conf);
5138
NeilBrownfef9c612009-03-31 15:16:46 +11005139 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11005140 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005141 atomic_set(&conf->reshape_stripes, 0);
5142 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5143 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5144 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5145 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5146 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005147 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005148 }
5149
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150
5151 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005152 if (mddev->to_remove == &raid5_attrs_group)
5153 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005154 else if (mddev->kobj.sd &&
5155 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005156 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10005157 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08005158 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10005159 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5160
5161 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005162 int chunk_size;
NeilBrown4a5add42010-06-01 19:37:28 +10005163 /* read-ahead size must cover two whole stripes, which
5164 * is 2 * (datadisks) * chunksize where 'n' is the
5165 * number of raid devices
5166 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5168 int stripe = data_disks *
5169 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5170 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5171 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10005172
5173 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005174
5175 mddev->queue->backing_dev_info.congested_data = mddev;
5176 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10005177
5178 chunk_size = mddev->chunk_sectors << 9;
5179 blk_queue_io_min(mddev->queue, chunk_size);
5180 blk_queue_io_opt(mddev->queue, chunk_size *
5181 (conf->raid_disks - conf->max_degraded));
5182
NeilBrowndafb20f2012-03-19 12:46:39 +11005183 rdev_for_each(rdev, mddev)
NeilBrown9f7c2222010-07-26 12:04:13 +10005184 disk_stack_limits(mddev->gendisk, rdev->bdev,
5185 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 }
5187
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 return 0;
5189abort:
NeilBrown01f96c02011-09-21 15:30:20 +10005190 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11005191 print_raid5_conf(conf);
5192 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005194 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 return -EIO;
5196}
5197
NeilBrownfd01b882011-10-11 16:47:53 +11005198static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199{
NeilBrownd1688a62011-10-11 16:49:52 +11005200 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201
NeilBrown01f96c02011-09-21 15:30:20 +10005202 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005203 if (mddev->queue)
5204 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10005205 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005206 mddev->private = NULL;
5207 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 return 0;
5209}
5210
NeilBrownfd01b882011-10-11 16:47:53 +11005211static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212{
NeilBrownd1688a62011-10-11 16:49:52 +11005213 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 int i;
5215
Andre Noll9d8f0362009-06-18 08:45:01 +10005216 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5217 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005218 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 for (i = 0; i < conf->raid_disks; i++)
5220 seq_printf (seq, "%s",
5221 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005222 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224}
5225
NeilBrownd1688a62011-10-11 16:49:52 +11005226static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227{
5228 int i;
5229 struct disk_info *tmp;
5230
NeilBrown0c55e022010-05-03 14:09:02 +10005231 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232 if (!conf) {
5233 printk("(conf==NULL)\n");
5234 return;
5235 }
NeilBrown0c55e022010-05-03 14:09:02 +10005236 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5237 conf->raid_disks,
5238 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239
5240 for (i = 0; i < conf->raid_disks; i++) {
5241 char b[BDEVNAME_SIZE];
5242 tmp = conf->disks + i;
5243 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005244 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5245 i, !test_bit(Faulty, &tmp->rdev->flags),
5246 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247 }
5248}
5249
NeilBrownfd01b882011-10-11 16:47:53 +11005250static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251{
5252 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005253 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005255 int count = 0;
5256 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257
5258 for (i = 0; i < conf->raid_disks; i++) {
5259 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11005260 if (tmp->replacement
5261 && tmp->replacement->recovery_offset == MaxSector
5262 && !test_bit(Faulty, &tmp->replacement->flags)
5263 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
5264 /* Replacement has just become active. */
5265 if (!tmp->rdev
5266 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
5267 count++;
5268 if (tmp->rdev) {
5269 /* Replaced device not technically faulty,
5270 * but we need to be sure it gets removed
5271 * and never re-added.
5272 */
5273 set_bit(Faulty, &tmp->rdev->flags);
5274 sysfs_notify_dirent_safe(
5275 tmp->rdev->sysfs_state);
5276 }
5277 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
5278 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005279 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005280 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005281 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005282 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005283 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 }
5285 }
NeilBrown6b965622010-08-18 11:56:59 +10005286 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005287 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005288 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005290 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291}
5292
NeilBrownb8321b62011-12-23 10:17:51 +11005293static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294{
NeilBrownd1688a62011-10-11 16:49:52 +11005295 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11005297 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11005298 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 struct disk_info *p = conf->disks + number;
5300
5301 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11005302 if (rdev == p->rdev)
5303 rdevp = &p->rdev;
5304 else if (rdev == p->replacement)
5305 rdevp = &p->replacement;
5306 else
5307 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11005308
NeilBrown657e3e42011-12-23 10:17:52 +11005309 if (number >= conf->raid_disks &&
5310 conf->reshape_progress == MaxSector)
5311 clear_bit(In_sync, &rdev->flags);
5312
5313 if (test_bit(In_sync, &rdev->flags) ||
5314 atomic_read(&rdev->nr_pending)) {
5315 err = -EBUSY;
5316 goto abort;
5317 }
5318 /* Only remove non-faulty devices if recovery
5319 * isn't possible.
5320 */
5321 if (!test_bit(Faulty, &rdev->flags) &&
5322 mddev->recovery_disabled != conf->recovery_disabled &&
5323 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11005324 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11005325 number < conf->raid_disks) {
5326 err = -EBUSY;
5327 goto abort;
5328 }
5329 *rdevp = NULL;
5330 synchronize_rcu();
5331 if (atomic_read(&rdev->nr_pending)) {
5332 /* lost the race, try later */
5333 err = -EBUSY;
5334 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11005335 } else if (p->replacement) {
5336 /* We must have just cleared 'rdev' */
5337 p->rdev = p->replacement;
5338 clear_bit(Replacement, &p->replacement->flags);
5339 smp_mb(); /* Make sure other CPUs may see both as identical
5340 * but will never see neither - if they are careful
5341 */
5342 p->replacement = NULL;
5343 clear_bit(WantReplacement, &rdev->flags);
5344 } else
5345 /* We might have just removed the Replacement as faulty-
5346 * clear the bit just in case
5347 */
5348 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349abort:
5350
5351 print_raid5_conf(conf);
5352 return err;
5353}
5354
NeilBrownfd01b882011-10-11 16:47:53 +11005355static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356{
NeilBrownd1688a62011-10-11 16:49:52 +11005357 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005358 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359 int disk;
5360 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005361 int first = 0;
5362 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363
NeilBrown7f0da592011-07-28 11:39:22 +10005364 if (mddev->recovery_disabled == conf->recovery_disabled)
5365 return -EBUSY;
5366
NeilBrowndc10c642012-03-19 12:46:37 +11005367 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005369 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370
Neil Brown6c2fce22008-06-28 08:31:31 +10005371 if (rdev->raid_disk >= 0)
5372 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373
5374 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005375 * find the disk ... but prefer rdev->saved_raid_disk
5376 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005378 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005379 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005380 conf->disks[rdev->saved_raid_disk].rdev == NULL)
5381 disk = rdev->saved_raid_disk;
5382 else
Neil Brown6c2fce22008-06-28 08:31:31 +10005383 disk = first;
NeilBrown7bfec5f2011-12-23 10:17:53 +11005384 for ( ; disk <= last ; disk++) {
5385 p = conf->disks + disk;
5386 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005387 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005389 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005390 if (rdev->saved_raid_disk != disk)
5391 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005392 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 break;
5394 }
NeilBrown7bfec5f2011-12-23 10:17:53 +11005395 if (test_bit(WantReplacement, &p->rdev->flags) &&
5396 p->replacement == NULL) {
5397 clear_bit(In_sync, &rdev->flags);
5398 set_bit(Replacement, &rdev->flags);
5399 rdev->raid_disk = disk;
5400 err = 0;
5401 conf->fullsync = 1;
5402 rcu_assign_pointer(p->replacement, rdev);
5403 break;
5404 }
5405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005407 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408}
5409
NeilBrownfd01b882011-10-11 16:47:53 +11005410static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411{
5412 /* no resync is happening, and there is enough space
5413 * on all devices, so we can resize.
5414 * We need to make sure resync covers any new space.
5415 * If the array is shrinking we should possibly wait until
5416 * any io in the removed space completes, but it hardly seems
5417 * worth it.
5418 */
Andre Noll9d8f0362009-06-18 08:45:01 +10005419 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Dan Williams1f403622009-03-31 14:59:03 +11005420 md_set_array_sectors(mddev, raid5_size(mddev, sectors,
5421 mddev->raid_disks));
Dan Williamsb522adc2009-03-31 15:00:31 +11005422 if (mddev->array_sectors >
5423 raid5_size(mddev, sectors, mddev->raid_disks))
5424 return -EINVAL;
Andre Nollf233ea52008-07-21 17:05:22 +10005425 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005426 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005427 if (sectors > mddev->dev_sectors &&
5428 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005429 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5431 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005432 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005433 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 return 0;
5435}
5436
NeilBrownfd01b882011-10-11 16:47:53 +11005437static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005438{
5439 /* Can only proceed if there are plenty of stripe_heads.
5440 * We need a minimum of one full stripe,, and for sensible progress
5441 * it is best to have about 4 times that.
5442 * If we require 4 times, then the default 256 4K stripe_heads will
5443 * allow for chunk sizes up to 256K, which is probably OK.
5444 * If the chunk size is greater, user-space should request more
5445 * stripe_heads first.
5446 */
NeilBrownd1688a62011-10-11 16:49:52 +11005447 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005448 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5449 > conf->max_nr_stripes ||
5450 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5451 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005452 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5453 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005454 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5455 / STRIPE_SIZE)*4);
5456 return 0;
5457 }
5458 return 1;
5459}
5460
NeilBrownfd01b882011-10-11 16:47:53 +11005461static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005462{
NeilBrownd1688a62011-10-11 16:49:52 +11005463 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005464
NeilBrown88ce4932009-03-31 15:24:23 +11005465 if (mddev->delta_disks == 0 &&
5466 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005467 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005468 return 0; /* nothing to do */
NeilBrowndba034e2008-08-05 15:54:13 +10005469 if (mddev->bitmap)
5470 /* Cannot grow a bitmap yet */
5471 return -EBUSY;
NeilBrown674806d2010-06-16 17:17:53 +10005472 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005473 return -EINVAL;
5474 if (mddev->delta_disks < 0) {
5475 /* We might be able to shrink, but the devices must
5476 * be made bigger first.
5477 * For raid6, 4 is the minimum size.
5478 * Otherwise 2 is the minimum
5479 */
5480 int min = 2;
5481 if (mddev->level == 6)
5482 min = 4;
5483 if (mddev->raid_disks + mddev->delta_disks < min)
5484 return -EINVAL;
5485 }
NeilBrown29269552006-03-27 01:18:10 -08005486
NeilBrown01ee22b2009-06-18 08:47:20 +10005487 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005488 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005489
NeilBrownec32a2b2009-03-31 15:17:38 +11005490 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
NeilBrown63c70c42006-03-27 01:18:13 -08005491}
5492
NeilBrownfd01b882011-10-11 16:47:53 +11005493static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005494{
NeilBrownd1688a62011-10-11 16:49:52 +11005495 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005496 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005497 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005498 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005499
NeilBrownf4168852007-02-28 20:11:53 -08005500 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005501 return -EBUSY;
5502
NeilBrown01ee22b2009-06-18 08:47:20 +10005503 if (!check_stripe_cache(mddev))
5504 return -ENOSPC;
5505
NeilBrowndafb20f2012-03-19 12:46:39 +11005506 rdev_for_each(rdev, mddev)
NeilBrown469518a2011-01-31 11:57:43 +11005507 if (!test_bit(In_sync, &rdev->flags)
5508 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005509 spares++;
NeilBrown63c70c42006-03-27 01:18:13 -08005510
NeilBrownf4168852007-02-28 20:11:53 -08005511 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005512 /* Not enough devices even to make a degraded array
5513 * of that size
5514 */
5515 return -EINVAL;
5516
NeilBrownec32a2b2009-03-31 15:17:38 +11005517 /* Refuse to reduce size of the array. Any reductions in
5518 * array size must be through explicit setting of array_size
5519 * attribute.
5520 */
5521 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5522 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10005523 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11005524 "before number of disks\n", mdname(mddev));
5525 return -EINVAL;
5526 }
5527
NeilBrownf6705572006-03-27 01:18:11 -08005528 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005529 spin_lock_irq(&conf->device_lock);
5530 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005531 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005532 conf->prev_chunk_sectors = conf->chunk_sectors;
5533 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005534 conf->prev_algo = conf->algorithm;
5535 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11005536 if (mddev->delta_disks < 0)
5537 conf->reshape_progress = raid5_size(mddev, 0, 0);
5538 else
5539 conf->reshape_progress = 0;
5540 conf->reshape_safe = conf->reshape_progress;
NeilBrown86b42c72009-03-31 15:19:03 +11005541 conf->generation++;
NeilBrown29269552006-03-27 01:18:10 -08005542 spin_unlock_irq(&conf->device_lock);
5543
5544 /* Add some new drives, as many as will fit.
5545 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10005546 * Don't add devices if we are reducing the number of
5547 * devices in the array. This is because it is not possible
5548 * to correctly record the "partially reconstructed" state of
5549 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08005550 */
NeilBrown87a8dec2011-01-31 11:57:43 +11005551 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11005552 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11005553 if (rdev->raid_disk < 0 &&
5554 !test_bit(Faulty, &rdev->flags)) {
5555 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11005556 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11005557 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11005558 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11005559 else
NeilBrown87a8dec2011-01-31 11:57:43 +11005560 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10005561
5562 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11005563 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11005564 }
NeilBrown87a8dec2011-01-31 11:57:43 +11005565 } else if (rdev->raid_disk >= conf->previous_raid_disks
5566 && !test_bit(Faulty, &rdev->flags)) {
5567 /* This is a spare that was manually added */
5568 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11005569 }
NeilBrown29269552006-03-27 01:18:10 -08005570
NeilBrown87a8dec2011-01-31 11:57:43 +11005571 /* When a reshape changes the number of devices,
5572 * ->degraded is measured against the larger of the
5573 * pre and post number of devices.
5574 */
NeilBrownec32a2b2009-03-31 15:17:38 +11005575 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005576 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11005577 spin_unlock_irqrestore(&conf->device_lock, flags);
5578 }
NeilBrown63c70c42006-03-27 01:18:13 -08005579 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10005580 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b422006-10-03 01:15:46 -07005581 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005582
NeilBrown29269552006-03-27 01:18:10 -08005583 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5584 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5585 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5586 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5587 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005588 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08005589 if (!mddev->sync_thread) {
5590 mddev->recovery = 0;
5591 spin_lock_irq(&conf->device_lock);
5592 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005593 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11005594 mddev->reshape_position = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005595 spin_unlock_irq(&conf->device_lock);
5596 return -EAGAIN;
5597 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005598 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005599 md_wakeup_thread(mddev->sync_thread);
5600 md_new_event(mddev);
5601 return 0;
5602}
NeilBrown29269552006-03-27 01:18:10 -08005603
NeilBrownec32a2b2009-03-31 15:17:38 +11005604/* This is called from the reshape thread and should make any
5605 * changes needed in 'conf'
5606 */
NeilBrownd1688a62011-10-11 16:49:52 +11005607static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08005608{
NeilBrown29269552006-03-27 01:18:10 -08005609
NeilBrownf6705572006-03-27 01:18:11 -08005610 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
Dan Williams80c3a6c2009-03-17 18:10:40 -07005611
NeilBrownf6705572006-03-27 01:18:11 -08005612 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11005613 conf->previous_raid_disks = conf->raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005614 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08005615 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005616 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07005617
5618 /* read-ahead size must cover two whole stripes, which is
5619 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5620 */
NeilBrown4a5add42010-06-01 19:37:28 +10005621 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11005622 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005623 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11005624 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07005625 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5626 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5627 }
NeilBrown29269552006-03-27 01:18:10 -08005628 }
NeilBrown29269552006-03-27 01:18:10 -08005629}
5630
NeilBrownec32a2b2009-03-31 15:17:38 +11005631/* This is called from the raid5d thread with mddev_lock held.
5632 * It makes config changes to the device.
5633 */
NeilBrownfd01b882011-10-11 16:47:53 +11005634static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11005635{
NeilBrownd1688a62011-10-11 16:49:52 +11005636 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11005637
5638 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5639
NeilBrownec32a2b2009-03-31 15:17:38 +11005640 if (mddev->delta_disks > 0) {
5641 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5642 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005643 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11005644 } else {
5645 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11005646 spin_lock_irq(&conf->device_lock);
5647 mddev->degraded = calc_degraded(conf);
5648 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11005649 for (d = conf->raid_disks ;
5650 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10005651 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11005652 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownb8321b62011-12-23 10:17:51 +11005653 if (rdev &&
5654 raid5_remove_disk(mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10005655 sysfs_unlink_rdev(mddev, rdev);
NeilBrown1a67dde2009-08-13 10:41:49 +10005656 rdev->raid_disk = -1;
5657 }
5658 }
NeilBrowncea9c222009-03-31 15:15:05 +11005659 }
NeilBrown88ce4932009-03-31 15:24:23 +11005660 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005661 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11005662 mddev->reshape_position = MaxSector;
5663 mddev->delta_disks = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11005664 }
5665}
5666
NeilBrownfd01b882011-10-11 16:47:53 +11005667static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07005668{
NeilBrownd1688a62011-10-11 16:49:52 +11005669 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07005670
5671 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08005672 case 2: /* resume for a suspend */
5673 wake_up(&conf->wait_for_overlap);
5674 break;
5675
NeilBrown72626682005-09-09 16:23:54 -07005676 case 1: /* stop all writes */
5677 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005678 /* '2' tells resync/reshape to pause so that all
5679 * active stripes can drain
5680 */
5681 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07005682 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005683 atomic_read(&conf->active_stripes) == 0 &&
5684 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07005685 conf->device_lock, /* nothing */);
NeilBrown64bd6602009-08-03 10:59:58 +10005686 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07005687 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005688 /* allow reshape to continue */
5689 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005690 break;
5691
5692 case 0: /* re-enable writes */
5693 spin_lock_irq(&conf->device_lock);
5694 conf->quiesce = 0;
5695 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08005696 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005697 spin_unlock_irq(&conf->device_lock);
5698 break;
5699 }
NeilBrown72626682005-09-09 16:23:54 -07005700}
NeilBrownb15c2e52006-01-06 00:20:16 -08005701
NeilBrownd562b0c2009-03-31 14:39:39 +11005702
NeilBrownfd01b882011-10-11 16:47:53 +11005703static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11005704{
NeilBrowne373ab12011-10-11 16:48:59 +11005705 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07005706 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11005707
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005708 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11005709 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10005710 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
5711 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005712 return ERR_PTR(-EINVAL);
5713 }
5714
NeilBrowne373ab12011-10-11 16:48:59 +11005715 sectors = raid0_conf->strip_zone[0].zone_end;
5716 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10005717 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005718 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11005719 mddev->new_layout = ALGORITHM_PARITY_N;
5720 mddev->new_chunk_sectors = mddev->chunk_sectors;
5721 mddev->raid_disks += 1;
5722 mddev->delta_disks = 1;
5723 /* make sure it will be not marked as dirty */
5724 mddev->recovery_cp = MaxSector;
5725
5726 return setup_conf(mddev);
5727}
5728
5729
NeilBrownfd01b882011-10-11 16:47:53 +11005730static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005731{
5732 int chunksect;
5733
5734 if (mddev->raid_disks != 2 ||
5735 mddev->degraded > 1)
5736 return ERR_PTR(-EINVAL);
5737
5738 /* Should check if there are write-behind devices? */
5739
5740 chunksect = 64*2; /* 64K by default */
5741
5742 /* The array must be an exact multiple of chunksize */
5743 while (chunksect && (mddev->array_sectors & (chunksect-1)))
5744 chunksect >>= 1;
5745
5746 if ((chunksect<<9) < STRIPE_SIZE)
5747 /* array size does not allow a suitable chunk size */
5748 return ERR_PTR(-EINVAL);
5749
5750 mddev->new_level = 5;
5751 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10005752 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11005753
5754 return setup_conf(mddev);
5755}
5756
NeilBrownfd01b882011-10-11 16:47:53 +11005757static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11005758{
5759 int new_layout;
5760
5761 switch (mddev->layout) {
5762 case ALGORITHM_LEFT_ASYMMETRIC_6:
5763 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
5764 break;
5765 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5766 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
5767 break;
5768 case ALGORITHM_LEFT_SYMMETRIC_6:
5769 new_layout = ALGORITHM_LEFT_SYMMETRIC;
5770 break;
5771 case ALGORITHM_RIGHT_SYMMETRIC_6:
5772 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
5773 break;
5774 case ALGORITHM_PARITY_0_6:
5775 new_layout = ALGORITHM_PARITY_0;
5776 break;
5777 case ALGORITHM_PARITY_N:
5778 new_layout = ALGORITHM_PARITY_N;
5779 break;
5780 default:
5781 return ERR_PTR(-EINVAL);
5782 }
5783 mddev->new_level = 5;
5784 mddev->new_layout = new_layout;
5785 mddev->delta_disks = -1;
5786 mddev->raid_disks -= 1;
5787 return setup_conf(mddev);
5788}
5789
NeilBrownd562b0c2009-03-31 14:39:39 +11005790
NeilBrownfd01b882011-10-11 16:47:53 +11005791static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11005792{
NeilBrown88ce4932009-03-31 15:24:23 +11005793 /* For a 2-drive array, the layout and chunk size can be changed
5794 * immediately as not restriping is needed.
5795 * For larger arrays we record the new value - after validation
5796 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11005797 */
NeilBrownd1688a62011-10-11 16:49:52 +11005798 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10005799 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11005800
NeilBrown597a7112009-06-18 08:47:42 +10005801 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11005802 return -EINVAL;
5803 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005804 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11005805 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005806 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11005807 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005808 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11005809 /* not factor of array size */
5810 return -EINVAL;
5811 }
5812
5813 /* They look valid */
5814
NeilBrown88ce4932009-03-31 15:24:23 +11005815 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10005816 /* can make the change immediately */
5817 if (mddev->new_layout >= 0) {
5818 conf->algorithm = mddev->new_layout;
5819 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11005820 }
5821 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10005822 conf->chunk_sectors = new_chunk ;
5823 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11005824 }
5825 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5826 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11005827 }
NeilBrown50ac1682009-06-18 08:47:55 +10005828 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11005829}
5830
NeilBrownfd01b882011-10-11 16:47:53 +11005831static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11005832{
NeilBrown597a7112009-06-18 08:47:42 +10005833 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10005834
NeilBrown597a7112009-06-18 08:47:42 +10005835 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11005836 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005837 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005838 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11005839 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005840 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11005841 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005842 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11005843 /* not factor of array size */
5844 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005845 }
NeilBrown88ce4932009-03-31 15:24:23 +11005846
5847 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10005848 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11005849}
5850
NeilBrownfd01b882011-10-11 16:47:53 +11005851static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005852{
5853 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005854 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005855 * raid1 - if there are two drives. We need to know the chunk size
5856 * raid4 - trivial - just use a raid4 layout.
5857 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005858 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005859 if (mddev->level == 0)
5860 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11005861 if (mddev->level == 1)
5862 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11005863 if (mddev->level == 4) {
5864 mddev->new_layout = ALGORITHM_PARITY_N;
5865 mddev->new_level = 5;
5866 return setup_conf(mddev);
5867 }
NeilBrownfc9739c2009-03-31 14:57:20 +11005868 if (mddev->level == 6)
5869 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11005870
5871 return ERR_PTR(-EINVAL);
5872}
5873
NeilBrownfd01b882011-10-11 16:47:53 +11005874static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11005875{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005876 /* raid4 can take over:
5877 * raid0 - if there is only one strip zone
5878 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11005879 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005880 if (mddev->level == 0)
5881 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11005882 if (mddev->level == 5 &&
5883 mddev->layout == ALGORITHM_PARITY_N) {
5884 mddev->new_layout = 0;
5885 mddev->new_level = 4;
5886 return setup_conf(mddev);
5887 }
5888 return ERR_PTR(-EINVAL);
5889}
NeilBrownd562b0c2009-03-31 14:39:39 +11005890
NeilBrown84fc4b52011-10-11 16:49:58 +11005891static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11005892
NeilBrownfd01b882011-10-11 16:47:53 +11005893static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11005894{
5895 /* Currently can only take over a raid5. We map the
5896 * personality to an equivalent raid6 personality
5897 * with the Q block at the end.
5898 */
5899 int new_layout;
5900
5901 if (mddev->pers != &raid5_personality)
5902 return ERR_PTR(-EINVAL);
5903 if (mddev->degraded > 1)
5904 return ERR_PTR(-EINVAL);
5905 if (mddev->raid_disks > 253)
5906 return ERR_PTR(-EINVAL);
5907 if (mddev->raid_disks < 3)
5908 return ERR_PTR(-EINVAL);
5909
5910 switch (mddev->layout) {
5911 case ALGORITHM_LEFT_ASYMMETRIC:
5912 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
5913 break;
5914 case ALGORITHM_RIGHT_ASYMMETRIC:
5915 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
5916 break;
5917 case ALGORITHM_LEFT_SYMMETRIC:
5918 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
5919 break;
5920 case ALGORITHM_RIGHT_SYMMETRIC:
5921 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
5922 break;
5923 case ALGORITHM_PARITY_0:
5924 new_layout = ALGORITHM_PARITY_0_6;
5925 break;
5926 case ALGORITHM_PARITY_N:
5927 new_layout = ALGORITHM_PARITY_N;
5928 break;
5929 default:
5930 return ERR_PTR(-EINVAL);
5931 }
5932 mddev->new_level = 6;
5933 mddev->new_layout = new_layout;
5934 mddev->delta_disks = 1;
5935 mddev->raid_disks += 1;
5936 return setup_conf(mddev);
5937}
5938
5939
NeilBrown84fc4b52011-10-11 16:49:58 +11005940static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07005941{
5942 .name = "raid6",
5943 .level = 6,
5944 .owner = THIS_MODULE,
5945 .make_request = make_request,
5946 .run = run,
5947 .stop = stop,
5948 .status = status,
5949 .error_handler = error,
5950 .hot_add_disk = raid5_add_disk,
5951 .hot_remove_disk= raid5_remove_disk,
5952 .spare_active = raid5_spare_active,
5953 .sync_request = sync_request,
5954 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005955 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10005956 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08005957 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005958 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07005959 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11005960 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07005961};
NeilBrown84fc4b52011-10-11 16:49:58 +11005962static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963{
5964 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08005965 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966 .owner = THIS_MODULE,
5967 .make_request = make_request,
5968 .run = run,
5969 .stop = stop,
5970 .status = status,
5971 .error_handler = error,
5972 .hot_add_disk = raid5_add_disk,
5973 .hot_remove_disk= raid5_remove_disk,
5974 .spare_active = raid5_spare_active,
5975 .sync_request = sync_request,
5976 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005977 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08005978 .check_reshape = raid5_check_reshape,
5979 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005980 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07005981 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11005982 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983};
5984
NeilBrown84fc4b52011-10-11 16:49:58 +11005985static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986{
NeilBrown2604b702006-01-06 00:20:36 -08005987 .name = "raid4",
5988 .level = 4,
5989 .owner = THIS_MODULE,
5990 .make_request = make_request,
5991 .run = run,
5992 .stop = stop,
5993 .status = status,
5994 .error_handler = error,
5995 .hot_add_disk = raid5_add_disk,
5996 .hot_remove_disk= raid5_remove_disk,
5997 .spare_active = raid5_spare_active,
5998 .sync_request = sync_request,
5999 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006000 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08006001 .check_reshape = raid5_check_reshape,
6002 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006003 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08006004 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11006005 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08006006};
6007
6008static int __init raid5_init(void)
6009{
NeilBrown16a53ec2006-06-26 00:27:38 -07006010 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006011 register_md_personality(&raid5_personality);
6012 register_md_personality(&raid4_personality);
6013 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014}
6015
NeilBrown2604b702006-01-06 00:20:36 -08006016static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017{
NeilBrown16a53ec2006-06-26 00:27:38 -07006018 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006019 unregister_md_personality(&raid5_personality);
6020 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021}
6022
6023module_init(raid5_init);
6024module_exit(raid5_exit);
6025MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11006026MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08006028MODULE_ALIAS("md-raid5");
6029MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08006030MODULE_ALIAS("md-level-5");
6031MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07006032MODULE_ALIAS("md-personality-8"); /* RAID6 */
6033MODULE_ALIAS("md-raid6");
6034MODULE_ALIAS("md-level-6");
6035
6036/* This used to be two separate modules, they were: */
6037MODULE_ALIAS("raid5");
6038MODULE_ALIAS("raid6");