blob: 3f55145ff224105ad5780ee2a68247766d4e2e8a [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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
212 atomic_dec(&conf->preread_active_stripes);
213 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
214 md_wakeup_thread(conf->mddev->thread);
215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 atomic_dec(&conf->active_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800217 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
218 list_add_tail(&sh->lru, &conf->inactive_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 wake_up(&conf->wait_for_stripe);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -0800220 if (conf->retry_read_aligned)
221 md_wakeup_thread(conf->mddev->thread);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
224 }
225}
NeilBrownd0dabf72009-03-31 14:39:38 +1100226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227static void release_stripe(struct stripe_head *sh)
228{
NeilBrownd1688a62011-10-11 16:49:52 +1100229 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 unsigned long flags;
NeilBrown16a53ec2006-06-26 00:27:38 -0700231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 spin_lock_irqsave(&conf->device_lock, flags);
233 __release_stripe(conf, sh);
234 spin_unlock_irqrestore(&conf->device_lock, flags);
235}
236
NeilBrownfccddba2006-01-06 00:20:33 -0800237static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Dan Williams45b42332007-07-09 11:56:43 -0700239 pr_debug("remove_hash(), stripe %llu\n",
240 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
NeilBrownfccddba2006-01-06 00:20:33 -0800242 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
NeilBrownd1688a62011-10-11 16:49:52 +1100245static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
NeilBrownfccddba2006-01-06 00:20:33 -0800247 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Dan Williams45b42332007-07-09 11:56:43 -0700249 pr_debug("insert_hash(), stripe %llu\n",
250 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
NeilBrownfccddba2006-01-06 00:20:33 -0800252 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
255
256/* find an idle stripe, make sure it is unhashed, and return it. */
NeilBrownd1688a62011-10-11 16:49:52 +1100257static struct stripe_head *get_free_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 struct stripe_head *sh = NULL;
260 struct list_head *first;
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (list_empty(&conf->inactive_list))
263 goto out;
264 first = conf->inactive_list.next;
265 sh = list_entry(first, struct stripe_head, lru);
266 list_del_init(first);
267 remove_hash(sh);
268 atomic_inc(&conf->active_stripes);
269out:
270 return sh;
271}
272
NeilBrowne4e11e32010-06-16 16:45:16 +1000273static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 struct page *p;
276 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000277 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
NeilBrowne4e11e32010-06-16 16:45:16 +1000279 for (i = 0; i < num ; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 p = sh->dev[i].page;
281 if (!p)
282 continue;
283 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800284 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286}
287
NeilBrowne4e11e32010-06-16 16:45:16 +1000288static int grow_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000291 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
NeilBrowne4e11e32010-06-16 16:45:16 +1000293 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 struct page *page;
295
296 if (!(page = alloc_page(GFP_KERNEL))) {
297 return 1;
298 }
299 sh->dev[i].page = page;
300 }
301 return 0;
302}
303
NeilBrown784052e2009-03-31 15:19:07 +1100304static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrownd1688a62011-10-11 16:49:52 +1100305static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100306 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
NeilBrownb5663ba2009-03-31 14:39:38 +1100308static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
NeilBrownd1688a62011-10-11 16:49:52 +1100310 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800311 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200313 BUG_ON(atomic_read(&sh->count) != 0);
314 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000315 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700316
Dan Williams45b42332007-07-09 11:56:43 -0700317 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 (unsigned long long)sh->sector);
319
320 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700321
NeilBrown86b42c72009-03-31 15:19:03 +1100322 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100323 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100325 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 sh->state = 0;
327
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800328
329 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 struct r5dev *dev = &sh->dev[i];
331
Dan Williamsd84e0f12007-01-02 13:52:30 -0700332 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700334 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700336 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000338 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100341 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343 insert_hash(conf, sh);
344}
345
NeilBrownd1688a62011-10-11 16:49:52 +1100346static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100347 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -0800350 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Dan Williams45b42332007-07-09 11:56:43 -0700352 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
NeilBrownfccddba2006-01-06 00:20:33 -0800353 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100354 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700356 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return NULL;
358}
359
NeilBrown674806d2010-06-16 17:17:53 +1000360/*
361 * Need to check if array has failed when deciding whether to:
362 * - start an array
363 * - remove non-faulty devices
364 * - add a spare
365 * - allow a reshape
366 * This determination is simple when no reshape is happening.
367 * However if there is a reshape, we need to carefully check
368 * both the before and after sections.
369 * This is because some failed devices may only affect one
370 * of the two sections, and some non-in_sync devices may
371 * be insync in the section most affected by failed devices.
372 */
NeilBrown908f4fb2011-12-23 10:17:50 +1100373static int calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000374{
NeilBrown908f4fb2011-12-23 10:17:50 +1100375 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000376 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000377
378 rcu_read_lock();
379 degraded = 0;
380 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100381 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown674806d2010-06-16 17:17:53 +1000382 if (!rdev || test_bit(Faulty, &rdev->flags))
383 degraded++;
384 else if (test_bit(In_sync, &rdev->flags))
385 ;
386 else
387 /* not in-sync or faulty.
388 * If the reshape increases the number of devices,
389 * this is being recovered by the reshape, so
390 * this 'previous' section is not in_sync.
391 * If the number of devices is being reduced however,
392 * the device can only be part of the array if
393 * we are reverting a reshape, so this section will
394 * be in-sync.
395 */
396 if (conf->raid_disks >= conf->previous_raid_disks)
397 degraded++;
398 }
399 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100400 if (conf->raid_disks == conf->previous_raid_disks)
401 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000402 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100403 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000404 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100405 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown674806d2010-06-16 17:17:53 +1000406 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100407 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000408 else if (test_bit(In_sync, &rdev->flags))
409 ;
410 else
411 /* not in-sync or faulty.
412 * If reshape increases the number of devices, this
413 * section has already been recovered, else it
414 * almost certainly hasn't.
415 */
416 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100417 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000418 }
419 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100420 if (degraded2 > degraded)
421 return degraded2;
422 return degraded;
423}
424
425static int has_failed(struct r5conf *conf)
426{
427 int degraded;
428
429 if (conf->mddev->reshape_position == MaxSector)
430 return conf->mddev->degraded > conf->max_degraded;
431
432 degraded = calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000433 if (degraded > conf->max_degraded)
434 return 1;
435 return 0;
436}
437
NeilBrownb5663ba2009-03-31 14:39:38 +1100438static struct stripe_head *
NeilBrownd1688a62011-10-11 16:49:52 +1100439get_active_stripe(struct r5conf *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000440 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
442 struct stripe_head *sh;
443
Dan Williams45b42332007-07-09 11:56:43 -0700444 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 spin_lock_irq(&conf->device_lock);
447
448 do {
NeilBrown72626682005-09-09 16:23:54 -0700449 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000450 conf->quiesce == 0 || noquiesce,
NeilBrown72626682005-09-09 16:23:54 -0700451 conf->device_lock, /* nothing */);
NeilBrown86b42c72009-03-31 15:19:03 +1100452 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (!sh) {
454 if (!conf->inactive_blocked)
455 sh = get_free_stripe(conf);
456 if (noblock && sh == NULL)
457 break;
458 if (!sh) {
459 conf->inactive_blocked = 1;
460 wait_event_lock_irq(conf->wait_for_stripe,
461 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800462 (atomic_read(&conf->active_stripes)
463 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 || !conf->inactive_blocked),
465 conf->device_lock,
NeilBrown7c13edc2011-04-18 18:25:43 +1000466 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 conf->inactive_blocked = 0;
468 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100469 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 } else {
471 if (atomic_read(&sh->count)) {
NeilBrownab69ae12009-03-31 15:26:47 +1100472 BUG_ON(!list_empty(&sh->lru)
473 && !test_bit(STRIPE_EXPANDING, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 } else {
475 if (!test_bit(STRIPE_HANDLE, &sh->state))
476 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700477 if (list_empty(&sh->lru) &&
478 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700479 BUG();
480 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
482 }
483 } while (sh == NULL);
484
485 if (sh)
486 atomic_inc(&sh->count);
487
488 spin_unlock_irq(&conf->device_lock);
489 return sh;
490}
491
NeilBrown6712ecf2007-09-27 12:47:43 +0200492static void
493raid5_end_read_request(struct bio *bi, int error);
494static void
495raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700496
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000497static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700498{
NeilBrownd1688a62011-10-11 16:49:52 +1100499 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700500 int i, disks = sh->disks;
501
502 might_sleep();
503
504 for (i = disks; i--; ) {
505 int rw;
NeilBrown9a3e1102011-12-23 10:17:53 +1100506 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100507 struct bio *bi, *rbi;
508 struct md_rdev *rdev, *rrdev = NULL;
Tejun Heoe9c74692010-09-03 11:56:18 +0200509 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
510 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
511 rw = WRITE_FUA;
512 else
513 rw = WRITE;
514 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700515 rw = READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100516 else if (test_and_clear_bit(R5_WantReplace,
517 &sh->dev[i].flags)) {
518 rw = WRITE;
519 replace_only = 1;
520 } else
Dan Williams91c00922007-01-02 13:52:30 -0700521 continue;
522
523 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100524 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700525
526 bi->bi_rw = rw;
NeilBrown977df362011-12-23 10:17:53 +1100527 rbi->bi_rw = rw;
528 if (rw & WRITE) {
Dan Williams91c00922007-01-02 13:52:30 -0700529 bi->bi_end_io = raid5_end_write_request;
NeilBrown977df362011-12-23 10:17:53 +1100530 rbi->bi_end_io = raid5_end_write_request;
531 } else
Dan Williams91c00922007-01-02 13:52:30 -0700532 bi->bi_end_io = raid5_end_read_request;
533
534 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100535 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100536 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
537 rdev = rcu_dereference(conf->disks[i].rdev);
538 if (!rdev) {
539 rdev = rrdev;
540 rrdev = NULL;
541 }
NeilBrown9a3e1102011-12-23 10:17:53 +1100542 if (rw & WRITE) {
543 if (replace_only)
544 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100545 if (rdev == rrdev)
546 /* We raced and saw duplicates */
547 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100548 } else {
NeilBrowndd054fc2011-12-23 10:17:53 +1100549 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100550 rdev = rrdev;
551 rrdev = NULL;
552 }
NeilBrown977df362011-12-23 10:17:53 +1100553
Dan Williams91c00922007-01-02 13:52:30 -0700554 if (rdev && test_bit(Faulty, &rdev->flags))
555 rdev = NULL;
556 if (rdev)
557 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100558 if (rrdev && test_bit(Faulty, &rrdev->flags))
559 rrdev = NULL;
560 if (rrdev)
561 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700562 rcu_read_unlock();
563
NeilBrown73e92e52011-07-28 11:39:22 +1000564 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100565 * need to check for writes. We never accept write errors
566 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000567 */
568 while ((rw & WRITE) && rdev &&
569 test_bit(WriteErrorSeen, &rdev->flags)) {
570 sector_t first_bad;
571 int bad_sectors;
572 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
573 &first_bad, &bad_sectors);
574 if (!bad)
575 break;
576
577 if (bad < 0) {
578 set_bit(BlockedBadBlocks, &rdev->flags);
579 if (!conf->mddev->external &&
580 conf->mddev->flags) {
581 /* It is very unlikely, but we might
582 * still need to write out the
583 * bad block log - better give it
584 * a chance*/
585 md_check_recovery(conf->mddev);
586 }
587 md_wait_for_blocked_rdev(rdev, conf->mddev);
588 } else {
589 /* Acknowledged bad block - skip the write */
590 rdev_dec_pending(rdev, conf->mddev);
591 rdev = NULL;
592 }
593 }
594
Dan Williams91c00922007-01-02 13:52:30 -0700595 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100596 if (s->syncing || s->expanding || s->expanded
597 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700598 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
599
Dan Williams2b7497f2008-06-28 08:31:52 +1000600 set_bit(STRIPE_IO_STARTED, &sh->state);
601
Dan Williams91c00922007-01-02 13:52:30 -0700602 bi->bi_bdev = rdev->bdev;
603 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700604 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700605 bi->bi_rw, i);
606 atomic_inc(&sh->count);
607 bi->bi_sector = sh->sector + rdev->data_offset;
608 bi->bi_flags = 1 << BIO_UPTODATE;
Dan Williams91c00922007-01-02 13:52:30 -0700609 bi->bi_idx = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700610 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
611 bi->bi_io_vec[0].bv_offset = 0;
612 bi->bi_size = STRIPE_SIZE;
613 bi->bi_next = NULL;
NeilBrown977df362011-12-23 10:17:53 +1100614 if (rrdev)
615 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Dan Williams91c00922007-01-02 13:52:30 -0700616 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100617 }
618 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100619 if (s->syncing || s->expanding || s->expanded
620 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +1100621 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
622
623 set_bit(STRIPE_IO_STARTED, &sh->state);
624
625 rbi->bi_bdev = rrdev->bdev;
626 pr_debug("%s: for %llu schedule op %ld on "
627 "replacement disc %d\n",
628 __func__, (unsigned long long)sh->sector,
629 rbi->bi_rw, i);
630 atomic_inc(&sh->count);
631 rbi->bi_sector = sh->sector + rrdev->data_offset;
632 rbi->bi_flags = 1 << BIO_UPTODATE;
633 rbi->bi_idx = 0;
634 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
635 rbi->bi_io_vec[0].bv_offset = 0;
636 rbi->bi_size = STRIPE_SIZE;
637 rbi->bi_next = NULL;
638 generic_make_request(rbi);
639 }
640 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000641 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700642 set_bit(STRIPE_DEGRADED, &sh->state);
643 pr_debug("skip op %ld on disc %d for sector %llu\n",
644 bi->bi_rw, i, (unsigned long long)sh->sector);
645 clear_bit(R5_LOCKED, &sh->dev[i].flags);
646 set_bit(STRIPE_HANDLE, &sh->state);
647 }
648 }
649}
650
651static struct dma_async_tx_descriptor *
652async_copy_data(int frombio, struct bio *bio, struct page *page,
653 sector_t sector, struct dma_async_tx_descriptor *tx)
654{
655 struct bio_vec *bvl;
656 struct page *bio_page;
657 int i;
658 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700659 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700660 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700661
662 if (bio->bi_sector >= sector)
663 page_offset = (signed)(bio->bi_sector - sector) * 512;
664 else
665 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700666
Dan Williams0403e382009-09-08 17:42:50 -0700667 if (frombio)
668 flags |= ASYNC_TX_FENCE;
669 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
670
Dan Williams91c00922007-01-02 13:52:30 -0700671 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000672 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700673 int clen;
674 int b_offset = 0;
675
676 if (page_offset < 0) {
677 b_offset = -page_offset;
678 page_offset += b_offset;
679 len -= b_offset;
680 }
681
682 if (len > 0 && page_offset + len > STRIPE_SIZE)
683 clen = STRIPE_SIZE - page_offset;
684 else
685 clen = len;
686
687 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000688 b_offset += bvl->bv_offset;
689 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700690 if (frombio)
691 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700692 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700693 else
694 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700695 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700696 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700697 /* chain the operations */
698 submit.depend_tx = tx;
699
Dan Williams91c00922007-01-02 13:52:30 -0700700 if (clen < len) /* hit end of page */
701 break;
702 page_offset += len;
703 }
704
705 return tx;
706}
707
708static void ops_complete_biofill(void *stripe_head_ref)
709{
710 struct stripe_head *sh = stripe_head_ref;
711 struct bio *return_bi = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +1100712 struct r5conf *conf = sh->raid_conf;
Dan Williamse4d84902007-09-24 10:06:13 -0700713 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700714
Harvey Harrisone46b2722008-04-28 02:15:50 -0700715 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700716 (unsigned long long)sh->sector);
717
718 /* clear completed biofills */
Dan Williams83de75c2008-06-28 08:31:58 +1000719 spin_lock_irq(&conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700720 for (i = sh->disks; i--; ) {
721 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700722
723 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700724 /* and check if we need to reply to a read request,
725 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000726 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700727 */
728 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700729 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700730
Dan Williams91c00922007-01-02 13:52:30 -0700731 BUG_ON(!dev->read);
732 rbi = dev->read;
733 dev->read = NULL;
734 while (rbi && rbi->bi_sector <
735 dev->sector + STRIPE_SECTORS) {
736 rbi2 = r5_next_bio(rbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +0200737 if (!raid5_dec_bi_phys_segments(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700738 rbi->bi_next = return_bi;
739 return_bi = rbi;
740 }
Dan Williams91c00922007-01-02 13:52:30 -0700741 rbi = rbi2;
742 }
743 }
744 }
Dan Williams83de75c2008-06-28 08:31:58 +1000745 spin_unlock_irq(&conf->device_lock);
746 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700747
748 return_io(return_bi);
749
Dan Williamse4d84902007-09-24 10:06:13 -0700750 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700751 release_stripe(sh);
752}
753
754static void ops_run_biofill(struct stripe_head *sh)
755{
756 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +1100757 struct r5conf *conf = sh->raid_conf;
Dan Williamsa08abd82009-06-03 11:43:59 -0700758 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700759 int i;
760
Harvey Harrisone46b2722008-04-28 02:15:50 -0700761 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700762 (unsigned long long)sh->sector);
763
764 for (i = sh->disks; i--; ) {
765 struct r5dev *dev = &sh->dev[i];
766 if (test_bit(R5_Wantfill, &dev->flags)) {
767 struct bio *rbi;
768 spin_lock_irq(&conf->device_lock);
769 dev->read = rbi = dev->toread;
770 dev->toread = NULL;
771 spin_unlock_irq(&conf->device_lock);
772 while (rbi && rbi->bi_sector <
773 dev->sector + STRIPE_SECTORS) {
774 tx = async_copy_data(0, rbi, dev->page,
775 dev->sector, tx);
776 rbi = r5_next_bio(rbi, dev->sector);
777 }
778 }
779 }
780
781 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700782 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
783 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700784}
785
Dan Williams4e7d2c02009-08-29 19:13:11 -0700786static void mark_target_uptodate(struct stripe_head *sh, int target)
787{
788 struct r5dev *tgt;
789
790 if (target < 0)
791 return;
792
793 tgt = &sh->dev[target];
794 set_bit(R5_UPTODATE, &tgt->flags);
795 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
796 clear_bit(R5_Wantcompute, &tgt->flags);
797}
798
Dan Williamsac6b53b2009-07-14 13:40:19 -0700799static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700800{
801 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700802
Harvey Harrisone46b2722008-04-28 02:15:50 -0700803 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700804 (unsigned long long)sh->sector);
805
Dan Williamsac6b53b2009-07-14 13:40:19 -0700806 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700807 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700808 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700809
Dan Williamsecc65c92008-06-28 08:31:57 +1000810 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
811 if (sh->check_state == check_state_compute_run)
812 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700813 set_bit(STRIPE_HANDLE, &sh->state);
814 release_stripe(sh);
815}
816
Dan Williamsd6f38f32009-07-14 11:50:52 -0700817/* return a pointer to the address conversion region of the scribble buffer */
818static addr_conv_t *to_addr_conv(struct stripe_head *sh,
819 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700820{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700821 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
822}
823
824static struct dma_async_tx_descriptor *
825ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
826{
Dan Williams91c00922007-01-02 13:52:30 -0700827 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700828 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700829 int target = sh->ops.target;
830 struct r5dev *tgt = &sh->dev[target];
831 struct page *xor_dest = tgt->page;
832 int count = 0;
833 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700834 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700835 int i;
836
837 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700838 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700839 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
840
841 for (i = disks; i--; )
842 if (i != target)
843 xor_srcs[count++] = sh->dev[i].page;
844
845 atomic_inc(&sh->count);
846
Dan Williams0403e382009-09-08 17:42:50 -0700847 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700848 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700849 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700850 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700851 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700852 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700853
Dan Williams91c00922007-01-02 13:52:30 -0700854 return tx;
855}
856
Dan Williamsac6b53b2009-07-14 13:40:19 -0700857/* set_syndrome_sources - populate source buffers for gen_syndrome
858 * @srcs - (struct page *) array of size sh->disks
859 * @sh - stripe_head to parse
860 *
861 * Populates srcs in proper layout order for the stripe and returns the
862 * 'count' of sources to be used in a call to async_gen_syndrome. The P
863 * destination buffer is recorded in srcs[count] and the Q destination
864 * is recorded in srcs[count+1]].
865 */
866static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
867{
868 int disks = sh->disks;
869 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
870 int d0_idx = raid6_d0(sh);
871 int count;
872 int i;
873
874 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100875 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700876
877 count = 0;
878 i = d0_idx;
879 do {
880 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
881
882 srcs[slot] = sh->dev[i].page;
883 i = raid6_next_disk(i, disks);
884 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700885
NeilBrowne4424fe2009-10-16 16:27:34 +1100886 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700887}
888
889static struct dma_async_tx_descriptor *
890ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
891{
892 int disks = sh->disks;
893 struct page **blocks = percpu->scribble;
894 int target;
895 int qd_idx = sh->qd_idx;
896 struct dma_async_tx_descriptor *tx;
897 struct async_submit_ctl submit;
898 struct r5dev *tgt;
899 struct page *dest;
900 int i;
901 int count;
902
903 if (sh->ops.target < 0)
904 target = sh->ops.target2;
905 else if (sh->ops.target2 < 0)
906 target = sh->ops.target;
907 else
908 /* we should only have one valid target */
909 BUG();
910 BUG_ON(target < 0);
911 pr_debug("%s: stripe %llu block: %d\n",
912 __func__, (unsigned long long)sh->sector, target);
913
914 tgt = &sh->dev[target];
915 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
916 dest = tgt->page;
917
918 atomic_inc(&sh->count);
919
920 if (target == qd_idx) {
921 count = set_syndrome_sources(blocks, sh);
922 blocks[count] = NULL; /* regenerating p is not necessary */
923 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700924 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
925 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700926 to_addr_conv(sh, percpu));
927 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
928 } else {
929 /* Compute any data- or p-drive using XOR */
930 count = 0;
931 for (i = disks; i-- ; ) {
932 if (i == target || i == qd_idx)
933 continue;
934 blocks[count++] = sh->dev[i].page;
935 }
936
Dan Williams0403e382009-09-08 17:42:50 -0700937 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
938 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700939 to_addr_conv(sh, percpu));
940 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
941 }
942
943 return tx;
944}
945
946static struct dma_async_tx_descriptor *
947ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
948{
949 int i, count, disks = sh->disks;
950 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
951 int d0_idx = raid6_d0(sh);
952 int faila = -1, failb = -1;
953 int target = sh->ops.target;
954 int target2 = sh->ops.target2;
955 struct r5dev *tgt = &sh->dev[target];
956 struct r5dev *tgt2 = &sh->dev[target2];
957 struct dma_async_tx_descriptor *tx;
958 struct page **blocks = percpu->scribble;
959 struct async_submit_ctl submit;
960
961 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
962 __func__, (unsigned long long)sh->sector, target, target2);
963 BUG_ON(target < 0 || target2 < 0);
964 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
965 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
966
Dan Williams6c910a72009-09-16 12:24:54 -0700967 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -0700968 * slot number conversion for 'faila' and 'failb'
969 */
970 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100971 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700972 count = 0;
973 i = d0_idx;
974 do {
975 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
976
977 blocks[slot] = sh->dev[i].page;
978
979 if (i == target)
980 faila = slot;
981 if (i == target2)
982 failb = slot;
983 i = raid6_next_disk(i, disks);
984 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700985
986 BUG_ON(faila == failb);
987 if (failb < faila)
988 swap(faila, failb);
989 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
990 __func__, (unsigned long long)sh->sector, faila, failb);
991
992 atomic_inc(&sh->count);
993
994 if (failb == syndrome_disks+1) {
995 /* Q disk is one of the missing disks */
996 if (faila == syndrome_disks) {
997 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -0700998 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
999 ops_complete_compute, sh,
1000 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +11001001 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001002 STRIPE_SIZE, &submit);
1003 } else {
1004 struct page *dest;
1005 int data_target;
1006 int qd_idx = sh->qd_idx;
1007
1008 /* Missing D+Q: recompute D from P, then recompute Q */
1009 if (target == qd_idx)
1010 data_target = target2;
1011 else
1012 data_target = target;
1013
1014 count = 0;
1015 for (i = disks; i-- ; ) {
1016 if (i == data_target || i == qd_idx)
1017 continue;
1018 blocks[count++] = sh->dev[i].page;
1019 }
1020 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001021 init_async_submit(&submit,
1022 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1023 NULL, NULL, NULL,
1024 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001025 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1026 &submit);
1027
1028 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001029 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1030 ops_complete_compute, sh,
1031 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001032 return async_gen_syndrome(blocks, 0, count+2,
1033 STRIPE_SIZE, &submit);
1034 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001035 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001036 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1037 ops_complete_compute, sh,
1038 to_addr_conv(sh, percpu));
1039 if (failb == syndrome_disks) {
1040 /* We're missing D+P. */
1041 return async_raid6_datap_recov(syndrome_disks+2,
1042 STRIPE_SIZE, faila,
1043 blocks, &submit);
1044 } else {
1045 /* We're missing D+D. */
1046 return async_raid6_2data_recov(syndrome_disks+2,
1047 STRIPE_SIZE, faila, failb,
1048 blocks, &submit);
1049 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001050 }
1051}
1052
1053
Dan Williams91c00922007-01-02 13:52:30 -07001054static void ops_complete_prexor(void *stripe_head_ref)
1055{
1056 struct stripe_head *sh = stripe_head_ref;
1057
Harvey Harrisone46b2722008-04-28 02:15:50 -07001058 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001059 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001060}
1061
1062static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001063ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1064 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001065{
Dan Williams91c00922007-01-02 13:52:30 -07001066 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001067 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001068 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001069 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001070
1071 /* existing parity data subtracted */
1072 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1073
Harvey Harrisone46b2722008-04-28 02:15:50 -07001074 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001075 (unsigned long long)sh->sector);
1076
1077 for (i = disks; i--; ) {
1078 struct r5dev *dev = &sh->dev[i];
1079 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001080 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001081 xor_srcs[count++] = dev->page;
1082 }
1083
Dan Williams0403e382009-09-08 17:42:50 -07001084 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001085 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001086 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001087
1088 return tx;
1089}
1090
1091static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001092ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001093{
1094 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001095 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001096
Harvey Harrisone46b2722008-04-28 02:15:50 -07001097 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001098 (unsigned long long)sh->sector);
1099
1100 for (i = disks; i--; ) {
1101 struct r5dev *dev = &sh->dev[i];
1102 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001103
Dan Williamsd8ee0722008-06-28 08:32:06 +10001104 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001105 struct bio *wbi;
1106
NeilBrowncbe47ec2011-07-26 11:20:35 +10001107 spin_lock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001108 chosen = dev->towrite;
1109 dev->towrite = NULL;
1110 BUG_ON(dev->written);
1111 wbi = dev->written = chosen;
NeilBrowncbe47ec2011-07-26 11:20:35 +10001112 spin_unlock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001113
1114 while (wbi && wbi->bi_sector <
1115 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001116 if (wbi->bi_rw & REQ_FUA)
1117 set_bit(R5_WantFUA, &dev->flags);
Dan Williams91c00922007-01-02 13:52:30 -07001118 tx = async_copy_data(1, wbi, dev->page,
1119 dev->sector, tx);
1120 wbi = r5_next_bio(wbi, dev->sector);
1121 }
1122 }
1123 }
1124
1125 return tx;
1126}
1127
Dan Williamsac6b53b2009-07-14 13:40:19 -07001128static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001129{
1130 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001131 int disks = sh->disks;
1132 int pd_idx = sh->pd_idx;
1133 int qd_idx = sh->qd_idx;
1134 int i;
Tejun Heoe9c74692010-09-03 11:56:18 +02001135 bool fua = false;
Dan Williams91c00922007-01-02 13:52:30 -07001136
Harvey Harrisone46b2722008-04-28 02:15:50 -07001137 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001138 (unsigned long long)sh->sector);
1139
Tejun Heoe9c74692010-09-03 11:56:18 +02001140 for (i = disks; i--; )
1141 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
1142
Dan Williams91c00922007-01-02 13:52:30 -07001143 for (i = disks; i--; ) {
1144 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001145
Tejun Heoe9c74692010-09-03 11:56:18 +02001146 if (dev->written || i == pd_idx || i == qd_idx) {
Dan Williams91c00922007-01-02 13:52:30 -07001147 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001148 if (fua)
1149 set_bit(R5_WantFUA, &dev->flags);
1150 }
Dan Williams91c00922007-01-02 13:52:30 -07001151 }
1152
Dan Williamsd8ee0722008-06-28 08:32:06 +10001153 if (sh->reconstruct_state == reconstruct_state_drain_run)
1154 sh->reconstruct_state = reconstruct_state_drain_result;
1155 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1156 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1157 else {
1158 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1159 sh->reconstruct_state = reconstruct_state_result;
1160 }
Dan Williams91c00922007-01-02 13:52:30 -07001161
1162 set_bit(STRIPE_HANDLE, &sh->state);
1163 release_stripe(sh);
1164}
1165
1166static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001167ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1168 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001169{
Dan Williams91c00922007-01-02 13:52:30 -07001170 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001171 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001172 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001173 int count = 0, pd_idx = sh->pd_idx, i;
1174 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001175 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001176 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001177
Harvey Harrisone46b2722008-04-28 02:15:50 -07001178 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001179 (unsigned long long)sh->sector);
1180
1181 /* check if prexor is active which means only process blocks
1182 * that are part of a read-modify-write (written)
1183 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001184 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1185 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001186 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1187 for (i = disks; i--; ) {
1188 struct r5dev *dev = &sh->dev[i];
1189 if (dev->written)
1190 xor_srcs[count++] = dev->page;
1191 }
1192 } else {
1193 xor_dest = sh->dev[pd_idx].page;
1194 for (i = disks; i--; ) {
1195 struct r5dev *dev = &sh->dev[i];
1196 if (i != pd_idx)
1197 xor_srcs[count++] = dev->page;
1198 }
1199 }
1200
Dan Williams91c00922007-01-02 13:52:30 -07001201 /* 1/ if we prexor'd then the dest is reused as a source
1202 * 2/ if we did not prexor then we are redoing the parity
1203 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1204 * for the synchronous xor case
1205 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001206 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001207 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1208
1209 atomic_inc(&sh->count);
1210
Dan Williamsac6b53b2009-07-14 13:40:19 -07001211 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001212 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001213 if (unlikely(count == 1))
1214 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1215 else
1216 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001217}
1218
Dan Williamsac6b53b2009-07-14 13:40:19 -07001219static void
1220ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1221 struct dma_async_tx_descriptor *tx)
1222{
1223 struct async_submit_ctl submit;
1224 struct page **blocks = percpu->scribble;
1225 int count;
1226
1227 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1228
1229 count = set_syndrome_sources(blocks, sh);
1230
1231 atomic_inc(&sh->count);
1232
1233 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1234 sh, to_addr_conv(sh, percpu));
1235 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001236}
1237
1238static void ops_complete_check(void *stripe_head_ref)
1239{
1240 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001241
Harvey Harrisone46b2722008-04-28 02:15:50 -07001242 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001243 (unsigned long long)sh->sector);
1244
Dan Williamsecc65c92008-06-28 08:31:57 +10001245 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001246 set_bit(STRIPE_HANDLE, &sh->state);
1247 release_stripe(sh);
1248}
1249
Dan Williamsac6b53b2009-07-14 13:40:19 -07001250static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001251{
Dan Williams91c00922007-01-02 13:52:30 -07001252 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001253 int pd_idx = sh->pd_idx;
1254 int qd_idx = sh->qd_idx;
1255 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001256 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001257 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001258 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001259 int count;
1260 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001261
Harvey Harrisone46b2722008-04-28 02:15:50 -07001262 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001263 (unsigned long long)sh->sector);
1264
Dan Williamsac6b53b2009-07-14 13:40:19 -07001265 count = 0;
1266 xor_dest = sh->dev[pd_idx].page;
1267 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001268 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001269 if (i == pd_idx || i == qd_idx)
1270 continue;
1271 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001272 }
1273
Dan Williamsd6f38f32009-07-14 11:50:52 -07001274 init_async_submit(&submit, 0, NULL, NULL, NULL,
1275 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001276 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001277 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001278
Dan Williams91c00922007-01-02 13:52:30 -07001279 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001280 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1281 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001282}
1283
Dan Williamsac6b53b2009-07-14 13:40:19 -07001284static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1285{
1286 struct page **srcs = percpu->scribble;
1287 struct async_submit_ctl submit;
1288 int count;
1289
1290 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1291 (unsigned long long)sh->sector, checkp);
1292
1293 count = set_syndrome_sources(srcs, sh);
1294 if (!checkp)
1295 srcs[count] = NULL;
1296
1297 atomic_inc(&sh->count);
1298 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1299 sh, to_addr_conv(sh, percpu));
1300 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1301 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1302}
1303
Dan Williams417b8d42009-10-16 16:25:22 +11001304static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001305{
1306 int overlap_clear = 0, i, disks = sh->disks;
1307 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001308 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001309 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001310 struct raid5_percpu *percpu;
1311 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001312
Dan Williamsd6f38f32009-07-14 11:50:52 -07001313 cpu = get_cpu();
1314 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001315 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001316 ops_run_biofill(sh);
1317 overlap_clear++;
1318 }
1319
Dan Williams7b3a8712008-06-28 08:32:09 +10001320 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001321 if (level < 6)
1322 tx = ops_run_compute5(sh, percpu);
1323 else {
1324 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1325 tx = ops_run_compute6_1(sh, percpu);
1326 else
1327 tx = ops_run_compute6_2(sh, percpu);
1328 }
1329 /* terminate the chain if reconstruct is not set to be run */
1330 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001331 async_tx_ack(tx);
1332 }
Dan Williams91c00922007-01-02 13:52:30 -07001333
Dan Williams600aa102008-06-28 08:32:05 +10001334 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001335 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001336
Dan Williams600aa102008-06-28 08:32:05 +10001337 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001338 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001339 overlap_clear++;
1340 }
1341
Dan Williamsac6b53b2009-07-14 13:40:19 -07001342 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1343 if (level < 6)
1344 ops_run_reconstruct5(sh, percpu, tx);
1345 else
1346 ops_run_reconstruct6(sh, percpu, tx);
1347 }
Dan Williams91c00922007-01-02 13:52:30 -07001348
Dan Williamsac6b53b2009-07-14 13:40:19 -07001349 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1350 if (sh->check_state == check_state_run)
1351 ops_run_check_p(sh, percpu);
1352 else if (sh->check_state == check_state_run_q)
1353 ops_run_check_pq(sh, percpu, 0);
1354 else if (sh->check_state == check_state_run_pq)
1355 ops_run_check_pq(sh, percpu, 1);
1356 else
1357 BUG();
1358 }
Dan Williams91c00922007-01-02 13:52:30 -07001359
Dan Williams91c00922007-01-02 13:52:30 -07001360 if (overlap_clear)
1361 for (i = disks; i--; ) {
1362 struct r5dev *dev = &sh->dev[i];
1363 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1364 wake_up(&sh->raid_conf->wait_for_overlap);
1365 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001366 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001367}
1368
Dan Williams417b8d42009-10-16 16:25:22 +11001369#ifdef CONFIG_MULTICORE_RAID456
1370static void async_run_ops(void *param, async_cookie_t cookie)
1371{
1372 struct stripe_head *sh = param;
1373 unsigned long ops_request = sh->ops.request;
1374
1375 clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
1376 wake_up(&sh->ops.wait_for_ops);
1377
1378 __raid_run_ops(sh, ops_request);
1379 release_stripe(sh);
1380}
1381
1382static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1383{
1384 /* since handle_stripe can be called outside of raid5d context
1385 * we need to ensure sh->ops.request is de-staged before another
1386 * request arrives
1387 */
1388 wait_event(sh->ops.wait_for_ops,
1389 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
1390 sh->ops.request = ops_request;
1391
1392 atomic_inc(&sh->count);
1393 async_schedule(async_run_ops, sh);
1394}
1395#else
1396#define raid_run_ops __raid_run_ops
1397#endif
1398
NeilBrownd1688a62011-10-11 16:49:52 +11001399static int grow_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
1401 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001402 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001403 if (!sh)
1404 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001405
NeilBrown3f294f42005-11-08 21:39:25 -08001406 sh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001407 #ifdef CONFIG_MULTICORE_RAID456
1408 init_waitqueue_head(&sh->ops.wait_for_ops);
1409 #endif
NeilBrown3f294f42005-11-08 21:39:25 -08001410
NeilBrowne4e11e32010-06-16 16:45:16 +10001411 if (grow_buffers(sh)) {
1412 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001413 kmem_cache_free(conf->slab_cache, sh);
1414 return 0;
1415 }
1416 /* we just created an active stripe so... */
1417 atomic_set(&sh->count, 1);
1418 atomic_inc(&conf->active_stripes);
1419 INIT_LIST_HEAD(&sh->lru);
1420 release_stripe(sh);
1421 return 1;
1422}
1423
NeilBrownd1688a62011-10-11 16:49:52 +11001424static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001425{
Christoph Lametere18b8902006-12-06 20:33:20 -08001426 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001427 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
NeilBrownf4be6b42010-06-01 19:37:25 +10001429 if (conf->mddev->gendisk)
1430 sprintf(conf->cache_name[0],
1431 "raid%d-%s", conf->level, mdname(conf->mddev));
1432 else
1433 sprintf(conf->cache_name[0],
1434 "raid%d-%p", conf->level, conf->mddev);
1435 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1436
NeilBrownad01c9e2006-03-27 01:18:07 -08001437 conf->active_name = 0;
1438 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001440 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 if (!sc)
1442 return 1;
1443 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001444 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001445 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001446 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 return 0;
1449}
NeilBrown29269552006-03-27 01:18:10 -08001450
Dan Williamsd6f38f32009-07-14 11:50:52 -07001451/**
1452 * scribble_len - return the required size of the scribble region
1453 * @num - total number of disks in the array
1454 *
1455 * The size must be enough to contain:
1456 * 1/ a struct page pointer for each device in the array +2
1457 * 2/ room to convert each entry in (1) to its corresponding dma
1458 * (dma_map_page()) or page (page_address()) address.
1459 *
1460 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1461 * calculate over all devices (not just the data blocks), using zeros in place
1462 * of the P and Q blocks.
1463 */
1464static size_t scribble_len(int num)
1465{
1466 size_t len;
1467
1468 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1469
1470 return len;
1471}
1472
NeilBrownd1688a62011-10-11 16:49:52 +11001473static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001474{
1475 /* Make all the stripes able to hold 'newsize' devices.
1476 * New slots in each stripe get 'page' set to a new page.
1477 *
1478 * This happens in stages:
1479 * 1/ create a new kmem_cache and allocate the required number of
1480 * stripe_heads.
1481 * 2/ gather all the old stripe_heads and tranfer the pages across
1482 * to the new stripe_heads. This will have the side effect of
1483 * freezing the array as once all stripe_heads have been collected,
1484 * no IO will be possible. Old stripe heads are freed once their
1485 * pages have been transferred over, and the old kmem_cache is
1486 * freed when all stripes are done.
1487 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1488 * we simple return a failre status - no need to clean anything up.
1489 * 4/ allocate new pages for the new slots in the new stripe_heads.
1490 * If this fails, we don't bother trying the shrink the
1491 * stripe_heads down again, we just leave them as they are.
1492 * As each stripe_head is processed the new one is released into
1493 * active service.
1494 *
1495 * Once step2 is started, we cannot afford to wait for a write,
1496 * so we use GFP_NOIO allocations.
1497 */
1498 struct stripe_head *osh, *nsh;
1499 LIST_HEAD(newstripes);
1500 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001501 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001502 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001503 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001504 int i;
1505
1506 if (newsize <= conf->pool_size)
1507 return 0; /* never bother to shrink */
1508
Dan Williamsb5470dc2008-06-27 21:44:04 -07001509 err = md_allow_write(conf->mddev);
1510 if (err)
1511 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001512
NeilBrownad01c9e2006-03-27 01:18:07 -08001513 /* Step 1 */
1514 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1515 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001516 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001517 if (!sc)
1518 return -ENOMEM;
1519
1520 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001521 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001522 if (!nsh)
1523 break;
1524
NeilBrownad01c9e2006-03-27 01:18:07 -08001525 nsh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001526 #ifdef CONFIG_MULTICORE_RAID456
1527 init_waitqueue_head(&nsh->ops.wait_for_ops);
1528 #endif
NeilBrownad01c9e2006-03-27 01:18:07 -08001529
1530 list_add(&nsh->lru, &newstripes);
1531 }
1532 if (i) {
1533 /* didn't get enough, give up */
1534 while (!list_empty(&newstripes)) {
1535 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1536 list_del(&nsh->lru);
1537 kmem_cache_free(sc, nsh);
1538 }
1539 kmem_cache_destroy(sc);
1540 return -ENOMEM;
1541 }
1542 /* Step 2 - Must use GFP_NOIO now.
1543 * OK, we have enough stripes, start collecting inactive
1544 * stripes and copying them over
1545 */
1546 list_for_each_entry(nsh, &newstripes, lru) {
1547 spin_lock_irq(&conf->device_lock);
1548 wait_event_lock_irq(conf->wait_for_stripe,
1549 !list_empty(&conf->inactive_list),
1550 conf->device_lock,
NeilBrown482c0832011-04-18 18:25:42 +10001551 );
NeilBrownad01c9e2006-03-27 01:18:07 -08001552 osh = get_free_stripe(conf);
1553 spin_unlock_irq(&conf->device_lock);
1554 atomic_set(&nsh->count, 1);
1555 for(i=0; i<conf->pool_size; i++)
1556 nsh->dev[i].page = osh->dev[i].page;
1557 for( ; i<newsize; i++)
1558 nsh->dev[i].page = NULL;
1559 kmem_cache_free(conf->slab_cache, osh);
1560 }
1561 kmem_cache_destroy(conf->slab_cache);
1562
1563 /* Step 3.
1564 * At this point, we are holding all the stripes so the array
1565 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001566 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001567 */
1568 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1569 if (ndisks) {
1570 for (i=0; i<conf->raid_disks; i++)
1571 ndisks[i] = conf->disks[i];
1572 kfree(conf->disks);
1573 conf->disks = ndisks;
1574 } else
1575 err = -ENOMEM;
1576
Dan Williamsd6f38f32009-07-14 11:50:52 -07001577 get_online_cpus();
1578 conf->scribble_len = scribble_len(newsize);
1579 for_each_present_cpu(cpu) {
1580 struct raid5_percpu *percpu;
1581 void *scribble;
1582
1583 percpu = per_cpu_ptr(conf->percpu, cpu);
1584 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1585
1586 if (scribble) {
1587 kfree(percpu->scribble);
1588 percpu->scribble = scribble;
1589 } else {
1590 err = -ENOMEM;
1591 break;
1592 }
1593 }
1594 put_online_cpus();
1595
NeilBrownad01c9e2006-03-27 01:18:07 -08001596 /* Step 4, return new stripes to service */
1597 while(!list_empty(&newstripes)) {
1598 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1599 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001600
NeilBrownad01c9e2006-03-27 01:18:07 -08001601 for (i=conf->raid_disks; i < newsize; i++)
1602 if (nsh->dev[i].page == NULL) {
1603 struct page *p = alloc_page(GFP_NOIO);
1604 nsh->dev[i].page = p;
1605 if (!p)
1606 err = -ENOMEM;
1607 }
1608 release_stripe(nsh);
1609 }
1610 /* critical section pass, GFP_NOIO no longer needed */
1611
1612 conf->slab_cache = sc;
1613 conf->active_name = 1-conf->active_name;
1614 conf->pool_size = newsize;
1615 return err;
1616}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
NeilBrownd1688a62011-10-11 16:49:52 +11001618static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
1620 struct stripe_head *sh;
1621
NeilBrown3f294f42005-11-08 21:39:25 -08001622 spin_lock_irq(&conf->device_lock);
1623 sh = get_free_stripe(conf);
1624 spin_unlock_irq(&conf->device_lock);
1625 if (!sh)
1626 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001627 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001628 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001629 kmem_cache_free(conf->slab_cache, sh);
1630 atomic_dec(&conf->active_stripes);
1631 return 1;
1632}
1633
NeilBrownd1688a62011-10-11 16:49:52 +11001634static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001635{
1636 while (drop_one_stripe(conf))
1637 ;
1638
NeilBrown29fc7e32006-02-03 03:03:41 -08001639 if (conf->slab_cache)
1640 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 conf->slab_cache = NULL;
1642}
1643
NeilBrown6712ecf2007-09-27 12:47:43 +02001644static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645{
NeilBrown99c0fb52009-03-31 14:39:38 +11001646 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001647 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001648 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001650 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11001651 struct md_rdev *rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
1654 for (i=0 ; i<disks; i++)
1655 if (bi == &sh->dev[i].req)
1656 break;
1657
Dan Williams45b42332007-07-09 11:56:43 -07001658 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1659 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 uptodate);
1661 if (i == disks) {
1662 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001663 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
NeilBrown14a75d32011-12-23 10:17:52 +11001665 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11001666 /* If replacement finished while this request was outstanding,
1667 * 'replacement' might be NULL already.
1668 * In that case it moved down to 'rdev'.
1669 * rdev is not removed until all requests are finished.
1670 */
NeilBrown14a75d32011-12-23 10:17:52 +11001671 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001672 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11001673 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
1675 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001677 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11001678 /* Note that this cannot happen on a
1679 * replacement device. We just fail those on
1680 * any error
1681 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001682 printk_ratelimited(
1683 KERN_INFO
1684 "md/raid:%s: read error corrected"
1685 " (%lu sectors at %llu on %s)\n",
1686 mdname(conf->mddev), STRIPE_SECTORS,
1687 (unsigned long long)(sh->sector
1688 + rdev->data_offset),
1689 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001690 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001691 clear_bit(R5_ReadError, &sh->dev[i].flags);
1692 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1693 }
NeilBrown14a75d32011-12-23 10:17:52 +11001694 if (atomic_read(&rdev->read_errors))
1695 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11001697 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001698 int retry = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001701 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11001702 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1703 printk_ratelimited(
1704 KERN_WARNING
1705 "md/raid:%s: read error on replacement device "
1706 "(sector %llu on %s).\n",
1707 mdname(conf->mddev),
1708 (unsigned long long)(sh->sector
1709 + rdev->data_offset),
1710 bdn);
1711 else if (conf->mddev->degraded >= conf->max_degraded)
Christian Dietrich8bda4702011-07-27 11:00:36 +10001712 printk_ratelimited(
1713 KERN_WARNING
1714 "md/raid:%s: read error not correctable "
1715 "(sector %llu on %s).\n",
1716 mdname(conf->mddev),
1717 (unsigned long long)(sh->sector
1718 + rdev->data_offset),
1719 bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001720 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
NeilBrown4e5314b2005-11-08 21:39:22 -08001721 /* Oh, no!!! */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001722 printk_ratelimited(
1723 KERN_WARNING
1724 "md/raid:%s: read error NOT corrected!! "
1725 "(sector %llu on %s).\n",
1726 mdname(conf->mddev),
1727 (unsigned long long)(sh->sector
1728 + rdev->data_offset),
1729 bdn);
NeilBrownd6950432006-07-10 04:44:20 -07001730 else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001731 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001732 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001733 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001734 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001735 else
1736 retry = 1;
1737 if (retry)
1738 set_bit(R5_ReadError, &sh->dev[i].flags);
1739 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001740 clear_bit(R5_ReadError, &sh->dev[i].flags);
1741 clear_bit(R5_ReWrite, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001742 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 }
NeilBrown14a75d32011-12-23 10:17:52 +11001745 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1747 set_bit(STRIPE_HANDLE, &sh->state);
1748 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749}
1750
NeilBrownd710e132008-10-13 11:55:12 +11001751static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752{
NeilBrown99c0fb52009-03-31 14:39:38 +11001753 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001754 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001755 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11001756 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001758 sector_t first_bad;
1759 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11001760 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
NeilBrown977df362011-12-23 10:17:53 +11001762 for (i = 0 ; i < disks; i++) {
1763 if (bi == &sh->dev[i].req) {
1764 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 break;
NeilBrown977df362011-12-23 10:17:53 +11001766 }
1767 if (bi == &sh->dev[i].rreq) {
1768 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001769 if (rdev)
1770 replacement = 1;
1771 else
1772 /* rdev was removed and 'replacement'
1773 * replaced it. rdev is not removed
1774 * until all requests are finished.
1775 */
1776 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11001777 break;
1778 }
1779 }
Dan Williams45b42332007-07-09 11:56:43 -07001780 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1782 uptodate);
1783 if (i == disks) {
1784 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001785 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 }
1787
NeilBrown977df362011-12-23 10:17:53 +11001788 if (replacement) {
1789 if (!uptodate)
1790 md_error(conf->mddev, rdev);
1791 else if (is_badblock(rdev, sh->sector,
1792 STRIPE_SECTORS,
1793 &first_bad, &bad_sectors))
1794 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1795 } else {
1796 if (!uptodate) {
1797 set_bit(WriteErrorSeen, &rdev->flags);
1798 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11001799 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1800 set_bit(MD_RECOVERY_NEEDED,
1801 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11001802 } else if (is_badblock(rdev, sh->sector,
1803 STRIPE_SECTORS,
1804 &first_bad, &bad_sectors))
1805 set_bit(R5_MadeGood, &sh->dev[i].flags);
1806 }
1807 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
NeilBrown977df362011-12-23 10:17:53 +11001809 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1810 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001812 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813}
1814
NeilBrown784052e2009-03-31 15:19:07 +11001815static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
NeilBrown784052e2009-03-31 15:19:07 +11001817static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818{
1819 struct r5dev *dev = &sh->dev[i];
1820
1821 bio_init(&dev->req);
1822 dev->req.bi_io_vec = &dev->vec;
1823 dev->req.bi_vcnt++;
1824 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11001826 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
NeilBrown977df362011-12-23 10:17:53 +11001828 bio_init(&dev->rreq);
1829 dev->rreq.bi_io_vec = &dev->rvec;
1830 dev->rreq.bi_vcnt++;
1831 dev->rreq.bi_max_vecs++;
1832 dev->rreq.bi_private = sh;
1833 dev->rvec.bv_page = dev->page;
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001836 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837}
1838
NeilBrownfd01b882011-10-11 16:47:53 +11001839static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840{
1841 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11001842 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11001843 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10001844 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
NeilBrown908f4fb2011-12-23 10:17:50 +11001846 spin_lock_irqsave(&conf->device_lock, flags);
1847 clear_bit(In_sync, &rdev->flags);
1848 mddev->degraded = calc_degraded(conf);
1849 spin_unlock_irqrestore(&conf->device_lock, flags);
1850 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1851
NeilBrownde393cd2011-07-28 11:31:48 +10001852 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001853 set_bit(Faulty, &rdev->flags);
1854 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1855 printk(KERN_ALERT
1856 "md/raid:%s: Disk failure on %s, disabling device.\n"
1857 "md/raid:%s: Operation continuing on %d devices.\n",
1858 mdname(mddev),
1859 bdevname(rdev->bdev, b),
1860 mdname(mddev),
1861 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001862}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
1864/*
1865 * Input: a 'big' sector number,
1866 * Output: index of the data and parity disk, and the sector # in them.
1867 */
NeilBrownd1688a62011-10-11 16:49:52 +11001868static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001869 int previous, int *dd_idx,
1870 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001872 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001873 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001875 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001876 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001878 int algorithm = previous ? conf->prev_algo
1879 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001880 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1881 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001882 int raid_disks = previous ? conf->previous_raid_disks
1883 : conf->raid_disks;
1884 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
1886 /* First compute the information on this sector */
1887
1888 /*
1889 * Compute the chunk number and the sector offset inside the chunk
1890 */
1891 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1892 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
1894 /*
1895 * Compute the stripe number
1896 */
NeilBrown35f2a592010-04-20 14:13:34 +10001897 stripe = chunk_number;
1898 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10001899 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 /*
1901 * Select the parity disk based on the user selected algorithm.
1902 */
NeilBrown84789552011-07-27 11:00:36 +10001903 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07001904 switch(conf->level) {
1905 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001906 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001907 break;
1908 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001909 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001911 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001912 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 (*dd_idx)++;
1914 break;
1915 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001916 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001917 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 (*dd_idx)++;
1919 break;
1920 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001921 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001922 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 break;
1924 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001925 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001926 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001928 case ALGORITHM_PARITY_0:
1929 pd_idx = 0;
1930 (*dd_idx)++;
1931 break;
1932 case ALGORITHM_PARITY_N:
1933 pd_idx = data_disks;
1934 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11001936 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001937 }
1938 break;
1939 case 6:
1940
NeilBrowne183eae2009-03-31 15:20:22 +11001941 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001942 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001943 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001944 qd_idx = pd_idx + 1;
1945 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001946 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001947 qd_idx = 0;
1948 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001949 (*dd_idx) += 2; /* D D P Q D */
1950 break;
1951 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001952 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001953 qd_idx = pd_idx + 1;
1954 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001955 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001956 qd_idx = 0;
1957 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001958 (*dd_idx) += 2; /* D D P Q D */
1959 break;
1960 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001961 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001962 qd_idx = (pd_idx + 1) % raid_disks;
1963 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001964 break;
1965 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001966 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001967 qd_idx = (pd_idx + 1) % raid_disks;
1968 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001969 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001970
1971 case ALGORITHM_PARITY_0:
1972 pd_idx = 0;
1973 qd_idx = 1;
1974 (*dd_idx) += 2;
1975 break;
1976 case ALGORITHM_PARITY_N:
1977 pd_idx = data_disks;
1978 qd_idx = data_disks + 1;
1979 break;
1980
1981 case ALGORITHM_ROTATING_ZERO_RESTART:
1982 /* Exactly the same as RIGHT_ASYMMETRIC, but or
1983 * of blocks for computing Q is different.
1984 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001985 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11001986 qd_idx = pd_idx + 1;
1987 if (pd_idx == raid_disks-1) {
1988 (*dd_idx)++; /* Q D D D P */
1989 qd_idx = 0;
1990 } else if (*dd_idx >= pd_idx)
1991 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001992 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001993 break;
1994
1995 case ALGORITHM_ROTATING_N_RESTART:
1996 /* Same a left_asymmetric, by first stripe is
1997 * D D D P Q rather than
1998 * Q D D D P
1999 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002000 stripe2 += 1;
2001 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002002 qd_idx = pd_idx + 1;
2003 if (pd_idx == raid_disks-1) {
2004 (*dd_idx)++; /* Q D D D P */
2005 qd_idx = 0;
2006 } else if (*dd_idx >= pd_idx)
2007 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002008 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002009 break;
2010
2011 case ALGORITHM_ROTATING_N_CONTINUE:
2012 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002013 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002014 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2015 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002016 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002017 break;
2018
2019 case ALGORITHM_LEFT_ASYMMETRIC_6:
2020 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002021 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002022 if (*dd_idx >= pd_idx)
2023 (*dd_idx)++;
2024 qd_idx = raid_disks - 1;
2025 break;
2026
2027 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002028 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002029 if (*dd_idx >= pd_idx)
2030 (*dd_idx)++;
2031 qd_idx = raid_disks - 1;
2032 break;
2033
2034 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002035 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002036 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2037 qd_idx = raid_disks - 1;
2038 break;
2039
2040 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002041 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002042 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2043 qd_idx = raid_disks - 1;
2044 break;
2045
2046 case ALGORITHM_PARITY_0_6:
2047 pd_idx = 0;
2048 (*dd_idx)++;
2049 qd_idx = raid_disks - 1;
2050 break;
2051
NeilBrown16a53ec2006-06-26 00:27:38 -07002052 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002053 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002054 }
2055 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 }
2057
NeilBrown911d4ee2009-03-31 14:39:38 +11002058 if (sh) {
2059 sh->pd_idx = pd_idx;
2060 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002061 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 /*
2064 * Finally, compute the new sector number
2065 */
2066 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2067 return new_sector;
2068}
2069
2070
NeilBrown784052e2009-03-31 15:19:07 +11002071static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072{
NeilBrownd1688a62011-10-11 16:49:52 +11002073 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002074 int raid_disks = sh->disks;
2075 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002077 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2078 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002079 int algorithm = previous ? conf->prev_algo
2080 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 sector_t stripe;
2082 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002083 sector_t chunk_number;
2084 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002086 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
NeilBrown16a53ec2006-06-26 00:27:38 -07002088
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2090 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
NeilBrown16a53ec2006-06-26 00:27:38 -07002092 if (i == sh->pd_idx)
2093 return 0;
2094 switch(conf->level) {
2095 case 4: break;
2096 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002097 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 case ALGORITHM_LEFT_ASYMMETRIC:
2099 case ALGORITHM_RIGHT_ASYMMETRIC:
2100 if (i > sh->pd_idx)
2101 i--;
2102 break;
2103 case ALGORITHM_LEFT_SYMMETRIC:
2104 case ALGORITHM_RIGHT_SYMMETRIC:
2105 if (i < sh->pd_idx)
2106 i += raid_disks;
2107 i -= (sh->pd_idx + 1);
2108 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002109 case ALGORITHM_PARITY_0:
2110 i -= 1;
2111 break;
2112 case ALGORITHM_PARITY_N:
2113 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002115 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002116 }
2117 break;
2118 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002119 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002120 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002121 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002122 case ALGORITHM_LEFT_ASYMMETRIC:
2123 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002124 case ALGORITHM_ROTATING_ZERO_RESTART:
2125 case ALGORITHM_ROTATING_N_RESTART:
2126 if (sh->pd_idx == raid_disks-1)
2127 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002128 else if (i > sh->pd_idx)
2129 i -= 2; /* D D P Q D */
2130 break;
2131 case ALGORITHM_LEFT_SYMMETRIC:
2132 case ALGORITHM_RIGHT_SYMMETRIC:
2133 if (sh->pd_idx == raid_disks-1)
2134 i--; /* Q D D D P */
2135 else {
2136 /* D D P Q D */
2137 if (i < sh->pd_idx)
2138 i += raid_disks;
2139 i -= (sh->pd_idx + 2);
2140 }
2141 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002142 case ALGORITHM_PARITY_0:
2143 i -= 2;
2144 break;
2145 case ALGORITHM_PARITY_N:
2146 break;
2147 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002148 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002149 if (sh->pd_idx == 0)
2150 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002151 else {
2152 /* D D Q P D */
2153 if (i < sh->pd_idx)
2154 i += raid_disks;
2155 i -= (sh->pd_idx + 1);
2156 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002157 break;
2158 case ALGORITHM_LEFT_ASYMMETRIC_6:
2159 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2160 if (i > sh->pd_idx)
2161 i--;
2162 break;
2163 case ALGORITHM_LEFT_SYMMETRIC_6:
2164 case ALGORITHM_RIGHT_SYMMETRIC_6:
2165 if (i < sh->pd_idx)
2166 i += data_disks + 1;
2167 i -= (sh->pd_idx + 1);
2168 break;
2169 case ALGORITHM_PARITY_0_6:
2170 i -= 1;
2171 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002172 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002173 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002174 }
2175 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 }
2177
2178 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002179 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
NeilBrown112bf892009-03-31 14:39:38 +11002181 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002182 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002183 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2184 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002185 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2186 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 return 0;
2188 }
2189 return r_sector;
2190}
2191
2192
Dan Williams600aa102008-06-28 08:32:05 +10002193static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002194schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002195 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002196{
2197 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002198 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002199 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002200
Dan Williamse33129d2007-01-02 13:52:30 -07002201 if (rcw) {
2202 /* if we are not expanding this is a proper write request, and
2203 * there will be bios with new data to be drained into the
2204 * stripe cache
2205 */
2206 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10002207 sh->reconstruct_state = reconstruct_state_drain_run;
2208 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2209 } else
2210 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07002211
Dan Williamsac6b53b2009-07-14 13:40:19 -07002212 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002213
2214 for (i = disks; i--; ) {
2215 struct r5dev *dev = &sh->dev[i];
2216
2217 if (dev->towrite) {
2218 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002219 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002220 if (!expand)
2221 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002222 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002223 }
2224 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002225 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002226 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002227 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002228 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002229 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002230 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2231 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2232
Dan Williamsd8ee0722008-06-28 08:32:06 +10002233 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002234 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2235 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002236 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002237
2238 for (i = disks; i--; ) {
2239 struct r5dev *dev = &sh->dev[i];
2240 if (i == pd_idx)
2241 continue;
2242
Dan Williamse33129d2007-01-02 13:52:30 -07002243 if (dev->towrite &&
2244 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002245 test_bit(R5_Wantcompute, &dev->flags))) {
2246 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002247 set_bit(R5_LOCKED, &dev->flags);
2248 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002249 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002250 }
2251 }
2252 }
2253
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002254 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002255 * are in flight
2256 */
2257 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2258 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002259 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002260
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002261 if (level == 6) {
2262 int qd_idx = sh->qd_idx;
2263 struct r5dev *dev = &sh->dev[qd_idx];
2264
2265 set_bit(R5_LOCKED, &dev->flags);
2266 clear_bit(R5_UPTODATE, &dev->flags);
2267 s->locked++;
2268 }
2269
Dan Williams600aa102008-06-28 08:32:05 +10002270 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002271 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002272 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002273}
NeilBrown16a53ec2006-06-26 00:27:38 -07002274
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275/*
2276 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002277 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 * The bi_next chain must be in order.
2279 */
2280static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2281{
2282 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002283 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002284 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
NeilBrowncbe47ec2011-07-26 11:20:35 +10002286 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 (unsigned long long)bi->bi_sector,
2288 (unsigned long long)sh->sector);
2289
2290
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07002292 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -07002294 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
2295 firstwrite = 1;
2296 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 bip = &sh->dev[dd_idx].toread;
2298 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2299 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2300 goto overlap;
2301 bip = & (*bip)->bi_next;
2302 }
2303 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2304 goto overlap;
2305
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002306 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 if (*bip)
2308 bi->bi_next = *bip;
2309 *bip = bi;
Jens Axboe960e7392008-08-15 10:41:18 +02002310 bi->bi_phys_segments++;
NeilBrown72626682005-09-09 16:23:54 -07002311
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 if (forwrite) {
2313 /* check if page is covered */
2314 sector_t sector = sh->dev[dd_idx].sector;
2315 for (bi=sh->dev[dd_idx].towrite;
2316 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2317 bi && bi->bi_sector <= sector;
2318 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2319 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2320 sector = bi->bi_sector + (bi->bi_size>>9);
2321 }
2322 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2323 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2324 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002325 spin_unlock_irq(&conf->device_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002326
2327 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2328 (unsigned long long)(*bip)->bi_sector,
2329 (unsigned long long)sh->sector, dd_idx);
2330
2331 if (conf->mddev->bitmap && firstwrite) {
2332 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2333 STRIPE_SECTORS, 0);
2334 sh->bm_seq = conf->seq_flush+1;
2335 set_bit(STRIPE_BIT_DELAY, &sh->state);
2336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 return 1;
2338
2339 overlap:
2340 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
2341 spin_unlock_irq(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 return 0;
2343}
2344
NeilBrownd1688a62011-10-11 16:49:52 +11002345static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002346
NeilBrownd1688a62011-10-11 16:49:52 +11002347static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002348 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002349{
NeilBrown784052e2009-03-31 15:19:07 +11002350 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002351 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002352 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002353 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002354 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002355
NeilBrown112bf892009-03-31 14:39:38 +11002356 raid5_compute_sector(conf,
2357 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002358 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002359 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002360 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002361}
2362
Dan Williamsa4456852007-07-09 11:56:43 -07002363static void
NeilBrownd1688a62011-10-11 16:49:52 +11002364handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002365 struct stripe_head_state *s, int disks,
2366 struct bio **return_bi)
2367{
2368 int i;
2369 for (i = disks; i--; ) {
2370 struct bio *bi;
2371 int bitmap_end = 0;
2372
2373 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002374 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002375 rcu_read_lock();
2376 rdev = rcu_dereference(conf->disks[i].rdev);
2377 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002378 atomic_inc(&rdev->nr_pending);
2379 else
2380 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002381 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002382 if (rdev) {
2383 if (!rdev_set_badblocks(
2384 rdev,
2385 sh->sector,
2386 STRIPE_SECTORS, 0))
2387 md_error(conf->mddev, rdev);
2388 rdev_dec_pending(rdev, conf->mddev);
2389 }
Dan Williamsa4456852007-07-09 11:56:43 -07002390 }
2391 spin_lock_irq(&conf->device_lock);
2392 /* fail all writes first */
2393 bi = sh->dev[i].towrite;
2394 sh->dev[i].towrite = NULL;
2395 if (bi) {
2396 s->to_write--;
2397 bitmap_end = 1;
2398 }
2399
2400 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2401 wake_up(&conf->wait_for_overlap);
2402
2403 while (bi && bi->bi_sector <
2404 sh->dev[i].sector + STRIPE_SECTORS) {
2405 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2406 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002407 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002408 md_write_end(conf->mddev);
2409 bi->bi_next = *return_bi;
2410 *return_bi = bi;
2411 }
2412 bi = nextbi;
2413 }
2414 /* and fail all 'written' */
2415 bi = sh->dev[i].written;
2416 sh->dev[i].written = NULL;
2417 if (bi) bitmap_end = 1;
2418 while (bi && bi->bi_sector <
2419 sh->dev[i].sector + STRIPE_SECTORS) {
2420 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2421 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002422 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002423 md_write_end(conf->mddev);
2424 bi->bi_next = *return_bi;
2425 *return_bi = bi;
2426 }
2427 bi = bi2;
2428 }
2429
Dan Williamsb5e98d62007-01-02 13:52:31 -07002430 /* fail any reads if this device is non-operational and
2431 * the data has not reached the cache yet.
2432 */
2433 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2434 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2435 test_bit(R5_ReadError, &sh->dev[i].flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002436 bi = sh->dev[i].toread;
2437 sh->dev[i].toread = NULL;
2438 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2439 wake_up(&conf->wait_for_overlap);
2440 if (bi) s->to_read--;
2441 while (bi && bi->bi_sector <
2442 sh->dev[i].sector + STRIPE_SECTORS) {
2443 struct bio *nextbi =
2444 r5_next_bio(bi, sh->dev[i].sector);
2445 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002446 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002447 bi->bi_next = *return_bi;
2448 *return_bi = bi;
2449 }
2450 bi = nextbi;
2451 }
2452 }
2453 spin_unlock_irq(&conf->device_lock);
2454 if (bitmap_end)
2455 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2456 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002457 /* If we were in the middle of a write the parity block might
2458 * still be locked - so just clear all R5_LOCKED flags
2459 */
2460 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002461 }
2462
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002463 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2464 if (atomic_dec_and_test(&conf->pending_full_writes))
2465 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002466}
2467
NeilBrown7f0da592011-07-28 11:39:22 +10002468static void
NeilBrownd1688a62011-10-11 16:49:52 +11002469handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002470 struct stripe_head_state *s)
2471{
2472 int abort = 0;
2473 int i;
2474
2475 md_done_sync(conf->mddev, STRIPE_SECTORS, 0);
2476 clear_bit(STRIPE_SYNCING, &sh->state);
2477 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002478 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002479 /* There is nothing more to do for sync/check/repair.
NeilBrown9a3e1102011-12-23 10:17:53 +11002480 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002481 * non-sync devices, or abort the recovery
2482 */
2483 if (!test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery))
2484 return;
2485 /* During recovery devices cannot be removed, so locking and
2486 * refcounting of rdevs is not needed
2487 */
2488 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +11002489 struct md_rdev *rdev = conf->disks[i].rdev;
NeilBrown9a3e1102011-12-23 10:17:53 +11002490 if (rdev
2491 && !test_bit(Faulty, &rdev->flags)
2492 && !test_bit(In_sync, &rdev->flags)
2493 && !rdev_set_badblocks(rdev, sh->sector,
2494 STRIPE_SECTORS, 0))
2495 abort = 1;
2496 rdev = conf->disks[i].replacement;
2497 if (rdev
2498 && !test_bit(Faulty, &rdev->flags)
2499 && !test_bit(In_sync, &rdev->flags)
2500 && !rdev_set_badblocks(rdev, sh->sector,
2501 STRIPE_SECTORS, 0))
NeilBrown7f0da592011-07-28 11:39:22 +10002502 abort = 1;
2503 }
2504 if (abort) {
2505 conf->recovery_disabled = conf->mddev->recovery_disabled;
2506 set_bit(MD_RECOVERY_INTR, &conf->mddev->recovery);
2507 }
2508}
2509
NeilBrown9a3e1102011-12-23 10:17:53 +11002510static int want_replace(struct stripe_head *sh, int disk_idx)
2511{
2512 struct md_rdev *rdev;
2513 int rv = 0;
2514 /* Doing recovery so rcu locking not required */
2515 rdev = sh->raid_conf->disks[disk_idx].replacement;
2516 if (rdev
2517 && !test_bit(Faulty, &rdev->flags)
2518 && !test_bit(In_sync, &rdev->flags)
2519 && (rdev->recovery_offset <= sh->sector
2520 || rdev->mddev->recovery_cp <= sh->sector))
2521 rv = 1;
2522
2523 return rv;
2524}
2525
NeilBrown93b3dbc2011-07-27 11:00:36 +10002526/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002527 * to be read or computed to satisfy a request.
2528 *
2529 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002530 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002531 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002532static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2533 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002534{
2535 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002536 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2537 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002538
Dan Williamsf38e1212007-01-02 13:52:30 -07002539 /* is the data in this block needed, and can we get it? */
2540 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002541 !test_bit(R5_UPTODATE, &dev->flags) &&
2542 (dev->toread ||
2543 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2544 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002545 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002546 (s->failed >= 1 && fdev[0]->toread) ||
2547 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002548 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2549 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2550 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002551 /* we would like to get this block, possibly by computing it,
2552 * otherwise read it if the backing disk is insync
2553 */
2554 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2555 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2556 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002557 (s->failed && (disk_idx == s->failed_num[0] ||
2558 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002559 /* have disk failed, and we're requested to fetch it;
2560 * do compute it
2561 */
2562 pr_debug("Computing stripe %llu block %d\n",
2563 (unsigned long long)sh->sector, disk_idx);
2564 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2565 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2566 set_bit(R5_Wantcompute, &dev->flags);
2567 sh->ops.target = disk_idx;
2568 sh->ops.target2 = -1; /* no 2nd target */
2569 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002570 /* Careful: from this point on 'uptodate' is in the eye
2571 * of raid_run_ops which services 'compute' operations
2572 * before writes. R5_Wantcompute flags a block that will
2573 * be R5_UPTODATE by the time it is needed for a
2574 * subsequent operation.
2575 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002576 s->uptodate++;
2577 return 1;
2578 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2579 /* Computing 2-failure is *very* expensive; only
2580 * do it if failed >= 2
2581 */
2582 int other;
2583 for (other = disks; other--; ) {
2584 if (other == disk_idx)
2585 continue;
2586 if (!test_bit(R5_UPTODATE,
2587 &sh->dev[other].flags))
2588 break;
2589 }
2590 BUG_ON(other < 0);
2591 pr_debug("Computing stripe %llu blocks %d,%d\n",
2592 (unsigned long long)sh->sector,
2593 disk_idx, other);
2594 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2595 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2596 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2597 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2598 sh->ops.target = disk_idx;
2599 sh->ops.target2 = other;
2600 s->uptodate += 2;
2601 s->req_compute = 1;
2602 return 1;
2603 } else if (test_bit(R5_Insync, &dev->flags)) {
2604 set_bit(R5_LOCKED, &dev->flags);
2605 set_bit(R5_Wantread, &dev->flags);
2606 s->locked++;
2607 pr_debug("Reading block %d (sync=%d)\n",
2608 disk_idx, s->syncing);
2609 }
2610 }
2611
2612 return 0;
2613}
2614
2615/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002616 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002617 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002618static void handle_stripe_fill(struct stripe_head *sh,
2619 struct stripe_head_state *s,
2620 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002621{
2622 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002623
2624 /* look for blocks to read/compute, skip this if a compute
2625 * is already in flight, or if the stripe contents are in the
2626 * midst of changing due to a write
2627 */
2628 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2629 !sh->reconstruct_state)
2630 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002631 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002632 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002633 set_bit(STRIPE_HANDLE, &sh->state);
2634}
2635
2636
Dan Williams1fe797e2008-06-28 09:16:30 +10002637/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002638 * any written block on an uptodate or failed drive can be returned.
2639 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2640 * never LOCKED, so we don't need to test 'failed' directly.
2641 */
NeilBrownd1688a62011-10-11 16:49:52 +11002642static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002643 struct stripe_head *sh, int disks, struct bio **return_bi)
2644{
2645 int i;
2646 struct r5dev *dev;
2647
2648 for (i = disks; i--; )
2649 if (sh->dev[i].written) {
2650 dev = &sh->dev[i];
2651 if (!test_bit(R5_LOCKED, &dev->flags) &&
2652 test_bit(R5_UPTODATE, &dev->flags)) {
2653 /* We can return any write requests */
2654 struct bio *wbi, *wbi2;
2655 int bitmap_end = 0;
Dan Williams45b42332007-07-09 11:56:43 -07002656 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002657 spin_lock_irq(&conf->device_lock);
2658 wbi = dev->written;
2659 dev->written = NULL;
2660 while (wbi && wbi->bi_sector <
2661 dev->sector + STRIPE_SECTORS) {
2662 wbi2 = r5_next_bio(wbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +02002663 if (!raid5_dec_bi_phys_segments(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002664 md_write_end(conf->mddev);
2665 wbi->bi_next = *return_bi;
2666 *return_bi = wbi;
2667 }
2668 wbi = wbi2;
2669 }
2670 if (dev->towrite == NULL)
2671 bitmap_end = 1;
2672 spin_unlock_irq(&conf->device_lock);
2673 if (bitmap_end)
2674 bitmap_endwrite(conf->mddev->bitmap,
2675 sh->sector,
2676 STRIPE_SECTORS,
2677 !test_bit(STRIPE_DEGRADED, &sh->state),
2678 0);
2679 }
2680 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002681
2682 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2683 if (atomic_dec_and_test(&conf->pending_full_writes))
2684 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002685}
2686
NeilBrownd1688a62011-10-11 16:49:52 +11002687static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002688 struct stripe_head *sh,
2689 struct stripe_head_state *s,
2690 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002691{
2692 int rmw = 0, rcw = 0, i;
NeilBrownc8ac1802011-07-27 11:00:36 +10002693 if (conf->max_degraded == 2) {
2694 /* RAID6 requires 'rcw' in current implementation
2695 * Calculate the real rcw later - for now fake it
2696 * look like rcw is cheaper
2697 */
2698 rcw = 1; rmw = 2;
2699 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002700 /* would I have to read this buffer for read_modify_write */
2701 struct r5dev *dev = &sh->dev[i];
2702 if ((dev->towrite || i == sh->pd_idx) &&
2703 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002704 !(test_bit(R5_UPTODATE, &dev->flags) ||
2705 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002706 if (test_bit(R5_Insync, &dev->flags))
2707 rmw++;
2708 else
2709 rmw += 2*disks; /* cannot read it */
2710 }
2711 /* Would I have to read this buffer for reconstruct_write */
2712 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2713 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002714 !(test_bit(R5_UPTODATE, &dev->flags) ||
2715 test_bit(R5_Wantcompute, &dev->flags))) {
2716 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002717 else
2718 rcw += 2*disks;
2719 }
2720 }
Dan Williams45b42332007-07-09 11:56:43 -07002721 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002722 (unsigned long long)sh->sector, rmw, rcw);
2723 set_bit(STRIPE_HANDLE, &sh->state);
2724 if (rmw < rcw && rmw > 0)
2725 /* prefer read-modify-write, but need to get some data */
2726 for (i = disks; i--; ) {
2727 struct r5dev *dev = &sh->dev[i];
2728 if ((dev->towrite || i == sh->pd_idx) &&
2729 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002730 !(test_bit(R5_UPTODATE, &dev->flags) ||
2731 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002732 test_bit(R5_Insync, &dev->flags)) {
2733 if (
2734 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002735 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002736 "%d for r-m-w\n", i);
2737 set_bit(R5_LOCKED, &dev->flags);
2738 set_bit(R5_Wantread, &dev->flags);
2739 s->locked++;
2740 } else {
2741 set_bit(STRIPE_DELAYED, &sh->state);
2742 set_bit(STRIPE_HANDLE, &sh->state);
2743 }
2744 }
2745 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002746 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002747 /* want reconstruct write, but need to get some data */
NeilBrownc8ac1802011-07-27 11:00:36 +10002748 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002749 for (i = disks; i--; ) {
2750 struct r5dev *dev = &sh->dev[i];
2751 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002752 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002753 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002754 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002755 test_bit(R5_Wantcompute, &dev->flags))) {
2756 rcw++;
2757 if (!test_bit(R5_Insync, &dev->flags))
2758 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002759 if (
2760 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002761 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002762 "%d for Reconstruct\n", i);
2763 set_bit(R5_LOCKED, &dev->flags);
2764 set_bit(R5_Wantread, &dev->flags);
2765 s->locked++;
2766 } else {
2767 set_bit(STRIPE_DELAYED, &sh->state);
2768 set_bit(STRIPE_HANDLE, &sh->state);
2769 }
2770 }
2771 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002772 }
Dan Williamsa4456852007-07-09 11:56:43 -07002773 /* now if nothing is locked, and if we have enough data,
2774 * we can start a write request
2775 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002776 /* since handle_stripe can be called at any time we need to handle the
2777 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002778 * subsequent call wants to start a write request. raid_run_ops only
2779 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002780 * simultaneously. If this is not the case then new writes need to be
2781 * held off until the compute completes.
2782 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002783 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2784 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2785 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002786 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002787}
2788
NeilBrownd1688a62011-10-11 16:49:52 +11002789static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002790 struct stripe_head_state *s, int disks)
2791{
Dan Williamsecc65c92008-06-28 08:31:57 +10002792 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002793
Dan Williamsbd2ab672008-04-10 21:29:27 -07002794 set_bit(STRIPE_HANDLE, &sh->state);
2795
Dan Williamsecc65c92008-06-28 08:31:57 +10002796 switch (sh->check_state) {
2797 case check_state_idle:
2798 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002799 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002800 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002801 sh->check_state = check_state_run;
2802 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002803 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002804 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002805 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002806 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002807 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002808 /* fall through */
2809 case check_state_compute_result:
2810 sh->check_state = check_state_idle;
2811 if (!dev)
2812 dev = &sh->dev[sh->pd_idx];
2813
2814 /* check that a write has not made the stripe insync */
2815 if (test_bit(STRIPE_INSYNC, &sh->state))
2816 break;
2817
2818 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002819 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2820 BUG_ON(s->uptodate != disks);
2821
2822 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002823 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002824 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002825
Dan Williamsa4456852007-07-09 11:56:43 -07002826 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002827 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002828 break;
2829 case check_state_run:
2830 break; /* we will be called again upon completion */
2831 case check_state_check_result:
2832 sh->check_state = check_state_idle;
2833
2834 /* if a failure occurred during the check operation, leave
2835 * STRIPE_INSYNC not set and let the stripe be handled again
2836 */
2837 if (s->failed)
2838 break;
2839
2840 /* handle a successful check operation, if parity is correct
2841 * we are done. Otherwise update the mismatch count and repair
2842 * parity if !MD_RECOVERY_CHECK
2843 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002844 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002845 /* parity is correct (on disc,
2846 * not in buffer any more)
2847 */
2848 set_bit(STRIPE_INSYNC, &sh->state);
2849 else {
2850 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2851 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2852 /* don't try to repair!! */
2853 set_bit(STRIPE_INSYNC, &sh->state);
2854 else {
2855 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002856 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002857 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2858 set_bit(R5_Wantcompute,
2859 &sh->dev[sh->pd_idx].flags);
2860 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002861 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10002862 s->uptodate++;
2863 }
2864 }
2865 break;
2866 case check_state_compute_run:
2867 break;
2868 default:
2869 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2870 __func__, sh->check_state,
2871 (unsigned long long) sh->sector);
2872 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002873 }
2874}
2875
2876
NeilBrownd1688a62011-10-11 16:49:52 +11002877static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07002878 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10002879 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002880{
Dan Williamsa4456852007-07-09 11:56:43 -07002881 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11002882 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07002883 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07002884
2885 set_bit(STRIPE_HANDLE, &sh->state);
2886
2887 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002888
Dan Williamsa4456852007-07-09 11:56:43 -07002889 /* Want to check and possibly repair P and Q.
2890 * However there could be one 'failed' device, in which
2891 * case we can only check one of them, possibly using the
2892 * other to generate missing data
2893 */
2894
Dan Williamsd82dfee2009-07-14 13:40:57 -07002895 switch (sh->check_state) {
2896 case check_state_idle:
2897 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10002898 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002899 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07002900 * makes sense to check P (If anything else were failed,
2901 * we would have used P to recreate it).
2902 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002903 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07002904 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002905 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002906 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07002907 * anything, so it makes sense to check it
2908 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002909 if (sh->check_state == check_state_run)
2910 sh->check_state = check_state_run_pq;
2911 else
2912 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07002913 }
Dan Williams36d1c642009-07-14 11:48:22 -07002914
Dan Williamsd82dfee2009-07-14 13:40:57 -07002915 /* discard potentially stale zero_sum_result */
2916 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07002917
Dan Williamsd82dfee2009-07-14 13:40:57 -07002918 if (sh->check_state == check_state_run) {
2919 /* async_xor_zero_sum destroys the contents of P */
2920 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
2921 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07002922 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002923 if (sh->check_state >= check_state_run &&
2924 sh->check_state <= check_state_run_pq) {
2925 /* async_syndrome_zero_sum preserves P and Q, so
2926 * no need to mark them !uptodate here
2927 */
2928 set_bit(STRIPE_OP_CHECK, &s->ops_request);
2929 break;
2930 }
Dan Williams36d1c642009-07-14 11:48:22 -07002931
Dan Williamsd82dfee2009-07-14 13:40:57 -07002932 /* we have 2-disk failure */
2933 BUG_ON(s->failed != 2);
2934 /* fall through */
2935 case check_state_compute_result:
2936 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07002937
Dan Williamsd82dfee2009-07-14 13:40:57 -07002938 /* check that a write has not made the stripe insync */
2939 if (test_bit(STRIPE_INSYNC, &sh->state))
2940 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002941
2942 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07002943 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07002944 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002945 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07002946 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10002947 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07002948 s->locked++;
2949 set_bit(R5_LOCKED, &dev->flags);
2950 set_bit(R5_Wantwrite, &dev->flags);
2951 }
2952 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10002953 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07002954 s->locked++;
2955 set_bit(R5_LOCKED, &dev->flags);
2956 set_bit(R5_Wantwrite, &dev->flags);
2957 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002958 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002959 dev = &sh->dev[pd_idx];
2960 s->locked++;
2961 set_bit(R5_LOCKED, &dev->flags);
2962 set_bit(R5_Wantwrite, &dev->flags);
2963 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002964 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002965 dev = &sh->dev[qd_idx];
2966 s->locked++;
2967 set_bit(R5_LOCKED, &dev->flags);
2968 set_bit(R5_Wantwrite, &dev->flags);
2969 }
2970 clear_bit(STRIPE_DEGRADED, &sh->state);
2971
2972 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002973 break;
2974 case check_state_run:
2975 case check_state_run_q:
2976 case check_state_run_pq:
2977 break; /* we will be called again upon completion */
2978 case check_state_check_result:
2979 sh->check_state = check_state_idle;
2980
2981 /* handle a successful check operation, if parity is correct
2982 * we are done. Otherwise update the mismatch count and repair
2983 * parity if !MD_RECOVERY_CHECK
2984 */
2985 if (sh->ops.zero_sum_result == 0) {
2986 /* both parities are correct */
2987 if (!s->failed)
2988 set_bit(STRIPE_INSYNC, &sh->state);
2989 else {
2990 /* in contrast to the raid5 case we can validate
2991 * parity, but still have a failure to write
2992 * back
2993 */
2994 sh->check_state = check_state_compute_result;
2995 /* Returning at this point means that we may go
2996 * off and bring p and/or q uptodate again so
2997 * we make sure to check zero_sum_result again
2998 * to verify if p or q need writeback
2999 */
3000 }
3001 } else {
3002 conf->mddev->resync_mismatches += STRIPE_SECTORS;
3003 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3004 /* don't try to repair!! */
3005 set_bit(STRIPE_INSYNC, &sh->state);
3006 else {
3007 int *target = &sh->ops.target;
3008
3009 sh->ops.target = -1;
3010 sh->ops.target2 = -1;
3011 sh->check_state = check_state_compute_run;
3012 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3013 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3014 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3015 set_bit(R5_Wantcompute,
3016 &sh->dev[pd_idx].flags);
3017 *target = pd_idx;
3018 target = &sh->ops.target2;
3019 s->uptodate++;
3020 }
3021 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3022 set_bit(R5_Wantcompute,
3023 &sh->dev[qd_idx].flags);
3024 *target = qd_idx;
3025 s->uptodate++;
3026 }
3027 }
3028 }
3029 break;
3030 case check_state_compute_run:
3031 break;
3032 default:
3033 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3034 __func__, sh->check_state,
3035 (unsigned long long) sh->sector);
3036 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003037 }
3038}
3039
NeilBrownd1688a62011-10-11 16:49:52 +11003040static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003041{
3042 int i;
3043
3044 /* We have read all the blocks in this stripe and now we need to
3045 * copy some of them into a target stripe for expand.
3046 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003047 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003048 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3049 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003050 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003051 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003052 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003053 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003054
NeilBrown784052e2009-03-31 15:19:07 +11003055 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003056 sector_t s = raid5_compute_sector(conf, bn, 0,
3057 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003058 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003059 if (sh2 == NULL)
3060 /* so far only the early blocks of this stripe
3061 * have been requested. When later blocks
3062 * get requested, we will try again
3063 */
3064 continue;
3065 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3066 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3067 /* must have already done this block */
3068 release_stripe(sh2);
3069 continue;
3070 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003071
3072 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003073 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003074 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003075 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003076 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003077
Dan Williamsa4456852007-07-09 11:56:43 -07003078 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3079 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3080 for (j = 0; j < conf->raid_disks; j++)
3081 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003082 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003083 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3084 break;
3085 if (j == conf->raid_disks) {
3086 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3087 set_bit(STRIPE_HANDLE, &sh2->state);
3088 }
3089 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003090
Dan Williamsa4456852007-07-09 11:56:43 -07003091 }
NeilBrowna2e08552007-09-11 15:23:36 -07003092 /* done submitting copies, wait for them to complete */
3093 if (tx) {
3094 async_tx_ack(tx);
3095 dma_wait_for_async_tx(tx);
3096 }
Dan Williamsa4456852007-07-09 11:56:43 -07003097}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
3099/*
3100 * handle_stripe - do things to a stripe.
3101 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003102 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3103 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003105 * return some read requests which now have data
3106 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 * schedule a read on some buffers
3108 * schedule a write of some buffers
3109 * return confirmation of parity correctness
3110 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 */
Dan Williamsa4456852007-07-09 11:56:43 -07003112
NeilBrownacfe7262011-07-27 11:00:36 +10003113static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003114{
NeilBrownd1688a62011-10-11 16:49:52 +11003115 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003116 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003117 struct r5dev *dev;
3118 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003119 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003120
NeilBrownacfe7262011-07-27 11:00:36 +10003121 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003122
NeilBrownacfe7262011-07-27 11:00:36 +10003123 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3124 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3125 s->failed_num[0] = -1;
3126 s->failed_num[1] = -1;
3127
3128 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003129 rcu_read_lock();
NeilBrownc4c16632011-07-26 11:34:20 +10003130 spin_lock_irq(&conf->device_lock);
NeilBrown16a53ec2006-06-26 00:27:38 -07003131 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003132 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003133 sector_t first_bad;
3134 int bad_sectors;
3135 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003136
NeilBrown16a53ec2006-06-26 00:27:38 -07003137 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003138
Dan Williams45b42332007-07-09 11:56:43 -07003139 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003140 i, dev->flags,
3141 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003142 /* maybe we can reply to a read
3143 *
3144 * new wantfill requests are only permitted while
3145 * ops_complete_biofill is guaranteed to be inactive
3146 */
3147 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3148 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3149 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003150
3151 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003152 if (test_bit(R5_LOCKED, &dev->flags))
3153 s->locked++;
3154 if (test_bit(R5_UPTODATE, &dev->flags))
3155 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003156 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003157 s->compute++;
3158 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003159 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003160
NeilBrownacfe7262011-07-27 11:00:36 +10003161 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003162 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003163 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003164 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003165 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003166 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003167 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003168 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003169 }
Dan Williamsa4456852007-07-09 11:56:43 -07003170 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003171 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003172 /* Prefer to use the replacement for reads, but only
3173 * if it is recovered enough and has no bad blocks.
3174 */
3175 rdev = rcu_dereference(conf->disks[i].replacement);
3176 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3177 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3178 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3179 &first_bad, &bad_sectors))
3180 set_bit(R5_ReadRepl, &dev->flags);
3181 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003182 if (rdev)
3183 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003184 rdev = rcu_dereference(conf->disks[i].rdev);
3185 clear_bit(R5_ReadRepl, &dev->flags);
3186 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003187 if (rdev && test_bit(Faulty, &rdev->flags))
3188 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003189 if (rdev) {
3190 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3191 &first_bad, &bad_sectors);
3192 if (s->blocked_rdev == NULL
3193 && (test_bit(Blocked, &rdev->flags)
3194 || is_bad < 0)) {
3195 if (is_bad < 0)
3196 set_bit(BlockedBadBlocks,
3197 &rdev->flags);
3198 s->blocked_rdev = rdev;
3199 atomic_inc(&rdev->nr_pending);
3200 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003201 }
NeilBrown415e72d2010-06-17 17:25:21 +10003202 clear_bit(R5_Insync, &dev->flags);
3203 if (!rdev)
3204 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003205 else if (is_bad) {
3206 /* also not in-sync */
3207 if (!test_bit(WriteErrorSeen, &rdev->flags)) {
3208 /* treat as in-sync, but with a read error
3209 * which we can now try to correct
3210 */
3211 set_bit(R5_Insync, &dev->flags);
3212 set_bit(R5_ReadError, &dev->flags);
3213 }
3214 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003215 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003216 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003217 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003218 set_bit(R5_Insync, &dev->flags);
3219 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3220 test_bit(R5_Expanded, &dev->flags))
3221 /* If we've reshaped into here, we assume it is Insync.
3222 * We will shortly update recovery_offset to make
3223 * it official.
3224 */
3225 set_bit(R5_Insync, &dev->flags);
3226
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003227 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003228 /* This flag does not apply to '.replacement'
3229 * only to .rdev, so make sure to check that*/
3230 struct md_rdev *rdev2 = rcu_dereference(
3231 conf->disks[i].rdev);
3232 if (rdev2 == rdev)
3233 clear_bit(R5_Insync, &dev->flags);
3234 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003235 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003236 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003237 } else
3238 clear_bit(R5_WriteError, &dev->flags);
3239 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003240 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003241 /* This flag does not apply to '.replacement'
3242 * only to .rdev, so make sure to check that*/
3243 struct md_rdev *rdev2 = rcu_dereference(
3244 conf->disks[i].rdev);
3245 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003246 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003247 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003248 } else
3249 clear_bit(R5_MadeGood, &dev->flags);
3250 }
NeilBrown977df362011-12-23 10:17:53 +11003251 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3252 struct md_rdev *rdev2 = rcu_dereference(
3253 conf->disks[i].replacement);
3254 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3255 s->handle_bad_blocks = 1;
3256 atomic_inc(&rdev2->nr_pending);
3257 } else
3258 clear_bit(R5_MadeGoodRepl, &dev->flags);
3259 }
NeilBrown415e72d2010-06-17 17:25:21 +10003260 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003261 /* The ReadError flag will just be confusing now */
3262 clear_bit(R5_ReadError, &dev->flags);
3263 clear_bit(R5_ReWrite, &dev->flags);
3264 }
NeilBrown415e72d2010-06-17 17:25:21 +10003265 if (test_bit(R5_ReadError, &dev->flags))
3266 clear_bit(R5_Insync, &dev->flags);
3267 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003268 if (s->failed < 2)
3269 s->failed_num[s->failed] = i;
3270 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003271 if (rdev && !test_bit(Faulty, &rdev->flags))
3272 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003273 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003274 }
NeilBrownc4c16632011-07-26 11:34:20 +10003275 spin_unlock_irq(&conf->device_lock);
NeilBrown9a3e1102011-12-23 10:17:53 +11003276 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3277 /* If there is a failed device being replaced,
3278 * we must be recovering.
3279 * else if we are after recovery_cp, we must be syncing
3280 * else we can only be replacing
3281 * sync and recovery both need to read all devices, and so
3282 * use the same flag.
3283 */
3284 if (do_recovery ||
3285 sh->sector >= conf->mddev->recovery_cp)
3286 s->syncing = 1;
3287 else
3288 s->replacing = 1;
3289 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003290 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003291}
NeilBrownf4168852007-02-28 20:11:53 -08003292
NeilBrowncc940152011-07-26 11:35:35 +10003293static void handle_stripe(struct stripe_head *sh)
3294{
3295 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003296 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003297 int i;
NeilBrown84789552011-07-27 11:00:36 +10003298 int prexor;
3299 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003300 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003301
3302 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003303 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003304 /* already being handled, ensure it gets handled
3305 * again when current action finishes */
3306 set_bit(STRIPE_HANDLE, &sh->state);
3307 return;
3308 }
3309
3310 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3311 set_bit(STRIPE_SYNCING, &sh->state);
3312 clear_bit(STRIPE_INSYNC, &sh->state);
3313 }
3314 clear_bit(STRIPE_DELAYED, &sh->state);
3315
3316 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3317 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3318 (unsigned long long)sh->sector, sh->state,
3319 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3320 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003321
NeilBrownacfe7262011-07-27 11:00:36 +10003322 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003323
NeilBrownbc2607f2011-07-28 11:39:22 +10003324 if (s.handle_bad_blocks) {
3325 set_bit(STRIPE_HANDLE, &sh->state);
3326 goto finish;
3327 }
3328
NeilBrown474af965fe2011-07-27 11:00:36 +10003329 if (unlikely(s.blocked_rdev)) {
3330 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003331 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003332 set_bit(STRIPE_HANDLE, &sh->state);
3333 goto finish;
3334 }
3335 /* There is nothing for the blocked_rdev to block */
3336 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3337 s.blocked_rdev = NULL;
3338 }
3339
3340 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3341 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3342 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3343 }
3344
3345 pr_debug("locked=%d uptodate=%d to_read=%d"
3346 " to_write=%d failed=%d failed_num=%d,%d\n",
3347 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3348 s.failed_num[0], s.failed_num[1]);
3349 /* check if the array has lost more than max_degraded devices and,
3350 * if so, some requests might need to be failed.
3351 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003352 if (s.failed > conf->max_degraded) {
3353 sh->check_state = 0;
3354 sh->reconstruct_state = 0;
3355 if (s.to_read+s.to_write+s.written)
3356 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003357 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003358 handle_failed_sync(conf, sh, &s);
3359 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003360
3361 /*
3362 * might be able to return some write requests if the parity blocks
3363 * are safe, or on a failed drive
3364 */
3365 pdev = &sh->dev[sh->pd_idx];
3366 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3367 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3368 qdev = &sh->dev[sh->qd_idx];
3369 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3370 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3371 || conf->level < 6;
3372
3373 if (s.written &&
3374 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3375 && !test_bit(R5_LOCKED, &pdev->flags)
3376 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3377 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3378 && !test_bit(R5_LOCKED, &qdev->flags)
3379 && test_bit(R5_UPTODATE, &qdev->flags)))))
3380 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3381
3382 /* Now we might consider reading some blocks, either to check/generate
3383 * parity, or to satisfy requests
3384 * or to load a block that is being partially written.
3385 */
3386 if (s.to_read || s.non_overwrite
3387 || (conf->level == 6 && s.to_write && s.failed)
NeilBrown9a3e1102011-12-23 10:17:53 +11003388 || (s.syncing && (s.uptodate + s.compute < disks))
3389 || s.replacing
3390 || s.expanding)
NeilBrown474af965fe2011-07-27 11:00:36 +10003391 handle_stripe_fill(sh, &s, disks);
3392
NeilBrown84789552011-07-27 11:00:36 +10003393 /* Now we check to see if any write operations have recently
3394 * completed
3395 */
3396 prexor = 0;
3397 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3398 prexor = 1;
3399 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3400 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3401 sh->reconstruct_state = reconstruct_state_idle;
3402
3403 /* All the 'written' buffers and the parity block are ready to
3404 * be written back to disk
3405 */
3406 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3407 BUG_ON(sh->qd_idx >= 0 &&
3408 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags));
3409 for (i = disks; i--; ) {
3410 struct r5dev *dev = &sh->dev[i];
3411 if (test_bit(R5_LOCKED, &dev->flags) &&
3412 (i == sh->pd_idx || i == sh->qd_idx ||
3413 dev->written)) {
3414 pr_debug("Writing block %d\n", i);
3415 set_bit(R5_Wantwrite, &dev->flags);
3416 if (prexor)
3417 continue;
3418 if (!test_bit(R5_Insync, &dev->flags) ||
3419 ((i == sh->pd_idx || i == sh->qd_idx) &&
3420 s.failed == 0))
3421 set_bit(STRIPE_INSYNC, &sh->state);
3422 }
3423 }
3424 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3425 s.dec_preread_active = 1;
3426 }
3427
3428 /* Now to consider new write requests and what else, if anything
3429 * should be read. We do not handle new writes when:
3430 * 1/ A 'write' operation (copy+xor) is already in flight.
3431 * 2/ A 'check' operation is in flight, as it may clobber the parity
3432 * block.
3433 */
3434 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3435 handle_stripe_dirtying(conf, sh, &s, disks);
3436
3437 /* maybe we need to check and possibly fix the parity for this stripe
3438 * Any reads will already have been scheduled, so we just see if enough
3439 * data is available. The parity check is held off while parity
3440 * dependent operations are in flight.
3441 */
3442 if (sh->check_state ||
3443 (s.syncing && s.locked == 0 &&
3444 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3445 !test_bit(STRIPE_INSYNC, &sh->state))) {
3446 if (conf->level == 6)
3447 handle_parity_checks6(conf, sh, &s, disks);
3448 else
3449 handle_parity_checks5(conf, sh, &s, disks);
3450 }
NeilBrownc5a31002011-07-27 11:00:36 +10003451
NeilBrown9a3e1102011-12-23 10:17:53 +11003452 if (s.replacing && s.locked == 0
3453 && !test_bit(STRIPE_INSYNC, &sh->state)) {
3454 /* Write out to replacement devices where possible */
3455 for (i = 0; i < conf->raid_disks; i++)
3456 if (test_bit(R5_UPTODATE, &sh->dev[i].flags) &&
3457 test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3458 set_bit(R5_WantReplace, &sh->dev[i].flags);
3459 set_bit(R5_LOCKED, &sh->dev[i].flags);
3460 s.locked++;
3461 }
3462 set_bit(STRIPE_INSYNC, &sh->state);
3463 }
3464 if ((s.syncing || s.replacing) && s.locked == 0 &&
3465 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003466 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3467 clear_bit(STRIPE_SYNCING, &sh->state);
3468 }
3469
3470 /* If the failed drives are just a ReadError, then we might need
3471 * to progress the repair/check process
3472 */
3473 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3474 for (i = 0; i < s.failed; i++) {
3475 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3476 if (test_bit(R5_ReadError, &dev->flags)
3477 && !test_bit(R5_LOCKED, &dev->flags)
3478 && test_bit(R5_UPTODATE, &dev->flags)
3479 ) {
3480 if (!test_bit(R5_ReWrite, &dev->flags)) {
3481 set_bit(R5_Wantwrite, &dev->flags);
3482 set_bit(R5_ReWrite, &dev->flags);
3483 set_bit(R5_LOCKED, &dev->flags);
3484 s.locked++;
3485 } else {
3486 /* let's read it back */
3487 set_bit(R5_Wantread, &dev->flags);
3488 set_bit(R5_LOCKED, &dev->flags);
3489 s.locked++;
3490 }
3491 }
3492 }
3493
3494
NeilBrown3687c062011-07-27 11:00:36 +10003495 /* Finish reconstruct operations initiated by the expansion process */
3496 if (sh->reconstruct_state == reconstruct_state_result) {
3497 struct stripe_head *sh_src
3498 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3499 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3500 /* sh cannot be written until sh_src has been read.
3501 * so arrange for sh to be delayed a little
3502 */
3503 set_bit(STRIPE_DELAYED, &sh->state);
3504 set_bit(STRIPE_HANDLE, &sh->state);
3505 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3506 &sh_src->state))
3507 atomic_inc(&conf->preread_active_stripes);
3508 release_stripe(sh_src);
3509 goto finish;
3510 }
3511 if (sh_src)
3512 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003513
NeilBrown3687c062011-07-27 11:00:36 +10003514 sh->reconstruct_state = reconstruct_state_idle;
3515 clear_bit(STRIPE_EXPANDING, &sh->state);
3516 for (i = conf->raid_disks; i--; ) {
3517 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3518 set_bit(R5_LOCKED, &sh->dev[i].flags);
3519 s.locked++;
3520 }
3521 }
3522
3523 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3524 !sh->reconstruct_state) {
3525 /* Need to write out all blocks after computing parity */
3526 sh->disks = conf->raid_disks;
3527 stripe_set_idx(sh->sector, conf, 0, sh);
3528 schedule_reconstruction(sh, &s, 1, 1);
3529 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3530 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3531 atomic_dec(&conf->reshape_stripes);
3532 wake_up(&conf->wait_for_overlap);
3533 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3534 }
3535
3536 if (s.expanding && s.locked == 0 &&
3537 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3538 handle_stripe_expansion(conf, sh);
3539
3540finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003541 /* wait for this device to become unblocked */
NeilBrown43220aa2011-08-31 12:49:14 +10003542 if (conf->mddev->external && unlikely(s.blocked_rdev))
NeilBrownc5709ef2011-07-26 11:35:20 +10003543 md_wait_for_blocked_rdev(s.blocked_rdev, conf->mddev);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003544
NeilBrownbc2607f2011-07-28 11:39:22 +10003545 if (s.handle_bad_blocks)
3546 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003547 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003548 struct r5dev *dev = &sh->dev[i];
3549 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3550 /* We own a safe reference to the rdev */
3551 rdev = conf->disks[i].rdev;
3552 if (!rdev_set_badblocks(rdev, sh->sector,
3553 STRIPE_SECTORS, 0))
3554 md_error(conf->mddev, rdev);
3555 rdev_dec_pending(rdev, conf->mddev);
3556 }
NeilBrownb84db562011-07-28 11:39:23 +10003557 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3558 rdev = conf->disks[i].rdev;
3559 rdev_clear_badblocks(rdev, sh->sector,
3560 STRIPE_SECTORS);
3561 rdev_dec_pending(rdev, conf->mddev);
3562 }
NeilBrown977df362011-12-23 10:17:53 +11003563 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3564 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11003565 if (!rdev)
3566 /* rdev have been moved down */
3567 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11003568 rdev_clear_badblocks(rdev, sh->sector,
3569 STRIPE_SECTORS);
3570 rdev_dec_pending(rdev, conf->mddev);
3571 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003572 }
3573
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003574 if (s.ops_request)
3575 raid_run_ops(sh, s.ops_request);
3576
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003577 ops_run_io(sh, &s);
3578
NeilBrownc5709ef2011-07-26 11:35:20 +10003579 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003580 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003581 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003582 * have actually been submitted.
3583 */
3584 atomic_dec(&conf->preread_active_stripes);
3585 if (atomic_read(&conf->preread_active_stripes) <
3586 IO_THRESHOLD)
3587 md_wakeup_thread(conf->mddev->thread);
3588 }
3589
NeilBrownc5709ef2011-07-26 11:35:20 +10003590 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003591
Dan Williams257a4b42011-11-08 16:22:06 +11003592 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003593}
3594
NeilBrownd1688a62011-10-11 16:49:52 +11003595static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596{
3597 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3598 while (!list_empty(&conf->delayed_list)) {
3599 struct list_head *l = conf->delayed_list.next;
3600 struct stripe_head *sh;
3601 sh = list_entry(l, struct stripe_head, lru);
3602 list_del_init(l);
3603 clear_bit(STRIPE_DELAYED, &sh->state);
3604 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3605 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003606 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 }
NeilBrown482c0832011-04-18 18:25:42 +10003608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609}
3610
NeilBrownd1688a62011-10-11 16:49:52 +11003611static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003612{
3613 /* device_lock is held */
3614 struct list_head head;
3615 list_add(&head, &conf->bitmap_list);
3616 list_del_init(&conf->bitmap_list);
3617 while (!list_empty(&head)) {
3618 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3619 list_del_init(&sh->lru);
3620 atomic_inc(&sh->count);
3621 __release_stripe(conf, sh);
3622 }
3623}
3624
NeilBrownfd01b882011-10-11 16:47:53 +11003625int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003626{
NeilBrownd1688a62011-10-11 16:49:52 +11003627 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003628
3629 /* No difference between reads and writes. Just check
3630 * how busy the stripe_cache is
3631 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003632
NeilBrownf022b2f2006-10-03 01:15:56 -07003633 if (conf->inactive_blocked)
3634 return 1;
3635 if (conf->quiesce)
3636 return 1;
3637 if (list_empty_careful(&conf->inactive_list))
3638 return 1;
3639
3640 return 0;
3641}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003642EXPORT_SYMBOL_GPL(md_raid5_congested);
3643
3644static int raid5_congested(void *data, int bits)
3645{
NeilBrownfd01b882011-10-11 16:47:53 +11003646 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003647
3648 return mddev_congested(mddev, bits) ||
3649 md_raid5_congested(mddev, bits);
3650}
NeilBrownf022b2f2006-10-03 01:15:56 -07003651
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003652/* We want read requests to align with chunks where possible,
3653 * but write requests don't need to.
3654 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003655static int raid5_mergeable_bvec(struct request_queue *q,
3656 struct bvec_merge_data *bvm,
3657 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003658{
NeilBrownfd01b882011-10-11 16:47:53 +11003659 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003660 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003661 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003662 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003663 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003664
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003665 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003666 return biovec->bv_len; /* always allow writes to be mergeable */
3667
Andre Noll664e7c42009-06-18 08:45:27 +10003668 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3669 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003670 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3671 if (max < 0) max = 0;
3672 if (max <= biovec->bv_len && bio_sectors == 0)
3673 return biovec->bv_len;
3674 else
3675 return max;
3676}
3677
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003678
NeilBrownfd01b882011-10-11 16:47:53 +11003679static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003680{
3681 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003682 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003683 unsigned int bio_sectors = bio->bi_size >> 9;
3684
Andre Noll664e7c42009-06-18 08:45:27 +10003685 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3686 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003687 return chunk_sectors >=
3688 ((sector & (chunk_sectors - 1)) + bio_sectors);
3689}
3690
3691/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003692 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3693 * later sampled by raid5d.
3694 */
NeilBrownd1688a62011-10-11 16:49:52 +11003695static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003696{
3697 unsigned long flags;
3698
3699 spin_lock_irqsave(&conf->device_lock, flags);
3700
3701 bi->bi_next = conf->retry_read_aligned_list;
3702 conf->retry_read_aligned_list = bi;
3703
3704 spin_unlock_irqrestore(&conf->device_lock, flags);
3705 md_wakeup_thread(conf->mddev->thread);
3706}
3707
3708
NeilBrownd1688a62011-10-11 16:49:52 +11003709static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003710{
3711 struct bio *bi;
3712
3713 bi = conf->retry_read_aligned;
3714 if (bi) {
3715 conf->retry_read_aligned = NULL;
3716 return bi;
3717 }
3718 bi = conf->retry_read_aligned_list;
3719 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003720 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003721 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003722 /*
3723 * this sets the active strip count to 1 and the processed
3724 * strip count to zero (upper 8 bits)
3725 */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003726 bi->bi_phys_segments = 1; /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003727 }
3728
3729 return bi;
3730}
3731
3732
3733/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003734 * The "raid5_align_endio" should check if the read succeeded and if it
3735 * did, call bio_endio on the original bio (having bio_put the new bio
3736 * first).
3737 * If the read failed..
3738 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003739static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003740{
3741 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003742 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003743 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003744 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003745 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003746
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003747 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003748
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003749 rdev = (void*)raid_bi->bi_next;
3750 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003751 mddev = rdev->mddev;
3752 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003753
3754 rdev_dec_pending(rdev, conf->mddev);
3755
3756 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003757 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003758 if (atomic_dec_and_test(&conf->active_aligned_reads))
3759 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003760 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003761 }
3762
3763
Dan Williams45b42332007-07-09 11:56:43 -07003764 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003765
3766 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003767}
3768
Neil Brown387bb172007-02-08 14:20:29 -08003769static int bio_fits_rdev(struct bio *bi)
3770{
Jens Axboe165125e2007-07-24 09:28:11 +02003771 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003772
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003773 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003774 return 0;
3775 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003776 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003777 return 0;
3778
3779 if (q->merge_bvec_fn)
3780 /* it's too hard to apply the merge_bvec_fn at this stage,
3781 * just just give up
3782 */
3783 return 0;
3784
3785 return 1;
3786}
3787
3788
NeilBrownfd01b882011-10-11 16:47:53 +11003789static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003790{
NeilBrownd1688a62011-10-11 16:49:52 +11003791 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11003792 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003793 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11003794 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11003795 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003796
3797 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003798 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003799 return 0;
3800 }
3801 /*
NeilBrowna167f662010-10-26 18:31:13 +11003802 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003803 */
NeilBrowna167f662010-10-26 18:31:13 +11003804 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003805 if (!align_bi)
3806 return 0;
3807 /*
3808 * set bi_end_io to a new function, and set bi_private to the
3809 * original bio.
3810 */
3811 align_bi->bi_end_io = raid5_align_endio;
3812 align_bi->bi_private = raid_bio;
3813 /*
3814 * compute position
3815 */
NeilBrown112bf892009-03-31 14:39:38 +11003816 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3817 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003818 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003819
NeilBrown671488c2011-12-23 10:17:52 +11003820 end_sector = align_bi->bi_sector + (align_bi->bi_size >> 9);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003821 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11003822 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
3823 if (!rdev || test_bit(Faulty, &rdev->flags) ||
3824 rdev->recovery_offset < end_sector) {
3825 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3826 if (rdev &&
3827 (test_bit(Faulty, &rdev->flags) ||
3828 !(test_bit(In_sync, &rdev->flags) ||
3829 rdev->recovery_offset >= end_sector)))
3830 rdev = NULL;
3831 }
3832 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10003833 sector_t first_bad;
3834 int bad_sectors;
3835
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003836 atomic_inc(&rdev->nr_pending);
3837 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003838 raid_bio->bi_next = (void*)rdev;
3839 align_bi->bi_bdev = rdev->bdev;
3840 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
3841 align_bi->bi_sector += rdev->data_offset;
3842
NeilBrown31c176e2011-07-28 11:39:22 +10003843 if (!bio_fits_rdev(align_bi) ||
3844 is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
3845 &first_bad, &bad_sectors)) {
3846 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08003847 bio_put(align_bi);
3848 rdev_dec_pending(rdev, mddev);
3849 return 0;
3850 }
3851
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003852 spin_lock_irq(&conf->device_lock);
3853 wait_event_lock_irq(conf->wait_for_stripe,
3854 conf->quiesce == 0,
3855 conf->device_lock, /* nothing */);
3856 atomic_inc(&conf->active_aligned_reads);
3857 spin_unlock_irq(&conf->device_lock);
3858
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003859 generic_make_request(align_bi);
3860 return 1;
3861 } else {
3862 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003863 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003864 return 0;
3865 }
3866}
3867
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003868/* __get_priority_stripe - get the next stripe to process
3869 *
3870 * Full stripe writes are allowed to pass preread active stripes up until
3871 * the bypass_threshold is exceeded. In general the bypass_count
3872 * increments when the handle_list is handled before the hold_list; however, it
3873 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3874 * stripe with in flight i/o. The bypass_count will be reset when the
3875 * head of the hold_list has changed, i.e. the head was promoted to the
3876 * handle_list.
3877 */
NeilBrownd1688a62011-10-11 16:49:52 +11003878static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003879{
3880 struct stripe_head *sh;
3881
3882 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3883 __func__,
3884 list_empty(&conf->handle_list) ? "empty" : "busy",
3885 list_empty(&conf->hold_list) ? "empty" : "busy",
3886 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3887
3888 if (!list_empty(&conf->handle_list)) {
3889 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3890
3891 if (list_empty(&conf->hold_list))
3892 conf->bypass_count = 0;
3893 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3894 if (conf->hold_list.next == conf->last_hold)
3895 conf->bypass_count++;
3896 else {
3897 conf->last_hold = conf->hold_list.next;
3898 conf->bypass_count -= conf->bypass_threshold;
3899 if (conf->bypass_count < 0)
3900 conf->bypass_count = 0;
3901 }
3902 }
3903 } else if (!list_empty(&conf->hold_list) &&
3904 ((conf->bypass_threshold &&
3905 conf->bypass_count > conf->bypass_threshold) ||
3906 atomic_read(&conf->pending_full_writes) == 0)) {
3907 sh = list_entry(conf->hold_list.next,
3908 typeof(*sh), lru);
3909 conf->bypass_count -= conf->bypass_threshold;
3910 if (conf->bypass_count < 0)
3911 conf->bypass_count = 0;
3912 } else
3913 return NULL;
3914
3915 list_del_init(&sh->lru);
3916 atomic_inc(&sh->count);
3917 BUG_ON(atomic_read(&sh->count) != 1);
3918 return sh;
3919}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003920
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07003921static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922{
NeilBrownd1688a62011-10-11 16:49:52 +11003923 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11003924 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 sector_t new_sector;
3926 sector_t logical_sector, last_sector;
3927 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01003928 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11003929 int remaining;
NeilBrown7c13edc2011-04-18 18:25:43 +10003930 int plugged;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931
Tejun Heoe9c74692010-09-03 11:56:18 +02003932 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
3933 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02003934 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07003935 }
3936
NeilBrown3d310eb2005-06-21 17:17:26 -07003937 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07003938
NeilBrown802ba062006-12-13 00:34:13 -08003939 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003940 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11003941 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02003942 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003943
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
3945 last_sector = bi->bi_sector + (bi->bi_size>>9);
3946 bi->bi_next = NULL;
3947 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07003948
NeilBrown7c13edc2011-04-18 18:25:43 +10003949 plugged = mddev_check_plugged(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
3951 DEFINE_WAIT(w);
NeilBrown16a53ec2006-06-26 00:27:38 -07003952 int disks, data_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003953 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08003954
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003955 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11003956 previous = 0;
NeilBrownb0f9ec02009-03-31 15:27:18 +11003957 disks = conf->raid_disks;
NeilBrownb578d552006-03-27 01:18:12 -08003958 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11003959 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11003960 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08003961 * 64bit on a 32bit platform, and so it might be
3962 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02003963 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08003964 * the lock is dropped, so once we get a reference
3965 * to the stripe that we think it is, we will have
3966 * to check again.
3967 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003968 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11003969 if (mddev->delta_disks < 0
3970 ? logical_sector < conf->reshape_progress
3971 : logical_sector >= conf->reshape_progress) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003972 disks = conf->previous_raid_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003973 previous = 1;
3974 } else {
NeilBrownfef9c612009-03-31 15:16:46 +11003975 if (mddev->delta_disks < 0
3976 ? logical_sector < conf->reshape_safe
3977 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08003978 spin_unlock_irq(&conf->device_lock);
3979 schedule();
3980 goto retry;
3981 }
3982 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003983 spin_unlock_irq(&conf->device_lock);
3984 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003985 data_disks = disks - conf->max_degraded;
3986
NeilBrown112bf892009-03-31 14:39:38 +11003987 new_sector = raid5_compute_sector(conf, logical_sector,
3988 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003989 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10003990 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 (unsigned long long)new_sector,
3992 (unsigned long long)logical_sector);
3993
NeilBrownb5663ba2009-03-31 14:39:38 +11003994 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10003995 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11003997 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003998 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08003999 * stripe, so we must do the range check again.
4000 * Expansion could still move past after this
4001 * test, but as we are holding a reference to
4002 * 'sh', we know that if that happens,
4003 * STRIPE_EXPANDING will get set and the expansion
4004 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004005 */
4006 int must_retry = 0;
4007 spin_lock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004008 if (mddev->delta_disks < 0
4009 ? logical_sector >= conf->reshape_progress
4010 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004011 /* mismatch, need to try again */
4012 must_retry = 1;
4013 spin_unlock_irq(&conf->device_lock);
4014 if (must_retry) {
4015 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004016 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004017 goto retry;
4018 }
4019 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004020
Namhyung Kimffd96e32011-07-18 17:38:51 +10004021 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004022 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004023 logical_sector < mddev->suspend_hi) {
4024 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004025 /* As the suspend_* range is controlled by
4026 * userspace, we want an interruptible
4027 * wait.
4028 */
4029 flush_signals(current);
4030 prepare_to_wait(&conf->wait_for_overlap,
4031 &w, TASK_INTERRUPTIBLE);
4032 if (logical_sector >= mddev->suspend_lo &&
4033 logical_sector < mddev->suspend_hi)
4034 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004035 goto retry;
4036 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004037
4038 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004039 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004040 /* Stripe is busy expanding or
4041 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 * and wait a while
4043 */
NeilBrown482c0832011-04-18 18:25:42 +10004044 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 release_stripe(sh);
4046 schedule();
4047 goto retry;
4048 }
4049 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004050 set_bit(STRIPE_HANDLE, &sh->state);
4051 clear_bit(STRIPE_DELAYED, &sh->state);
Tejun Heoe9c74692010-09-03 11:56:18 +02004052 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004053 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4054 atomic_inc(&conf->preread_active_stripes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 } else {
4057 /* cannot get stripe for read-ahead, just give-up */
4058 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4059 finish_wait(&conf->wait_for_overlap, &w);
4060 break;
4061 }
4062
4063 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004064 if (!plugged)
4065 md_wakeup_thread(mddev->thread);
4066
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004068 remaining = raid5_dec_bi_phys_segments(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004069 spin_unlock_irq(&conf->device_lock);
4070 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071
NeilBrown16a53ec2006-06-26 00:27:38 -07004072 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004074
Neil Brown0e13fe232008-06-28 08:31:20 +10004075 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077}
4078
NeilBrownfd01b882011-10-11 16:47:53 +11004079static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004080
NeilBrownfd01b882011-10-11 16:47:53 +11004081static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082{
NeilBrown52c03292006-06-26 00:27:43 -07004083 /* reshaping is quite different to recovery/resync so it is
4084 * handled quite separately ... here.
4085 *
4086 * On each call to sync_request, we gather one chunk worth of
4087 * destination stripes and flag them as expanding.
4088 * Then we find all the source stripes and request reads.
4089 * As the reads complete, handle_stripe will copy the data
4090 * into the destination stripe and release that stripe.
4091 */
NeilBrownd1688a62011-10-11 16:49:52 +11004092 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004094 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004095 int raid_disks = conf->previous_raid_disks;
4096 int data_disks = raid_disks - conf->max_degraded;
4097 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004098 int i;
4099 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004100 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004101 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004102 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004103 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004104
NeilBrownfef9c612009-03-31 15:16:46 +11004105 if (sector_nr == 0) {
4106 /* If restarting in the middle, skip the initial sectors */
4107 if (mddev->delta_disks < 0 &&
4108 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4109 sector_nr = raid5_size(mddev, 0, 0)
4110 - conf->reshape_progress;
NeilBrowna6397552009-08-13 10:13:00 +10004111 } else if (mddev->delta_disks >= 0 &&
NeilBrownfef9c612009-03-31 15:16:46 +11004112 conf->reshape_progress > 0)
4113 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004114 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004115 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004116 mddev->curr_resync_completed = sector_nr;
4117 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004118 *skipped = 1;
4119 return sector_nr;
4120 }
NeilBrown52c03292006-06-26 00:27:43 -07004121 }
4122
NeilBrown7a661382009-03-31 15:21:40 +11004123 /* We need to process a full chunk at a time.
4124 * If old and new chunk sizes differ, we need to process the
4125 * largest of these
4126 */
Andre Noll664e7c42009-06-18 08:45:27 +10004127 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4128 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004129 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004130 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004131
NeilBrown52c03292006-06-26 00:27:43 -07004132 /* we update the metadata when there is more than 3Meg
4133 * in the block range (that is rather arbitrary, should
4134 * probably be time based) or when the data about to be
4135 * copied would over-write the source of the data at
4136 * the front of the range.
NeilBrownfef9c612009-03-31 15:16:46 +11004137 * i.e. one new_stripe along from reshape_progress new_maps
4138 * to after where reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004139 */
NeilBrownfef9c612009-03-31 15:16:46 +11004140 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004141 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004142 readpos = conf->reshape_progress;
4143 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004144 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004145 sector_div(safepos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004146 if (mddev->delta_disks < 0) {
NeilBrowned37d832009-05-27 21:39:05 +10004147 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004148 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004149 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004150 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004151 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004152 readpos -= min_t(sector_t, reshape_sectors, readpos);
4153 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004154 }
NeilBrown52c03292006-06-26 00:27:43 -07004155
NeilBrownc8f517c2009-03-31 15:28:40 +11004156 /* 'writepos' is the most advanced device address we might write.
4157 * 'readpos' is the least advanced device address we might read.
4158 * 'safepos' is the least address recorded in the metadata as having
4159 * been reshaped.
4160 * If 'readpos' is behind 'writepos', then there is no way that we can
4161 * ensure safety in the face of a crash - that must be done by userspace
4162 * making a backup of the data. So in that case there is no particular
4163 * rush to update metadata.
4164 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4165 * update the metadata to advance 'safepos' to match 'readpos' so that
4166 * we can be safe in the event of a crash.
4167 * So we insist on updating metadata if safepos is behind writepos and
4168 * readpos is beyond writepos.
4169 * In any case, update the metadata every 10 seconds.
4170 * Maybe that number should be configurable, but I'm not sure it is
4171 * worth it.... maybe it could be a multiple of safemode_delay???
4172 */
NeilBrownfef9c612009-03-31 15:16:46 +11004173 if ((mddev->delta_disks < 0
NeilBrownc8f517c2009-03-31 15:28:40 +11004174 ? (safepos > writepos && readpos < writepos)
4175 : (safepos < writepos && readpos > writepos)) ||
4176 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004177 /* Cannot proceed until we've updated the superblock... */
4178 wait_event(conf->wait_for_overlap,
4179 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004180 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004181 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004182 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07004183 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004184 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07004185 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004186 kthread_should_stop());
4187 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004188 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004189 spin_unlock_irq(&conf->device_lock);
4190 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004191 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004192 }
4193
NeilBrownec32a2b2009-03-31 15:17:38 +11004194 if (mddev->delta_disks < 0) {
4195 BUG_ON(conf->reshape_progress == 0);
4196 stripe_addr = writepos;
4197 BUG_ON((mddev->dev_sectors &
NeilBrown7a661382009-03-31 15:21:40 +11004198 ~((sector_t)reshape_sectors - 1))
4199 - reshape_sectors - stripe_addr
NeilBrownec32a2b2009-03-31 15:17:38 +11004200 != sector_nr);
4201 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004202 BUG_ON(writepos != sector_nr + reshape_sectors);
NeilBrownec32a2b2009-03-31 15:17:38 +11004203 stripe_addr = sector_nr;
4204 }
NeilBrownab69ae12009-03-31 15:26:47 +11004205 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004206 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004207 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004208 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004209 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004210 set_bit(STRIPE_EXPANDING, &sh->state);
4211 atomic_inc(&conf->reshape_stripes);
4212 /* If any of this stripe is beyond the end of the old
4213 * array, then we need to zero those blocks
4214 */
4215 for (j=sh->disks; j--;) {
4216 sector_t s;
4217 if (j == sh->pd_idx)
4218 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004219 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004220 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004221 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004222 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004223 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004224 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004225 continue;
4226 }
4227 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4228 set_bit(R5_Expanded, &sh->dev[j].flags);
4229 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4230 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004231 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004232 set_bit(STRIPE_EXPAND_READY, &sh->state);
4233 set_bit(STRIPE_HANDLE, &sh->state);
4234 }
NeilBrownab69ae12009-03-31 15:26:47 +11004235 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004236 }
4237 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004238 if (mddev->delta_disks < 0)
NeilBrown7a661382009-03-31 15:21:40 +11004239 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004240 else
NeilBrown7a661382009-03-31 15:21:40 +11004241 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004242 spin_unlock_irq(&conf->device_lock);
4243 /* Ok, those stripe are ready. We can start scheduling
4244 * reads on the source stripes.
4245 * The source stripes are determined by mapping the first and last
4246 * block on the destination stripes.
4247 */
NeilBrown52c03292006-06-26 00:27:43 -07004248 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004249 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004250 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004251 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004252 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004253 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004254 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004255 if (last_sector >= mddev->dev_sectors)
4256 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004257 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004258 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004259 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4260 set_bit(STRIPE_HANDLE, &sh->state);
4261 release_stripe(sh);
4262 first_sector += STRIPE_SECTORS;
4263 }
NeilBrownab69ae12009-03-31 15:26:47 +11004264 /* Now that the sources are clearly marked, we can release
4265 * the destination stripes
4266 */
4267 while (!list_empty(&stripes)) {
4268 sh = list_entry(stripes.next, struct stripe_head, lru);
4269 list_del_init(&sh->lru);
4270 release_stripe(sh);
4271 }
NeilBrownc6207272008-02-06 01:39:52 -08004272 /* If this takes us to the resync_max point where we have to pause,
4273 * then we need to write out the superblock.
4274 */
NeilBrown7a661382009-03-31 15:21:40 +11004275 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004276 if ((sector_nr - mddev->curr_resync_completed) * 2
4277 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004278 /* Cannot proceed until we've updated the superblock... */
4279 wait_event(conf->wait_for_overlap,
4280 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004281 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004282 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004283 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004284 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4285 md_wakeup_thread(mddev->thread);
4286 wait_event(mddev->sb_wait,
4287 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4288 || kthread_should_stop());
4289 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004290 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004291 spin_unlock_irq(&conf->device_lock);
4292 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004293 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004294 }
NeilBrown7a661382009-03-31 15:21:40 +11004295 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004296}
4297
4298/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004299static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004300{
NeilBrownd1688a62011-10-11 16:49:52 +11004301 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004302 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004303 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004304 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004305 int still_degraded = 0;
4306 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307
NeilBrown72626682005-09-09 16:23:54 -07004308 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004310
NeilBrown29269552006-03-27 01:18:10 -08004311 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4312 end_reshape(conf);
4313 return 0;
4314 }
NeilBrown72626682005-09-09 16:23:54 -07004315
4316 if (mddev->curr_resync < max_sector) /* aborted */
4317 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4318 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004319 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004320 conf->fullsync = 0;
4321 bitmap_close_sync(mddev->bitmap);
4322
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 return 0;
4324 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004325
NeilBrown64bd6602009-08-03 10:59:58 +10004326 /* Allow raid5_quiesce to complete */
4327 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4328
NeilBrown52c03292006-06-26 00:27:43 -07004329 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4330 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004331
NeilBrownc6207272008-02-06 01:39:52 -08004332 /* No need to check resync_max as we never do more than one
4333 * stripe, and as resync_max will always be on a chunk boundary,
4334 * if the check in md_do_sync didn't fire, there is no chance
4335 * of overstepping resync_max here
4336 */
4337
NeilBrown16a53ec2006-06-26 00:27:38 -07004338 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 * to resync, then assert that we are finished, because there is
4340 * nothing we can do.
4341 */
NeilBrown3285edf2006-06-26 00:27:55 -07004342 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004343 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004344 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004345 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 return rv;
4347 }
NeilBrown72626682005-09-09 16:23:54 -07004348 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004349 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004350 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4351 /* we can skip this block, and probably more */
4352 sync_blocks /= STRIPE_SECTORS;
4353 *skipped = 1;
4354 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356
NeilBrownb47490c2008-02-06 01:39:50 -08004357 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4358
NeilBrowna8c906c2009-06-09 14:39:59 +10004359 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004361 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004363 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004365 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004367 /* Need to check if array will still be degraded after recovery/resync
4368 * We don't need to check the 'failed' flag as when that gets set,
4369 * recovery aborts.
4370 */
NeilBrownf001a702009-06-09 14:30:31 +10004371 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004372 if (conf->disks[i].rdev == NULL)
4373 still_degraded = 1;
4374
4375 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4376
NeilBrown83206d62011-07-26 11:19:49 +10004377 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378
NeilBrown14425772009-10-16 15:55:25 +11004379 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 release_stripe(sh);
4381
4382 return STRIPE_SECTORS;
4383}
4384
NeilBrownd1688a62011-10-11 16:49:52 +11004385static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004386{
4387 /* We may not be able to submit a whole bio at once as there
4388 * may not be enough stripe_heads available.
4389 * We cannot pre-allocate enough stripe_heads as we may need
4390 * more than exist in the cache (if we allow ever large chunks).
4391 * So we do one stripe head at a time and record in
4392 * ->bi_hw_segments how many have been done.
4393 *
4394 * We *know* that this entire raid_bio is in one chunk, so
4395 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4396 */
4397 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004398 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004399 sector_t sector, logical_sector, last_sector;
4400 int scnt = 0;
4401 int remaining;
4402 int handled = 0;
4403
4404 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004405 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004406 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004407 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4408
4409 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004410 logical_sector += STRIPE_SECTORS,
4411 sector += STRIPE_SECTORS,
4412 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004413
Jens Axboe960e7392008-08-15 10:41:18 +02004414 if (scnt < raid5_bi_hw_segments(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004415 /* already done this stripe */
4416 continue;
4417
NeilBrowna8c906c2009-06-09 14:39:59 +10004418 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004419
4420 if (!sh) {
4421 /* failed to get a stripe - must wait */
Jens Axboe960e7392008-08-15 10:41:18 +02004422 raid5_set_bi_hw_segments(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004423 conf->retry_read_aligned = raid_bio;
4424 return handled;
4425 }
4426
Neil Brown387bb172007-02-08 14:20:29 -08004427 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4428 release_stripe(sh);
Jens Axboe960e7392008-08-15 10:41:18 +02004429 raid5_set_bi_hw_segments(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004430 conf->retry_read_aligned = raid_bio;
4431 return handled;
4432 }
4433
Dan Williams36d1c642009-07-14 11:48:22 -07004434 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004435 release_stripe(sh);
4436 handled++;
4437 }
4438 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004439 remaining = raid5_dec_bi_phys_segments(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004440 spin_unlock_irq(&conf->device_lock);
Neil Brown0e13fe232008-06-28 08:31:20 +10004441 if (remaining == 0)
4442 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004443 if (atomic_dec_and_test(&conf->active_aligned_reads))
4444 wake_up(&conf->wait_for_stripe);
4445 return handled;
4446}
4447
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004448
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449/*
4450 * This is our raid5 kernel thread.
4451 *
4452 * We scan the hash table for stripes which can be handled now.
4453 * During the scan, completed stripes are saved for us by the interrupt
4454 * handler, so that they will not have to wait for our next wakeup.
4455 */
NeilBrownfd01b882011-10-11 16:47:53 +11004456static void raid5d(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457{
4458 struct stripe_head *sh;
NeilBrownd1688a62011-10-11 16:49:52 +11004459 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004461 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462
Dan Williams45b42332007-07-09 11:56:43 -07004463 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464
4465 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004467 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 handled = 0;
4469 spin_lock_irq(&conf->device_lock);
4470 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004471 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472
NeilBrown7c13edc2011-04-18 18:25:43 +10004473 if (atomic_read(&mddev->plug_cnt) == 0 &&
4474 !list_empty(&conf->bitmap_list)) {
4475 /* Now is a good time to flush some bitmap updates */
4476 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004477 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004478 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004479 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004480 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004481 activate_bit_delay(conf);
4482 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004483 if (atomic_read(&mddev->plug_cnt) == 0)
4484 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004485
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004486 while ((bio = remove_bio_from_retry(conf))) {
4487 int ok;
4488 spin_unlock_irq(&conf->device_lock);
4489 ok = retry_aligned_read(conf, bio);
4490 spin_lock_irq(&conf->device_lock);
4491 if (!ok)
4492 break;
4493 handled++;
4494 }
4495
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004496 sh = __get_priority_stripe(conf);
4497
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004498 if (!sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 spin_unlock_irq(&conf->device_lock);
4501
4502 handled++;
Dan Williams417b8d42009-10-16 16:25:22 +11004503 handle_stripe(sh);
4504 release_stripe(sh);
4505 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506
NeilBrownde393cd2011-07-28 11:31:48 +10004507 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
4508 md_check_recovery(mddev);
4509
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 spin_lock_irq(&conf->device_lock);
4511 }
Dan Williams45b42332007-07-09 11:56:43 -07004512 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513
4514 spin_unlock_irq(&conf->device_lock);
4515
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004516 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004517 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518
Dan Williams45b42332007-07-09 11:56:43 -07004519 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520}
4521
NeilBrown3f294f42005-11-08 21:39:25 -08004522static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004523raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004524{
NeilBrownd1688a62011-10-11 16:49:52 +11004525 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004526 if (conf)
4527 return sprintf(page, "%d\n", conf->max_nr_stripes);
4528 else
4529 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004530}
4531
NeilBrownc41d4ac2010-06-01 19:37:24 +10004532int
NeilBrownfd01b882011-10-11 16:47:53 +11004533raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004534{
NeilBrownd1688a62011-10-11 16:49:52 +11004535 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004536 int err;
4537
4538 if (size <= 16 || size > 32768)
4539 return -EINVAL;
4540 while (size < conf->max_nr_stripes) {
4541 if (drop_one_stripe(conf))
4542 conf->max_nr_stripes--;
4543 else
4544 break;
4545 }
4546 err = md_allow_write(mddev);
4547 if (err)
4548 return err;
4549 while (size > conf->max_nr_stripes) {
4550 if (grow_one_stripe(conf))
4551 conf->max_nr_stripes++;
4552 else break;
4553 }
4554 return 0;
4555}
4556EXPORT_SYMBOL(raid5_set_cache_size);
4557
NeilBrown3f294f42005-11-08 21:39:25 -08004558static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004559raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004560{
NeilBrownd1688a62011-10-11 16:49:52 +11004561 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004562 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004563 int err;
4564
NeilBrown3f294f42005-11-08 21:39:25 -08004565 if (len >= PAGE_SIZE)
4566 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004567 if (!conf)
4568 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004569
Dan Williams4ef197d82008-04-28 02:15:54 -07004570 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004571 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004572 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004573 if (err)
4574 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004575 return len;
4576}
NeilBrown007583c2005-11-08 21:39:30 -08004577
NeilBrown96de1e62005-11-08 21:39:39 -08004578static struct md_sysfs_entry
4579raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4580 raid5_show_stripe_cache_size,
4581 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004582
4583static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004584raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004585{
NeilBrownd1688a62011-10-11 16:49:52 +11004586 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004587 if (conf)
4588 return sprintf(page, "%d\n", conf->bypass_threshold);
4589 else
4590 return 0;
4591}
4592
4593static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004594raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004595{
NeilBrownd1688a62011-10-11 16:49:52 +11004596 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004597 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004598 if (len >= PAGE_SIZE)
4599 return -EINVAL;
4600 if (!conf)
4601 return -ENODEV;
4602
Dan Williams4ef197d82008-04-28 02:15:54 -07004603 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004604 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004605 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004606 return -EINVAL;
4607 conf->bypass_threshold = new;
4608 return len;
4609}
4610
4611static struct md_sysfs_entry
4612raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4613 S_IRUGO | S_IWUSR,
4614 raid5_show_preread_threshold,
4615 raid5_store_preread_threshold);
4616
4617static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004618stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004619{
NeilBrownd1688a62011-10-11 16:49:52 +11004620 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004621 if (conf)
4622 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4623 else
4624 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004625}
4626
NeilBrown96de1e62005-11-08 21:39:39 -08004627static struct md_sysfs_entry
4628raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004629
NeilBrown007583c2005-11-08 21:39:30 -08004630static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004631 &raid5_stripecache_size.attr,
4632 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004633 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004634 NULL,
4635};
NeilBrown007583c2005-11-08 21:39:30 -08004636static struct attribute_group raid5_attrs_group = {
4637 .name = NULL,
4638 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004639};
4640
Dan Williams80c3a6c2009-03-17 18:10:40 -07004641static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11004642raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07004643{
NeilBrownd1688a62011-10-11 16:49:52 +11004644 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004645
4646 if (!sectors)
4647 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004648 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11004649 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11004650 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004651
Andre Noll9d8f0362009-06-18 08:45:01 +10004652 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004653 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004654 return sectors * (raid_disks - conf->max_degraded);
4655}
4656
NeilBrownd1688a62011-10-11 16:49:52 +11004657static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004658{
4659 struct raid5_percpu *percpu;
4660 unsigned long cpu;
4661
4662 if (!conf->percpu)
4663 return;
4664
4665 get_online_cpus();
4666 for_each_possible_cpu(cpu) {
4667 percpu = per_cpu_ptr(conf->percpu, cpu);
4668 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004669 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004670 }
4671#ifdef CONFIG_HOTPLUG_CPU
4672 unregister_cpu_notifier(&conf->cpu_notify);
4673#endif
4674 put_online_cpus();
4675
4676 free_percpu(conf->percpu);
4677}
4678
NeilBrownd1688a62011-10-11 16:49:52 +11004679static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10004680{
4681 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004682 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10004683 kfree(conf->disks);
4684 kfree(conf->stripe_hashtbl);
4685 kfree(conf);
4686}
4687
Dan Williams36d1c642009-07-14 11:48:22 -07004688#ifdef CONFIG_HOTPLUG_CPU
4689static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4690 void *hcpu)
4691{
NeilBrownd1688a62011-10-11 16:49:52 +11004692 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07004693 long cpu = (long)hcpu;
4694 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4695
4696 switch (action) {
4697 case CPU_UP_PREPARE:
4698 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07004699 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07004700 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004701 if (!percpu->scribble)
4702 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4703
4704 if (!percpu->scribble ||
4705 (conf->level == 6 && !percpu->spare_page)) {
4706 safe_put_page(percpu->spare_page);
4707 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004708 pr_err("%s: failed memory allocation for cpu%ld\n",
4709 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07004710 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07004711 }
4712 break;
4713 case CPU_DEAD:
4714 case CPU_DEAD_FROZEN:
4715 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004716 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004717 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004718 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07004719 break;
4720 default:
4721 break;
4722 }
4723 return NOTIFY_OK;
4724}
4725#endif
4726
NeilBrownd1688a62011-10-11 16:49:52 +11004727static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004728{
4729 unsigned long cpu;
4730 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09004731 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004732 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004733 int err;
4734
Dan Williams36d1c642009-07-14 11:48:22 -07004735 allcpus = alloc_percpu(struct raid5_percpu);
4736 if (!allcpus)
4737 return -ENOMEM;
4738 conf->percpu = allcpus;
4739
4740 get_online_cpus();
4741 err = 0;
4742 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07004743 if (conf->level == 6) {
4744 spare_page = alloc_page(GFP_KERNEL);
4745 if (!spare_page) {
4746 err = -ENOMEM;
4747 break;
4748 }
4749 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
4750 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11004751 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004752 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07004753 err = -ENOMEM;
4754 break;
4755 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07004756 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004757 }
4758#ifdef CONFIG_HOTPLUG_CPU
4759 conf->cpu_notify.notifier_call = raid456_cpu_notify;
4760 conf->cpu_notify.priority = 0;
4761 if (err == 0)
4762 err = register_cpu_notifier(&conf->cpu_notify);
4763#endif
4764 put_online_cpus();
4765
4766 return err;
4767}
4768
NeilBrownd1688a62011-10-11 16:49:52 +11004769static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770{
NeilBrownd1688a62011-10-11 16:49:52 +11004771 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004772 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11004773 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 struct disk_info *disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775
NeilBrown91adb562009-03-31 14:39:39 +11004776 if (mddev->new_level != 5
4777 && mddev->new_level != 4
4778 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10004779 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004780 mdname(mddev), mddev->new_level);
4781 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 }
NeilBrown91adb562009-03-31 14:39:39 +11004783 if ((mddev->new_level == 5
4784 && !algorithm_valid_raid5(mddev->new_layout)) ||
4785 (mddev->new_level == 6
4786 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004787 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11004788 mdname(mddev), mddev->new_layout);
4789 return ERR_PTR(-EIO);
4790 }
4791 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10004792 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004793 mdname(mddev), mddev->raid_disks);
4794 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11004795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796
Andre Noll664e7c42009-06-18 08:45:27 +10004797 if (!mddev->new_chunk_sectors ||
4798 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
4799 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10004800 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
4801 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11004802 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11004803 }
4804
NeilBrownd1688a62011-10-11 16:49:52 +11004805 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11004806 if (conf == NULL)
4807 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11004808 spin_lock_init(&conf->device_lock);
4809 init_waitqueue_head(&conf->wait_for_stripe);
4810 init_waitqueue_head(&conf->wait_for_overlap);
4811 INIT_LIST_HEAD(&conf->handle_list);
4812 INIT_LIST_HEAD(&conf->hold_list);
4813 INIT_LIST_HEAD(&conf->delayed_list);
4814 INIT_LIST_HEAD(&conf->bitmap_list);
4815 INIT_LIST_HEAD(&conf->inactive_list);
4816 atomic_set(&conf->active_stripes, 0);
4817 atomic_set(&conf->preread_active_stripes, 0);
4818 atomic_set(&conf->active_aligned_reads, 0);
4819 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11004820 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11004821
4822 conf->raid_disks = mddev->raid_disks;
4823 if (mddev->reshape_position == MaxSector)
4824 conf->previous_raid_disks = mddev->raid_disks;
4825 else
4826 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004827 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
4828 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11004829
NeilBrown5e5e3e72009-10-16 16:35:30 +11004830 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11004831 GFP_KERNEL);
4832 if (!conf->disks)
4833 goto abort;
4834
4835 conf->mddev = mddev;
4836
4837 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
4838 goto abort;
4839
Dan Williams36d1c642009-07-14 11:48:22 -07004840 conf->level = mddev->new_level;
4841 if (raid5_alloc_percpu(conf) != 0)
4842 goto abort;
4843
NeilBrown0c55e022010-05-03 14:09:02 +10004844 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11004845
4846 list_for_each_entry(rdev, &mddev->disks, same_set) {
4847 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004848 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11004849 || raid_disk < 0)
4850 continue;
4851 disk = conf->disks + raid_disk;
4852
NeilBrown17045f52011-12-23 10:17:53 +11004853 if (test_bit(Replacement, &rdev->flags)) {
4854 if (disk->replacement)
4855 goto abort;
4856 disk->replacement = rdev;
4857 } else {
4858 if (disk->rdev)
4859 goto abort;
4860 disk->rdev = rdev;
4861 }
NeilBrown91adb562009-03-31 14:39:39 +11004862
4863 if (test_bit(In_sync, &rdev->flags)) {
4864 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10004865 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
4866 " disk %d\n",
4867 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05004868 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11004869 /* Cannot rely on bitmap to complete recovery */
4870 conf->fullsync = 1;
4871 }
4872
Andre Noll09c9e5f2009-06-18 08:45:55 +10004873 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11004874 conf->level = mddev->new_level;
4875 if (conf->level == 6)
4876 conf->max_degraded = 2;
4877 else
4878 conf->max_degraded = 1;
4879 conf->algorithm = mddev->new_layout;
4880 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11004881 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11004882 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10004883 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11004884 conf->prev_algo = mddev->layout;
4885 }
NeilBrown91adb562009-03-31 14:39:39 +11004886
4887 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11004888 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11004889 if (grow_stripes(conf, conf->max_nr_stripes)) {
4890 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004891 "md/raid:%s: couldn't allocate %dkB for buffers\n",
4892 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004893 goto abort;
4894 } else
NeilBrown0c55e022010-05-03 14:09:02 +10004895 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
4896 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004897
NeilBrown0da3c612009-09-23 18:09:45 +10004898 conf->thread = md_register_thread(raid5d, mddev, NULL);
NeilBrown91adb562009-03-31 14:39:39 +11004899 if (!conf->thread) {
4900 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004901 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11004902 mdname(mddev));
4903 goto abort;
4904 }
4905
4906 return conf;
4907
4908 abort:
4909 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10004910 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11004911 return ERR_PTR(-EIO);
4912 } else
4913 return ERR_PTR(-ENOMEM);
4914}
4915
NeilBrownc148ffd2009-11-13 17:47:00 +11004916
4917static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
4918{
4919 switch (algo) {
4920 case ALGORITHM_PARITY_0:
4921 if (raid_disk < max_degraded)
4922 return 1;
4923 break;
4924 case ALGORITHM_PARITY_N:
4925 if (raid_disk >= raid_disks - max_degraded)
4926 return 1;
4927 break;
4928 case ALGORITHM_PARITY_0_6:
4929 if (raid_disk == 0 ||
4930 raid_disk == raid_disks - 1)
4931 return 1;
4932 break;
4933 case ALGORITHM_LEFT_ASYMMETRIC_6:
4934 case ALGORITHM_RIGHT_ASYMMETRIC_6:
4935 case ALGORITHM_LEFT_SYMMETRIC_6:
4936 case ALGORITHM_RIGHT_SYMMETRIC_6:
4937 if (raid_disk == raid_disks - 1)
4938 return 1;
4939 }
4940 return 0;
4941}
4942
NeilBrownfd01b882011-10-11 16:47:53 +11004943static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11004944{
NeilBrownd1688a62011-10-11 16:49:52 +11004945 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10004946 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11004947 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11004948 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11004949 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11004950 int i;
NeilBrown91adb562009-03-31 14:39:39 +11004951
Andre Noll8c6ac862009-06-18 08:48:06 +10004952 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10004953 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10004954 " -- starting background reconstruction\n",
4955 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004956 if (mddev->reshape_position != MaxSector) {
4957 /* Check that we can continue the reshape.
4958 * Currently only disks can change, it must
4959 * increase, and we must be past the point where
4960 * a stripe over-writes itself
4961 */
4962 sector_t here_new, here_old;
4963 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11004964 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08004965
NeilBrown88ce4932009-03-31 15:24:23 +11004966 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10004967 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08004968 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08004969 mdname(mddev));
4970 return -EINVAL;
4971 }
NeilBrownf6705572006-03-27 01:18:11 -08004972 old_disks = mddev->raid_disks - mddev->delta_disks;
4973 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08004974 * further up in new geometry must map after here in old
4975 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08004976 */
4977 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10004978 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08004979 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004980 printk(KERN_ERR "md/raid:%s: reshape_position not "
4981 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004982 return -EINVAL;
4983 }
NeilBrownc148ffd2009-11-13 17:47:00 +11004984 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08004985 /* here_new is the stripe we will write to */
4986 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10004987 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08004988 (old_disks-max_degraded));
4989 /* here_old is the first stripe that we might need to read
4990 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10004991 if (mddev->delta_disks == 0) {
4992 /* We cannot be sure it is safe to start an in-place
4993 * reshape. It is only safe if user-space if monitoring
4994 * and taking constant backups.
4995 * mdadm always starts a situation like this in
4996 * readonly mode so it can take control before
4997 * allowing any writes. So just check for that.
4998 */
4999 if ((here_new * mddev->new_chunk_sectors !=
5000 here_old * mddev->chunk_sectors) ||
5001 mddev->ro == 0) {
NeilBrown0c55e022010-05-03 14:09:02 +10005002 printk(KERN_ERR "md/raid:%s: in-place reshape must be started"
5003 " in read-only mode - aborting\n",
5004 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005005 return -EINVAL;
5006 }
5007 } else if (mddev->delta_disks < 0
5008 ? (here_new * mddev->new_chunk_sectors <=
5009 here_old * mddev->chunk_sectors)
5010 : (here_new * mddev->new_chunk_sectors >=
5011 here_old * mddev->chunk_sectors)) {
NeilBrownf6705572006-03-27 01:18:11 -08005012 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005013 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5014 "auto-recovery - aborting.\n",
5015 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005016 return -EINVAL;
5017 }
NeilBrown0c55e022010-05-03 14:09:02 +10005018 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5019 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005020 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005021 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005022 BUG_ON(mddev->level != mddev->new_level);
5023 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005024 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005025 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005026 }
5027
NeilBrown245f46c2009-03-31 14:39:39 +11005028 if (mddev->private == NULL)
5029 conf = setup_conf(mddev);
5030 else
5031 conf = mddev->private;
5032
NeilBrown91adb562009-03-31 14:39:39 +11005033 if (IS_ERR(conf))
5034 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005035
NeilBrown91adb562009-03-31 14:39:39 +11005036 mddev->thread = conf->thread;
5037 conf->thread = NULL;
5038 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039
NeilBrown17045f52011-12-23 10:17:53 +11005040 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5041 i++) {
5042 rdev = conf->disks[i].rdev;
5043 if (!rdev && conf->disks[i].replacement) {
5044 /* The replacement is all we have yet */
5045 rdev = conf->disks[i].replacement;
5046 conf->disks[i].replacement = NULL;
5047 clear_bit(Replacement, &rdev->flags);
5048 conf->disks[i].rdev = rdev;
5049 }
5050 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11005051 continue;
NeilBrown17045f52011-12-23 10:17:53 +11005052 if (conf->disks[i].replacement &&
5053 conf->reshape_progress != MaxSector) {
5054 /* replacements and reshape simply do not mix. */
5055 printk(KERN_ERR "md: cannot handle concurrent "
5056 "replacement and reshape.\n");
5057 goto abort;
5058 }
NeilBrown2f115882010-06-17 17:41:03 +10005059 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005060 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005061 continue;
5062 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005063 /* This disc is not fully in-sync. However if it
5064 * just stored parity (beyond the recovery_offset),
5065 * when we don't need to be concerned about the
5066 * array being dirty.
5067 * When reshape goes 'backwards', we never have
5068 * partially completed devices, so we only need
5069 * to worry about reshape going forwards.
5070 */
5071 /* Hack because v0.91 doesn't store recovery_offset properly. */
5072 if (mddev->major_version == 0 &&
5073 mddev->minor_version > 90)
5074 rdev->recovery_offset = reshape_offset;
5075
NeilBrownc148ffd2009-11-13 17:47:00 +11005076 if (rdev->recovery_offset < reshape_offset) {
5077 /* We need to check old and new layout */
5078 if (!only_parity(rdev->raid_disk,
5079 conf->algorithm,
5080 conf->raid_disks,
5081 conf->max_degraded))
5082 continue;
5083 }
5084 if (!only_parity(rdev->raid_disk,
5085 conf->prev_algo,
5086 conf->previous_raid_disks,
5087 conf->max_degraded))
5088 continue;
5089 dirty_parity_disks++;
5090 }
NeilBrown91adb562009-03-31 14:39:39 +11005091
NeilBrown17045f52011-12-23 10:17:53 +11005092 /*
5093 * 0 for a fully functional array, 1 or 2 for a degraded array.
5094 */
NeilBrown908f4fb2011-12-23 10:17:50 +11005095 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096
NeilBrown674806d2010-06-16 17:17:53 +10005097 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005098 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07005100 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 goto abort;
5102 }
5103
NeilBrown91adb562009-03-31 14:39:39 +11005104 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10005105 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11005106 mddev->resync_max_sectors = mddev->dev_sectors;
5107
NeilBrownc148ffd2009-11-13 17:47:00 +11005108 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005110 if (mddev->ok_start_degraded)
5111 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10005112 "md/raid:%s: starting dirty degraded array"
5113 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005114 mdname(mddev));
5115 else {
5116 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005117 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005118 mdname(mddev));
5119 goto abort;
5120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 }
5122
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10005124 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5125 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11005126 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5127 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 else
NeilBrown0c55e022010-05-03 14:09:02 +10005129 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5130 " out of %d devices, algorithm %d\n",
5131 mdname(mddev), conf->level,
5132 mddev->raid_disks - mddev->degraded,
5133 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134
5135 print_raid5_conf(conf);
5136
NeilBrownfef9c612009-03-31 15:16:46 +11005137 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11005138 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005139 atomic_set(&conf->reshape_stripes, 0);
5140 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5141 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5142 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5143 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5144 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005145 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005146 }
5147
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148
5149 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005150 if (mddev->to_remove == &raid5_attrs_group)
5151 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005152 else if (mddev->kobj.sd &&
5153 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005154 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10005155 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08005156 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10005157 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5158
5159 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005160 int chunk_size;
NeilBrown4a5add42010-06-01 19:37:28 +10005161 /* read-ahead size must cover two whole stripes, which
5162 * is 2 * (datadisks) * chunksize where 'n' is the
5163 * number of raid devices
5164 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5166 int stripe = data_disks *
5167 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5168 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5169 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10005170
5171 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005172
5173 mddev->queue->backing_dev_info.congested_data = mddev;
5174 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10005175
5176 chunk_size = mddev->chunk_sectors << 9;
5177 blk_queue_io_min(mddev->queue, chunk_size);
5178 blk_queue_io_opt(mddev->queue, chunk_size *
5179 (conf->raid_disks - conf->max_degraded));
5180
5181 list_for_each_entry(rdev, &mddev->disks, same_set)
5182 disk_stack_limits(mddev->gendisk, rdev->bdev,
5183 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 }
5185
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 return 0;
5187abort:
NeilBrown01f96c02011-09-21 15:30:20 +10005188 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11005189 print_raid5_conf(conf);
5190 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005192 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 return -EIO;
5194}
5195
NeilBrownfd01b882011-10-11 16:47:53 +11005196static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197{
NeilBrownd1688a62011-10-11 16:49:52 +11005198 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199
NeilBrown01f96c02011-09-21 15:30:20 +10005200 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005201 if (mddev->queue)
5202 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10005203 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005204 mddev->private = NULL;
5205 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 return 0;
5207}
5208
NeilBrownfd01b882011-10-11 16:47:53 +11005209static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210{
NeilBrownd1688a62011-10-11 16:49:52 +11005211 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212 int i;
5213
Andre Noll9d8f0362009-06-18 08:45:01 +10005214 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5215 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005216 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 for (i = 0; i < conf->raid_disks; i++)
5218 seq_printf (seq, "%s",
5219 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005220 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222}
5223
NeilBrownd1688a62011-10-11 16:49:52 +11005224static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225{
5226 int i;
5227 struct disk_info *tmp;
5228
NeilBrown0c55e022010-05-03 14:09:02 +10005229 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 if (!conf) {
5231 printk("(conf==NULL)\n");
5232 return;
5233 }
NeilBrown0c55e022010-05-03 14:09:02 +10005234 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5235 conf->raid_disks,
5236 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237
5238 for (i = 0; i < conf->raid_disks; i++) {
5239 char b[BDEVNAME_SIZE];
5240 tmp = conf->disks + i;
5241 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005242 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5243 i, !test_bit(Faulty, &tmp->rdev->flags),
5244 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 }
5246}
5247
NeilBrownfd01b882011-10-11 16:47:53 +11005248static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249{
5250 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005251 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005253 int count = 0;
5254 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255
5256 for (i = 0; i < conf->raid_disks; i++) {
5257 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11005258 if (tmp->replacement
5259 && tmp->replacement->recovery_offset == MaxSector
5260 && !test_bit(Faulty, &tmp->replacement->flags)
5261 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
5262 /* Replacement has just become active. */
5263 if (!tmp->rdev
5264 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
5265 count++;
5266 if (tmp->rdev) {
5267 /* Replaced device not technically faulty,
5268 * but we need to be sure it gets removed
5269 * and never re-added.
5270 */
5271 set_bit(Faulty, &tmp->rdev->flags);
5272 sysfs_notify_dirent_safe(
5273 tmp->rdev->sysfs_state);
5274 }
5275 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
5276 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005277 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005278 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005279 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005280 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005281 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 }
5283 }
NeilBrown6b965622010-08-18 11:56:59 +10005284 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005285 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005286 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005288 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289}
5290
NeilBrownb8321b62011-12-23 10:17:51 +11005291static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292{
NeilBrownd1688a62011-10-11 16:49:52 +11005293 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11005295 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11005296 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 struct disk_info *p = conf->disks + number;
5298
5299 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11005300 if (rdev == p->rdev)
5301 rdevp = &p->rdev;
5302 else if (rdev == p->replacement)
5303 rdevp = &p->replacement;
5304 else
5305 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11005306
NeilBrown657e3e42011-12-23 10:17:52 +11005307 if (number >= conf->raid_disks &&
5308 conf->reshape_progress == MaxSector)
5309 clear_bit(In_sync, &rdev->flags);
5310
5311 if (test_bit(In_sync, &rdev->flags) ||
5312 atomic_read(&rdev->nr_pending)) {
5313 err = -EBUSY;
5314 goto abort;
5315 }
5316 /* Only remove non-faulty devices if recovery
5317 * isn't possible.
5318 */
5319 if (!test_bit(Faulty, &rdev->flags) &&
5320 mddev->recovery_disabled != conf->recovery_disabled &&
5321 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11005322 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11005323 number < conf->raid_disks) {
5324 err = -EBUSY;
5325 goto abort;
5326 }
5327 *rdevp = NULL;
5328 synchronize_rcu();
5329 if (atomic_read(&rdev->nr_pending)) {
5330 /* lost the race, try later */
5331 err = -EBUSY;
5332 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11005333 } else if (p->replacement) {
5334 /* We must have just cleared 'rdev' */
5335 p->rdev = p->replacement;
5336 clear_bit(Replacement, &p->replacement->flags);
5337 smp_mb(); /* Make sure other CPUs may see both as identical
5338 * but will never see neither - if they are careful
5339 */
5340 p->replacement = NULL;
5341 clear_bit(WantReplacement, &rdev->flags);
5342 } else
5343 /* We might have just removed the Replacement as faulty-
5344 * clear the bit just in case
5345 */
5346 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347abort:
5348
5349 print_raid5_conf(conf);
5350 return err;
5351}
5352
NeilBrownfd01b882011-10-11 16:47:53 +11005353static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354{
NeilBrownd1688a62011-10-11 16:49:52 +11005355 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005356 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 int disk;
5358 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005359 int first = 0;
5360 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361
NeilBrown7f0da592011-07-28 11:39:22 +10005362 if (mddev->recovery_disabled == conf->recovery_disabled)
5363 return -EBUSY;
5364
NeilBrown674806d2010-06-16 17:17:53 +10005365 if (has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005367 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368
Neil Brown6c2fce22008-06-28 08:31:31 +10005369 if (rdev->raid_disk >= 0)
5370 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371
5372 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005373 * find the disk ... but prefer rdev->saved_raid_disk
5374 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005376 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005377 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005378 conf->disks[rdev->saved_raid_disk].rdev == NULL)
5379 disk = rdev->saved_raid_disk;
5380 else
Neil Brown6c2fce22008-06-28 08:31:31 +10005381 disk = first;
NeilBrown7bfec5f2011-12-23 10:17:53 +11005382 for ( ; disk <= last ; disk++) {
5383 p = conf->disks + disk;
5384 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005385 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005387 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005388 if (rdev->saved_raid_disk != disk)
5389 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005390 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 break;
5392 }
NeilBrown7bfec5f2011-12-23 10:17:53 +11005393 if (test_bit(WantReplacement, &p->rdev->flags) &&
5394 p->replacement == NULL) {
5395 clear_bit(In_sync, &rdev->flags);
5396 set_bit(Replacement, &rdev->flags);
5397 rdev->raid_disk = disk;
5398 err = 0;
5399 conf->fullsync = 1;
5400 rcu_assign_pointer(p->replacement, rdev);
5401 break;
5402 }
5403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005405 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406}
5407
NeilBrownfd01b882011-10-11 16:47:53 +11005408static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409{
5410 /* no resync is happening, and there is enough space
5411 * on all devices, so we can resize.
5412 * We need to make sure resync covers any new space.
5413 * If the array is shrinking we should possibly wait until
5414 * any io in the removed space completes, but it hardly seems
5415 * worth it.
5416 */
Andre Noll9d8f0362009-06-18 08:45:01 +10005417 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Dan Williams1f403622009-03-31 14:59:03 +11005418 md_set_array_sectors(mddev, raid5_size(mddev, sectors,
5419 mddev->raid_disks));
Dan Williamsb522adc2009-03-31 15:00:31 +11005420 if (mddev->array_sectors >
5421 raid5_size(mddev, sectors, mddev->raid_disks))
5422 return -EINVAL;
Andre Nollf233ea52008-07-21 17:05:22 +10005423 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005424 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005425 if (sectors > mddev->dev_sectors &&
5426 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005427 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5429 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005430 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005431 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 return 0;
5433}
5434
NeilBrownfd01b882011-10-11 16:47:53 +11005435static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005436{
5437 /* Can only proceed if there are plenty of stripe_heads.
5438 * We need a minimum of one full stripe,, and for sensible progress
5439 * it is best to have about 4 times that.
5440 * If we require 4 times, then the default 256 4K stripe_heads will
5441 * allow for chunk sizes up to 256K, which is probably OK.
5442 * If the chunk size is greater, user-space should request more
5443 * stripe_heads first.
5444 */
NeilBrownd1688a62011-10-11 16:49:52 +11005445 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005446 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5447 > conf->max_nr_stripes ||
5448 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5449 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005450 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5451 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005452 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5453 / STRIPE_SIZE)*4);
5454 return 0;
5455 }
5456 return 1;
5457}
5458
NeilBrownfd01b882011-10-11 16:47:53 +11005459static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005460{
NeilBrownd1688a62011-10-11 16:49:52 +11005461 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005462
NeilBrown88ce4932009-03-31 15:24:23 +11005463 if (mddev->delta_disks == 0 &&
5464 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005465 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005466 return 0; /* nothing to do */
NeilBrowndba034e2008-08-05 15:54:13 +10005467 if (mddev->bitmap)
5468 /* Cannot grow a bitmap yet */
5469 return -EBUSY;
NeilBrown674806d2010-06-16 17:17:53 +10005470 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005471 return -EINVAL;
5472 if (mddev->delta_disks < 0) {
5473 /* We might be able to shrink, but the devices must
5474 * be made bigger first.
5475 * For raid6, 4 is the minimum size.
5476 * Otherwise 2 is the minimum
5477 */
5478 int min = 2;
5479 if (mddev->level == 6)
5480 min = 4;
5481 if (mddev->raid_disks + mddev->delta_disks < min)
5482 return -EINVAL;
5483 }
NeilBrown29269552006-03-27 01:18:10 -08005484
NeilBrown01ee22b2009-06-18 08:47:20 +10005485 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005486 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005487
NeilBrownec32a2b2009-03-31 15:17:38 +11005488 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
NeilBrown63c70c42006-03-27 01:18:13 -08005489}
5490
NeilBrownfd01b882011-10-11 16:47:53 +11005491static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005492{
NeilBrownd1688a62011-10-11 16:49:52 +11005493 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005494 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005495 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005496 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005497
NeilBrownf4168852007-02-28 20:11:53 -08005498 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005499 return -EBUSY;
5500
NeilBrown01ee22b2009-06-18 08:47:20 +10005501 if (!check_stripe_cache(mddev))
5502 return -ENOSPC;
5503
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005504 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown469518a2011-01-31 11:57:43 +11005505 if (!test_bit(In_sync, &rdev->flags)
5506 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005507 spares++;
NeilBrown63c70c42006-03-27 01:18:13 -08005508
NeilBrownf4168852007-02-28 20:11:53 -08005509 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005510 /* Not enough devices even to make a degraded array
5511 * of that size
5512 */
5513 return -EINVAL;
5514
NeilBrownec32a2b2009-03-31 15:17:38 +11005515 /* Refuse to reduce size of the array. Any reductions in
5516 * array size must be through explicit setting of array_size
5517 * attribute.
5518 */
5519 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5520 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10005521 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11005522 "before number of disks\n", mdname(mddev));
5523 return -EINVAL;
5524 }
5525
NeilBrownf6705572006-03-27 01:18:11 -08005526 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005527 spin_lock_irq(&conf->device_lock);
5528 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005529 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005530 conf->prev_chunk_sectors = conf->chunk_sectors;
5531 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005532 conf->prev_algo = conf->algorithm;
5533 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11005534 if (mddev->delta_disks < 0)
5535 conf->reshape_progress = raid5_size(mddev, 0, 0);
5536 else
5537 conf->reshape_progress = 0;
5538 conf->reshape_safe = conf->reshape_progress;
NeilBrown86b42c72009-03-31 15:19:03 +11005539 conf->generation++;
NeilBrown29269552006-03-27 01:18:10 -08005540 spin_unlock_irq(&conf->device_lock);
5541
5542 /* Add some new drives, as many as will fit.
5543 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10005544 * Don't add devices if we are reducing the number of
5545 * devices in the array. This is because it is not possible
5546 * to correctly record the "partially reconstructed" state of
5547 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08005548 */
NeilBrown87a8dec2011-01-31 11:57:43 +11005549 if (mddev->delta_disks >= 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11005550 list_for_each_entry(rdev, &mddev->disks, same_set)
5551 if (rdev->raid_disk < 0 &&
5552 !test_bit(Faulty, &rdev->flags)) {
5553 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11005554 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11005555 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11005556 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11005557 else
NeilBrown87a8dec2011-01-31 11:57:43 +11005558 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10005559
5560 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11005561 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11005562 }
NeilBrown87a8dec2011-01-31 11:57:43 +11005563 } else if (rdev->raid_disk >= conf->previous_raid_disks
5564 && !test_bit(Faulty, &rdev->flags)) {
5565 /* This is a spare that was manually added */
5566 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11005567 }
NeilBrown29269552006-03-27 01:18:10 -08005568
NeilBrown87a8dec2011-01-31 11:57:43 +11005569 /* When a reshape changes the number of devices,
5570 * ->degraded is measured against the larger of the
5571 * pre and post number of devices.
5572 */
NeilBrownec32a2b2009-03-31 15:17:38 +11005573 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005574 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11005575 spin_unlock_irqrestore(&conf->device_lock, flags);
5576 }
NeilBrown63c70c42006-03-27 01:18:13 -08005577 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10005578 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07005579 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005580
NeilBrown29269552006-03-27 01:18:10 -08005581 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5582 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5583 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5584 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5585 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005586 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08005587 if (!mddev->sync_thread) {
5588 mddev->recovery = 0;
5589 spin_lock_irq(&conf->device_lock);
5590 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005591 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11005592 mddev->reshape_position = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005593 spin_unlock_irq(&conf->device_lock);
5594 return -EAGAIN;
5595 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005596 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005597 md_wakeup_thread(mddev->sync_thread);
5598 md_new_event(mddev);
5599 return 0;
5600}
NeilBrown29269552006-03-27 01:18:10 -08005601
NeilBrownec32a2b2009-03-31 15:17:38 +11005602/* This is called from the reshape thread and should make any
5603 * changes needed in 'conf'
5604 */
NeilBrownd1688a62011-10-11 16:49:52 +11005605static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08005606{
NeilBrown29269552006-03-27 01:18:10 -08005607
NeilBrownf6705572006-03-27 01:18:11 -08005608 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
Dan Williams80c3a6c2009-03-17 18:10:40 -07005609
NeilBrownf6705572006-03-27 01:18:11 -08005610 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11005611 conf->previous_raid_disks = conf->raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005612 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08005613 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005614 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07005615
5616 /* read-ahead size must cover two whole stripes, which is
5617 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5618 */
NeilBrown4a5add42010-06-01 19:37:28 +10005619 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11005620 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005621 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11005622 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07005623 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5624 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5625 }
NeilBrown29269552006-03-27 01:18:10 -08005626 }
NeilBrown29269552006-03-27 01:18:10 -08005627}
5628
NeilBrownec32a2b2009-03-31 15:17:38 +11005629/* This is called from the raid5d thread with mddev_lock held.
5630 * It makes config changes to the device.
5631 */
NeilBrownfd01b882011-10-11 16:47:53 +11005632static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11005633{
NeilBrownd1688a62011-10-11 16:49:52 +11005634 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11005635
5636 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5637
NeilBrownec32a2b2009-03-31 15:17:38 +11005638 if (mddev->delta_disks > 0) {
5639 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5640 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005641 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11005642 } else {
5643 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11005644 spin_lock_irq(&conf->device_lock);
5645 mddev->degraded = calc_degraded(conf);
5646 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11005647 for (d = conf->raid_disks ;
5648 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10005649 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11005650 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownb8321b62011-12-23 10:17:51 +11005651 if (rdev &&
5652 raid5_remove_disk(mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10005653 sysfs_unlink_rdev(mddev, rdev);
NeilBrown1a67dde2009-08-13 10:41:49 +10005654 rdev->raid_disk = -1;
5655 }
5656 }
NeilBrowncea9c222009-03-31 15:15:05 +11005657 }
NeilBrown88ce4932009-03-31 15:24:23 +11005658 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005659 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11005660 mddev->reshape_position = MaxSector;
5661 mddev->delta_disks = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11005662 }
5663}
5664
NeilBrownfd01b882011-10-11 16:47:53 +11005665static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07005666{
NeilBrownd1688a62011-10-11 16:49:52 +11005667 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07005668
5669 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08005670 case 2: /* resume for a suspend */
5671 wake_up(&conf->wait_for_overlap);
5672 break;
5673
NeilBrown72626682005-09-09 16:23:54 -07005674 case 1: /* stop all writes */
5675 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005676 /* '2' tells resync/reshape to pause so that all
5677 * active stripes can drain
5678 */
5679 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07005680 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005681 atomic_read(&conf->active_stripes) == 0 &&
5682 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07005683 conf->device_lock, /* nothing */);
NeilBrown64bd6602009-08-03 10:59:58 +10005684 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07005685 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005686 /* allow reshape to continue */
5687 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005688 break;
5689
5690 case 0: /* re-enable writes */
5691 spin_lock_irq(&conf->device_lock);
5692 conf->quiesce = 0;
5693 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08005694 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005695 spin_unlock_irq(&conf->device_lock);
5696 break;
5697 }
NeilBrown72626682005-09-09 16:23:54 -07005698}
NeilBrownb15c2e52006-01-06 00:20:16 -08005699
NeilBrownd562b0c2009-03-31 14:39:39 +11005700
NeilBrownfd01b882011-10-11 16:47:53 +11005701static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11005702{
NeilBrowne373ab12011-10-11 16:48:59 +11005703 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07005704 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11005705
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005706 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11005707 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10005708 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
5709 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005710 return ERR_PTR(-EINVAL);
5711 }
5712
NeilBrowne373ab12011-10-11 16:48:59 +11005713 sectors = raid0_conf->strip_zone[0].zone_end;
5714 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10005715 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005716 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11005717 mddev->new_layout = ALGORITHM_PARITY_N;
5718 mddev->new_chunk_sectors = mddev->chunk_sectors;
5719 mddev->raid_disks += 1;
5720 mddev->delta_disks = 1;
5721 /* make sure it will be not marked as dirty */
5722 mddev->recovery_cp = MaxSector;
5723
5724 return setup_conf(mddev);
5725}
5726
5727
NeilBrownfd01b882011-10-11 16:47:53 +11005728static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005729{
5730 int chunksect;
5731
5732 if (mddev->raid_disks != 2 ||
5733 mddev->degraded > 1)
5734 return ERR_PTR(-EINVAL);
5735
5736 /* Should check if there are write-behind devices? */
5737
5738 chunksect = 64*2; /* 64K by default */
5739
5740 /* The array must be an exact multiple of chunksize */
5741 while (chunksect && (mddev->array_sectors & (chunksect-1)))
5742 chunksect >>= 1;
5743
5744 if ((chunksect<<9) < STRIPE_SIZE)
5745 /* array size does not allow a suitable chunk size */
5746 return ERR_PTR(-EINVAL);
5747
5748 mddev->new_level = 5;
5749 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10005750 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11005751
5752 return setup_conf(mddev);
5753}
5754
NeilBrownfd01b882011-10-11 16:47:53 +11005755static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11005756{
5757 int new_layout;
5758
5759 switch (mddev->layout) {
5760 case ALGORITHM_LEFT_ASYMMETRIC_6:
5761 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
5762 break;
5763 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5764 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
5765 break;
5766 case ALGORITHM_LEFT_SYMMETRIC_6:
5767 new_layout = ALGORITHM_LEFT_SYMMETRIC;
5768 break;
5769 case ALGORITHM_RIGHT_SYMMETRIC_6:
5770 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
5771 break;
5772 case ALGORITHM_PARITY_0_6:
5773 new_layout = ALGORITHM_PARITY_0;
5774 break;
5775 case ALGORITHM_PARITY_N:
5776 new_layout = ALGORITHM_PARITY_N;
5777 break;
5778 default:
5779 return ERR_PTR(-EINVAL);
5780 }
5781 mddev->new_level = 5;
5782 mddev->new_layout = new_layout;
5783 mddev->delta_disks = -1;
5784 mddev->raid_disks -= 1;
5785 return setup_conf(mddev);
5786}
5787
NeilBrownd562b0c2009-03-31 14:39:39 +11005788
NeilBrownfd01b882011-10-11 16:47:53 +11005789static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11005790{
NeilBrown88ce4932009-03-31 15:24:23 +11005791 /* For a 2-drive array, the layout and chunk size can be changed
5792 * immediately as not restriping is needed.
5793 * For larger arrays we record the new value - after validation
5794 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11005795 */
NeilBrownd1688a62011-10-11 16:49:52 +11005796 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10005797 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11005798
NeilBrown597a7112009-06-18 08:47:42 +10005799 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11005800 return -EINVAL;
5801 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005802 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11005803 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005804 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11005805 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005806 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11005807 /* not factor of array size */
5808 return -EINVAL;
5809 }
5810
5811 /* They look valid */
5812
NeilBrown88ce4932009-03-31 15:24:23 +11005813 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10005814 /* can make the change immediately */
5815 if (mddev->new_layout >= 0) {
5816 conf->algorithm = mddev->new_layout;
5817 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11005818 }
5819 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10005820 conf->chunk_sectors = new_chunk ;
5821 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11005822 }
5823 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5824 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11005825 }
NeilBrown50ac1682009-06-18 08:47:55 +10005826 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11005827}
5828
NeilBrownfd01b882011-10-11 16:47:53 +11005829static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11005830{
NeilBrown597a7112009-06-18 08:47:42 +10005831 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10005832
NeilBrown597a7112009-06-18 08:47:42 +10005833 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11005834 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005835 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005836 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11005837 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005838 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11005839 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005840 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11005841 /* not factor of array size */
5842 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005843 }
NeilBrown88ce4932009-03-31 15:24:23 +11005844
5845 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10005846 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11005847}
5848
NeilBrownfd01b882011-10-11 16:47:53 +11005849static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005850{
5851 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005852 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005853 * raid1 - if there are two drives. We need to know the chunk size
5854 * raid4 - trivial - just use a raid4 layout.
5855 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005856 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005857 if (mddev->level == 0)
5858 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11005859 if (mddev->level == 1)
5860 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11005861 if (mddev->level == 4) {
5862 mddev->new_layout = ALGORITHM_PARITY_N;
5863 mddev->new_level = 5;
5864 return setup_conf(mddev);
5865 }
NeilBrownfc9739c2009-03-31 14:57:20 +11005866 if (mddev->level == 6)
5867 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11005868
5869 return ERR_PTR(-EINVAL);
5870}
5871
NeilBrownfd01b882011-10-11 16:47:53 +11005872static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11005873{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005874 /* raid4 can take over:
5875 * raid0 - if there is only one strip zone
5876 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11005877 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005878 if (mddev->level == 0)
5879 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11005880 if (mddev->level == 5 &&
5881 mddev->layout == ALGORITHM_PARITY_N) {
5882 mddev->new_layout = 0;
5883 mddev->new_level = 4;
5884 return setup_conf(mddev);
5885 }
5886 return ERR_PTR(-EINVAL);
5887}
NeilBrownd562b0c2009-03-31 14:39:39 +11005888
NeilBrown84fc4b52011-10-11 16:49:58 +11005889static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11005890
NeilBrownfd01b882011-10-11 16:47:53 +11005891static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11005892{
5893 /* Currently can only take over a raid5. We map the
5894 * personality to an equivalent raid6 personality
5895 * with the Q block at the end.
5896 */
5897 int new_layout;
5898
5899 if (mddev->pers != &raid5_personality)
5900 return ERR_PTR(-EINVAL);
5901 if (mddev->degraded > 1)
5902 return ERR_PTR(-EINVAL);
5903 if (mddev->raid_disks > 253)
5904 return ERR_PTR(-EINVAL);
5905 if (mddev->raid_disks < 3)
5906 return ERR_PTR(-EINVAL);
5907
5908 switch (mddev->layout) {
5909 case ALGORITHM_LEFT_ASYMMETRIC:
5910 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
5911 break;
5912 case ALGORITHM_RIGHT_ASYMMETRIC:
5913 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
5914 break;
5915 case ALGORITHM_LEFT_SYMMETRIC:
5916 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
5917 break;
5918 case ALGORITHM_RIGHT_SYMMETRIC:
5919 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
5920 break;
5921 case ALGORITHM_PARITY_0:
5922 new_layout = ALGORITHM_PARITY_0_6;
5923 break;
5924 case ALGORITHM_PARITY_N:
5925 new_layout = ALGORITHM_PARITY_N;
5926 break;
5927 default:
5928 return ERR_PTR(-EINVAL);
5929 }
5930 mddev->new_level = 6;
5931 mddev->new_layout = new_layout;
5932 mddev->delta_disks = 1;
5933 mddev->raid_disks += 1;
5934 return setup_conf(mddev);
5935}
5936
5937
NeilBrown84fc4b52011-10-11 16:49:58 +11005938static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07005939{
5940 .name = "raid6",
5941 .level = 6,
5942 .owner = THIS_MODULE,
5943 .make_request = make_request,
5944 .run = run,
5945 .stop = stop,
5946 .status = status,
5947 .error_handler = error,
5948 .hot_add_disk = raid5_add_disk,
5949 .hot_remove_disk= raid5_remove_disk,
5950 .spare_active = raid5_spare_active,
5951 .sync_request = sync_request,
5952 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005953 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10005954 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08005955 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005956 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07005957 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11005958 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07005959};
NeilBrown84fc4b52011-10-11 16:49:58 +11005960static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961{
5962 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08005963 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964 .owner = THIS_MODULE,
5965 .make_request = make_request,
5966 .run = run,
5967 .stop = stop,
5968 .status = status,
5969 .error_handler = error,
5970 .hot_add_disk = raid5_add_disk,
5971 .hot_remove_disk= raid5_remove_disk,
5972 .spare_active = raid5_spare_active,
5973 .sync_request = sync_request,
5974 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005975 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08005976 .check_reshape = raid5_check_reshape,
5977 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005978 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07005979 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11005980 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981};
5982
NeilBrown84fc4b52011-10-11 16:49:58 +11005983static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984{
NeilBrown2604b702006-01-06 00:20:36 -08005985 .name = "raid4",
5986 .level = 4,
5987 .owner = THIS_MODULE,
5988 .make_request = make_request,
5989 .run = run,
5990 .stop = stop,
5991 .status = status,
5992 .error_handler = error,
5993 .hot_add_disk = raid5_add_disk,
5994 .hot_remove_disk= raid5_remove_disk,
5995 .spare_active = raid5_spare_active,
5996 .sync_request = sync_request,
5997 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005998 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08005999 .check_reshape = raid5_check_reshape,
6000 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006001 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08006002 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11006003 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08006004};
6005
6006static int __init raid5_init(void)
6007{
NeilBrown16a53ec2006-06-26 00:27:38 -07006008 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006009 register_md_personality(&raid5_personality);
6010 register_md_personality(&raid4_personality);
6011 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012}
6013
NeilBrown2604b702006-01-06 00:20:36 -08006014static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015{
NeilBrown16a53ec2006-06-26 00:27:38 -07006016 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006017 unregister_md_personality(&raid5_personality);
6018 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019}
6020
6021module_init(raid5_init);
6022module_exit(raid5_exit);
6023MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11006024MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08006026MODULE_ALIAS("md-raid5");
6027MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08006028MODULE_ALIAS("md-level-5");
6029MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07006030MODULE_ALIAS("md-personality-8"); /* RAID6 */
6031MODULE_ALIAS("md-raid6");
6032MODULE_ALIAS("md-level-6");
6033
6034/* This used to be two separate modules, they were: */
6035MODULE_ALIAS("raid5");
6036MODULE_ALIAS("raid6");