blob: eab6168bb7f46fa5110369311de7dacdb732fa7a [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
NeilBrown05616be2012-05-21 09:27:00 +1000491/* Determine if 'data_offset' or 'new_data_offset' should be used
492 * in this stripe_head.
493 */
494static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
495{
496 sector_t progress = conf->reshape_progress;
497 /* Need a memory barrier to make sure we see the value
498 * of conf->generation, or ->data_offset that was set before
499 * reshape_progress was updated.
500 */
501 smp_rmb();
502 if (progress == MaxSector)
503 return 0;
504 if (sh->generation == conf->generation - 1)
505 return 0;
506 /* We are in a reshape, and this is a new-generation stripe,
507 * so use new_data_offset.
508 */
509 return 1;
510}
511
NeilBrown6712ecf2007-09-27 12:47:43 +0200512static void
513raid5_end_read_request(struct bio *bi, int error);
514static void
515raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700516
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000517static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700518{
NeilBrownd1688a62011-10-11 16:49:52 +1100519 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700520 int i, disks = sh->disks;
521
522 might_sleep();
523
524 for (i = disks; i--; ) {
525 int rw;
NeilBrown9a3e1102011-12-23 10:17:53 +1100526 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100527 struct bio *bi, *rbi;
528 struct md_rdev *rdev, *rrdev = NULL;
Tejun Heoe9c74692010-09-03 11:56:18 +0200529 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
530 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
531 rw = WRITE_FUA;
532 else
533 rw = WRITE;
534 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700535 rw = READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100536 else if (test_and_clear_bit(R5_WantReplace,
537 &sh->dev[i].flags)) {
538 rw = WRITE;
539 replace_only = 1;
540 } else
Dan Williams91c00922007-01-02 13:52:30 -0700541 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +1000542 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
543 rw |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -0700544
545 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100546 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700547
548 bi->bi_rw = rw;
NeilBrown977df362011-12-23 10:17:53 +1100549 rbi->bi_rw = rw;
550 if (rw & WRITE) {
Dan Williams91c00922007-01-02 13:52:30 -0700551 bi->bi_end_io = raid5_end_write_request;
NeilBrown977df362011-12-23 10:17:53 +1100552 rbi->bi_end_io = raid5_end_write_request;
553 } else
Dan Williams91c00922007-01-02 13:52:30 -0700554 bi->bi_end_io = raid5_end_read_request;
555
556 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100557 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100558 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
559 rdev = rcu_dereference(conf->disks[i].rdev);
560 if (!rdev) {
561 rdev = rrdev;
562 rrdev = NULL;
563 }
NeilBrown9a3e1102011-12-23 10:17:53 +1100564 if (rw & WRITE) {
565 if (replace_only)
566 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100567 if (rdev == rrdev)
568 /* We raced and saw duplicates */
569 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100570 } else {
NeilBrowndd054fc2011-12-23 10:17:53 +1100571 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100572 rdev = rrdev;
573 rrdev = NULL;
574 }
NeilBrown977df362011-12-23 10:17:53 +1100575
Dan Williams91c00922007-01-02 13:52:30 -0700576 if (rdev && test_bit(Faulty, &rdev->flags))
577 rdev = NULL;
578 if (rdev)
579 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100580 if (rrdev && test_bit(Faulty, &rrdev->flags))
581 rrdev = NULL;
582 if (rrdev)
583 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700584 rcu_read_unlock();
585
NeilBrown73e92e52011-07-28 11:39:22 +1000586 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100587 * need to check for writes. We never accept write errors
588 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000589 */
590 while ((rw & WRITE) && rdev &&
591 test_bit(WriteErrorSeen, &rdev->flags)) {
592 sector_t first_bad;
593 int bad_sectors;
594 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
595 &first_bad, &bad_sectors);
596 if (!bad)
597 break;
598
599 if (bad < 0) {
600 set_bit(BlockedBadBlocks, &rdev->flags);
601 if (!conf->mddev->external &&
602 conf->mddev->flags) {
603 /* It is very unlikely, but we might
604 * still need to write out the
605 * bad block log - better give it
606 * a chance*/
607 md_check_recovery(conf->mddev);
608 }
609 md_wait_for_blocked_rdev(rdev, conf->mddev);
610 } else {
611 /* Acknowledged bad block - skip the write */
612 rdev_dec_pending(rdev, conf->mddev);
613 rdev = NULL;
614 }
615 }
616
Dan Williams91c00922007-01-02 13:52:30 -0700617 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100618 if (s->syncing || s->expanding || s->expanded
619 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700620 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
621
Dan Williams2b7497f2008-06-28 08:31:52 +1000622 set_bit(STRIPE_IO_STARTED, &sh->state);
623
Dan Williams91c00922007-01-02 13:52:30 -0700624 bi->bi_bdev = rdev->bdev;
625 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700626 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700627 bi->bi_rw, i);
628 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000629 if (use_new_offset(conf, sh))
630 bi->bi_sector = (sh->sector
631 + rdev->new_data_offset);
632 else
633 bi->bi_sector = (sh->sector
634 + rdev->data_offset);
Dan Williams91c00922007-01-02 13:52:30 -0700635 bi->bi_flags = 1 << BIO_UPTODATE;
Dan Williams91c00922007-01-02 13:52:30 -0700636 bi->bi_idx = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700637 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
638 bi->bi_io_vec[0].bv_offset = 0;
639 bi->bi_size = STRIPE_SIZE;
640 bi->bi_next = NULL;
NeilBrown977df362011-12-23 10:17:53 +1100641 if (rrdev)
642 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Dan Williams91c00922007-01-02 13:52:30 -0700643 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100644 }
645 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100646 if (s->syncing || s->expanding || s->expanded
647 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +1100648 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
649
650 set_bit(STRIPE_IO_STARTED, &sh->state);
651
652 rbi->bi_bdev = rrdev->bdev;
653 pr_debug("%s: for %llu schedule op %ld on "
654 "replacement disc %d\n",
655 __func__, (unsigned long long)sh->sector,
656 rbi->bi_rw, i);
657 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000658 if (use_new_offset(conf, sh))
659 rbi->bi_sector = (sh->sector
660 + rrdev->new_data_offset);
661 else
662 rbi->bi_sector = (sh->sector
663 + rrdev->data_offset);
NeilBrown977df362011-12-23 10:17:53 +1100664 rbi->bi_flags = 1 << BIO_UPTODATE;
665 rbi->bi_idx = 0;
666 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
667 rbi->bi_io_vec[0].bv_offset = 0;
668 rbi->bi_size = STRIPE_SIZE;
669 rbi->bi_next = NULL;
670 generic_make_request(rbi);
671 }
672 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000673 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700674 set_bit(STRIPE_DEGRADED, &sh->state);
675 pr_debug("skip op %ld on disc %d for sector %llu\n",
676 bi->bi_rw, i, (unsigned long long)sh->sector);
677 clear_bit(R5_LOCKED, &sh->dev[i].flags);
678 set_bit(STRIPE_HANDLE, &sh->state);
679 }
680 }
681}
682
683static struct dma_async_tx_descriptor *
684async_copy_data(int frombio, struct bio *bio, struct page *page,
685 sector_t sector, struct dma_async_tx_descriptor *tx)
686{
687 struct bio_vec *bvl;
688 struct page *bio_page;
689 int i;
690 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700691 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700692 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700693
694 if (bio->bi_sector >= sector)
695 page_offset = (signed)(bio->bi_sector - sector) * 512;
696 else
697 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700698
Dan Williams0403e382009-09-08 17:42:50 -0700699 if (frombio)
700 flags |= ASYNC_TX_FENCE;
701 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
702
Dan Williams91c00922007-01-02 13:52:30 -0700703 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000704 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700705 int clen;
706 int b_offset = 0;
707
708 if (page_offset < 0) {
709 b_offset = -page_offset;
710 page_offset += b_offset;
711 len -= b_offset;
712 }
713
714 if (len > 0 && page_offset + len > STRIPE_SIZE)
715 clen = STRIPE_SIZE - page_offset;
716 else
717 clen = len;
718
719 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000720 b_offset += bvl->bv_offset;
721 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700722 if (frombio)
723 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700724 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700725 else
726 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700727 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700728 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700729 /* chain the operations */
730 submit.depend_tx = tx;
731
Dan Williams91c00922007-01-02 13:52:30 -0700732 if (clen < len) /* hit end of page */
733 break;
734 page_offset += len;
735 }
736
737 return tx;
738}
739
740static void ops_complete_biofill(void *stripe_head_ref)
741{
742 struct stripe_head *sh = stripe_head_ref;
743 struct bio *return_bi = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +1100744 struct r5conf *conf = sh->raid_conf;
Dan Williamse4d84902007-09-24 10:06:13 -0700745 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700746
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700747 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700748 (unsigned long long)sh->sector);
749
750 /* clear completed biofills */
Dan Williams83de75c2008-06-28 08:31:58 +1000751 spin_lock_irq(&conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700752 for (i = sh->disks; i--; ) {
753 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700754
755 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700756 /* and check if we need to reply to a read request,
757 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000758 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700759 */
760 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700761 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700762
Dan Williams91c00922007-01-02 13:52:30 -0700763 BUG_ON(!dev->read);
764 rbi = dev->read;
765 dev->read = NULL;
766 while (rbi && rbi->bi_sector <
767 dev->sector + STRIPE_SECTORS) {
768 rbi2 = r5_next_bio(rbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +0200769 if (!raid5_dec_bi_phys_segments(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700770 rbi->bi_next = return_bi;
771 return_bi = rbi;
772 }
Dan Williams91c00922007-01-02 13:52:30 -0700773 rbi = rbi2;
774 }
775 }
776 }
Dan Williams83de75c2008-06-28 08:31:58 +1000777 spin_unlock_irq(&conf->device_lock);
778 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700779
780 return_io(return_bi);
781
Dan Williamse4d84902007-09-24 10:06:13 -0700782 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700783 release_stripe(sh);
784}
785
786static void ops_run_biofill(struct stripe_head *sh)
787{
788 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +1100789 struct r5conf *conf = sh->raid_conf;
Dan Williamsa08abd82009-06-03 11:43:59 -0700790 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700791 int i;
792
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700793 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700794 (unsigned long long)sh->sector);
795
796 for (i = sh->disks; i--; ) {
797 struct r5dev *dev = &sh->dev[i];
798 if (test_bit(R5_Wantfill, &dev->flags)) {
799 struct bio *rbi;
800 spin_lock_irq(&conf->device_lock);
801 dev->read = rbi = dev->toread;
802 dev->toread = NULL;
803 spin_unlock_irq(&conf->device_lock);
804 while (rbi && rbi->bi_sector <
805 dev->sector + STRIPE_SECTORS) {
806 tx = async_copy_data(0, rbi, dev->page,
807 dev->sector, tx);
808 rbi = r5_next_bio(rbi, dev->sector);
809 }
810 }
811 }
812
813 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700814 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
815 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700816}
817
Dan Williams4e7d2c02009-08-29 19:13:11 -0700818static void mark_target_uptodate(struct stripe_head *sh, int target)
819{
820 struct r5dev *tgt;
821
822 if (target < 0)
823 return;
824
825 tgt = &sh->dev[target];
826 set_bit(R5_UPTODATE, &tgt->flags);
827 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
828 clear_bit(R5_Wantcompute, &tgt->flags);
829}
830
Dan Williamsac6b53b2009-07-14 13:40:19 -0700831static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700832{
833 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700834
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700835 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700836 (unsigned long long)sh->sector);
837
Dan Williamsac6b53b2009-07-14 13:40:19 -0700838 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700839 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700840 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700841
Dan Williamsecc65c92008-06-28 08:31:57 +1000842 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
843 if (sh->check_state == check_state_compute_run)
844 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700845 set_bit(STRIPE_HANDLE, &sh->state);
846 release_stripe(sh);
847}
848
Dan Williamsd6f38f32009-07-14 11:50:52 -0700849/* return a pointer to the address conversion region of the scribble buffer */
850static addr_conv_t *to_addr_conv(struct stripe_head *sh,
851 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700852{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700853 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
854}
855
856static struct dma_async_tx_descriptor *
857ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
858{
Dan Williams91c00922007-01-02 13:52:30 -0700859 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700860 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700861 int target = sh->ops.target;
862 struct r5dev *tgt = &sh->dev[target];
863 struct page *xor_dest = tgt->page;
864 int count = 0;
865 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700866 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700867 int i;
868
869 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700870 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700871 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
872
873 for (i = disks; i--; )
874 if (i != target)
875 xor_srcs[count++] = sh->dev[i].page;
876
877 atomic_inc(&sh->count);
878
Dan Williams0403e382009-09-08 17:42:50 -0700879 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700880 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700881 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700882 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700883 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700884 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700885
Dan Williams91c00922007-01-02 13:52:30 -0700886 return tx;
887}
888
Dan Williamsac6b53b2009-07-14 13:40:19 -0700889/* set_syndrome_sources - populate source buffers for gen_syndrome
890 * @srcs - (struct page *) array of size sh->disks
891 * @sh - stripe_head to parse
892 *
893 * Populates srcs in proper layout order for the stripe and returns the
894 * 'count' of sources to be used in a call to async_gen_syndrome. The P
895 * destination buffer is recorded in srcs[count] and the Q destination
896 * is recorded in srcs[count+1]].
897 */
898static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
899{
900 int disks = sh->disks;
901 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
902 int d0_idx = raid6_d0(sh);
903 int count;
904 int i;
905
906 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100907 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700908
909 count = 0;
910 i = d0_idx;
911 do {
912 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
913
914 srcs[slot] = sh->dev[i].page;
915 i = raid6_next_disk(i, disks);
916 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700917
NeilBrowne4424fe2009-10-16 16:27:34 +1100918 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700919}
920
921static struct dma_async_tx_descriptor *
922ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
923{
924 int disks = sh->disks;
925 struct page **blocks = percpu->scribble;
926 int target;
927 int qd_idx = sh->qd_idx;
928 struct dma_async_tx_descriptor *tx;
929 struct async_submit_ctl submit;
930 struct r5dev *tgt;
931 struct page *dest;
932 int i;
933 int count;
934
935 if (sh->ops.target < 0)
936 target = sh->ops.target2;
937 else if (sh->ops.target2 < 0)
938 target = sh->ops.target;
939 else
940 /* we should only have one valid target */
941 BUG();
942 BUG_ON(target < 0);
943 pr_debug("%s: stripe %llu block: %d\n",
944 __func__, (unsigned long long)sh->sector, target);
945
946 tgt = &sh->dev[target];
947 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
948 dest = tgt->page;
949
950 atomic_inc(&sh->count);
951
952 if (target == qd_idx) {
953 count = set_syndrome_sources(blocks, sh);
954 blocks[count] = NULL; /* regenerating p is not necessary */
955 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700956 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
957 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700958 to_addr_conv(sh, percpu));
959 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
960 } else {
961 /* Compute any data- or p-drive using XOR */
962 count = 0;
963 for (i = disks; i-- ; ) {
964 if (i == target || i == qd_idx)
965 continue;
966 blocks[count++] = sh->dev[i].page;
967 }
968
Dan Williams0403e382009-09-08 17:42:50 -0700969 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
970 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700971 to_addr_conv(sh, percpu));
972 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
973 }
974
975 return tx;
976}
977
978static struct dma_async_tx_descriptor *
979ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
980{
981 int i, count, disks = sh->disks;
982 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
983 int d0_idx = raid6_d0(sh);
984 int faila = -1, failb = -1;
985 int target = sh->ops.target;
986 int target2 = sh->ops.target2;
987 struct r5dev *tgt = &sh->dev[target];
988 struct r5dev *tgt2 = &sh->dev[target2];
989 struct dma_async_tx_descriptor *tx;
990 struct page **blocks = percpu->scribble;
991 struct async_submit_ctl submit;
992
993 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
994 __func__, (unsigned long long)sh->sector, target, target2);
995 BUG_ON(target < 0 || target2 < 0);
996 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
997 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
998
Dan Williams6c910a72009-09-16 12:24:54 -0700999 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001000 * slot number conversion for 'faila' and 'failb'
1001 */
1002 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001003 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001004 count = 0;
1005 i = d0_idx;
1006 do {
1007 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1008
1009 blocks[slot] = sh->dev[i].page;
1010
1011 if (i == target)
1012 faila = slot;
1013 if (i == target2)
1014 failb = slot;
1015 i = raid6_next_disk(i, disks);
1016 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001017
1018 BUG_ON(faila == failb);
1019 if (failb < faila)
1020 swap(faila, failb);
1021 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1022 __func__, (unsigned long long)sh->sector, faila, failb);
1023
1024 atomic_inc(&sh->count);
1025
1026 if (failb == syndrome_disks+1) {
1027 /* Q disk is one of the missing disks */
1028 if (faila == syndrome_disks) {
1029 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001030 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1031 ops_complete_compute, sh,
1032 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +11001033 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001034 STRIPE_SIZE, &submit);
1035 } else {
1036 struct page *dest;
1037 int data_target;
1038 int qd_idx = sh->qd_idx;
1039
1040 /* Missing D+Q: recompute D from P, then recompute Q */
1041 if (target == qd_idx)
1042 data_target = target2;
1043 else
1044 data_target = target;
1045
1046 count = 0;
1047 for (i = disks; i-- ; ) {
1048 if (i == data_target || i == qd_idx)
1049 continue;
1050 blocks[count++] = sh->dev[i].page;
1051 }
1052 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001053 init_async_submit(&submit,
1054 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1055 NULL, NULL, NULL,
1056 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001057 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1058 &submit);
1059
1060 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001061 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1062 ops_complete_compute, sh,
1063 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001064 return async_gen_syndrome(blocks, 0, count+2,
1065 STRIPE_SIZE, &submit);
1066 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001067 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001068 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1069 ops_complete_compute, sh,
1070 to_addr_conv(sh, percpu));
1071 if (failb == syndrome_disks) {
1072 /* We're missing D+P. */
1073 return async_raid6_datap_recov(syndrome_disks+2,
1074 STRIPE_SIZE, faila,
1075 blocks, &submit);
1076 } else {
1077 /* We're missing D+D. */
1078 return async_raid6_2data_recov(syndrome_disks+2,
1079 STRIPE_SIZE, faila, failb,
1080 blocks, &submit);
1081 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001082 }
1083}
1084
1085
Dan Williams91c00922007-01-02 13:52:30 -07001086static void ops_complete_prexor(void *stripe_head_ref)
1087{
1088 struct stripe_head *sh = stripe_head_ref;
1089
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001090 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001091 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001092}
1093
1094static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001095ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1096 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001097{
Dan Williams91c00922007-01-02 13:52:30 -07001098 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001099 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001100 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001101 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001102
1103 /* existing parity data subtracted */
1104 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1105
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001106 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001107 (unsigned long long)sh->sector);
1108
1109 for (i = disks; i--; ) {
1110 struct r5dev *dev = &sh->dev[i];
1111 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001112 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001113 xor_srcs[count++] = dev->page;
1114 }
1115
Dan Williams0403e382009-09-08 17:42:50 -07001116 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001117 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001118 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001119
1120 return tx;
1121}
1122
1123static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001124ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001125{
1126 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001127 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001128
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001129 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001130 (unsigned long long)sh->sector);
1131
1132 for (i = disks; i--; ) {
1133 struct r5dev *dev = &sh->dev[i];
1134 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001135
Dan Williamsd8ee0722008-06-28 08:32:06 +10001136 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001137 struct bio *wbi;
1138
NeilBrowncbe47ec2011-07-26 11:20:35 +10001139 spin_lock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001140 chosen = dev->towrite;
1141 dev->towrite = NULL;
1142 BUG_ON(dev->written);
1143 wbi = dev->written = chosen;
NeilBrowncbe47ec2011-07-26 11:20:35 +10001144 spin_unlock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001145
1146 while (wbi && wbi->bi_sector <
1147 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001148 if (wbi->bi_rw & REQ_FUA)
1149 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001150 if (wbi->bi_rw & REQ_SYNC)
1151 set_bit(R5_SyncIO, &dev->flags);
Dan Williams91c00922007-01-02 13:52:30 -07001152 tx = async_copy_data(1, wbi, dev->page,
1153 dev->sector, tx);
1154 wbi = r5_next_bio(wbi, dev->sector);
1155 }
1156 }
1157 }
1158
1159 return tx;
1160}
1161
Dan Williamsac6b53b2009-07-14 13:40:19 -07001162static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001163{
1164 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001165 int disks = sh->disks;
1166 int pd_idx = sh->pd_idx;
1167 int qd_idx = sh->qd_idx;
1168 int i;
Shaohua Libc0934f2012-05-22 13:55:05 +10001169 bool fua = false, sync = false;
Dan Williams91c00922007-01-02 13:52:30 -07001170
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001171 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001172 (unsigned long long)sh->sector);
1173
Shaohua Libc0934f2012-05-22 13:55:05 +10001174 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001175 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001176 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
1177 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001178
Dan Williams91c00922007-01-02 13:52:30 -07001179 for (i = disks; i--; ) {
1180 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001181
Tejun Heoe9c74692010-09-03 11:56:18 +02001182 if (dev->written || i == pd_idx || i == qd_idx) {
Dan Williams91c00922007-01-02 13:52:30 -07001183 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001184 if (fua)
1185 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001186 if (sync)
1187 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001188 }
Dan Williams91c00922007-01-02 13:52:30 -07001189 }
1190
Dan Williamsd8ee0722008-06-28 08:32:06 +10001191 if (sh->reconstruct_state == reconstruct_state_drain_run)
1192 sh->reconstruct_state = reconstruct_state_drain_result;
1193 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1194 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1195 else {
1196 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1197 sh->reconstruct_state = reconstruct_state_result;
1198 }
Dan Williams91c00922007-01-02 13:52:30 -07001199
1200 set_bit(STRIPE_HANDLE, &sh->state);
1201 release_stripe(sh);
1202}
1203
1204static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001205ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1206 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001207{
Dan Williams91c00922007-01-02 13:52:30 -07001208 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001209 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001210 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001211 int count = 0, pd_idx = sh->pd_idx, i;
1212 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001213 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001214 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001215
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001216 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001217 (unsigned long long)sh->sector);
1218
1219 /* check if prexor is active which means only process blocks
1220 * that are part of a read-modify-write (written)
1221 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001222 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1223 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001224 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1225 for (i = disks; i--; ) {
1226 struct r5dev *dev = &sh->dev[i];
1227 if (dev->written)
1228 xor_srcs[count++] = dev->page;
1229 }
1230 } else {
1231 xor_dest = sh->dev[pd_idx].page;
1232 for (i = disks; i--; ) {
1233 struct r5dev *dev = &sh->dev[i];
1234 if (i != pd_idx)
1235 xor_srcs[count++] = dev->page;
1236 }
1237 }
1238
Dan Williams91c00922007-01-02 13:52:30 -07001239 /* 1/ if we prexor'd then the dest is reused as a source
1240 * 2/ if we did not prexor then we are redoing the parity
1241 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1242 * for the synchronous xor case
1243 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001244 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001245 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1246
1247 atomic_inc(&sh->count);
1248
Dan Williamsac6b53b2009-07-14 13:40:19 -07001249 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001250 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001251 if (unlikely(count == 1))
1252 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1253 else
1254 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001255}
1256
Dan Williamsac6b53b2009-07-14 13:40:19 -07001257static void
1258ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1259 struct dma_async_tx_descriptor *tx)
1260{
1261 struct async_submit_ctl submit;
1262 struct page **blocks = percpu->scribble;
1263 int count;
1264
1265 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1266
1267 count = set_syndrome_sources(blocks, sh);
1268
1269 atomic_inc(&sh->count);
1270
1271 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1272 sh, to_addr_conv(sh, percpu));
1273 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001274}
1275
1276static void ops_complete_check(void *stripe_head_ref)
1277{
1278 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001279
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001280 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001281 (unsigned long long)sh->sector);
1282
Dan Williamsecc65c92008-06-28 08:31:57 +10001283 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001284 set_bit(STRIPE_HANDLE, &sh->state);
1285 release_stripe(sh);
1286}
1287
Dan Williamsac6b53b2009-07-14 13:40:19 -07001288static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001289{
Dan Williams91c00922007-01-02 13:52:30 -07001290 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001291 int pd_idx = sh->pd_idx;
1292 int qd_idx = sh->qd_idx;
1293 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001294 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001295 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001296 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001297 int count;
1298 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001299
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001300 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001301 (unsigned long long)sh->sector);
1302
Dan Williamsac6b53b2009-07-14 13:40:19 -07001303 count = 0;
1304 xor_dest = sh->dev[pd_idx].page;
1305 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001306 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001307 if (i == pd_idx || i == qd_idx)
1308 continue;
1309 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001310 }
1311
Dan Williamsd6f38f32009-07-14 11:50:52 -07001312 init_async_submit(&submit, 0, NULL, NULL, NULL,
1313 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001314 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001315 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001316
Dan Williams91c00922007-01-02 13:52:30 -07001317 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001318 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1319 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001320}
1321
Dan Williamsac6b53b2009-07-14 13:40:19 -07001322static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1323{
1324 struct page **srcs = percpu->scribble;
1325 struct async_submit_ctl submit;
1326 int count;
1327
1328 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1329 (unsigned long long)sh->sector, checkp);
1330
1331 count = set_syndrome_sources(srcs, sh);
1332 if (!checkp)
1333 srcs[count] = NULL;
1334
1335 atomic_inc(&sh->count);
1336 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1337 sh, to_addr_conv(sh, percpu));
1338 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1339 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1340}
1341
Dan Williams417b8d42009-10-16 16:25:22 +11001342static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001343{
1344 int overlap_clear = 0, i, disks = sh->disks;
1345 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001346 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001347 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001348 struct raid5_percpu *percpu;
1349 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001350
Dan Williamsd6f38f32009-07-14 11:50:52 -07001351 cpu = get_cpu();
1352 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001353 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001354 ops_run_biofill(sh);
1355 overlap_clear++;
1356 }
1357
Dan Williams7b3a8712008-06-28 08:32:09 +10001358 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001359 if (level < 6)
1360 tx = ops_run_compute5(sh, percpu);
1361 else {
1362 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1363 tx = ops_run_compute6_1(sh, percpu);
1364 else
1365 tx = ops_run_compute6_2(sh, percpu);
1366 }
1367 /* terminate the chain if reconstruct is not set to be run */
1368 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001369 async_tx_ack(tx);
1370 }
Dan Williams91c00922007-01-02 13:52:30 -07001371
Dan Williams600aa102008-06-28 08:32:05 +10001372 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001373 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001374
Dan Williams600aa102008-06-28 08:32:05 +10001375 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001376 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001377 overlap_clear++;
1378 }
1379
Dan Williamsac6b53b2009-07-14 13:40:19 -07001380 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1381 if (level < 6)
1382 ops_run_reconstruct5(sh, percpu, tx);
1383 else
1384 ops_run_reconstruct6(sh, percpu, tx);
1385 }
Dan Williams91c00922007-01-02 13:52:30 -07001386
Dan Williamsac6b53b2009-07-14 13:40:19 -07001387 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1388 if (sh->check_state == check_state_run)
1389 ops_run_check_p(sh, percpu);
1390 else if (sh->check_state == check_state_run_q)
1391 ops_run_check_pq(sh, percpu, 0);
1392 else if (sh->check_state == check_state_run_pq)
1393 ops_run_check_pq(sh, percpu, 1);
1394 else
1395 BUG();
1396 }
Dan Williams91c00922007-01-02 13:52:30 -07001397
Dan Williams91c00922007-01-02 13:52:30 -07001398 if (overlap_clear)
1399 for (i = disks; i--; ) {
1400 struct r5dev *dev = &sh->dev[i];
1401 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1402 wake_up(&sh->raid_conf->wait_for_overlap);
1403 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001404 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001405}
1406
Dan Williams417b8d42009-10-16 16:25:22 +11001407#ifdef CONFIG_MULTICORE_RAID456
1408static void async_run_ops(void *param, async_cookie_t cookie)
1409{
1410 struct stripe_head *sh = param;
1411 unsigned long ops_request = sh->ops.request;
1412
1413 clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
1414 wake_up(&sh->ops.wait_for_ops);
1415
1416 __raid_run_ops(sh, ops_request);
1417 release_stripe(sh);
1418}
1419
1420static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1421{
1422 /* since handle_stripe can be called outside of raid5d context
1423 * we need to ensure sh->ops.request is de-staged before another
1424 * request arrives
1425 */
1426 wait_event(sh->ops.wait_for_ops,
1427 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
1428 sh->ops.request = ops_request;
1429
1430 atomic_inc(&sh->count);
1431 async_schedule(async_run_ops, sh);
1432}
1433#else
1434#define raid_run_ops __raid_run_ops
1435#endif
1436
NeilBrownd1688a62011-10-11 16:49:52 +11001437static int grow_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
1439 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001440 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001441 if (!sh)
1442 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001443
NeilBrown3f294f42005-11-08 21:39:25 -08001444 sh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001445 #ifdef CONFIG_MULTICORE_RAID456
1446 init_waitqueue_head(&sh->ops.wait_for_ops);
1447 #endif
NeilBrown3f294f42005-11-08 21:39:25 -08001448
NeilBrowne4e11e32010-06-16 16:45:16 +10001449 if (grow_buffers(sh)) {
1450 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001451 kmem_cache_free(conf->slab_cache, sh);
1452 return 0;
1453 }
1454 /* we just created an active stripe so... */
1455 atomic_set(&sh->count, 1);
1456 atomic_inc(&conf->active_stripes);
1457 INIT_LIST_HEAD(&sh->lru);
1458 release_stripe(sh);
1459 return 1;
1460}
1461
NeilBrownd1688a62011-10-11 16:49:52 +11001462static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001463{
Christoph Lametere18b8902006-12-06 20:33:20 -08001464 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001465 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
NeilBrownf4be6b42010-06-01 19:37:25 +10001467 if (conf->mddev->gendisk)
1468 sprintf(conf->cache_name[0],
1469 "raid%d-%s", conf->level, mdname(conf->mddev));
1470 else
1471 sprintf(conf->cache_name[0],
1472 "raid%d-%p", conf->level, conf->mddev);
1473 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1474
NeilBrownad01c9e2006-03-27 01:18:07 -08001475 conf->active_name = 0;
1476 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001478 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 if (!sc)
1480 return 1;
1481 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001482 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001483 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001484 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 return 0;
1487}
NeilBrown29269552006-03-27 01:18:10 -08001488
Dan Williamsd6f38f32009-07-14 11:50:52 -07001489/**
1490 * scribble_len - return the required size of the scribble region
1491 * @num - total number of disks in the array
1492 *
1493 * The size must be enough to contain:
1494 * 1/ a struct page pointer for each device in the array +2
1495 * 2/ room to convert each entry in (1) to its corresponding dma
1496 * (dma_map_page()) or page (page_address()) address.
1497 *
1498 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1499 * calculate over all devices (not just the data blocks), using zeros in place
1500 * of the P and Q blocks.
1501 */
1502static size_t scribble_len(int num)
1503{
1504 size_t len;
1505
1506 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1507
1508 return len;
1509}
1510
NeilBrownd1688a62011-10-11 16:49:52 +11001511static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001512{
1513 /* Make all the stripes able to hold 'newsize' devices.
1514 * New slots in each stripe get 'page' set to a new page.
1515 *
1516 * This happens in stages:
1517 * 1/ create a new kmem_cache and allocate the required number of
1518 * stripe_heads.
1519 * 2/ gather all the old stripe_heads and tranfer the pages across
1520 * to the new stripe_heads. This will have the side effect of
1521 * freezing the array as once all stripe_heads have been collected,
1522 * no IO will be possible. Old stripe heads are freed once their
1523 * pages have been transferred over, and the old kmem_cache is
1524 * freed when all stripes are done.
1525 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1526 * we simple return a failre status - no need to clean anything up.
1527 * 4/ allocate new pages for the new slots in the new stripe_heads.
1528 * If this fails, we don't bother trying the shrink the
1529 * stripe_heads down again, we just leave them as they are.
1530 * As each stripe_head is processed the new one is released into
1531 * active service.
1532 *
1533 * Once step2 is started, we cannot afford to wait for a write,
1534 * so we use GFP_NOIO allocations.
1535 */
1536 struct stripe_head *osh, *nsh;
1537 LIST_HEAD(newstripes);
1538 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001539 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001540 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001541 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001542 int i;
1543
1544 if (newsize <= conf->pool_size)
1545 return 0; /* never bother to shrink */
1546
Dan Williamsb5470dc2008-06-27 21:44:04 -07001547 err = md_allow_write(conf->mddev);
1548 if (err)
1549 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001550
NeilBrownad01c9e2006-03-27 01:18:07 -08001551 /* Step 1 */
1552 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1553 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001554 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001555 if (!sc)
1556 return -ENOMEM;
1557
1558 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001559 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001560 if (!nsh)
1561 break;
1562
NeilBrownad01c9e2006-03-27 01:18:07 -08001563 nsh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001564 #ifdef CONFIG_MULTICORE_RAID456
1565 init_waitqueue_head(&nsh->ops.wait_for_ops);
1566 #endif
NeilBrownad01c9e2006-03-27 01:18:07 -08001567
1568 list_add(&nsh->lru, &newstripes);
1569 }
1570 if (i) {
1571 /* didn't get enough, give up */
1572 while (!list_empty(&newstripes)) {
1573 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1574 list_del(&nsh->lru);
1575 kmem_cache_free(sc, nsh);
1576 }
1577 kmem_cache_destroy(sc);
1578 return -ENOMEM;
1579 }
1580 /* Step 2 - Must use GFP_NOIO now.
1581 * OK, we have enough stripes, start collecting inactive
1582 * stripes and copying them over
1583 */
1584 list_for_each_entry(nsh, &newstripes, lru) {
1585 spin_lock_irq(&conf->device_lock);
1586 wait_event_lock_irq(conf->wait_for_stripe,
1587 !list_empty(&conf->inactive_list),
1588 conf->device_lock,
NeilBrown482c0832011-04-18 18:25:42 +10001589 );
NeilBrownad01c9e2006-03-27 01:18:07 -08001590 osh = get_free_stripe(conf);
1591 spin_unlock_irq(&conf->device_lock);
1592 atomic_set(&nsh->count, 1);
1593 for(i=0; i<conf->pool_size; i++)
1594 nsh->dev[i].page = osh->dev[i].page;
1595 for( ; i<newsize; i++)
1596 nsh->dev[i].page = NULL;
1597 kmem_cache_free(conf->slab_cache, osh);
1598 }
1599 kmem_cache_destroy(conf->slab_cache);
1600
1601 /* Step 3.
1602 * At this point, we are holding all the stripes so the array
1603 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001604 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001605 */
1606 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1607 if (ndisks) {
1608 for (i=0; i<conf->raid_disks; i++)
1609 ndisks[i] = conf->disks[i];
1610 kfree(conf->disks);
1611 conf->disks = ndisks;
1612 } else
1613 err = -ENOMEM;
1614
Dan Williamsd6f38f32009-07-14 11:50:52 -07001615 get_online_cpus();
1616 conf->scribble_len = scribble_len(newsize);
1617 for_each_present_cpu(cpu) {
1618 struct raid5_percpu *percpu;
1619 void *scribble;
1620
1621 percpu = per_cpu_ptr(conf->percpu, cpu);
1622 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1623
1624 if (scribble) {
1625 kfree(percpu->scribble);
1626 percpu->scribble = scribble;
1627 } else {
1628 err = -ENOMEM;
1629 break;
1630 }
1631 }
1632 put_online_cpus();
1633
NeilBrownad01c9e2006-03-27 01:18:07 -08001634 /* Step 4, return new stripes to service */
1635 while(!list_empty(&newstripes)) {
1636 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1637 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001638
NeilBrownad01c9e2006-03-27 01:18:07 -08001639 for (i=conf->raid_disks; i < newsize; i++)
1640 if (nsh->dev[i].page == NULL) {
1641 struct page *p = alloc_page(GFP_NOIO);
1642 nsh->dev[i].page = p;
1643 if (!p)
1644 err = -ENOMEM;
1645 }
1646 release_stripe(nsh);
1647 }
1648 /* critical section pass, GFP_NOIO no longer needed */
1649
1650 conf->slab_cache = sc;
1651 conf->active_name = 1-conf->active_name;
1652 conf->pool_size = newsize;
1653 return err;
1654}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
NeilBrownd1688a62011-10-11 16:49:52 +11001656static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
1658 struct stripe_head *sh;
1659
NeilBrown3f294f42005-11-08 21:39:25 -08001660 spin_lock_irq(&conf->device_lock);
1661 sh = get_free_stripe(conf);
1662 spin_unlock_irq(&conf->device_lock);
1663 if (!sh)
1664 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001665 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001666 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001667 kmem_cache_free(conf->slab_cache, sh);
1668 atomic_dec(&conf->active_stripes);
1669 return 1;
1670}
1671
NeilBrownd1688a62011-10-11 16:49:52 +11001672static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001673{
1674 while (drop_one_stripe(conf))
1675 ;
1676
NeilBrown29fc7e32006-02-03 03:03:41 -08001677 if (conf->slab_cache)
1678 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 conf->slab_cache = NULL;
1680}
1681
NeilBrown6712ecf2007-09-27 12:47:43 +02001682static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683{
NeilBrown99c0fb52009-03-31 14:39:38 +11001684 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001685 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001686 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001688 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11001689 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10001690 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
1692 for (i=0 ; i<disks; i++)
1693 if (bi == &sh->dev[i].req)
1694 break;
1695
Dan Williams45b42332007-07-09 11:56:43 -07001696 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1697 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 uptodate);
1699 if (i == disks) {
1700 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001701 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 }
NeilBrown14a75d32011-12-23 10:17:52 +11001703 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11001704 /* If replacement finished while this request was outstanding,
1705 * 'replacement' might be NULL already.
1706 * In that case it moved down to 'rdev'.
1707 * rdev is not removed until all requests are finished.
1708 */
NeilBrown14a75d32011-12-23 10:17:52 +11001709 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001710 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11001711 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
NeilBrown05616be2012-05-21 09:27:00 +10001713 if (use_new_offset(conf, sh))
1714 s = sh->sector + rdev->new_data_offset;
1715 else
1716 s = sh->sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001719 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11001720 /* Note that this cannot happen on a
1721 * replacement device. We just fail those on
1722 * any error
1723 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001724 printk_ratelimited(
1725 KERN_INFO
1726 "md/raid:%s: read error corrected"
1727 " (%lu sectors at %llu on %s)\n",
1728 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10001729 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001730 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001731 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001732 clear_bit(R5_ReadError, &sh->dev[i].flags);
1733 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1734 }
NeilBrown14a75d32011-12-23 10:17:52 +11001735 if (atomic_read(&rdev->read_errors))
1736 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11001738 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001739 int retry = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001740
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001742 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11001743 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1744 printk_ratelimited(
1745 KERN_WARNING
1746 "md/raid:%s: read error on replacement device "
1747 "(sector %llu on %s).\n",
1748 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001749 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11001750 bdn);
1751 else if (conf->mddev->degraded >= conf->max_degraded)
Christian Dietrich8bda4702011-07-27 11:00:36 +10001752 printk_ratelimited(
1753 KERN_WARNING
1754 "md/raid:%s: read error not correctable "
1755 "(sector %llu on %s).\n",
1756 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001757 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001758 bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001759 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
NeilBrown4e5314b2005-11-08 21:39:22 -08001760 /* Oh, no!!! */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001761 printk_ratelimited(
1762 KERN_WARNING
1763 "md/raid:%s: read error NOT corrected!! "
1764 "(sector %llu on %s).\n",
1765 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001766 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001767 bdn);
NeilBrownd6950432006-07-10 04:44:20 -07001768 else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001769 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001770 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001771 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001772 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001773 else
1774 retry = 1;
1775 if (retry)
1776 set_bit(R5_ReadError, &sh->dev[i].flags);
1777 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001778 clear_bit(R5_ReadError, &sh->dev[i].flags);
1779 clear_bit(R5_ReWrite, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001780 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 }
NeilBrown14a75d32011-12-23 10:17:52 +11001783 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1785 set_bit(STRIPE_HANDLE, &sh->state);
1786 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787}
1788
NeilBrownd710e132008-10-13 11:55:12 +11001789static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
NeilBrown99c0fb52009-03-31 14:39:38 +11001791 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001792 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001793 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11001794 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001796 sector_t first_bad;
1797 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11001798 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
NeilBrown977df362011-12-23 10:17:53 +11001800 for (i = 0 ; i < disks; i++) {
1801 if (bi == &sh->dev[i].req) {
1802 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 break;
NeilBrown977df362011-12-23 10:17:53 +11001804 }
1805 if (bi == &sh->dev[i].rreq) {
1806 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001807 if (rdev)
1808 replacement = 1;
1809 else
1810 /* rdev was removed and 'replacement'
1811 * replaced it. rdev is not removed
1812 * until all requests are finished.
1813 */
1814 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11001815 break;
1816 }
1817 }
Dan Williams45b42332007-07-09 11:56:43 -07001818 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1820 uptodate);
1821 if (i == disks) {
1822 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001823 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 }
1825
NeilBrown977df362011-12-23 10:17:53 +11001826 if (replacement) {
1827 if (!uptodate)
1828 md_error(conf->mddev, rdev);
1829 else if (is_badblock(rdev, sh->sector,
1830 STRIPE_SECTORS,
1831 &first_bad, &bad_sectors))
1832 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1833 } else {
1834 if (!uptodate) {
1835 set_bit(WriteErrorSeen, &rdev->flags);
1836 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11001837 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1838 set_bit(MD_RECOVERY_NEEDED,
1839 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11001840 } else if (is_badblock(rdev, sh->sector,
1841 STRIPE_SECTORS,
1842 &first_bad, &bad_sectors))
1843 set_bit(R5_MadeGood, &sh->dev[i].flags);
1844 }
1845 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
NeilBrown977df362011-12-23 10:17:53 +11001847 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1848 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001850 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851}
1852
NeilBrown784052e2009-03-31 15:19:07 +11001853static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
NeilBrown784052e2009-03-31 15:19:07 +11001855static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
1857 struct r5dev *dev = &sh->dev[i];
1858
1859 bio_init(&dev->req);
1860 dev->req.bi_io_vec = &dev->vec;
1861 dev->req.bi_vcnt++;
1862 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11001864 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
NeilBrown977df362011-12-23 10:17:53 +11001866 bio_init(&dev->rreq);
1867 dev->rreq.bi_io_vec = &dev->rvec;
1868 dev->rreq.bi_vcnt++;
1869 dev->rreq.bi_max_vecs++;
1870 dev->rreq.bi_private = sh;
1871 dev->rvec.bv_page = dev->page;
1872
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001874 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875}
1876
NeilBrownfd01b882011-10-11 16:47:53 +11001877static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
1879 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11001880 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11001881 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10001882 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
NeilBrown908f4fb2011-12-23 10:17:50 +11001884 spin_lock_irqsave(&conf->device_lock, flags);
1885 clear_bit(In_sync, &rdev->flags);
1886 mddev->degraded = calc_degraded(conf);
1887 spin_unlock_irqrestore(&conf->device_lock, flags);
1888 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1889
NeilBrownde393cd2011-07-28 11:31:48 +10001890 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001891 set_bit(Faulty, &rdev->flags);
1892 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1893 printk(KERN_ALERT
1894 "md/raid:%s: Disk failure on %s, disabling device.\n"
1895 "md/raid:%s: Operation continuing on %d devices.\n",
1896 mdname(mddev),
1897 bdevname(rdev->bdev, b),
1898 mdname(mddev),
1899 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001900}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
1902/*
1903 * Input: a 'big' sector number,
1904 * Output: index of the data and parity disk, and the sector # in them.
1905 */
NeilBrownd1688a62011-10-11 16:49:52 +11001906static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001907 int previous, int *dd_idx,
1908 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001910 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001911 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001913 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001914 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001916 int algorithm = previous ? conf->prev_algo
1917 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001918 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1919 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001920 int raid_disks = previous ? conf->previous_raid_disks
1921 : conf->raid_disks;
1922 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
1924 /* First compute the information on this sector */
1925
1926 /*
1927 * Compute the chunk number and the sector offset inside the chunk
1928 */
1929 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1930 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
1932 /*
1933 * Compute the stripe number
1934 */
NeilBrown35f2a592010-04-20 14:13:34 +10001935 stripe = chunk_number;
1936 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10001937 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 /*
1939 * Select the parity disk based on the user selected algorithm.
1940 */
NeilBrown84789552011-07-27 11:00:36 +10001941 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07001942 switch(conf->level) {
1943 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001944 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001945 break;
1946 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001947 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001949 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001950 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 (*dd_idx)++;
1952 break;
1953 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001954 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001955 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 (*dd_idx)++;
1957 break;
1958 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001959 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001960 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 break;
1962 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001963 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001964 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001966 case ALGORITHM_PARITY_0:
1967 pd_idx = 0;
1968 (*dd_idx)++;
1969 break;
1970 case ALGORITHM_PARITY_N:
1971 pd_idx = data_disks;
1972 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11001974 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001975 }
1976 break;
1977 case 6:
1978
NeilBrowne183eae2009-03-31 15:20:22 +11001979 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001980 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001981 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001982 qd_idx = pd_idx + 1;
1983 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001984 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001985 qd_idx = 0;
1986 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001987 (*dd_idx) += 2; /* D D P Q D */
1988 break;
1989 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001990 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001991 qd_idx = pd_idx + 1;
1992 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001993 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001994 qd_idx = 0;
1995 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001996 (*dd_idx) += 2; /* D D P Q D */
1997 break;
1998 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001999 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002000 qd_idx = (pd_idx + 1) % raid_disks;
2001 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002002 break;
2003 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002004 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002005 qd_idx = (pd_idx + 1) % raid_disks;
2006 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002007 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002008
2009 case ALGORITHM_PARITY_0:
2010 pd_idx = 0;
2011 qd_idx = 1;
2012 (*dd_idx) += 2;
2013 break;
2014 case ALGORITHM_PARITY_N:
2015 pd_idx = data_disks;
2016 qd_idx = data_disks + 1;
2017 break;
2018
2019 case ALGORITHM_ROTATING_ZERO_RESTART:
2020 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2021 * of blocks for computing Q is different.
2022 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002023 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002024 qd_idx = pd_idx + 1;
2025 if (pd_idx == raid_disks-1) {
2026 (*dd_idx)++; /* Q D D D P */
2027 qd_idx = 0;
2028 } else if (*dd_idx >= pd_idx)
2029 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002030 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002031 break;
2032
2033 case ALGORITHM_ROTATING_N_RESTART:
2034 /* Same a left_asymmetric, by first stripe is
2035 * D D D P Q rather than
2036 * Q D D D P
2037 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002038 stripe2 += 1;
2039 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002040 qd_idx = pd_idx + 1;
2041 if (pd_idx == raid_disks-1) {
2042 (*dd_idx)++; /* Q D D D P */
2043 qd_idx = 0;
2044 } else if (*dd_idx >= pd_idx)
2045 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002046 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002047 break;
2048
2049 case ALGORITHM_ROTATING_N_CONTINUE:
2050 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002051 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002052 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2053 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002054 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002055 break;
2056
2057 case ALGORITHM_LEFT_ASYMMETRIC_6:
2058 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002059 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002060 if (*dd_idx >= pd_idx)
2061 (*dd_idx)++;
2062 qd_idx = raid_disks - 1;
2063 break;
2064
2065 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002066 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002067 if (*dd_idx >= pd_idx)
2068 (*dd_idx)++;
2069 qd_idx = raid_disks - 1;
2070 break;
2071
2072 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002073 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002074 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2075 qd_idx = raid_disks - 1;
2076 break;
2077
2078 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002079 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002080 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2081 qd_idx = raid_disks - 1;
2082 break;
2083
2084 case ALGORITHM_PARITY_0_6:
2085 pd_idx = 0;
2086 (*dd_idx)++;
2087 qd_idx = raid_disks - 1;
2088 break;
2089
NeilBrown16a53ec2006-06-26 00:27:38 -07002090 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002091 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002092 }
2093 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 }
2095
NeilBrown911d4ee2009-03-31 14:39:38 +11002096 if (sh) {
2097 sh->pd_idx = pd_idx;
2098 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002099 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 /*
2102 * Finally, compute the new sector number
2103 */
2104 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2105 return new_sector;
2106}
2107
2108
NeilBrown784052e2009-03-31 15:19:07 +11002109static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110{
NeilBrownd1688a62011-10-11 16:49:52 +11002111 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002112 int raid_disks = sh->disks;
2113 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002115 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2116 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002117 int algorithm = previous ? conf->prev_algo
2118 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 sector_t stripe;
2120 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002121 sector_t chunk_number;
2122 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002124 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
NeilBrown16a53ec2006-06-26 00:27:38 -07002126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2128 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
NeilBrown16a53ec2006-06-26 00:27:38 -07002130 if (i == sh->pd_idx)
2131 return 0;
2132 switch(conf->level) {
2133 case 4: break;
2134 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002135 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 case ALGORITHM_LEFT_ASYMMETRIC:
2137 case ALGORITHM_RIGHT_ASYMMETRIC:
2138 if (i > sh->pd_idx)
2139 i--;
2140 break;
2141 case ALGORITHM_LEFT_SYMMETRIC:
2142 case ALGORITHM_RIGHT_SYMMETRIC:
2143 if (i < sh->pd_idx)
2144 i += raid_disks;
2145 i -= (sh->pd_idx + 1);
2146 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002147 case ALGORITHM_PARITY_0:
2148 i -= 1;
2149 break;
2150 case ALGORITHM_PARITY_N:
2151 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002153 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002154 }
2155 break;
2156 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002157 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002158 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002159 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002160 case ALGORITHM_LEFT_ASYMMETRIC:
2161 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002162 case ALGORITHM_ROTATING_ZERO_RESTART:
2163 case ALGORITHM_ROTATING_N_RESTART:
2164 if (sh->pd_idx == raid_disks-1)
2165 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002166 else if (i > sh->pd_idx)
2167 i -= 2; /* D D P Q D */
2168 break;
2169 case ALGORITHM_LEFT_SYMMETRIC:
2170 case ALGORITHM_RIGHT_SYMMETRIC:
2171 if (sh->pd_idx == raid_disks-1)
2172 i--; /* Q D D D P */
2173 else {
2174 /* D D P Q D */
2175 if (i < sh->pd_idx)
2176 i += raid_disks;
2177 i -= (sh->pd_idx + 2);
2178 }
2179 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002180 case ALGORITHM_PARITY_0:
2181 i -= 2;
2182 break;
2183 case ALGORITHM_PARITY_N:
2184 break;
2185 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002186 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002187 if (sh->pd_idx == 0)
2188 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002189 else {
2190 /* D D Q P D */
2191 if (i < sh->pd_idx)
2192 i += raid_disks;
2193 i -= (sh->pd_idx + 1);
2194 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002195 break;
2196 case ALGORITHM_LEFT_ASYMMETRIC_6:
2197 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2198 if (i > sh->pd_idx)
2199 i--;
2200 break;
2201 case ALGORITHM_LEFT_SYMMETRIC_6:
2202 case ALGORITHM_RIGHT_SYMMETRIC_6:
2203 if (i < sh->pd_idx)
2204 i += data_disks + 1;
2205 i -= (sh->pd_idx + 1);
2206 break;
2207 case ALGORITHM_PARITY_0_6:
2208 i -= 1;
2209 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002210 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002211 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002212 }
2213 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 }
2215
2216 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002217 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
NeilBrown112bf892009-03-31 14:39:38 +11002219 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002220 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002221 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2222 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002223 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2224 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 return 0;
2226 }
2227 return r_sector;
2228}
2229
2230
Dan Williams600aa102008-06-28 08:32:05 +10002231static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002232schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002233 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002234{
2235 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002236 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002237 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002238
Dan Williamse33129d2007-01-02 13:52:30 -07002239 if (rcw) {
2240 /* if we are not expanding this is a proper write request, and
2241 * there will be bios with new data to be drained into the
2242 * stripe cache
2243 */
2244 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10002245 sh->reconstruct_state = reconstruct_state_drain_run;
2246 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2247 } else
2248 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07002249
Dan Williamsac6b53b2009-07-14 13:40:19 -07002250 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002251
2252 for (i = disks; i--; ) {
2253 struct r5dev *dev = &sh->dev[i];
2254
2255 if (dev->towrite) {
2256 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002257 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002258 if (!expand)
2259 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002260 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002261 }
2262 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002263 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002264 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002265 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002266 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002267 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002268 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2269 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2270
Dan Williamsd8ee0722008-06-28 08:32:06 +10002271 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002272 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2273 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002274 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002275
2276 for (i = disks; i--; ) {
2277 struct r5dev *dev = &sh->dev[i];
2278 if (i == pd_idx)
2279 continue;
2280
Dan Williamse33129d2007-01-02 13:52:30 -07002281 if (dev->towrite &&
2282 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002283 test_bit(R5_Wantcompute, &dev->flags))) {
2284 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002285 set_bit(R5_LOCKED, &dev->flags);
2286 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002287 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002288 }
2289 }
2290 }
2291
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002292 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002293 * are in flight
2294 */
2295 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2296 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002297 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002298
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002299 if (level == 6) {
2300 int qd_idx = sh->qd_idx;
2301 struct r5dev *dev = &sh->dev[qd_idx];
2302
2303 set_bit(R5_LOCKED, &dev->flags);
2304 clear_bit(R5_UPTODATE, &dev->flags);
2305 s->locked++;
2306 }
2307
Dan Williams600aa102008-06-28 08:32:05 +10002308 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07002309 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002310 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002311}
NeilBrown16a53ec2006-06-26 00:27:38 -07002312
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313/*
2314 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002315 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 * The bi_next chain must be in order.
2317 */
2318static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2319{
2320 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002321 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002322 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
NeilBrowncbe47ec2011-07-26 11:20:35 +10002324 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 (unsigned long long)bi->bi_sector,
2326 (unsigned long long)sh->sector);
2327
2328
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07002330 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -07002332 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
2333 firstwrite = 1;
2334 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 bip = &sh->dev[dd_idx].toread;
2336 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2337 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2338 goto overlap;
2339 bip = & (*bip)->bi_next;
2340 }
2341 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2342 goto overlap;
2343
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002344 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 if (*bip)
2346 bi->bi_next = *bip;
2347 *bip = bi;
Jens Axboe960e7392008-08-15 10:41:18 +02002348 bi->bi_phys_segments++;
NeilBrown72626682005-09-09 16:23:54 -07002349
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 if (forwrite) {
2351 /* check if page is covered */
2352 sector_t sector = sh->dev[dd_idx].sector;
2353 for (bi=sh->dev[dd_idx].towrite;
2354 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2355 bi && bi->bi_sector <= sector;
2356 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2357 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2358 sector = bi->bi_sector + (bi->bi_size>>9);
2359 }
2360 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2361 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2362 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002363 spin_unlock_irq(&conf->device_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002364
2365 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2366 (unsigned long long)(*bip)->bi_sector,
2367 (unsigned long long)sh->sector, dd_idx);
2368
2369 if (conf->mddev->bitmap && firstwrite) {
2370 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2371 STRIPE_SECTORS, 0);
2372 sh->bm_seq = conf->seq_flush+1;
2373 set_bit(STRIPE_BIT_DELAY, &sh->state);
2374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 return 1;
2376
2377 overlap:
2378 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
2379 spin_unlock_irq(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 return 0;
2381}
2382
NeilBrownd1688a62011-10-11 16:49:52 +11002383static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002384
NeilBrownd1688a62011-10-11 16:49:52 +11002385static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002386 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002387{
NeilBrown784052e2009-03-31 15:19:07 +11002388 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002389 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002390 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002391 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002392 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002393
NeilBrown112bf892009-03-31 14:39:38 +11002394 raid5_compute_sector(conf,
2395 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002396 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002397 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002398 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002399}
2400
Dan Williamsa4456852007-07-09 11:56:43 -07002401static void
NeilBrownd1688a62011-10-11 16:49:52 +11002402handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002403 struct stripe_head_state *s, int disks,
2404 struct bio **return_bi)
2405{
2406 int i;
2407 for (i = disks; i--; ) {
2408 struct bio *bi;
2409 int bitmap_end = 0;
2410
2411 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002412 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002413 rcu_read_lock();
2414 rdev = rcu_dereference(conf->disks[i].rdev);
2415 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002416 atomic_inc(&rdev->nr_pending);
2417 else
2418 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002419 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002420 if (rdev) {
2421 if (!rdev_set_badblocks(
2422 rdev,
2423 sh->sector,
2424 STRIPE_SECTORS, 0))
2425 md_error(conf->mddev, rdev);
2426 rdev_dec_pending(rdev, conf->mddev);
2427 }
Dan Williamsa4456852007-07-09 11:56:43 -07002428 }
2429 spin_lock_irq(&conf->device_lock);
2430 /* fail all writes first */
2431 bi = sh->dev[i].towrite;
2432 sh->dev[i].towrite = NULL;
2433 if (bi) {
2434 s->to_write--;
2435 bitmap_end = 1;
2436 }
2437
2438 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2439 wake_up(&conf->wait_for_overlap);
2440
2441 while (bi && bi->bi_sector <
2442 sh->dev[i].sector + STRIPE_SECTORS) {
2443 struct bio *nextbi = 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 md_write_end(conf->mddev);
2447 bi->bi_next = *return_bi;
2448 *return_bi = bi;
2449 }
2450 bi = nextbi;
2451 }
2452 /* and fail all 'written' */
2453 bi = sh->dev[i].written;
2454 sh->dev[i].written = NULL;
2455 if (bi) bitmap_end = 1;
2456 while (bi && bi->bi_sector <
2457 sh->dev[i].sector + STRIPE_SECTORS) {
2458 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2459 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002460 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002461 md_write_end(conf->mddev);
2462 bi->bi_next = *return_bi;
2463 *return_bi = bi;
2464 }
2465 bi = bi2;
2466 }
2467
Dan Williamsb5e98d62007-01-02 13:52:31 -07002468 /* fail any reads if this device is non-operational and
2469 * the data has not reached the cache yet.
2470 */
2471 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2472 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2473 test_bit(R5_ReadError, &sh->dev[i].flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002474 bi = sh->dev[i].toread;
2475 sh->dev[i].toread = NULL;
2476 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2477 wake_up(&conf->wait_for_overlap);
2478 if (bi) s->to_read--;
2479 while (bi && bi->bi_sector <
2480 sh->dev[i].sector + STRIPE_SECTORS) {
2481 struct bio *nextbi =
2482 r5_next_bio(bi, sh->dev[i].sector);
2483 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002484 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002485 bi->bi_next = *return_bi;
2486 *return_bi = bi;
2487 }
2488 bi = nextbi;
2489 }
2490 }
2491 spin_unlock_irq(&conf->device_lock);
2492 if (bitmap_end)
2493 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2494 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002495 /* If we were in the middle of a write the parity block might
2496 * still be locked - so just clear all R5_LOCKED flags
2497 */
2498 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002499 }
2500
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002501 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2502 if (atomic_dec_and_test(&conf->pending_full_writes))
2503 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002504}
2505
NeilBrown7f0da592011-07-28 11:39:22 +10002506static void
NeilBrownd1688a62011-10-11 16:49:52 +11002507handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002508 struct stripe_head_state *s)
2509{
2510 int abort = 0;
2511 int i;
2512
NeilBrown7f0da592011-07-28 11:39:22 +10002513 clear_bit(STRIPE_SYNCING, &sh->state);
2514 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002515 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002516 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10002517 * Don't even need to abort as that is handled elsewhere
2518 * if needed, and not always wanted e.g. if there is a known
2519 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11002520 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002521 * non-sync devices, or abort the recovery
2522 */
NeilBrown18b98372012-04-01 23:48:38 +10002523 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2524 /* During recovery devices cannot be removed, so
2525 * locking and refcounting of rdevs is not needed
2526 */
2527 for (i = 0; i < conf->raid_disks; i++) {
2528 struct md_rdev *rdev = conf->disks[i].rdev;
2529 if (rdev
2530 && !test_bit(Faulty, &rdev->flags)
2531 && !test_bit(In_sync, &rdev->flags)
2532 && !rdev_set_badblocks(rdev, sh->sector,
2533 STRIPE_SECTORS, 0))
2534 abort = 1;
2535 rdev = conf->disks[i].replacement;
2536 if (rdev
2537 && !test_bit(Faulty, &rdev->flags)
2538 && !test_bit(In_sync, &rdev->flags)
2539 && !rdev_set_badblocks(rdev, sh->sector,
2540 STRIPE_SECTORS, 0))
2541 abort = 1;
2542 }
2543 if (abort)
2544 conf->recovery_disabled =
2545 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10002546 }
NeilBrown18b98372012-04-01 23:48:38 +10002547 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10002548}
2549
NeilBrown9a3e1102011-12-23 10:17:53 +11002550static int want_replace(struct stripe_head *sh, int disk_idx)
2551{
2552 struct md_rdev *rdev;
2553 int rv = 0;
2554 /* Doing recovery so rcu locking not required */
2555 rdev = sh->raid_conf->disks[disk_idx].replacement;
2556 if (rdev
2557 && !test_bit(Faulty, &rdev->flags)
2558 && !test_bit(In_sync, &rdev->flags)
2559 && (rdev->recovery_offset <= sh->sector
2560 || rdev->mddev->recovery_cp <= sh->sector))
2561 rv = 1;
2562
2563 return rv;
2564}
2565
NeilBrown93b3dbc2011-07-27 11:00:36 +10002566/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002567 * to be read or computed to satisfy a request.
2568 *
2569 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002570 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002571 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002572static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2573 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002574{
2575 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002576 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2577 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002578
Dan Williamsf38e1212007-01-02 13:52:30 -07002579 /* is the data in this block needed, and can we get it? */
2580 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002581 !test_bit(R5_UPTODATE, &dev->flags) &&
2582 (dev->toread ||
2583 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2584 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002585 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002586 (s->failed >= 1 && fdev[0]->toread) ||
2587 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002588 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2589 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2590 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002591 /* we would like to get this block, possibly by computing it,
2592 * otherwise read it if the backing disk is insync
2593 */
2594 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2595 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2596 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002597 (s->failed && (disk_idx == s->failed_num[0] ||
2598 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002599 /* have disk failed, and we're requested to fetch it;
2600 * do compute it
2601 */
2602 pr_debug("Computing stripe %llu block %d\n",
2603 (unsigned long long)sh->sector, disk_idx);
2604 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2605 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2606 set_bit(R5_Wantcompute, &dev->flags);
2607 sh->ops.target = disk_idx;
2608 sh->ops.target2 = -1; /* no 2nd target */
2609 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002610 /* Careful: from this point on 'uptodate' is in the eye
2611 * of raid_run_ops which services 'compute' operations
2612 * before writes. R5_Wantcompute flags a block that will
2613 * be R5_UPTODATE by the time it is needed for a
2614 * subsequent operation.
2615 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002616 s->uptodate++;
2617 return 1;
2618 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2619 /* Computing 2-failure is *very* expensive; only
2620 * do it if failed >= 2
2621 */
2622 int other;
2623 for (other = disks; other--; ) {
2624 if (other == disk_idx)
2625 continue;
2626 if (!test_bit(R5_UPTODATE,
2627 &sh->dev[other].flags))
2628 break;
2629 }
2630 BUG_ON(other < 0);
2631 pr_debug("Computing stripe %llu blocks %d,%d\n",
2632 (unsigned long long)sh->sector,
2633 disk_idx, other);
2634 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2635 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2636 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2637 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2638 sh->ops.target = disk_idx;
2639 sh->ops.target2 = other;
2640 s->uptodate += 2;
2641 s->req_compute = 1;
2642 return 1;
2643 } else if (test_bit(R5_Insync, &dev->flags)) {
2644 set_bit(R5_LOCKED, &dev->flags);
2645 set_bit(R5_Wantread, &dev->flags);
2646 s->locked++;
2647 pr_debug("Reading block %d (sync=%d)\n",
2648 disk_idx, s->syncing);
2649 }
2650 }
2651
2652 return 0;
2653}
2654
2655/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002656 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002657 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002658static void handle_stripe_fill(struct stripe_head *sh,
2659 struct stripe_head_state *s,
2660 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002661{
2662 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002663
2664 /* look for blocks to read/compute, skip this if a compute
2665 * is already in flight, or if the stripe contents are in the
2666 * midst of changing due to a write
2667 */
2668 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2669 !sh->reconstruct_state)
2670 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002671 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002672 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002673 set_bit(STRIPE_HANDLE, &sh->state);
2674}
2675
2676
Dan Williams1fe797e2008-06-28 09:16:30 +10002677/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002678 * any written block on an uptodate or failed drive can be returned.
2679 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2680 * never LOCKED, so we don't need to test 'failed' directly.
2681 */
NeilBrownd1688a62011-10-11 16:49:52 +11002682static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002683 struct stripe_head *sh, int disks, struct bio **return_bi)
2684{
2685 int i;
2686 struct r5dev *dev;
2687
2688 for (i = disks; i--; )
2689 if (sh->dev[i].written) {
2690 dev = &sh->dev[i];
2691 if (!test_bit(R5_LOCKED, &dev->flags) &&
2692 test_bit(R5_UPTODATE, &dev->flags)) {
2693 /* We can return any write requests */
2694 struct bio *wbi, *wbi2;
2695 int bitmap_end = 0;
Dan Williams45b42332007-07-09 11:56:43 -07002696 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002697 spin_lock_irq(&conf->device_lock);
2698 wbi = dev->written;
2699 dev->written = NULL;
2700 while (wbi && wbi->bi_sector <
2701 dev->sector + STRIPE_SECTORS) {
2702 wbi2 = r5_next_bio(wbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +02002703 if (!raid5_dec_bi_phys_segments(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002704 md_write_end(conf->mddev);
2705 wbi->bi_next = *return_bi;
2706 *return_bi = wbi;
2707 }
2708 wbi = wbi2;
2709 }
2710 if (dev->towrite == NULL)
2711 bitmap_end = 1;
2712 spin_unlock_irq(&conf->device_lock);
2713 if (bitmap_end)
2714 bitmap_endwrite(conf->mddev->bitmap,
2715 sh->sector,
2716 STRIPE_SECTORS,
2717 !test_bit(STRIPE_DEGRADED, &sh->state),
2718 0);
2719 }
2720 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002721
2722 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2723 if (atomic_dec_and_test(&conf->pending_full_writes))
2724 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002725}
2726
NeilBrownd1688a62011-10-11 16:49:52 +11002727static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002728 struct stripe_head *sh,
2729 struct stripe_head_state *s,
2730 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002731{
2732 int rmw = 0, rcw = 0, i;
NeilBrownc8ac1802011-07-27 11:00:36 +10002733 if (conf->max_degraded == 2) {
2734 /* RAID6 requires 'rcw' in current implementation
2735 * Calculate the real rcw later - for now fake it
2736 * look like rcw is cheaper
2737 */
2738 rcw = 1; rmw = 2;
2739 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002740 /* would I have to read this buffer for read_modify_write */
2741 struct r5dev *dev = &sh->dev[i];
2742 if ((dev->towrite || i == sh->pd_idx) &&
2743 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002744 !(test_bit(R5_UPTODATE, &dev->flags) ||
2745 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002746 if (test_bit(R5_Insync, &dev->flags))
2747 rmw++;
2748 else
2749 rmw += 2*disks; /* cannot read it */
2750 }
2751 /* Would I have to read this buffer for reconstruct_write */
2752 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2753 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002754 !(test_bit(R5_UPTODATE, &dev->flags) ||
2755 test_bit(R5_Wantcompute, &dev->flags))) {
2756 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002757 else
2758 rcw += 2*disks;
2759 }
2760 }
Dan Williams45b42332007-07-09 11:56:43 -07002761 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002762 (unsigned long long)sh->sector, rmw, rcw);
2763 set_bit(STRIPE_HANDLE, &sh->state);
2764 if (rmw < rcw && rmw > 0)
2765 /* prefer read-modify-write, but need to get some data */
2766 for (i = disks; i--; ) {
2767 struct r5dev *dev = &sh->dev[i];
2768 if ((dev->towrite || i == sh->pd_idx) &&
2769 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002770 !(test_bit(R5_UPTODATE, &dev->flags) ||
2771 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002772 test_bit(R5_Insync, &dev->flags)) {
2773 if (
2774 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002775 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002776 "%d for r-m-w\n", i);
2777 set_bit(R5_LOCKED, &dev->flags);
2778 set_bit(R5_Wantread, &dev->flags);
2779 s->locked++;
2780 } else {
2781 set_bit(STRIPE_DELAYED, &sh->state);
2782 set_bit(STRIPE_HANDLE, &sh->state);
2783 }
2784 }
2785 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002786 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002787 /* want reconstruct write, but need to get some data */
NeilBrownc8ac1802011-07-27 11:00:36 +10002788 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002789 for (i = disks; i--; ) {
2790 struct r5dev *dev = &sh->dev[i];
2791 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002792 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002793 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002794 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002795 test_bit(R5_Wantcompute, &dev->flags))) {
2796 rcw++;
2797 if (!test_bit(R5_Insync, &dev->flags))
2798 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002799 if (
2800 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002801 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002802 "%d for Reconstruct\n", i);
2803 set_bit(R5_LOCKED, &dev->flags);
2804 set_bit(R5_Wantread, &dev->flags);
2805 s->locked++;
2806 } else {
2807 set_bit(STRIPE_DELAYED, &sh->state);
2808 set_bit(STRIPE_HANDLE, &sh->state);
2809 }
2810 }
2811 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002812 }
Dan Williamsa4456852007-07-09 11:56:43 -07002813 /* now if nothing is locked, and if we have enough data,
2814 * we can start a write request
2815 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002816 /* since handle_stripe can be called at any time we need to handle the
2817 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002818 * subsequent call wants to start a write request. raid_run_ops only
2819 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002820 * simultaneously. If this is not the case then new writes need to be
2821 * held off until the compute completes.
2822 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002823 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2824 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2825 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002826 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002827}
2828
NeilBrownd1688a62011-10-11 16:49:52 +11002829static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002830 struct stripe_head_state *s, int disks)
2831{
Dan Williamsecc65c92008-06-28 08:31:57 +10002832 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002833
Dan Williamsbd2ab672008-04-10 21:29:27 -07002834 set_bit(STRIPE_HANDLE, &sh->state);
2835
Dan Williamsecc65c92008-06-28 08:31:57 +10002836 switch (sh->check_state) {
2837 case check_state_idle:
2838 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002839 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002840 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002841 sh->check_state = check_state_run;
2842 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002843 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002844 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002845 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002846 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002847 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002848 /* fall through */
2849 case check_state_compute_result:
2850 sh->check_state = check_state_idle;
2851 if (!dev)
2852 dev = &sh->dev[sh->pd_idx];
2853
2854 /* check that a write has not made the stripe insync */
2855 if (test_bit(STRIPE_INSYNC, &sh->state))
2856 break;
2857
2858 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002859 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2860 BUG_ON(s->uptodate != disks);
2861
2862 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002863 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002864 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002865
Dan Williamsa4456852007-07-09 11:56:43 -07002866 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002867 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002868 break;
2869 case check_state_run:
2870 break; /* we will be called again upon completion */
2871 case check_state_check_result:
2872 sh->check_state = check_state_idle;
2873
2874 /* if a failure occurred during the check operation, leave
2875 * STRIPE_INSYNC not set and let the stripe be handled again
2876 */
2877 if (s->failed)
2878 break;
2879
2880 /* handle a successful check operation, if parity is correct
2881 * we are done. Otherwise update the mismatch count and repair
2882 * parity if !MD_RECOVERY_CHECK
2883 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002884 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002885 /* parity is correct (on disc,
2886 * not in buffer any more)
2887 */
2888 set_bit(STRIPE_INSYNC, &sh->state);
2889 else {
2890 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2891 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2892 /* don't try to repair!! */
2893 set_bit(STRIPE_INSYNC, &sh->state);
2894 else {
2895 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002896 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002897 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2898 set_bit(R5_Wantcompute,
2899 &sh->dev[sh->pd_idx].flags);
2900 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002901 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10002902 s->uptodate++;
2903 }
2904 }
2905 break;
2906 case check_state_compute_run:
2907 break;
2908 default:
2909 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2910 __func__, sh->check_state,
2911 (unsigned long long) sh->sector);
2912 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002913 }
2914}
2915
2916
NeilBrownd1688a62011-10-11 16:49:52 +11002917static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07002918 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10002919 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002920{
Dan Williamsa4456852007-07-09 11:56:43 -07002921 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11002922 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07002923 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07002924
2925 set_bit(STRIPE_HANDLE, &sh->state);
2926
2927 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002928
Dan Williamsa4456852007-07-09 11:56:43 -07002929 /* Want to check and possibly repair P and Q.
2930 * However there could be one 'failed' device, in which
2931 * case we can only check one of them, possibly using the
2932 * other to generate missing data
2933 */
2934
Dan Williamsd82dfee2009-07-14 13:40:57 -07002935 switch (sh->check_state) {
2936 case check_state_idle:
2937 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10002938 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002939 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07002940 * makes sense to check P (If anything else were failed,
2941 * we would have used P to recreate it).
2942 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002943 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07002944 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002945 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002946 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07002947 * anything, so it makes sense to check it
2948 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002949 if (sh->check_state == check_state_run)
2950 sh->check_state = check_state_run_pq;
2951 else
2952 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07002953 }
Dan Williams36d1c642009-07-14 11:48:22 -07002954
Dan Williamsd82dfee2009-07-14 13:40:57 -07002955 /* discard potentially stale zero_sum_result */
2956 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07002957
Dan Williamsd82dfee2009-07-14 13:40:57 -07002958 if (sh->check_state == check_state_run) {
2959 /* async_xor_zero_sum destroys the contents of P */
2960 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
2961 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07002962 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002963 if (sh->check_state >= check_state_run &&
2964 sh->check_state <= check_state_run_pq) {
2965 /* async_syndrome_zero_sum preserves P and Q, so
2966 * no need to mark them !uptodate here
2967 */
2968 set_bit(STRIPE_OP_CHECK, &s->ops_request);
2969 break;
2970 }
Dan Williams36d1c642009-07-14 11:48:22 -07002971
Dan Williamsd82dfee2009-07-14 13:40:57 -07002972 /* we have 2-disk failure */
2973 BUG_ON(s->failed != 2);
2974 /* fall through */
2975 case check_state_compute_result:
2976 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07002977
Dan Williamsd82dfee2009-07-14 13:40:57 -07002978 /* check that a write has not made the stripe insync */
2979 if (test_bit(STRIPE_INSYNC, &sh->state))
2980 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002981
2982 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07002983 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07002984 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002985 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07002986 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10002987 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07002988 s->locked++;
2989 set_bit(R5_LOCKED, &dev->flags);
2990 set_bit(R5_Wantwrite, &dev->flags);
2991 }
2992 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10002993 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07002994 s->locked++;
2995 set_bit(R5_LOCKED, &dev->flags);
2996 set_bit(R5_Wantwrite, &dev->flags);
2997 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002998 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002999 dev = &sh->dev[pd_idx];
3000 s->locked++;
3001 set_bit(R5_LOCKED, &dev->flags);
3002 set_bit(R5_Wantwrite, &dev->flags);
3003 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003004 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003005 dev = &sh->dev[qd_idx];
3006 s->locked++;
3007 set_bit(R5_LOCKED, &dev->flags);
3008 set_bit(R5_Wantwrite, &dev->flags);
3009 }
3010 clear_bit(STRIPE_DEGRADED, &sh->state);
3011
3012 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003013 break;
3014 case check_state_run:
3015 case check_state_run_q:
3016 case check_state_run_pq:
3017 break; /* we will be called again upon completion */
3018 case check_state_check_result:
3019 sh->check_state = check_state_idle;
3020
3021 /* handle a successful check operation, if parity is correct
3022 * we are done. Otherwise update the mismatch count and repair
3023 * parity if !MD_RECOVERY_CHECK
3024 */
3025 if (sh->ops.zero_sum_result == 0) {
3026 /* both parities are correct */
3027 if (!s->failed)
3028 set_bit(STRIPE_INSYNC, &sh->state);
3029 else {
3030 /* in contrast to the raid5 case we can validate
3031 * parity, but still have a failure to write
3032 * back
3033 */
3034 sh->check_state = check_state_compute_result;
3035 /* Returning at this point means that we may go
3036 * off and bring p and/or q uptodate again so
3037 * we make sure to check zero_sum_result again
3038 * to verify if p or q need writeback
3039 */
3040 }
3041 } else {
3042 conf->mddev->resync_mismatches += STRIPE_SECTORS;
3043 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3044 /* don't try to repair!! */
3045 set_bit(STRIPE_INSYNC, &sh->state);
3046 else {
3047 int *target = &sh->ops.target;
3048
3049 sh->ops.target = -1;
3050 sh->ops.target2 = -1;
3051 sh->check_state = check_state_compute_run;
3052 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3053 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3054 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3055 set_bit(R5_Wantcompute,
3056 &sh->dev[pd_idx].flags);
3057 *target = pd_idx;
3058 target = &sh->ops.target2;
3059 s->uptodate++;
3060 }
3061 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3062 set_bit(R5_Wantcompute,
3063 &sh->dev[qd_idx].flags);
3064 *target = qd_idx;
3065 s->uptodate++;
3066 }
3067 }
3068 }
3069 break;
3070 case check_state_compute_run:
3071 break;
3072 default:
3073 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3074 __func__, sh->check_state,
3075 (unsigned long long) sh->sector);
3076 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003077 }
3078}
3079
NeilBrownd1688a62011-10-11 16:49:52 +11003080static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003081{
3082 int i;
3083
3084 /* We have read all the blocks in this stripe and now we need to
3085 * copy some of them into a target stripe for expand.
3086 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003087 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003088 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3089 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003090 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003091 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003092 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003093 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003094
NeilBrown784052e2009-03-31 15:19:07 +11003095 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003096 sector_t s = raid5_compute_sector(conf, bn, 0,
3097 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003098 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003099 if (sh2 == NULL)
3100 /* so far only the early blocks of this stripe
3101 * have been requested. When later blocks
3102 * get requested, we will try again
3103 */
3104 continue;
3105 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3106 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3107 /* must have already done this block */
3108 release_stripe(sh2);
3109 continue;
3110 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003111
3112 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003113 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003114 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003115 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003116 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003117
Dan Williamsa4456852007-07-09 11:56:43 -07003118 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3119 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3120 for (j = 0; j < conf->raid_disks; j++)
3121 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003122 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003123 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3124 break;
3125 if (j == conf->raid_disks) {
3126 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3127 set_bit(STRIPE_HANDLE, &sh2->state);
3128 }
3129 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003130
Dan Williamsa4456852007-07-09 11:56:43 -07003131 }
NeilBrowna2e08552007-09-11 15:23:36 -07003132 /* done submitting copies, wait for them to complete */
3133 if (tx) {
3134 async_tx_ack(tx);
3135 dma_wait_for_async_tx(tx);
3136 }
Dan Williamsa4456852007-07-09 11:56:43 -07003137}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138
3139/*
3140 * handle_stripe - do things to a stripe.
3141 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003142 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3143 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003145 * return some read requests which now have data
3146 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 * schedule a read on some buffers
3148 * schedule a write of some buffers
3149 * return confirmation of parity correctness
3150 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 */
Dan Williamsa4456852007-07-09 11:56:43 -07003152
NeilBrownacfe7262011-07-27 11:00:36 +10003153static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003154{
NeilBrownd1688a62011-10-11 16:49:52 +11003155 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003156 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003157 struct r5dev *dev;
3158 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003159 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003160
NeilBrownacfe7262011-07-27 11:00:36 +10003161 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003162
NeilBrownacfe7262011-07-27 11:00:36 +10003163 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3164 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3165 s->failed_num[0] = -1;
3166 s->failed_num[1] = -1;
3167
3168 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003169 rcu_read_lock();
NeilBrownc4c16632011-07-26 11:34:20 +10003170 spin_lock_irq(&conf->device_lock);
NeilBrown16a53ec2006-06-26 00:27:38 -07003171 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003172 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003173 sector_t first_bad;
3174 int bad_sectors;
3175 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003176
NeilBrown16a53ec2006-06-26 00:27:38 -07003177 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003178
Dan Williams45b42332007-07-09 11:56:43 -07003179 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003180 i, dev->flags,
3181 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003182 /* maybe we can reply to a read
3183 *
3184 * new wantfill requests are only permitted while
3185 * ops_complete_biofill is guaranteed to be inactive
3186 */
3187 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3188 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3189 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003190
3191 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003192 if (test_bit(R5_LOCKED, &dev->flags))
3193 s->locked++;
3194 if (test_bit(R5_UPTODATE, &dev->flags))
3195 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003196 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003197 s->compute++;
3198 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003199 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003200
NeilBrownacfe7262011-07-27 11:00:36 +10003201 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003202 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003203 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003204 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003205 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003206 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003207 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003208 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003209 }
Dan Williamsa4456852007-07-09 11:56:43 -07003210 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003211 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003212 /* Prefer to use the replacement for reads, but only
3213 * if it is recovered enough and has no bad blocks.
3214 */
3215 rdev = rcu_dereference(conf->disks[i].replacement);
3216 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3217 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3218 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3219 &first_bad, &bad_sectors))
3220 set_bit(R5_ReadRepl, &dev->flags);
3221 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003222 if (rdev)
3223 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003224 rdev = rcu_dereference(conf->disks[i].rdev);
3225 clear_bit(R5_ReadRepl, &dev->flags);
3226 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003227 if (rdev && test_bit(Faulty, &rdev->flags))
3228 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003229 if (rdev) {
3230 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3231 &first_bad, &bad_sectors);
3232 if (s->blocked_rdev == NULL
3233 && (test_bit(Blocked, &rdev->flags)
3234 || is_bad < 0)) {
3235 if (is_bad < 0)
3236 set_bit(BlockedBadBlocks,
3237 &rdev->flags);
3238 s->blocked_rdev = rdev;
3239 atomic_inc(&rdev->nr_pending);
3240 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003241 }
NeilBrown415e72d2010-06-17 17:25:21 +10003242 clear_bit(R5_Insync, &dev->flags);
3243 if (!rdev)
3244 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003245 else if (is_bad) {
3246 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10003247 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3248 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003249 /* treat as in-sync, but with a read error
3250 * which we can now try to correct
3251 */
3252 set_bit(R5_Insync, &dev->flags);
3253 set_bit(R5_ReadError, &dev->flags);
3254 }
3255 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003256 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003257 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003258 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003259 set_bit(R5_Insync, &dev->flags);
3260 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3261 test_bit(R5_Expanded, &dev->flags))
3262 /* If we've reshaped into here, we assume it is Insync.
3263 * We will shortly update recovery_offset to make
3264 * it official.
3265 */
3266 set_bit(R5_Insync, &dev->flags);
3267
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003268 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003269 /* This flag does not apply to '.replacement'
3270 * only to .rdev, so make sure to check that*/
3271 struct md_rdev *rdev2 = rcu_dereference(
3272 conf->disks[i].rdev);
3273 if (rdev2 == rdev)
3274 clear_bit(R5_Insync, &dev->flags);
3275 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003276 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003277 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003278 } else
3279 clear_bit(R5_WriteError, &dev->flags);
3280 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003281 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003282 /* This flag does not apply to '.replacement'
3283 * only to .rdev, so make sure to check that*/
3284 struct md_rdev *rdev2 = rcu_dereference(
3285 conf->disks[i].rdev);
3286 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003287 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003288 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003289 } else
3290 clear_bit(R5_MadeGood, &dev->flags);
3291 }
NeilBrown977df362011-12-23 10:17:53 +11003292 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3293 struct md_rdev *rdev2 = rcu_dereference(
3294 conf->disks[i].replacement);
3295 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3296 s->handle_bad_blocks = 1;
3297 atomic_inc(&rdev2->nr_pending);
3298 } else
3299 clear_bit(R5_MadeGoodRepl, &dev->flags);
3300 }
NeilBrown415e72d2010-06-17 17:25:21 +10003301 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003302 /* The ReadError flag will just be confusing now */
3303 clear_bit(R5_ReadError, &dev->flags);
3304 clear_bit(R5_ReWrite, &dev->flags);
3305 }
NeilBrown415e72d2010-06-17 17:25:21 +10003306 if (test_bit(R5_ReadError, &dev->flags))
3307 clear_bit(R5_Insync, &dev->flags);
3308 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003309 if (s->failed < 2)
3310 s->failed_num[s->failed] = i;
3311 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003312 if (rdev && !test_bit(Faulty, &rdev->flags))
3313 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003314 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003315 }
NeilBrownc4c16632011-07-26 11:34:20 +10003316 spin_unlock_irq(&conf->device_lock);
NeilBrown9a3e1102011-12-23 10:17:53 +11003317 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3318 /* If there is a failed device being replaced,
3319 * we must be recovering.
3320 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10003321 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11003322 * else we can only be replacing
3323 * sync and recovery both need to read all devices, and so
3324 * use the same flag.
3325 */
3326 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10003327 sh->sector >= conf->mddev->recovery_cp ||
3328 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11003329 s->syncing = 1;
3330 else
3331 s->replacing = 1;
3332 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003333 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003334}
NeilBrownf4168852007-02-28 20:11:53 -08003335
NeilBrowncc940152011-07-26 11:35:35 +10003336static void handle_stripe(struct stripe_head *sh)
3337{
3338 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003339 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003340 int i;
NeilBrown84789552011-07-27 11:00:36 +10003341 int prexor;
3342 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003343 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003344
3345 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003346 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003347 /* already being handled, ensure it gets handled
3348 * again when current action finishes */
3349 set_bit(STRIPE_HANDLE, &sh->state);
3350 return;
3351 }
3352
3353 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3354 set_bit(STRIPE_SYNCING, &sh->state);
3355 clear_bit(STRIPE_INSYNC, &sh->state);
3356 }
3357 clear_bit(STRIPE_DELAYED, &sh->state);
3358
3359 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3360 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3361 (unsigned long long)sh->sector, sh->state,
3362 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3363 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003364
NeilBrownacfe7262011-07-27 11:00:36 +10003365 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003366
NeilBrownbc2607f2011-07-28 11:39:22 +10003367 if (s.handle_bad_blocks) {
3368 set_bit(STRIPE_HANDLE, &sh->state);
3369 goto finish;
3370 }
3371
NeilBrown474af965fe2011-07-27 11:00:36 +10003372 if (unlikely(s.blocked_rdev)) {
3373 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003374 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003375 set_bit(STRIPE_HANDLE, &sh->state);
3376 goto finish;
3377 }
3378 /* There is nothing for the blocked_rdev to block */
3379 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3380 s.blocked_rdev = NULL;
3381 }
3382
3383 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3384 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3385 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3386 }
3387
3388 pr_debug("locked=%d uptodate=%d to_read=%d"
3389 " to_write=%d failed=%d failed_num=%d,%d\n",
3390 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3391 s.failed_num[0], s.failed_num[1]);
3392 /* check if the array has lost more than max_degraded devices and,
3393 * if so, some requests might need to be failed.
3394 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003395 if (s.failed > conf->max_degraded) {
3396 sh->check_state = 0;
3397 sh->reconstruct_state = 0;
3398 if (s.to_read+s.to_write+s.written)
3399 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003400 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003401 handle_failed_sync(conf, sh, &s);
3402 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003403
3404 /*
3405 * might be able to return some write requests if the parity blocks
3406 * are safe, or on a failed drive
3407 */
3408 pdev = &sh->dev[sh->pd_idx];
3409 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3410 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3411 qdev = &sh->dev[sh->qd_idx];
3412 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3413 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3414 || conf->level < 6;
3415
3416 if (s.written &&
3417 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3418 && !test_bit(R5_LOCKED, &pdev->flags)
3419 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3420 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3421 && !test_bit(R5_LOCKED, &qdev->flags)
3422 && test_bit(R5_UPTODATE, &qdev->flags)))))
3423 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3424
3425 /* Now we might consider reading some blocks, either to check/generate
3426 * parity, or to satisfy requests
3427 * or to load a block that is being partially written.
3428 */
3429 if (s.to_read || s.non_overwrite
3430 || (conf->level == 6 && s.to_write && s.failed)
NeilBrown9a3e1102011-12-23 10:17:53 +11003431 || (s.syncing && (s.uptodate + s.compute < disks))
3432 || s.replacing
3433 || s.expanding)
NeilBrown474af965fe2011-07-27 11:00:36 +10003434 handle_stripe_fill(sh, &s, disks);
3435
NeilBrown84789552011-07-27 11:00:36 +10003436 /* Now we check to see if any write operations have recently
3437 * completed
3438 */
3439 prexor = 0;
3440 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3441 prexor = 1;
3442 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3443 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3444 sh->reconstruct_state = reconstruct_state_idle;
3445
3446 /* All the 'written' buffers and the parity block are ready to
3447 * be written back to disk
3448 */
3449 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3450 BUG_ON(sh->qd_idx >= 0 &&
3451 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags));
3452 for (i = disks; i--; ) {
3453 struct r5dev *dev = &sh->dev[i];
3454 if (test_bit(R5_LOCKED, &dev->flags) &&
3455 (i == sh->pd_idx || i == sh->qd_idx ||
3456 dev->written)) {
3457 pr_debug("Writing block %d\n", i);
3458 set_bit(R5_Wantwrite, &dev->flags);
3459 if (prexor)
3460 continue;
3461 if (!test_bit(R5_Insync, &dev->flags) ||
3462 ((i == sh->pd_idx || i == sh->qd_idx) &&
3463 s.failed == 0))
3464 set_bit(STRIPE_INSYNC, &sh->state);
3465 }
3466 }
3467 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3468 s.dec_preread_active = 1;
3469 }
3470
3471 /* Now to consider new write requests and what else, if anything
3472 * should be read. We do not handle new writes when:
3473 * 1/ A 'write' operation (copy+xor) is already in flight.
3474 * 2/ A 'check' operation is in flight, as it may clobber the parity
3475 * block.
3476 */
3477 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3478 handle_stripe_dirtying(conf, sh, &s, disks);
3479
3480 /* maybe we need to check and possibly fix the parity for this stripe
3481 * Any reads will already have been scheduled, so we just see if enough
3482 * data is available. The parity check is held off while parity
3483 * dependent operations are in flight.
3484 */
3485 if (sh->check_state ||
3486 (s.syncing && s.locked == 0 &&
3487 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3488 !test_bit(STRIPE_INSYNC, &sh->state))) {
3489 if (conf->level == 6)
3490 handle_parity_checks6(conf, sh, &s, disks);
3491 else
3492 handle_parity_checks5(conf, sh, &s, disks);
3493 }
NeilBrownc5a31002011-07-27 11:00:36 +10003494
NeilBrown9a3e1102011-12-23 10:17:53 +11003495 if (s.replacing && s.locked == 0
3496 && !test_bit(STRIPE_INSYNC, &sh->state)) {
3497 /* Write out to replacement devices where possible */
3498 for (i = 0; i < conf->raid_disks; i++)
3499 if (test_bit(R5_UPTODATE, &sh->dev[i].flags) &&
3500 test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3501 set_bit(R5_WantReplace, &sh->dev[i].flags);
3502 set_bit(R5_LOCKED, &sh->dev[i].flags);
3503 s.locked++;
3504 }
3505 set_bit(STRIPE_INSYNC, &sh->state);
3506 }
3507 if ((s.syncing || s.replacing) && s.locked == 0 &&
3508 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003509 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3510 clear_bit(STRIPE_SYNCING, &sh->state);
3511 }
3512
3513 /* If the failed drives are just a ReadError, then we might need
3514 * to progress the repair/check process
3515 */
3516 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3517 for (i = 0; i < s.failed; i++) {
3518 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3519 if (test_bit(R5_ReadError, &dev->flags)
3520 && !test_bit(R5_LOCKED, &dev->flags)
3521 && test_bit(R5_UPTODATE, &dev->flags)
3522 ) {
3523 if (!test_bit(R5_ReWrite, &dev->flags)) {
3524 set_bit(R5_Wantwrite, &dev->flags);
3525 set_bit(R5_ReWrite, &dev->flags);
3526 set_bit(R5_LOCKED, &dev->flags);
3527 s.locked++;
3528 } else {
3529 /* let's read it back */
3530 set_bit(R5_Wantread, &dev->flags);
3531 set_bit(R5_LOCKED, &dev->flags);
3532 s.locked++;
3533 }
3534 }
3535 }
3536
3537
NeilBrown3687c062011-07-27 11:00:36 +10003538 /* Finish reconstruct operations initiated by the expansion process */
3539 if (sh->reconstruct_state == reconstruct_state_result) {
3540 struct stripe_head *sh_src
3541 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3542 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3543 /* sh cannot be written until sh_src has been read.
3544 * so arrange for sh to be delayed a little
3545 */
3546 set_bit(STRIPE_DELAYED, &sh->state);
3547 set_bit(STRIPE_HANDLE, &sh->state);
3548 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3549 &sh_src->state))
3550 atomic_inc(&conf->preread_active_stripes);
3551 release_stripe(sh_src);
3552 goto finish;
3553 }
3554 if (sh_src)
3555 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003556
NeilBrown3687c062011-07-27 11:00:36 +10003557 sh->reconstruct_state = reconstruct_state_idle;
3558 clear_bit(STRIPE_EXPANDING, &sh->state);
3559 for (i = conf->raid_disks; i--; ) {
3560 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3561 set_bit(R5_LOCKED, &sh->dev[i].flags);
3562 s.locked++;
3563 }
3564 }
3565
3566 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3567 !sh->reconstruct_state) {
3568 /* Need to write out all blocks after computing parity */
3569 sh->disks = conf->raid_disks;
3570 stripe_set_idx(sh->sector, conf, 0, sh);
3571 schedule_reconstruction(sh, &s, 1, 1);
3572 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3573 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3574 atomic_dec(&conf->reshape_stripes);
3575 wake_up(&conf->wait_for_overlap);
3576 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3577 }
3578
3579 if (s.expanding && s.locked == 0 &&
3580 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3581 handle_stripe_expansion(conf, sh);
3582
3583finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003584 /* wait for this device to become unblocked */
NeilBrown43220aa2011-08-31 12:49:14 +10003585 if (conf->mddev->external && unlikely(s.blocked_rdev))
NeilBrownc5709ef2011-07-26 11:35:20 +10003586 md_wait_for_blocked_rdev(s.blocked_rdev, conf->mddev);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003587
NeilBrownbc2607f2011-07-28 11:39:22 +10003588 if (s.handle_bad_blocks)
3589 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003590 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003591 struct r5dev *dev = &sh->dev[i];
3592 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3593 /* We own a safe reference to the rdev */
3594 rdev = conf->disks[i].rdev;
3595 if (!rdev_set_badblocks(rdev, sh->sector,
3596 STRIPE_SECTORS, 0))
3597 md_error(conf->mddev, rdev);
3598 rdev_dec_pending(rdev, conf->mddev);
3599 }
NeilBrownb84db562011-07-28 11:39:23 +10003600 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3601 rdev = conf->disks[i].rdev;
3602 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003603 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10003604 rdev_dec_pending(rdev, conf->mddev);
3605 }
NeilBrown977df362011-12-23 10:17:53 +11003606 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3607 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11003608 if (!rdev)
3609 /* rdev have been moved down */
3610 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11003611 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003612 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11003613 rdev_dec_pending(rdev, conf->mddev);
3614 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003615 }
3616
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003617 if (s.ops_request)
3618 raid_run_ops(sh, s.ops_request);
3619
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003620 ops_run_io(sh, &s);
3621
NeilBrownc5709ef2011-07-26 11:35:20 +10003622 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003623 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003624 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003625 * have actually been submitted.
3626 */
3627 atomic_dec(&conf->preread_active_stripes);
3628 if (atomic_read(&conf->preread_active_stripes) <
3629 IO_THRESHOLD)
3630 md_wakeup_thread(conf->mddev->thread);
3631 }
3632
NeilBrownc5709ef2011-07-26 11:35:20 +10003633 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003634
Dan Williams257a4b42011-11-08 16:22:06 +11003635 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003636}
3637
NeilBrownd1688a62011-10-11 16:49:52 +11003638static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639{
3640 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3641 while (!list_empty(&conf->delayed_list)) {
3642 struct list_head *l = conf->delayed_list.next;
3643 struct stripe_head *sh;
3644 sh = list_entry(l, struct stripe_head, lru);
3645 list_del_init(l);
3646 clear_bit(STRIPE_DELAYED, &sh->state);
3647 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3648 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003649 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 }
NeilBrown482c0832011-04-18 18:25:42 +10003651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652}
3653
NeilBrownd1688a62011-10-11 16:49:52 +11003654static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003655{
3656 /* device_lock is held */
3657 struct list_head head;
3658 list_add(&head, &conf->bitmap_list);
3659 list_del_init(&conf->bitmap_list);
3660 while (!list_empty(&head)) {
3661 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3662 list_del_init(&sh->lru);
3663 atomic_inc(&sh->count);
3664 __release_stripe(conf, sh);
3665 }
3666}
3667
NeilBrownfd01b882011-10-11 16:47:53 +11003668int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003669{
NeilBrownd1688a62011-10-11 16:49:52 +11003670 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003671
3672 /* No difference between reads and writes. Just check
3673 * how busy the stripe_cache is
3674 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003675
NeilBrownf022b2f2006-10-03 01:15:56 -07003676 if (conf->inactive_blocked)
3677 return 1;
3678 if (conf->quiesce)
3679 return 1;
3680 if (list_empty_careful(&conf->inactive_list))
3681 return 1;
3682
3683 return 0;
3684}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003685EXPORT_SYMBOL_GPL(md_raid5_congested);
3686
3687static int raid5_congested(void *data, int bits)
3688{
NeilBrownfd01b882011-10-11 16:47:53 +11003689 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003690
3691 return mddev_congested(mddev, bits) ||
3692 md_raid5_congested(mddev, bits);
3693}
NeilBrownf022b2f2006-10-03 01:15:56 -07003694
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003695/* We want read requests to align with chunks where possible,
3696 * but write requests don't need to.
3697 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003698static int raid5_mergeable_bvec(struct request_queue *q,
3699 struct bvec_merge_data *bvm,
3700 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003701{
NeilBrownfd01b882011-10-11 16:47:53 +11003702 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003703 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003704 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003705 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003706 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003707
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003708 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003709 return biovec->bv_len; /* always allow writes to be mergeable */
3710
Andre Noll664e7c42009-06-18 08:45:27 +10003711 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3712 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003713 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3714 if (max < 0) max = 0;
3715 if (max <= biovec->bv_len && bio_sectors == 0)
3716 return biovec->bv_len;
3717 else
3718 return max;
3719}
3720
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003721
NeilBrownfd01b882011-10-11 16:47:53 +11003722static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003723{
3724 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003725 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003726 unsigned int bio_sectors = bio->bi_size >> 9;
3727
Andre Noll664e7c42009-06-18 08:45:27 +10003728 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3729 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003730 return chunk_sectors >=
3731 ((sector & (chunk_sectors - 1)) + bio_sectors);
3732}
3733
3734/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003735 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3736 * later sampled by raid5d.
3737 */
NeilBrownd1688a62011-10-11 16:49:52 +11003738static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003739{
3740 unsigned long flags;
3741
3742 spin_lock_irqsave(&conf->device_lock, flags);
3743
3744 bi->bi_next = conf->retry_read_aligned_list;
3745 conf->retry_read_aligned_list = bi;
3746
3747 spin_unlock_irqrestore(&conf->device_lock, flags);
3748 md_wakeup_thread(conf->mddev->thread);
3749}
3750
3751
NeilBrownd1688a62011-10-11 16:49:52 +11003752static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003753{
3754 struct bio *bi;
3755
3756 bi = conf->retry_read_aligned;
3757 if (bi) {
3758 conf->retry_read_aligned = NULL;
3759 return bi;
3760 }
3761 bi = conf->retry_read_aligned_list;
3762 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003763 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003764 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003765 /*
3766 * this sets the active strip count to 1 and the processed
3767 * strip count to zero (upper 8 bits)
3768 */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003769 bi->bi_phys_segments = 1; /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003770 }
3771
3772 return bi;
3773}
3774
3775
3776/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003777 * The "raid5_align_endio" should check if the read succeeded and if it
3778 * did, call bio_endio on the original bio (having bio_put the new bio
3779 * first).
3780 * If the read failed..
3781 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003782static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003783{
3784 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003785 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003786 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003787 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003788 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003789
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003790 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003791
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003792 rdev = (void*)raid_bi->bi_next;
3793 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003794 mddev = rdev->mddev;
3795 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003796
3797 rdev_dec_pending(rdev, conf->mddev);
3798
3799 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003800 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003801 if (atomic_dec_and_test(&conf->active_aligned_reads))
3802 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003803 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003804 }
3805
3806
Dan Williams45b42332007-07-09 11:56:43 -07003807 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003808
3809 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003810}
3811
Neil Brown387bb172007-02-08 14:20:29 -08003812static int bio_fits_rdev(struct bio *bi)
3813{
Jens Axboe165125e2007-07-24 09:28:11 +02003814 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003815
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003816 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003817 return 0;
3818 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003819 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003820 return 0;
3821
3822 if (q->merge_bvec_fn)
3823 /* it's too hard to apply the merge_bvec_fn at this stage,
3824 * just just give up
3825 */
3826 return 0;
3827
3828 return 1;
3829}
3830
3831
NeilBrownfd01b882011-10-11 16:47:53 +11003832static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003833{
NeilBrownd1688a62011-10-11 16:49:52 +11003834 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11003835 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003836 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11003837 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11003838 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003839
3840 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003841 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003842 return 0;
3843 }
3844 /*
NeilBrowna167f662010-10-26 18:31:13 +11003845 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003846 */
NeilBrowna167f662010-10-26 18:31:13 +11003847 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003848 if (!align_bi)
3849 return 0;
3850 /*
3851 * set bi_end_io to a new function, and set bi_private to the
3852 * original bio.
3853 */
3854 align_bi->bi_end_io = raid5_align_endio;
3855 align_bi->bi_private = raid_bio;
3856 /*
3857 * compute position
3858 */
NeilBrown112bf892009-03-31 14:39:38 +11003859 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3860 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003861 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003862
NeilBrown671488c2011-12-23 10:17:52 +11003863 end_sector = align_bi->bi_sector + (align_bi->bi_size >> 9);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003864 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11003865 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
3866 if (!rdev || test_bit(Faulty, &rdev->flags) ||
3867 rdev->recovery_offset < end_sector) {
3868 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3869 if (rdev &&
3870 (test_bit(Faulty, &rdev->flags) ||
3871 !(test_bit(In_sync, &rdev->flags) ||
3872 rdev->recovery_offset >= end_sector)))
3873 rdev = NULL;
3874 }
3875 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10003876 sector_t first_bad;
3877 int bad_sectors;
3878
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003879 atomic_inc(&rdev->nr_pending);
3880 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003881 raid_bio->bi_next = (void*)rdev;
3882 align_bi->bi_bdev = rdev->bdev;
3883 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
NeilBrown05616be2012-05-21 09:27:00 +10003884 /* No reshape active, so we can trust rdev->data_offset */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003885 align_bi->bi_sector += rdev->data_offset;
3886
NeilBrown31c176e2011-07-28 11:39:22 +10003887 if (!bio_fits_rdev(align_bi) ||
3888 is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
3889 &first_bad, &bad_sectors)) {
3890 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08003891 bio_put(align_bi);
3892 rdev_dec_pending(rdev, mddev);
3893 return 0;
3894 }
3895
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003896 spin_lock_irq(&conf->device_lock);
3897 wait_event_lock_irq(conf->wait_for_stripe,
3898 conf->quiesce == 0,
3899 conf->device_lock, /* nothing */);
3900 atomic_inc(&conf->active_aligned_reads);
3901 spin_unlock_irq(&conf->device_lock);
3902
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003903 generic_make_request(align_bi);
3904 return 1;
3905 } else {
3906 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003907 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003908 return 0;
3909 }
3910}
3911
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003912/* __get_priority_stripe - get the next stripe to process
3913 *
3914 * Full stripe writes are allowed to pass preread active stripes up until
3915 * the bypass_threshold is exceeded. In general the bypass_count
3916 * increments when the handle_list is handled before the hold_list; however, it
3917 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3918 * stripe with in flight i/o. The bypass_count will be reset when the
3919 * head of the hold_list has changed, i.e. the head was promoted to the
3920 * handle_list.
3921 */
NeilBrownd1688a62011-10-11 16:49:52 +11003922static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003923{
3924 struct stripe_head *sh;
3925
3926 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3927 __func__,
3928 list_empty(&conf->handle_list) ? "empty" : "busy",
3929 list_empty(&conf->hold_list) ? "empty" : "busy",
3930 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3931
3932 if (!list_empty(&conf->handle_list)) {
3933 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3934
3935 if (list_empty(&conf->hold_list))
3936 conf->bypass_count = 0;
3937 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3938 if (conf->hold_list.next == conf->last_hold)
3939 conf->bypass_count++;
3940 else {
3941 conf->last_hold = conf->hold_list.next;
3942 conf->bypass_count -= conf->bypass_threshold;
3943 if (conf->bypass_count < 0)
3944 conf->bypass_count = 0;
3945 }
3946 }
3947 } else if (!list_empty(&conf->hold_list) &&
3948 ((conf->bypass_threshold &&
3949 conf->bypass_count > conf->bypass_threshold) ||
3950 atomic_read(&conf->pending_full_writes) == 0)) {
3951 sh = list_entry(conf->hold_list.next,
3952 typeof(*sh), lru);
3953 conf->bypass_count -= conf->bypass_threshold;
3954 if (conf->bypass_count < 0)
3955 conf->bypass_count = 0;
3956 } else
3957 return NULL;
3958
3959 list_del_init(&sh->lru);
3960 atomic_inc(&sh->count);
3961 BUG_ON(atomic_read(&sh->count) != 1);
3962 return sh;
3963}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003964
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07003965static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966{
NeilBrownd1688a62011-10-11 16:49:52 +11003967 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11003968 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 sector_t new_sector;
3970 sector_t logical_sector, last_sector;
3971 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01003972 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11003973 int remaining;
NeilBrown7c13edc2011-04-18 18:25:43 +10003974 int plugged;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975
Tejun Heoe9c74692010-09-03 11:56:18 +02003976 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
3977 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02003978 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07003979 }
3980
NeilBrown3d310eb2005-06-21 17:17:26 -07003981 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07003982
NeilBrown802ba062006-12-13 00:34:13 -08003983 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003984 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11003985 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02003986 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003987
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
3989 last_sector = bi->bi_sector + (bi->bi_size>>9);
3990 bi->bi_next = NULL;
3991 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07003992
NeilBrown7c13edc2011-04-18 18:25:43 +10003993 plugged = mddev_check_plugged(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
3995 DEFINE_WAIT(w);
NeilBrownb5663ba2009-03-31 14:39:38 +11003996 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08003997
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003998 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11003999 previous = 0;
NeilBrownb578d552006-03-27 01:18:12 -08004000 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004001 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11004002 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f762006-03-27 01:18:15 -08004003 * 64bit on a 32bit platform, and so it might be
4004 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02004005 * Of course reshape_progress could change after
NeilBrowndf8e7f762006-03-27 01:18:15 -08004006 * the lock is dropped, so once we get a reference
4007 * to the stripe that we think it is, we will have
4008 * to check again.
4009 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004010 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004011 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004012 ? logical_sector < conf->reshape_progress
4013 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004014 previous = 1;
4015 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10004016 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004017 ? logical_sector < conf->reshape_safe
4018 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08004019 spin_unlock_irq(&conf->device_lock);
4020 schedule();
4021 goto retry;
4022 }
4023 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004024 spin_unlock_irq(&conf->device_lock);
4025 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004026
NeilBrown112bf892009-03-31 14:39:38 +11004027 new_sector = raid5_compute_sector(conf, logical_sector,
4028 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11004029 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10004030 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 (unsigned long long)new_sector,
4032 (unsigned long long)logical_sector);
4033
NeilBrownb5663ba2009-03-31 14:39:38 +11004034 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10004035 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004037 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004038 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f762006-03-27 01:18:15 -08004039 * stripe, so we must do the range check again.
4040 * Expansion could still move past after this
4041 * test, but as we are holding a reference to
4042 * 'sh', we know that if that happens,
4043 * STRIPE_EXPANDING will get set and the expansion
4044 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004045 */
4046 int must_retry = 0;
4047 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004048 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11004049 ? logical_sector >= conf->reshape_progress
4050 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004051 /* mismatch, need to try again */
4052 must_retry = 1;
4053 spin_unlock_irq(&conf->device_lock);
4054 if (must_retry) {
4055 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004056 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004057 goto retry;
4058 }
4059 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004060
Namhyung Kimffd96e32011-07-18 17:38:51 +10004061 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004062 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004063 logical_sector < mddev->suspend_hi) {
4064 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004065 /* As the suspend_* range is controlled by
4066 * userspace, we want an interruptible
4067 * wait.
4068 */
4069 flush_signals(current);
4070 prepare_to_wait(&conf->wait_for_overlap,
4071 &w, TASK_INTERRUPTIBLE);
4072 if (logical_sector >= mddev->suspend_lo &&
4073 logical_sector < mddev->suspend_hi)
4074 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004075 goto retry;
4076 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004077
4078 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004079 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004080 /* Stripe is busy expanding or
4081 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 * and wait a while
4083 */
NeilBrown482c0832011-04-18 18:25:42 +10004084 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 release_stripe(sh);
4086 schedule();
4087 goto retry;
4088 }
4089 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004090 set_bit(STRIPE_HANDLE, &sh->state);
4091 clear_bit(STRIPE_DELAYED, &sh->state);
Tejun Heoe9c74692010-09-03 11:56:18 +02004092 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004093 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4094 atomic_inc(&conf->preread_active_stripes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 } else {
4097 /* cannot get stripe for read-ahead, just give-up */
4098 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4099 finish_wait(&conf->wait_for_overlap, &w);
4100 break;
4101 }
4102
4103 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004104 if (!plugged)
4105 md_wakeup_thread(mddev->thread);
4106
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004108 remaining = raid5_dec_bi_phys_segments(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004109 spin_unlock_irq(&conf->device_lock);
4110 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111
NeilBrown16a53ec2006-06-26 00:27:38 -07004112 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004114
Neil Brown0e13fe232008-06-28 08:31:20 +10004115 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117}
4118
NeilBrownfd01b882011-10-11 16:47:53 +11004119static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004120
NeilBrownfd01b882011-10-11 16:47:53 +11004121static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122{
NeilBrown52c03292006-06-26 00:27:43 -07004123 /* reshaping is quite different to recovery/resync so it is
4124 * handled quite separately ... here.
4125 *
4126 * On each call to sync_request, we gather one chunk worth of
4127 * destination stripes and flag them as expanding.
4128 * Then we find all the source stripes and request reads.
4129 * As the reads complete, handle_stripe will copy the data
4130 * into the destination stripe and release that stripe.
4131 */
NeilBrownd1688a62011-10-11 16:49:52 +11004132 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004134 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004135 int raid_disks = conf->previous_raid_disks;
4136 int data_disks = raid_disks - conf->max_degraded;
4137 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004138 int i;
4139 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004140 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004141 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004142 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004143 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004144
NeilBrownfef9c612009-03-31 15:16:46 +11004145 if (sector_nr == 0) {
4146 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10004147 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004148 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4149 sector_nr = raid5_size(mddev, 0, 0)
4150 - conf->reshape_progress;
NeilBrown2c810cd2012-05-21 09:27:00 +10004151 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004152 conf->reshape_progress > 0)
4153 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004154 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004155 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004156 mddev->curr_resync_completed = sector_nr;
4157 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004158 *skipped = 1;
4159 return sector_nr;
4160 }
NeilBrown52c03292006-06-26 00:27:43 -07004161 }
4162
NeilBrown7a661382009-03-31 15:21:40 +11004163 /* We need to process a full chunk at a time.
4164 * If old and new chunk sizes differ, we need to process the
4165 * largest of these
4166 */
Andre Noll664e7c42009-06-18 08:45:27 +10004167 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4168 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004169 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004170 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004171
NeilBrownb5254dd2012-05-21 09:27:01 +10004172 /* We update the metadata at least every 10 seconds, or when
4173 * the data about to be copied would over-write the source of
4174 * the data at the front of the range. i.e. one new_stripe
4175 * along from reshape_progress new_maps to after where
4176 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004177 */
NeilBrownfef9c612009-03-31 15:16:46 +11004178 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004179 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004180 readpos = conf->reshape_progress;
4181 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004182 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004183 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10004184 if (mddev->reshape_backwards) {
NeilBrowned37d832009-05-27 21:39:05 +10004185 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004186 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004187 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004188 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004189 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004190 readpos -= min_t(sector_t, reshape_sectors, readpos);
4191 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004192 }
NeilBrown52c03292006-06-26 00:27:43 -07004193
NeilBrownb5254dd2012-05-21 09:27:01 +10004194 /* Having calculated the 'writepos' possibly use it
4195 * to set 'stripe_addr' which is where we will write to.
4196 */
4197 if (mddev->reshape_backwards) {
4198 BUG_ON(conf->reshape_progress == 0);
4199 stripe_addr = writepos;
4200 BUG_ON((mddev->dev_sectors &
4201 ~((sector_t)reshape_sectors - 1))
4202 - reshape_sectors - stripe_addr
4203 != sector_nr);
4204 } else {
4205 BUG_ON(writepos != sector_nr + reshape_sectors);
4206 stripe_addr = sector_nr;
4207 }
4208
NeilBrownc8f517c2009-03-31 15:28:40 +11004209 /* 'writepos' is the most advanced device address we might write.
4210 * 'readpos' is the least advanced device address we might read.
4211 * 'safepos' is the least address recorded in the metadata as having
4212 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10004213 * If there is a min_offset_diff, these are adjusted either by
4214 * increasing the safepos/readpos if diff is negative, or
4215 * increasing writepos if diff is positive.
4216 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11004217 * ensure safety in the face of a crash - that must be done by userspace
4218 * making a backup of the data. So in that case there is no particular
4219 * rush to update metadata.
4220 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4221 * update the metadata to advance 'safepos' to match 'readpos' so that
4222 * we can be safe in the event of a crash.
4223 * So we insist on updating metadata if safepos is behind writepos and
4224 * readpos is beyond writepos.
4225 * In any case, update the metadata every 10 seconds.
4226 * Maybe that number should be configurable, but I'm not sure it is
4227 * worth it.... maybe it could be a multiple of safemode_delay???
4228 */
NeilBrownb5254dd2012-05-21 09:27:01 +10004229 if (conf->min_offset_diff < 0) {
4230 safepos += -conf->min_offset_diff;
4231 readpos += -conf->min_offset_diff;
4232 } else
4233 writepos += conf->min_offset_diff;
4234
NeilBrown2c810cd2012-05-21 09:27:00 +10004235 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11004236 ? (safepos > writepos && readpos < writepos)
4237 : (safepos < writepos && readpos > writepos)) ||
4238 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004239 /* Cannot proceed until we've updated the superblock... */
4240 wait_event(conf->wait_for_overlap,
4241 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004242 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004243 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004244 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b422006-10-03 01:15:46 -07004245 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004246 md_wakeup_thread(mddev->thread);
NeilBrown850b2b422006-10-03 01:15:46 -07004247 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004248 kthread_should_stop());
4249 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004250 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004251 spin_unlock_irq(&conf->device_lock);
4252 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004253 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004254 }
4255
NeilBrownab69ae12009-03-31 15:26:47 +11004256 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004257 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004258 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004259 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004260 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004261 set_bit(STRIPE_EXPANDING, &sh->state);
4262 atomic_inc(&conf->reshape_stripes);
4263 /* If any of this stripe is beyond the end of the old
4264 * array, then we need to zero those blocks
4265 */
4266 for (j=sh->disks; j--;) {
4267 sector_t s;
4268 if (j == sh->pd_idx)
4269 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004270 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004271 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004272 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004273 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004274 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004275 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004276 continue;
4277 }
4278 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4279 set_bit(R5_Expanded, &sh->dev[j].flags);
4280 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4281 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004282 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004283 set_bit(STRIPE_EXPAND_READY, &sh->state);
4284 set_bit(STRIPE_HANDLE, &sh->state);
4285 }
NeilBrownab69ae12009-03-31 15:26:47 +11004286 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004287 }
4288 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004289 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11004290 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004291 else
NeilBrown7a661382009-03-31 15:21:40 +11004292 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004293 spin_unlock_irq(&conf->device_lock);
4294 /* Ok, those stripe are ready. We can start scheduling
4295 * reads on the source stripes.
4296 * The source stripes are determined by mapping the first and last
4297 * block on the destination stripes.
4298 */
NeilBrown52c03292006-06-26 00:27:43 -07004299 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004300 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004301 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004302 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004303 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004304 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004305 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004306 if (last_sector >= mddev->dev_sectors)
4307 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004308 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004309 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004310 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4311 set_bit(STRIPE_HANDLE, &sh->state);
4312 release_stripe(sh);
4313 first_sector += STRIPE_SECTORS;
4314 }
NeilBrownab69ae12009-03-31 15:26:47 +11004315 /* Now that the sources are clearly marked, we can release
4316 * the destination stripes
4317 */
4318 while (!list_empty(&stripes)) {
4319 sh = list_entry(stripes.next, struct stripe_head, lru);
4320 list_del_init(&sh->lru);
4321 release_stripe(sh);
4322 }
NeilBrownc6207272008-02-06 01:39:52 -08004323 /* If this takes us to the resync_max point where we have to pause,
4324 * then we need to write out the superblock.
4325 */
NeilBrown7a661382009-03-31 15:21:40 +11004326 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004327 if ((sector_nr - mddev->curr_resync_completed) * 2
4328 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004329 /* Cannot proceed until we've updated the superblock... */
4330 wait_event(conf->wait_for_overlap,
4331 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004332 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004333 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004334 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004335 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4336 md_wakeup_thread(mddev->thread);
4337 wait_event(mddev->sb_wait,
4338 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4339 || kthread_should_stop());
4340 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004341 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004342 spin_unlock_irq(&conf->device_lock);
4343 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004344 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004345 }
NeilBrown7a661382009-03-31 15:21:40 +11004346 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004347}
4348
4349/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004350static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004351{
NeilBrownd1688a62011-10-11 16:49:52 +11004352 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004353 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004354 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004355 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004356 int still_degraded = 0;
4357 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358
NeilBrown72626682005-09-09 16:23:54 -07004359 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004361
NeilBrown29269552006-03-27 01:18:10 -08004362 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4363 end_reshape(conf);
4364 return 0;
4365 }
NeilBrown72626682005-09-09 16:23:54 -07004366
4367 if (mddev->curr_resync < max_sector) /* aborted */
4368 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4369 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004370 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004371 conf->fullsync = 0;
4372 bitmap_close_sync(mddev->bitmap);
4373
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 return 0;
4375 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004376
NeilBrown64bd6602009-08-03 10:59:58 +10004377 /* Allow raid5_quiesce to complete */
4378 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4379
NeilBrown52c03292006-06-26 00:27:43 -07004380 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4381 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004382
NeilBrownc6207272008-02-06 01:39:52 -08004383 /* No need to check resync_max as we never do more than one
4384 * stripe, and as resync_max will always be on a chunk boundary,
4385 * if the check in md_do_sync didn't fire, there is no chance
4386 * of overstepping resync_max here
4387 */
4388
NeilBrown16a53ec2006-06-26 00:27:38 -07004389 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 * to resync, then assert that we are finished, because there is
4391 * nothing we can do.
4392 */
NeilBrown3285edf2006-06-26 00:27:55 -07004393 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004394 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004395 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004396 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 return rv;
4398 }
NeilBrown72626682005-09-09 16:23:54 -07004399 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004400 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004401 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4402 /* we can skip this block, and probably more */
4403 sync_blocks /= STRIPE_SECTORS;
4404 *skipped = 1;
4405 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407
NeilBrownb47490c2008-02-06 01:39:50 -08004408 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4409
NeilBrowna8c906c2009-06-09 14:39:59 +10004410 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004412 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004414 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004416 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004418 /* Need to check if array will still be degraded after recovery/resync
4419 * We don't need to check the 'failed' flag as when that gets set,
4420 * recovery aborts.
4421 */
NeilBrownf001a702009-06-09 14:30:31 +10004422 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004423 if (conf->disks[i].rdev == NULL)
4424 still_degraded = 1;
4425
4426 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4427
NeilBrown83206d62011-07-26 11:19:49 +10004428 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429
NeilBrown14425772009-10-16 15:55:25 +11004430 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 release_stripe(sh);
4432
4433 return STRIPE_SECTORS;
4434}
4435
NeilBrownd1688a62011-10-11 16:49:52 +11004436static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004437{
4438 /* We may not be able to submit a whole bio at once as there
4439 * may not be enough stripe_heads available.
4440 * We cannot pre-allocate enough stripe_heads as we may need
4441 * more than exist in the cache (if we allow ever large chunks).
4442 * So we do one stripe head at a time and record in
4443 * ->bi_hw_segments how many have been done.
4444 *
4445 * We *know* that this entire raid_bio is in one chunk, so
4446 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4447 */
4448 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004449 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004450 sector_t sector, logical_sector, last_sector;
4451 int scnt = 0;
4452 int remaining;
4453 int handled = 0;
4454
4455 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004456 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004457 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004458 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4459
4460 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004461 logical_sector += STRIPE_SECTORS,
4462 sector += STRIPE_SECTORS,
4463 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004464
Jens Axboe960e7392008-08-15 10:41:18 +02004465 if (scnt < raid5_bi_hw_segments(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004466 /* already done this stripe */
4467 continue;
4468
NeilBrowna8c906c2009-06-09 14:39:59 +10004469 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004470
4471 if (!sh) {
4472 /* failed to get a stripe - must wait */
Jens Axboe960e7392008-08-15 10:41:18 +02004473 raid5_set_bi_hw_segments(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004474 conf->retry_read_aligned = raid_bio;
4475 return handled;
4476 }
4477
Neil Brown387bb172007-02-08 14:20:29 -08004478 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4479 release_stripe(sh);
Jens Axboe960e7392008-08-15 10:41:18 +02004480 raid5_set_bi_hw_segments(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004481 conf->retry_read_aligned = raid_bio;
4482 return handled;
4483 }
4484
Dan Williams36d1c642009-07-14 11:48:22 -07004485 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004486 release_stripe(sh);
4487 handled++;
4488 }
4489 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004490 remaining = raid5_dec_bi_phys_segments(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004491 spin_unlock_irq(&conf->device_lock);
Neil Brown0e13fe232008-06-28 08:31:20 +10004492 if (remaining == 0)
4493 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004494 if (atomic_dec_and_test(&conf->active_aligned_reads))
4495 wake_up(&conf->wait_for_stripe);
4496 return handled;
4497}
4498
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004499
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500/*
4501 * This is our raid5 kernel thread.
4502 *
4503 * We scan the hash table for stripes which can be handled now.
4504 * During the scan, completed stripes are saved for us by the interrupt
4505 * handler, so that they will not have to wait for our next wakeup.
4506 */
NeilBrownfd01b882011-10-11 16:47:53 +11004507static void raid5d(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508{
4509 struct stripe_head *sh;
NeilBrownd1688a62011-10-11 16:49:52 +11004510 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004512 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513
Dan Williams45b42332007-07-09 11:56:43 -07004514 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515
4516 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004518 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 handled = 0;
4520 spin_lock_irq(&conf->device_lock);
4521 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004522 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523
NeilBrown7c13edc2011-04-18 18:25:43 +10004524 if (atomic_read(&mddev->plug_cnt) == 0 &&
4525 !list_empty(&conf->bitmap_list)) {
4526 /* Now is a good time to flush some bitmap updates */
4527 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004528 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004529 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004530 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004531 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004532 activate_bit_delay(conf);
4533 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004534 if (atomic_read(&mddev->plug_cnt) == 0)
4535 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004536
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004537 while ((bio = remove_bio_from_retry(conf))) {
4538 int ok;
4539 spin_unlock_irq(&conf->device_lock);
4540 ok = retry_aligned_read(conf, bio);
4541 spin_lock_irq(&conf->device_lock);
4542 if (!ok)
4543 break;
4544 handled++;
4545 }
4546
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004547 sh = __get_priority_stripe(conf);
4548
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004549 if (!sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 spin_unlock_irq(&conf->device_lock);
4552
4553 handled++;
Dan Williams417b8d42009-10-16 16:25:22 +11004554 handle_stripe(sh);
4555 release_stripe(sh);
4556 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557
NeilBrownde393cd2011-07-28 11:31:48 +10004558 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
4559 md_check_recovery(mddev);
4560
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 spin_lock_irq(&conf->device_lock);
4562 }
Dan Williams45b42332007-07-09 11:56:43 -07004563 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564
4565 spin_unlock_irq(&conf->device_lock);
4566
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004567 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004568 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569
Dan Williams45b42332007-07-09 11:56:43 -07004570 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571}
4572
NeilBrown3f294f42005-11-08 21:39:25 -08004573static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004574raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004575{
NeilBrownd1688a62011-10-11 16:49:52 +11004576 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004577 if (conf)
4578 return sprintf(page, "%d\n", conf->max_nr_stripes);
4579 else
4580 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004581}
4582
NeilBrownc41d4ac2010-06-01 19:37:24 +10004583int
NeilBrownfd01b882011-10-11 16:47:53 +11004584raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004585{
NeilBrownd1688a62011-10-11 16:49:52 +11004586 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004587 int err;
4588
4589 if (size <= 16 || size > 32768)
4590 return -EINVAL;
4591 while (size < conf->max_nr_stripes) {
4592 if (drop_one_stripe(conf))
4593 conf->max_nr_stripes--;
4594 else
4595 break;
4596 }
4597 err = md_allow_write(mddev);
4598 if (err)
4599 return err;
4600 while (size > conf->max_nr_stripes) {
4601 if (grow_one_stripe(conf))
4602 conf->max_nr_stripes++;
4603 else break;
4604 }
4605 return 0;
4606}
4607EXPORT_SYMBOL(raid5_set_cache_size);
4608
NeilBrown3f294f42005-11-08 21:39:25 -08004609static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004610raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004611{
NeilBrownd1688a62011-10-11 16:49:52 +11004612 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004613 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004614 int err;
4615
NeilBrown3f294f42005-11-08 21:39:25 -08004616 if (len >= PAGE_SIZE)
4617 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004618 if (!conf)
4619 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004620
Dan Williams4ef197d82008-04-28 02:15:54 -07004621 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004622 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004623 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004624 if (err)
4625 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004626 return len;
4627}
NeilBrown007583c2005-11-08 21:39:30 -08004628
NeilBrown96de1e62005-11-08 21:39:39 -08004629static struct md_sysfs_entry
4630raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4631 raid5_show_stripe_cache_size,
4632 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004633
4634static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004635raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004636{
NeilBrownd1688a62011-10-11 16:49:52 +11004637 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004638 if (conf)
4639 return sprintf(page, "%d\n", conf->bypass_threshold);
4640 else
4641 return 0;
4642}
4643
4644static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004645raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004646{
NeilBrownd1688a62011-10-11 16:49:52 +11004647 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004648 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004649 if (len >= PAGE_SIZE)
4650 return -EINVAL;
4651 if (!conf)
4652 return -ENODEV;
4653
Dan Williams4ef197d82008-04-28 02:15:54 -07004654 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004655 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004656 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004657 return -EINVAL;
4658 conf->bypass_threshold = new;
4659 return len;
4660}
4661
4662static struct md_sysfs_entry
4663raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4664 S_IRUGO | S_IWUSR,
4665 raid5_show_preread_threshold,
4666 raid5_store_preread_threshold);
4667
4668static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004669stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004670{
NeilBrownd1688a62011-10-11 16:49:52 +11004671 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004672 if (conf)
4673 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4674 else
4675 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004676}
4677
NeilBrown96de1e62005-11-08 21:39:39 -08004678static struct md_sysfs_entry
4679raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004680
NeilBrown007583c2005-11-08 21:39:30 -08004681static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004682 &raid5_stripecache_size.attr,
4683 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004684 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004685 NULL,
4686};
NeilBrown007583c2005-11-08 21:39:30 -08004687static struct attribute_group raid5_attrs_group = {
4688 .name = NULL,
4689 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004690};
4691
Dan Williams80c3a6c2009-03-17 18:10:40 -07004692static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11004693raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07004694{
NeilBrownd1688a62011-10-11 16:49:52 +11004695 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004696
4697 if (!sectors)
4698 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004699 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11004700 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11004701 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004702
Andre Noll9d8f0362009-06-18 08:45:01 +10004703 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004704 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004705 return sectors * (raid_disks - conf->max_degraded);
4706}
4707
NeilBrownd1688a62011-10-11 16:49:52 +11004708static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004709{
4710 struct raid5_percpu *percpu;
4711 unsigned long cpu;
4712
4713 if (!conf->percpu)
4714 return;
4715
4716 get_online_cpus();
4717 for_each_possible_cpu(cpu) {
4718 percpu = per_cpu_ptr(conf->percpu, cpu);
4719 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004720 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004721 }
4722#ifdef CONFIG_HOTPLUG_CPU
4723 unregister_cpu_notifier(&conf->cpu_notify);
4724#endif
4725 put_online_cpus();
4726
4727 free_percpu(conf->percpu);
4728}
4729
NeilBrownd1688a62011-10-11 16:49:52 +11004730static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10004731{
4732 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004733 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10004734 kfree(conf->disks);
4735 kfree(conf->stripe_hashtbl);
4736 kfree(conf);
4737}
4738
Dan Williams36d1c642009-07-14 11:48:22 -07004739#ifdef CONFIG_HOTPLUG_CPU
4740static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4741 void *hcpu)
4742{
NeilBrownd1688a62011-10-11 16:49:52 +11004743 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07004744 long cpu = (long)hcpu;
4745 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4746
4747 switch (action) {
4748 case CPU_UP_PREPARE:
4749 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07004750 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07004751 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004752 if (!percpu->scribble)
4753 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4754
4755 if (!percpu->scribble ||
4756 (conf->level == 6 && !percpu->spare_page)) {
4757 safe_put_page(percpu->spare_page);
4758 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004759 pr_err("%s: failed memory allocation for cpu%ld\n",
4760 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07004761 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07004762 }
4763 break;
4764 case CPU_DEAD:
4765 case CPU_DEAD_FROZEN:
4766 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004767 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004768 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004769 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07004770 break;
4771 default:
4772 break;
4773 }
4774 return NOTIFY_OK;
4775}
4776#endif
4777
NeilBrownd1688a62011-10-11 16:49:52 +11004778static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004779{
4780 unsigned long cpu;
4781 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09004782 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004783 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004784 int err;
4785
Dan Williams36d1c642009-07-14 11:48:22 -07004786 allcpus = alloc_percpu(struct raid5_percpu);
4787 if (!allcpus)
4788 return -ENOMEM;
4789 conf->percpu = allcpus;
4790
4791 get_online_cpus();
4792 err = 0;
4793 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07004794 if (conf->level == 6) {
4795 spare_page = alloc_page(GFP_KERNEL);
4796 if (!spare_page) {
4797 err = -ENOMEM;
4798 break;
4799 }
4800 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
4801 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11004802 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004803 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07004804 err = -ENOMEM;
4805 break;
4806 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07004807 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004808 }
4809#ifdef CONFIG_HOTPLUG_CPU
4810 conf->cpu_notify.notifier_call = raid456_cpu_notify;
4811 conf->cpu_notify.priority = 0;
4812 if (err == 0)
4813 err = register_cpu_notifier(&conf->cpu_notify);
4814#endif
4815 put_online_cpus();
4816
4817 return err;
4818}
4819
NeilBrownd1688a62011-10-11 16:49:52 +11004820static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821{
NeilBrownd1688a62011-10-11 16:49:52 +11004822 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004823 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11004824 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 struct disk_info *disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826
NeilBrown91adb562009-03-31 14:39:39 +11004827 if (mddev->new_level != 5
4828 && mddev->new_level != 4
4829 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10004830 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004831 mdname(mddev), mddev->new_level);
4832 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 }
NeilBrown91adb562009-03-31 14:39:39 +11004834 if ((mddev->new_level == 5
4835 && !algorithm_valid_raid5(mddev->new_layout)) ||
4836 (mddev->new_level == 6
4837 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004838 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11004839 mdname(mddev), mddev->new_layout);
4840 return ERR_PTR(-EIO);
4841 }
4842 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10004843 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004844 mdname(mddev), mddev->raid_disks);
4845 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11004846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847
Andre Noll664e7c42009-06-18 08:45:27 +10004848 if (!mddev->new_chunk_sectors ||
4849 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
4850 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10004851 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
4852 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11004853 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11004854 }
4855
NeilBrownd1688a62011-10-11 16:49:52 +11004856 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11004857 if (conf == NULL)
4858 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11004859 spin_lock_init(&conf->device_lock);
4860 init_waitqueue_head(&conf->wait_for_stripe);
4861 init_waitqueue_head(&conf->wait_for_overlap);
4862 INIT_LIST_HEAD(&conf->handle_list);
4863 INIT_LIST_HEAD(&conf->hold_list);
4864 INIT_LIST_HEAD(&conf->delayed_list);
4865 INIT_LIST_HEAD(&conf->bitmap_list);
4866 INIT_LIST_HEAD(&conf->inactive_list);
4867 atomic_set(&conf->active_stripes, 0);
4868 atomic_set(&conf->preread_active_stripes, 0);
4869 atomic_set(&conf->active_aligned_reads, 0);
4870 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11004871 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11004872
4873 conf->raid_disks = mddev->raid_disks;
4874 if (mddev->reshape_position == MaxSector)
4875 conf->previous_raid_disks = mddev->raid_disks;
4876 else
4877 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004878 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
4879 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11004880
NeilBrown5e5e3e72009-10-16 16:35:30 +11004881 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11004882 GFP_KERNEL);
4883 if (!conf->disks)
4884 goto abort;
4885
4886 conf->mddev = mddev;
4887
4888 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
4889 goto abort;
4890
Dan Williams36d1c642009-07-14 11:48:22 -07004891 conf->level = mddev->new_level;
4892 if (raid5_alloc_percpu(conf) != 0)
4893 goto abort;
4894
NeilBrown0c55e022010-05-03 14:09:02 +10004895 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11004896
NeilBrowndafb20f2012-03-19 12:46:39 +11004897 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11004898 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004899 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11004900 || raid_disk < 0)
4901 continue;
4902 disk = conf->disks + raid_disk;
4903
NeilBrown17045f52011-12-23 10:17:53 +11004904 if (test_bit(Replacement, &rdev->flags)) {
4905 if (disk->replacement)
4906 goto abort;
4907 disk->replacement = rdev;
4908 } else {
4909 if (disk->rdev)
4910 goto abort;
4911 disk->rdev = rdev;
4912 }
NeilBrown91adb562009-03-31 14:39:39 +11004913
4914 if (test_bit(In_sync, &rdev->flags)) {
4915 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10004916 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
4917 " disk %d\n",
4918 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05004919 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11004920 /* Cannot rely on bitmap to complete recovery */
4921 conf->fullsync = 1;
4922 }
4923
Andre Noll09c9e5f2009-06-18 08:45:55 +10004924 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11004925 conf->level = mddev->new_level;
4926 if (conf->level == 6)
4927 conf->max_degraded = 2;
4928 else
4929 conf->max_degraded = 1;
4930 conf->algorithm = mddev->new_layout;
4931 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11004932 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11004933 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10004934 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11004935 conf->prev_algo = mddev->layout;
4936 }
NeilBrown91adb562009-03-31 14:39:39 +11004937
4938 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11004939 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11004940 if (grow_stripes(conf, conf->max_nr_stripes)) {
4941 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004942 "md/raid:%s: couldn't allocate %dkB for buffers\n",
4943 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004944 goto abort;
4945 } else
NeilBrown0c55e022010-05-03 14:09:02 +10004946 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
4947 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004948
NeilBrown0da3c612009-09-23 18:09:45 +10004949 conf->thread = md_register_thread(raid5d, mddev, NULL);
NeilBrown91adb562009-03-31 14:39:39 +11004950 if (!conf->thread) {
4951 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004952 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11004953 mdname(mddev));
4954 goto abort;
4955 }
4956
4957 return conf;
4958
4959 abort:
4960 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10004961 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11004962 return ERR_PTR(-EIO);
4963 } else
4964 return ERR_PTR(-ENOMEM);
4965}
4966
NeilBrownc148ffd2009-11-13 17:47:00 +11004967
4968static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
4969{
4970 switch (algo) {
4971 case ALGORITHM_PARITY_0:
4972 if (raid_disk < max_degraded)
4973 return 1;
4974 break;
4975 case ALGORITHM_PARITY_N:
4976 if (raid_disk >= raid_disks - max_degraded)
4977 return 1;
4978 break;
4979 case ALGORITHM_PARITY_0_6:
4980 if (raid_disk == 0 ||
4981 raid_disk == raid_disks - 1)
4982 return 1;
4983 break;
4984 case ALGORITHM_LEFT_ASYMMETRIC_6:
4985 case ALGORITHM_RIGHT_ASYMMETRIC_6:
4986 case ALGORITHM_LEFT_SYMMETRIC_6:
4987 case ALGORITHM_RIGHT_SYMMETRIC_6:
4988 if (raid_disk == raid_disks - 1)
4989 return 1;
4990 }
4991 return 0;
4992}
4993
NeilBrownfd01b882011-10-11 16:47:53 +11004994static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11004995{
NeilBrownd1688a62011-10-11 16:49:52 +11004996 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10004997 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11004998 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11004999 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11005000 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11005001 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10005002 long long min_offset_diff = 0;
5003 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11005004
Andre Noll8c6ac8682009-06-18 08:48:06 +10005005 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10005006 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac8682009-06-18 08:48:06 +10005007 " -- starting background reconstruction\n",
5008 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10005009
5010 rdev_for_each(rdev, mddev) {
5011 long long diff;
5012 if (rdev->raid_disk < 0)
5013 continue;
5014 diff = (rdev->new_data_offset - rdev->data_offset);
5015 if (first) {
5016 min_offset_diff = diff;
5017 first = 0;
5018 } else if (mddev->reshape_backwards &&
5019 diff < min_offset_diff)
5020 min_offset_diff = diff;
5021 else if (!mddev->reshape_backwards &&
5022 diff > min_offset_diff)
5023 min_offset_diff = diff;
5024 }
5025
NeilBrownf6705572006-03-27 01:18:11 -08005026 if (mddev->reshape_position != MaxSector) {
5027 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10005028 * Difficulties arise if the stripe we would write to
5029 * next is at or after the stripe we would read from next.
5030 * For a reshape that changes the number of devices, this
5031 * is only possible for a very short time, and mdadm makes
5032 * sure that time appears to have past before assembling
5033 * the array. So we fail if that time hasn't passed.
5034 * For a reshape that keeps the number of devices the same
5035 * mdadm must be monitoring the reshape can keeping the
5036 * critical areas read-only and backed up. It will start
5037 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08005038 */
5039 sector_t here_new, here_old;
5040 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11005041 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08005042
NeilBrown88ce4932009-03-31 15:24:23 +11005043 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10005044 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08005045 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08005046 mdname(mddev));
5047 return -EINVAL;
5048 }
NeilBrownf6705572006-03-27 01:18:11 -08005049 old_disks = mddev->raid_disks - mddev->delta_disks;
5050 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08005051 * further up in new geometry must map after here in old
5052 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08005053 */
5054 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10005055 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005056 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005057 printk(KERN_ERR "md/raid:%s: reshape_position not "
5058 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005059 return -EINVAL;
5060 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005061 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005062 /* here_new is the stripe we will write to */
5063 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10005064 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005065 (old_disks-max_degraded));
5066 /* here_old is the first stripe that we might need to read
5067 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10005068 if (mddev->delta_disks == 0) {
NeilBrownb5254dd2012-05-21 09:27:01 +10005069 if ((here_new * mddev->new_chunk_sectors !=
5070 here_old * mddev->chunk_sectors)) {
5071 printk(KERN_ERR "md/raid:%s: reshape position is"
5072 " confused - aborting\n", mdname(mddev));
5073 return -EINVAL;
5074 }
NeilBrown67ac6012009-08-13 10:06:24 +10005075 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10005076 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10005077 * and taking constant backups.
5078 * mdadm always starts a situation like this in
5079 * readonly mode so it can take control before
5080 * allowing any writes. So just check for that.
5081 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005082 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
5083 abs(min_offset_diff) >= mddev->new_chunk_sectors)
5084 /* not really in-place - so OK */;
5085 else if (mddev->ro == 0) {
5086 printk(KERN_ERR "md/raid:%s: in-place reshape "
5087 "must be started in read-only mode "
5088 "- aborting\n",
NeilBrown0c55e022010-05-03 14:09:02 +10005089 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005090 return -EINVAL;
5091 }
NeilBrown2c810cd2012-05-21 09:27:00 +10005092 } else if (mddev->reshape_backwards
NeilBrownb5254dd2012-05-21 09:27:01 +10005093 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
NeilBrown67ac6012009-08-13 10:06:24 +10005094 here_old * mddev->chunk_sectors)
5095 : (here_new * mddev->new_chunk_sectors >=
NeilBrownb5254dd2012-05-21 09:27:01 +10005096 here_old * mddev->chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08005097 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005098 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5099 "auto-recovery - aborting.\n",
5100 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005101 return -EINVAL;
5102 }
NeilBrown0c55e022010-05-03 14:09:02 +10005103 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5104 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005105 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005106 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005107 BUG_ON(mddev->level != mddev->new_level);
5108 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005109 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005110 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005111 }
5112
NeilBrown245f46c2009-03-31 14:39:39 +11005113 if (mddev->private == NULL)
5114 conf = setup_conf(mddev);
5115 else
5116 conf = mddev->private;
5117
NeilBrown91adb562009-03-31 14:39:39 +11005118 if (IS_ERR(conf))
5119 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005120
NeilBrownb5254dd2012-05-21 09:27:01 +10005121 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11005122 mddev->thread = conf->thread;
5123 conf->thread = NULL;
5124 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125
NeilBrown17045f52011-12-23 10:17:53 +11005126 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5127 i++) {
5128 rdev = conf->disks[i].rdev;
5129 if (!rdev && conf->disks[i].replacement) {
5130 /* The replacement is all we have yet */
5131 rdev = conf->disks[i].replacement;
5132 conf->disks[i].replacement = NULL;
5133 clear_bit(Replacement, &rdev->flags);
5134 conf->disks[i].rdev = rdev;
5135 }
5136 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11005137 continue;
NeilBrown17045f52011-12-23 10:17:53 +11005138 if (conf->disks[i].replacement &&
5139 conf->reshape_progress != MaxSector) {
5140 /* replacements and reshape simply do not mix. */
5141 printk(KERN_ERR "md: cannot handle concurrent "
5142 "replacement and reshape.\n");
5143 goto abort;
5144 }
NeilBrown2f115882010-06-17 17:41:03 +10005145 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005146 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005147 continue;
5148 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005149 /* This disc is not fully in-sync. However if it
5150 * just stored parity (beyond the recovery_offset),
5151 * when we don't need to be concerned about the
5152 * array being dirty.
5153 * When reshape goes 'backwards', we never have
5154 * partially completed devices, so we only need
5155 * to worry about reshape going forwards.
5156 */
5157 /* Hack because v0.91 doesn't store recovery_offset properly. */
5158 if (mddev->major_version == 0 &&
5159 mddev->minor_version > 90)
5160 rdev->recovery_offset = reshape_offset;
5161
NeilBrownc148ffd2009-11-13 17:47:00 +11005162 if (rdev->recovery_offset < reshape_offset) {
5163 /* We need to check old and new layout */
5164 if (!only_parity(rdev->raid_disk,
5165 conf->algorithm,
5166 conf->raid_disks,
5167 conf->max_degraded))
5168 continue;
5169 }
5170 if (!only_parity(rdev->raid_disk,
5171 conf->prev_algo,
5172 conf->previous_raid_disks,
5173 conf->max_degraded))
5174 continue;
5175 dirty_parity_disks++;
5176 }
NeilBrown91adb562009-03-31 14:39:39 +11005177
NeilBrown17045f52011-12-23 10:17:53 +11005178 /*
5179 * 0 for a fully functional array, 1 or 2 for a degraded array.
5180 */
NeilBrown908f4fb2011-12-23 10:17:50 +11005181 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182
NeilBrown674806d2010-06-16 17:17:53 +10005183 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005184 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07005186 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 goto abort;
5188 }
5189
NeilBrown91adb562009-03-31 14:39:39 +11005190 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10005191 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11005192 mddev->resync_max_sectors = mddev->dev_sectors;
5193
NeilBrownc148ffd2009-11-13 17:47:00 +11005194 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005196 if (mddev->ok_start_degraded)
5197 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10005198 "md/raid:%s: starting dirty degraded array"
5199 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005200 mdname(mddev));
5201 else {
5202 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005203 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005204 mdname(mddev));
5205 goto abort;
5206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 }
5208
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10005210 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5211 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11005212 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5213 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 else
NeilBrown0c55e022010-05-03 14:09:02 +10005215 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5216 " out of %d devices, algorithm %d\n",
5217 mdname(mddev), conf->level,
5218 mddev->raid_disks - mddev->degraded,
5219 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220
5221 print_raid5_conf(conf);
5222
NeilBrownfef9c612009-03-31 15:16:46 +11005223 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11005224 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005225 atomic_set(&conf->reshape_stripes, 0);
5226 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5227 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5228 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5229 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5230 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005231 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005232 }
5233
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234
5235 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005236 if (mddev->to_remove == &raid5_attrs_group)
5237 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005238 else if (mddev->kobj.sd &&
5239 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005240 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10005241 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08005242 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10005243 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5244
5245 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005246 int chunk_size;
NeilBrown4a5add42010-06-01 19:37:28 +10005247 /* read-ahead size must cover two whole stripes, which
5248 * is 2 * (datadisks) * chunksize where 'n' is the
5249 * number of raid devices
5250 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5252 int stripe = data_disks *
5253 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5254 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5255 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10005256
5257 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005258
5259 mddev->queue->backing_dev_info.congested_data = mddev;
5260 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10005261
5262 chunk_size = mddev->chunk_sectors << 9;
5263 blk_queue_io_min(mddev->queue, chunk_size);
5264 blk_queue_io_opt(mddev->queue, chunk_size *
5265 (conf->raid_disks - conf->max_degraded));
5266
NeilBrown05616be2012-05-21 09:27:00 +10005267 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005268 disk_stack_limits(mddev->gendisk, rdev->bdev,
5269 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10005270 disk_stack_limits(mddev->gendisk, rdev->bdev,
5271 rdev->new_data_offset << 9);
5272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 }
5274
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275 return 0;
5276abort:
NeilBrown01f96c02011-09-21 15:30:20 +10005277 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11005278 print_raid5_conf(conf);
5279 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005281 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 return -EIO;
5283}
5284
NeilBrownfd01b882011-10-11 16:47:53 +11005285static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286{
NeilBrownd1688a62011-10-11 16:49:52 +11005287 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288
NeilBrown01f96c02011-09-21 15:30:20 +10005289 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005290 if (mddev->queue)
5291 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10005292 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005293 mddev->private = NULL;
5294 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 return 0;
5296}
5297
NeilBrownfd01b882011-10-11 16:47:53 +11005298static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299{
NeilBrownd1688a62011-10-11 16:49:52 +11005300 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 int i;
5302
Andre Noll9d8f0362009-06-18 08:45:01 +10005303 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5304 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005305 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306 for (i = 0; i < conf->raid_disks; i++)
5307 seq_printf (seq, "%s",
5308 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005309 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311}
5312
NeilBrownd1688a62011-10-11 16:49:52 +11005313static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314{
5315 int i;
5316 struct disk_info *tmp;
5317
NeilBrown0c55e022010-05-03 14:09:02 +10005318 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 if (!conf) {
5320 printk("(conf==NULL)\n");
5321 return;
5322 }
NeilBrown0c55e022010-05-03 14:09:02 +10005323 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5324 conf->raid_disks,
5325 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326
5327 for (i = 0; i < conf->raid_disks; i++) {
5328 char b[BDEVNAME_SIZE];
5329 tmp = conf->disks + i;
5330 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005331 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5332 i, !test_bit(Faulty, &tmp->rdev->flags),
5333 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334 }
5335}
5336
NeilBrownfd01b882011-10-11 16:47:53 +11005337static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338{
5339 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005340 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005342 int count = 0;
5343 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344
5345 for (i = 0; i < conf->raid_disks; i++) {
5346 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11005347 if (tmp->replacement
5348 && tmp->replacement->recovery_offset == MaxSector
5349 && !test_bit(Faulty, &tmp->replacement->flags)
5350 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
5351 /* Replacement has just become active. */
5352 if (!tmp->rdev
5353 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
5354 count++;
5355 if (tmp->rdev) {
5356 /* Replaced device not technically faulty,
5357 * but we need to be sure it gets removed
5358 * and never re-added.
5359 */
5360 set_bit(Faulty, &tmp->rdev->flags);
5361 sysfs_notify_dirent_safe(
5362 tmp->rdev->sysfs_state);
5363 }
5364 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
5365 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005366 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005367 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005368 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005369 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005370 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 }
5372 }
NeilBrown6b965622010-08-18 11:56:59 +10005373 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005374 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005375 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005377 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378}
5379
NeilBrownb8321b62011-12-23 10:17:51 +11005380static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381{
NeilBrownd1688a62011-10-11 16:49:52 +11005382 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11005384 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11005385 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 struct disk_info *p = conf->disks + number;
5387
5388 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11005389 if (rdev == p->rdev)
5390 rdevp = &p->rdev;
5391 else if (rdev == p->replacement)
5392 rdevp = &p->replacement;
5393 else
5394 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11005395
NeilBrown657e3e42011-12-23 10:17:52 +11005396 if (number >= conf->raid_disks &&
5397 conf->reshape_progress == MaxSector)
5398 clear_bit(In_sync, &rdev->flags);
5399
5400 if (test_bit(In_sync, &rdev->flags) ||
5401 atomic_read(&rdev->nr_pending)) {
5402 err = -EBUSY;
5403 goto abort;
5404 }
5405 /* Only remove non-faulty devices if recovery
5406 * isn't possible.
5407 */
5408 if (!test_bit(Faulty, &rdev->flags) &&
5409 mddev->recovery_disabled != conf->recovery_disabled &&
5410 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11005411 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11005412 number < conf->raid_disks) {
5413 err = -EBUSY;
5414 goto abort;
5415 }
5416 *rdevp = NULL;
5417 synchronize_rcu();
5418 if (atomic_read(&rdev->nr_pending)) {
5419 /* lost the race, try later */
5420 err = -EBUSY;
5421 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11005422 } else if (p->replacement) {
5423 /* We must have just cleared 'rdev' */
5424 p->rdev = p->replacement;
5425 clear_bit(Replacement, &p->replacement->flags);
5426 smp_mb(); /* Make sure other CPUs may see both as identical
5427 * but will never see neither - if they are careful
5428 */
5429 p->replacement = NULL;
5430 clear_bit(WantReplacement, &rdev->flags);
5431 } else
5432 /* We might have just removed the Replacement as faulty-
5433 * clear the bit just in case
5434 */
5435 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436abort:
5437
5438 print_raid5_conf(conf);
5439 return err;
5440}
5441
NeilBrownfd01b882011-10-11 16:47:53 +11005442static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443{
NeilBrownd1688a62011-10-11 16:49:52 +11005444 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005445 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446 int disk;
5447 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005448 int first = 0;
5449 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450
NeilBrown7f0da592011-07-28 11:39:22 +10005451 if (mddev->recovery_disabled == conf->recovery_disabled)
5452 return -EBUSY;
5453
NeilBrowndc10c642012-03-19 12:46:37 +11005454 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005456 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457
Neil Brown6c2fce22008-06-28 08:31:31 +10005458 if (rdev->raid_disk >= 0)
5459 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460
5461 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005462 * find the disk ... but prefer rdev->saved_raid_disk
5463 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005465 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005466 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005467 conf->disks[rdev->saved_raid_disk].rdev == NULL)
5468 disk = rdev->saved_raid_disk;
5469 else
Neil Brown6c2fce22008-06-28 08:31:31 +10005470 disk = first;
NeilBrown7bfec5f2011-12-23 10:17:53 +11005471 for ( ; disk <= last ; disk++) {
5472 p = conf->disks + disk;
5473 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005474 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005476 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005477 if (rdev->saved_raid_disk != disk)
5478 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005479 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 break;
5481 }
NeilBrown7bfec5f2011-12-23 10:17:53 +11005482 if (test_bit(WantReplacement, &p->rdev->flags) &&
5483 p->replacement == NULL) {
5484 clear_bit(In_sync, &rdev->flags);
5485 set_bit(Replacement, &rdev->flags);
5486 rdev->raid_disk = disk;
5487 err = 0;
5488 conf->fullsync = 1;
5489 rcu_assign_pointer(p->replacement, rdev);
5490 break;
5491 }
5492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005494 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495}
5496
NeilBrownfd01b882011-10-11 16:47:53 +11005497static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498{
5499 /* no resync is happening, and there is enough space
5500 * on all devices, so we can resize.
5501 * We need to make sure resync covers any new space.
5502 * If the array is shrinking we should possibly wait until
5503 * any io in the removed space completes, but it hardly seems
5504 * worth it.
5505 */
NeilBrowna4a61252012-05-22 13:55:27 +10005506 sector_t newsize;
Andre Noll9d8f0362009-06-18 08:45:01 +10005507 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10005508 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
5509 if (mddev->external_size &&
5510 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11005511 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10005512 if (mddev->bitmap) {
5513 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
5514 if (ret)
5515 return ret;
5516 }
5517 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10005518 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005519 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005520 if (sectors > mddev->dev_sectors &&
5521 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005522 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5524 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005525 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005526 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 return 0;
5528}
5529
NeilBrownfd01b882011-10-11 16:47:53 +11005530static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005531{
5532 /* Can only proceed if there are plenty of stripe_heads.
5533 * We need a minimum of one full stripe,, and for sensible progress
5534 * it is best to have about 4 times that.
5535 * If we require 4 times, then the default 256 4K stripe_heads will
5536 * allow for chunk sizes up to 256K, which is probably OK.
5537 * If the chunk size is greater, user-space should request more
5538 * stripe_heads first.
5539 */
NeilBrownd1688a62011-10-11 16:49:52 +11005540 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005541 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5542 > conf->max_nr_stripes ||
5543 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5544 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005545 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5546 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005547 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5548 / STRIPE_SIZE)*4);
5549 return 0;
5550 }
5551 return 1;
5552}
5553
NeilBrownfd01b882011-10-11 16:47:53 +11005554static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005555{
NeilBrownd1688a62011-10-11 16:49:52 +11005556 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005557
NeilBrown88ce4932009-03-31 15:24:23 +11005558 if (mddev->delta_disks == 0 &&
5559 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005560 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005561 return 0; /* nothing to do */
NeilBrowndba034e2008-08-05 15:54:13 +10005562 if (mddev->bitmap)
5563 /* Cannot grow a bitmap yet */
5564 return -EBUSY;
NeilBrown674806d2010-06-16 17:17:53 +10005565 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005566 return -EINVAL;
5567 if (mddev->delta_disks < 0) {
5568 /* We might be able to shrink, but the devices must
5569 * be made bigger first.
5570 * For raid6, 4 is the minimum size.
5571 * Otherwise 2 is the minimum
5572 */
5573 int min = 2;
5574 if (mddev->level == 6)
5575 min = 4;
5576 if (mddev->raid_disks + mddev->delta_disks < min)
5577 return -EINVAL;
5578 }
NeilBrown29269552006-03-27 01:18:10 -08005579
NeilBrown01ee22b2009-06-18 08:47:20 +10005580 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005581 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005582
NeilBrownec32a2b2009-03-31 15:17:38 +11005583 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
NeilBrown63c70c42006-03-27 01:18:13 -08005584}
5585
NeilBrownfd01b882011-10-11 16:47:53 +11005586static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005587{
NeilBrownd1688a62011-10-11 16:49:52 +11005588 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005589 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005590 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005591 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005592
NeilBrownf4168852007-02-28 20:11:53 -08005593 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005594 return -EBUSY;
5595
NeilBrown01ee22b2009-06-18 08:47:20 +10005596 if (!check_stripe_cache(mddev))
5597 return -ENOSPC;
5598
NeilBrownc6563a82012-05-21 09:27:00 +10005599 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11005600 if (!test_bit(In_sync, &rdev->flags)
5601 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005602 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10005603 }
NeilBrown63c70c42006-03-27 01:18:13 -08005604
NeilBrownf4168852007-02-28 20:11:53 -08005605 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005606 /* Not enough devices even to make a degraded array
5607 * of that size
5608 */
5609 return -EINVAL;
5610
NeilBrownec32a2b2009-03-31 15:17:38 +11005611 /* Refuse to reduce size of the array. Any reductions in
5612 * array size must be through explicit setting of array_size
5613 * attribute.
5614 */
5615 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5616 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10005617 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11005618 "before number of disks\n", mdname(mddev));
5619 return -EINVAL;
5620 }
5621
NeilBrownf6705572006-03-27 01:18:11 -08005622 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005623 spin_lock_irq(&conf->device_lock);
5624 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005625 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005626 conf->prev_chunk_sectors = conf->chunk_sectors;
5627 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005628 conf->prev_algo = conf->algorithm;
5629 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10005630 conf->generation++;
5631 /* Code that selects data_offset needs to see the generation update
5632 * if reshape_progress has been set - so a memory barrier needed.
5633 */
5634 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10005635 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11005636 conf->reshape_progress = raid5_size(mddev, 0, 0);
5637 else
5638 conf->reshape_progress = 0;
5639 conf->reshape_safe = conf->reshape_progress;
NeilBrown29269552006-03-27 01:18:10 -08005640 spin_unlock_irq(&conf->device_lock);
5641
5642 /* Add some new drives, as many as will fit.
5643 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10005644 * Don't add devices if we are reducing the number of
5645 * devices in the array. This is because it is not possible
5646 * to correctly record the "partially reconstructed" state of
5647 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08005648 */
NeilBrown87a8dec2011-01-31 11:57:43 +11005649 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11005650 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11005651 if (rdev->raid_disk < 0 &&
5652 !test_bit(Faulty, &rdev->flags)) {
5653 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11005654 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11005655 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11005656 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11005657 else
NeilBrown87a8dec2011-01-31 11:57:43 +11005658 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10005659
5660 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11005661 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11005662 }
NeilBrown87a8dec2011-01-31 11:57:43 +11005663 } else if (rdev->raid_disk >= conf->previous_raid_disks
5664 && !test_bit(Faulty, &rdev->flags)) {
5665 /* This is a spare that was manually added */
5666 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11005667 }
NeilBrown29269552006-03-27 01:18:10 -08005668
NeilBrown87a8dec2011-01-31 11:57:43 +11005669 /* When a reshape changes the number of devices,
5670 * ->degraded is measured against the larger of the
5671 * pre and post number of devices.
5672 */
NeilBrownec32a2b2009-03-31 15:17:38 +11005673 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005674 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11005675 spin_unlock_irqrestore(&conf->device_lock, flags);
5676 }
NeilBrown63c70c42006-03-27 01:18:13 -08005677 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10005678 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b422006-10-03 01:15:46 -07005679 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005680
NeilBrown29269552006-03-27 01:18:10 -08005681 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5682 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5683 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5684 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5685 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005686 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08005687 if (!mddev->sync_thread) {
5688 mddev->recovery = 0;
5689 spin_lock_irq(&conf->device_lock);
5690 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10005691 rdev_for_each(rdev, mddev)
5692 rdev->new_data_offset = rdev->data_offset;
5693 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11005694 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11005695 mddev->reshape_position = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005696 spin_unlock_irq(&conf->device_lock);
5697 return -EAGAIN;
5698 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005699 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005700 md_wakeup_thread(mddev->sync_thread);
5701 md_new_event(mddev);
5702 return 0;
5703}
NeilBrown29269552006-03-27 01:18:10 -08005704
NeilBrownec32a2b2009-03-31 15:17:38 +11005705/* This is called from the reshape thread and should make any
5706 * changes needed in 'conf'
5707 */
NeilBrownd1688a62011-10-11 16:49:52 +11005708static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08005709{
NeilBrown29269552006-03-27 01:18:10 -08005710
NeilBrownf6705572006-03-27 01:18:11 -08005711 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10005712 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07005713
NeilBrownf6705572006-03-27 01:18:11 -08005714 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11005715 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10005716 rdev_for_each(rdev, conf->mddev)
5717 rdev->data_offset = rdev->new_data_offset;
5718 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11005719 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08005720 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005721 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07005722
5723 /* read-ahead size must cover two whole stripes, which is
5724 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5725 */
NeilBrown4a5add42010-06-01 19:37:28 +10005726 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11005727 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005728 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11005729 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07005730 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5731 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5732 }
NeilBrown29269552006-03-27 01:18:10 -08005733 }
NeilBrown29269552006-03-27 01:18:10 -08005734}
5735
NeilBrownec32a2b2009-03-31 15:17:38 +11005736/* This is called from the raid5d thread with mddev_lock held.
5737 * It makes config changes to the device.
5738 */
NeilBrownfd01b882011-10-11 16:47:53 +11005739static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11005740{
NeilBrownd1688a62011-10-11 16:49:52 +11005741 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11005742
5743 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5744
NeilBrownec32a2b2009-03-31 15:17:38 +11005745 if (mddev->delta_disks > 0) {
5746 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5747 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005748 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11005749 } else {
5750 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11005751 spin_lock_irq(&conf->device_lock);
5752 mddev->degraded = calc_degraded(conf);
5753 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11005754 for (d = conf->raid_disks ;
5755 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10005756 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11005757 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownb8321b62011-12-23 10:17:51 +11005758 if (rdev &&
5759 raid5_remove_disk(mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10005760 sysfs_unlink_rdev(mddev, rdev);
NeilBrown1a67dde2009-08-13 10:41:49 +10005761 rdev->raid_disk = -1;
5762 }
5763 }
NeilBrowncea9c222009-03-31 15:15:05 +11005764 }
NeilBrown88ce4932009-03-31 15:24:23 +11005765 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005766 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11005767 mddev->reshape_position = MaxSector;
5768 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005769 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11005770 }
5771}
5772
NeilBrownfd01b882011-10-11 16:47:53 +11005773static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07005774{
NeilBrownd1688a62011-10-11 16:49:52 +11005775 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07005776
5777 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08005778 case 2: /* resume for a suspend */
5779 wake_up(&conf->wait_for_overlap);
5780 break;
5781
NeilBrown72626682005-09-09 16:23:54 -07005782 case 1: /* stop all writes */
5783 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005784 /* '2' tells resync/reshape to pause so that all
5785 * active stripes can drain
5786 */
5787 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07005788 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005789 atomic_read(&conf->active_stripes) == 0 &&
5790 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07005791 conf->device_lock, /* nothing */);
NeilBrown64bd6602009-08-03 10:59:58 +10005792 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07005793 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005794 /* allow reshape to continue */
5795 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005796 break;
5797
5798 case 0: /* re-enable writes */
5799 spin_lock_irq(&conf->device_lock);
5800 conf->quiesce = 0;
5801 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08005802 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005803 spin_unlock_irq(&conf->device_lock);
5804 break;
5805 }
NeilBrown72626682005-09-09 16:23:54 -07005806}
NeilBrownb15c2e52006-01-06 00:20:16 -08005807
NeilBrownd562b0c2009-03-31 14:39:39 +11005808
NeilBrownfd01b882011-10-11 16:47:53 +11005809static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11005810{
NeilBrowne373ab12011-10-11 16:48:59 +11005811 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07005812 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11005813
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005814 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11005815 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10005816 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
5817 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005818 return ERR_PTR(-EINVAL);
5819 }
5820
NeilBrowne373ab12011-10-11 16:48:59 +11005821 sectors = raid0_conf->strip_zone[0].zone_end;
5822 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10005823 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005824 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11005825 mddev->new_layout = ALGORITHM_PARITY_N;
5826 mddev->new_chunk_sectors = mddev->chunk_sectors;
5827 mddev->raid_disks += 1;
5828 mddev->delta_disks = 1;
5829 /* make sure it will be not marked as dirty */
5830 mddev->recovery_cp = MaxSector;
5831
5832 return setup_conf(mddev);
5833}
5834
5835
NeilBrownfd01b882011-10-11 16:47:53 +11005836static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005837{
5838 int chunksect;
5839
5840 if (mddev->raid_disks != 2 ||
5841 mddev->degraded > 1)
5842 return ERR_PTR(-EINVAL);
5843
5844 /* Should check if there are write-behind devices? */
5845
5846 chunksect = 64*2; /* 64K by default */
5847
5848 /* The array must be an exact multiple of chunksize */
5849 while (chunksect && (mddev->array_sectors & (chunksect-1)))
5850 chunksect >>= 1;
5851
5852 if ((chunksect<<9) < STRIPE_SIZE)
5853 /* array size does not allow a suitable chunk size */
5854 return ERR_PTR(-EINVAL);
5855
5856 mddev->new_level = 5;
5857 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10005858 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11005859
5860 return setup_conf(mddev);
5861}
5862
NeilBrownfd01b882011-10-11 16:47:53 +11005863static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11005864{
5865 int new_layout;
5866
5867 switch (mddev->layout) {
5868 case ALGORITHM_LEFT_ASYMMETRIC_6:
5869 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
5870 break;
5871 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5872 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
5873 break;
5874 case ALGORITHM_LEFT_SYMMETRIC_6:
5875 new_layout = ALGORITHM_LEFT_SYMMETRIC;
5876 break;
5877 case ALGORITHM_RIGHT_SYMMETRIC_6:
5878 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
5879 break;
5880 case ALGORITHM_PARITY_0_6:
5881 new_layout = ALGORITHM_PARITY_0;
5882 break;
5883 case ALGORITHM_PARITY_N:
5884 new_layout = ALGORITHM_PARITY_N;
5885 break;
5886 default:
5887 return ERR_PTR(-EINVAL);
5888 }
5889 mddev->new_level = 5;
5890 mddev->new_layout = new_layout;
5891 mddev->delta_disks = -1;
5892 mddev->raid_disks -= 1;
5893 return setup_conf(mddev);
5894}
5895
NeilBrownd562b0c2009-03-31 14:39:39 +11005896
NeilBrownfd01b882011-10-11 16:47:53 +11005897static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11005898{
NeilBrown88ce4932009-03-31 15:24:23 +11005899 /* For a 2-drive array, the layout and chunk size can be changed
5900 * immediately as not restriping is needed.
5901 * For larger arrays we record the new value - after validation
5902 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11005903 */
NeilBrownd1688a62011-10-11 16:49:52 +11005904 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10005905 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11005906
NeilBrown597a7112009-06-18 08:47:42 +10005907 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11005908 return -EINVAL;
5909 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005910 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11005911 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005912 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11005913 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005914 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11005915 /* not factor of array size */
5916 return -EINVAL;
5917 }
5918
5919 /* They look valid */
5920
NeilBrown88ce4932009-03-31 15:24:23 +11005921 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10005922 /* can make the change immediately */
5923 if (mddev->new_layout >= 0) {
5924 conf->algorithm = mddev->new_layout;
5925 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11005926 }
5927 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10005928 conf->chunk_sectors = new_chunk ;
5929 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11005930 }
5931 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5932 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11005933 }
NeilBrown50ac1682009-06-18 08:47:55 +10005934 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11005935}
5936
NeilBrownfd01b882011-10-11 16:47:53 +11005937static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11005938{
NeilBrown597a7112009-06-18 08:47:42 +10005939 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10005940
NeilBrown597a7112009-06-18 08:47:42 +10005941 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11005942 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005943 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005944 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11005945 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005946 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11005947 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005948 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11005949 /* not factor of array size */
5950 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005951 }
NeilBrown88ce4932009-03-31 15:24:23 +11005952
5953 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10005954 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11005955}
5956
NeilBrownfd01b882011-10-11 16:47:53 +11005957static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005958{
5959 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005960 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005961 * raid1 - if there are two drives. We need to know the chunk size
5962 * raid4 - trivial - just use a raid4 layout.
5963 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005964 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005965 if (mddev->level == 0)
5966 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11005967 if (mddev->level == 1)
5968 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11005969 if (mddev->level == 4) {
5970 mddev->new_layout = ALGORITHM_PARITY_N;
5971 mddev->new_level = 5;
5972 return setup_conf(mddev);
5973 }
NeilBrownfc9739c2009-03-31 14:57:20 +11005974 if (mddev->level == 6)
5975 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11005976
5977 return ERR_PTR(-EINVAL);
5978}
5979
NeilBrownfd01b882011-10-11 16:47:53 +11005980static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11005981{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005982 /* raid4 can take over:
5983 * raid0 - if there is only one strip zone
5984 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11005985 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005986 if (mddev->level == 0)
5987 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11005988 if (mddev->level == 5 &&
5989 mddev->layout == ALGORITHM_PARITY_N) {
5990 mddev->new_layout = 0;
5991 mddev->new_level = 4;
5992 return setup_conf(mddev);
5993 }
5994 return ERR_PTR(-EINVAL);
5995}
NeilBrownd562b0c2009-03-31 14:39:39 +11005996
NeilBrown84fc4b52011-10-11 16:49:58 +11005997static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11005998
NeilBrownfd01b882011-10-11 16:47:53 +11005999static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11006000{
6001 /* Currently can only take over a raid5. We map the
6002 * personality to an equivalent raid6 personality
6003 * with the Q block at the end.
6004 */
6005 int new_layout;
6006
6007 if (mddev->pers != &raid5_personality)
6008 return ERR_PTR(-EINVAL);
6009 if (mddev->degraded > 1)
6010 return ERR_PTR(-EINVAL);
6011 if (mddev->raid_disks > 253)
6012 return ERR_PTR(-EINVAL);
6013 if (mddev->raid_disks < 3)
6014 return ERR_PTR(-EINVAL);
6015
6016 switch (mddev->layout) {
6017 case ALGORITHM_LEFT_ASYMMETRIC:
6018 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
6019 break;
6020 case ALGORITHM_RIGHT_ASYMMETRIC:
6021 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
6022 break;
6023 case ALGORITHM_LEFT_SYMMETRIC:
6024 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
6025 break;
6026 case ALGORITHM_RIGHT_SYMMETRIC:
6027 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
6028 break;
6029 case ALGORITHM_PARITY_0:
6030 new_layout = ALGORITHM_PARITY_0_6;
6031 break;
6032 case ALGORITHM_PARITY_N:
6033 new_layout = ALGORITHM_PARITY_N;
6034 break;
6035 default:
6036 return ERR_PTR(-EINVAL);
6037 }
6038 mddev->new_level = 6;
6039 mddev->new_layout = new_layout;
6040 mddev->delta_disks = 1;
6041 mddev->raid_disks += 1;
6042 return setup_conf(mddev);
6043}
6044
6045
NeilBrown84fc4b52011-10-11 16:49:58 +11006046static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07006047{
6048 .name = "raid6",
6049 .level = 6,
6050 .owner = THIS_MODULE,
6051 .make_request = make_request,
6052 .run = run,
6053 .stop = stop,
6054 .status = status,
6055 .error_handler = error,
6056 .hot_add_disk = raid5_add_disk,
6057 .hot_remove_disk= raid5_remove_disk,
6058 .spare_active = raid5_spare_active,
6059 .sync_request = sync_request,
6060 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006061 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10006062 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08006063 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006064 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07006065 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11006066 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07006067};
NeilBrown84fc4b52011-10-11 16:49:58 +11006068static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069{
6070 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08006071 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 .owner = THIS_MODULE,
6073 .make_request = make_request,
6074 .run = run,
6075 .stop = stop,
6076 .status = status,
6077 .error_handler = error,
6078 .hot_add_disk = raid5_add_disk,
6079 .hot_remove_disk= raid5_remove_disk,
6080 .spare_active = raid5_spare_active,
6081 .sync_request = sync_request,
6082 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006083 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08006084 .check_reshape = raid5_check_reshape,
6085 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006086 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07006087 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11006088 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089};
6090
NeilBrown84fc4b52011-10-11 16:49:58 +11006091static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092{
NeilBrown2604b702006-01-06 00:20:36 -08006093 .name = "raid4",
6094 .level = 4,
6095 .owner = THIS_MODULE,
6096 .make_request = make_request,
6097 .run = run,
6098 .stop = stop,
6099 .status = status,
6100 .error_handler = error,
6101 .hot_add_disk = raid5_add_disk,
6102 .hot_remove_disk= raid5_remove_disk,
6103 .spare_active = raid5_spare_active,
6104 .sync_request = sync_request,
6105 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006106 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08006107 .check_reshape = raid5_check_reshape,
6108 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006109 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08006110 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11006111 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08006112};
6113
6114static int __init raid5_init(void)
6115{
NeilBrown16a53ec2006-06-26 00:27:38 -07006116 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006117 register_md_personality(&raid5_personality);
6118 register_md_personality(&raid4_personality);
6119 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120}
6121
NeilBrown2604b702006-01-06 00:20:36 -08006122static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123{
NeilBrown16a53ec2006-06-26 00:27:38 -07006124 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006125 unregister_md_personality(&raid5_personality);
6126 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127}
6128
6129module_init(raid5_init);
6130module_exit(raid5_exit);
6131MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11006132MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08006134MODULE_ALIAS("md-raid5");
6135MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08006136MODULE_ALIAS("md-level-5");
6137MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07006138MODULE_ALIAS("md-personality-8"); /* RAID6 */
6139MODULE_ALIAS("md-raid6");
6140MODULE_ALIAS("md-level-6");
6141
6142/* This used to be two separate modules, they were: */
6143MODULE_ALIAS("raid5");
6144MODULE_ALIAS("raid6");