blob: 8514e9aeb369b7ca510ac80ec80e003f3a937ccd [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>
Dan Williams07a3b412009-08-29 19:13:13 -070050#include <linux/async.h>
NeilBrownbff61972009-03-31 14:33:13 +110051#include <linux/seq_file.h>
Dan Williams36d1c642009-07-14 11:48:22 -070052#include <linux/cpu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090053#include <linux/slab.h>
Christian Dietrich8bda4702011-07-27 11:00:36 +100054#include <linux/ratelimit.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110055#include "md.h"
NeilBrownbff61972009-03-31 14:33:13 +110056#include "raid5.h"
Trela Maciej54071b32010-03-08 16:02:42 +110057#include "raid0.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110058#include "bitmap.h"
NeilBrown72626682005-09-09 16:23:54 -070059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/*
61 * Stripe cache
62 */
63
64#define NR_STRIPES 256
65#define STRIPE_SIZE PAGE_SIZE
66#define STRIPE_SHIFT (PAGE_SHIFT - 9)
67#define STRIPE_SECTORS (STRIPE_SIZE>>9)
68#define IO_THRESHOLD 1
Dan Williams8b3e6cd2008-04-28 02:15:53 -070069#define BYPASS_THRESHOLD 1
NeilBrownfccddba2006-01-06 00:20:33 -080070#define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define HASH_MASK (NR_HASH - 1)
72
NeilBrowndb298e12011-10-07 14:23:00 +110073static inline struct hlist_head *stripe_hash(raid5_conf_t *conf, sector_t sect)
74{
75 int hash = (sect >> STRIPE_SHIFT) & HASH_MASK;
76 return &conf->stripe_hashtbl[hash];
77}
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79/* bio's attached to a stripe+device for I/O are linked together in bi_sector
80 * order without overlap. There may be several bio's per stripe+device, and
81 * a bio could span several devices.
82 * When walking this list for a particular stripe+device, we must never proceed
83 * beyond a bio that extends past this device, as the next bio might no longer
84 * be valid.
NeilBrowndb298e12011-10-07 14:23:00 +110085 * This function is used to determine the 'next' bio in the list, given the sector
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 * of the current stripe+device
87 */
NeilBrowndb298e12011-10-07 14:23:00 +110088static inline struct bio *r5_next_bio(struct bio *bio, sector_t sector)
89{
90 int sectors = bio->bi_size >> 9;
91 if (bio->bi_sector + sectors < sector + STRIPE_SECTORS)
92 return bio->bi_next;
93 else
94 return NULL;
95}
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Jens Axboe960e7392008-08-15 10:41:18 +020097/*
Jens Axboe5b99c2f2008-08-15 10:56:11 +020098 * We maintain a biased count of active stripes in the bottom 16 bits of
99 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
Jens Axboe960e7392008-08-15 10:41:18 +0200100 */
101static inline int raid5_bi_phys_segments(struct bio *bio)
102{
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200103 return bio->bi_phys_segments & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200104}
105
106static inline int raid5_bi_hw_segments(struct bio *bio)
107{
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200108 return (bio->bi_phys_segments >> 16) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200109}
110
111static inline int raid5_dec_bi_phys_segments(struct bio *bio)
112{
113 --bio->bi_phys_segments;
114 return raid5_bi_phys_segments(bio);
115}
116
117static inline int raid5_dec_bi_hw_segments(struct bio *bio)
118{
119 unsigned short val = raid5_bi_hw_segments(bio);
120
121 --val;
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200122 bio->bi_phys_segments = (val << 16) | raid5_bi_phys_segments(bio);
Jens Axboe960e7392008-08-15 10:41:18 +0200123 return val;
124}
125
126static inline void raid5_set_bi_hw_segments(struct bio *bio, unsigned int cnt)
127{
Namhyung Kim9b2dc8b2011-06-13 14:48:22 +0900128 bio->bi_phys_segments = raid5_bi_phys_segments(bio) | (cnt << 16);
Jens Axboe960e7392008-08-15 10:41:18 +0200129}
130
NeilBrownd0dabf72009-03-31 14:39:38 +1100131/* Find first data disk in a raid6 stripe */
132static inline int raid6_d0(struct stripe_head *sh)
133{
NeilBrown67cc2b82009-03-31 14:39:38 +1100134 if (sh->ddf_layout)
135 /* ddf always start from first device */
136 return 0;
137 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100138 if (sh->qd_idx == sh->disks - 1)
139 return 0;
140 else
141 return sh->qd_idx + 1;
142}
NeilBrown16a53ec2006-06-26 00:27:38 -0700143static inline int raid6_next_disk(int disk, int raid_disks)
144{
145 disk++;
146 return (disk < raid_disks) ? disk : 0;
147}
Dan Williamsa4456852007-07-09 11:56:43 -0700148
NeilBrownd0dabf72009-03-31 14:39:38 +1100149/* When walking through the disks in a raid5, starting at raid6_d0,
150 * We need to map each disk to a 'slot', where the data disks are slot
151 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
152 * is raid_disks-1. This help does that mapping.
153 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100154static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
155 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100156{
Dan Williams66295422009-10-19 18:09:32 -0700157 int slot = *count;
NeilBrown67cc2b82009-03-31 14:39:38 +1100158
NeilBrowne4424fe2009-10-16 16:27:34 +1100159 if (sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700160 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100161 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100162 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100163 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100164 return syndrome_disks + 1;
NeilBrowne4424fe2009-10-16 16:27:34 +1100165 if (!sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700166 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100167 return slot;
168}
169
Dan Williamsa4456852007-07-09 11:56:43 -0700170static void return_io(struct bio *return_bi)
171{
172 struct bio *bi = return_bi;
173 while (bi) {
Dan Williamsa4456852007-07-09 11:56:43 -0700174
175 return_bi = bi->bi_next;
176 bi->bi_next = NULL;
177 bi->bi_size = 0;
Neil Brown0e13fe232008-06-28 08:31:20 +1000178 bio_endio(bi, 0);
Dan Williamsa4456852007-07-09 11:56:43 -0700179 bi = return_bi;
180 }
181}
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183static void print_raid5_conf (raid5_conf_t *conf);
184
Dan Williams600aa102008-06-28 08:32:05 +1000185static int stripe_operations_active(struct stripe_head *sh)
186{
187 return sh->check_state || sh->reconstruct_state ||
188 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
189 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
190}
191
Arjan van de Ven858119e2006-01-14 13:20:43 -0800192static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 if (atomic_dec_and_test(&sh->count)) {
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200195 BUG_ON(!list_empty(&sh->lru));
196 BUG_ON(atomic_read(&conf->active_stripes)==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if (test_bit(STRIPE_HANDLE, &sh->state)) {
NeilBrown482c0832011-04-18 18:25:42 +1000198 if (test_bit(STRIPE_DELAYED, &sh->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 list_add_tail(&sh->lru, &conf->delayed_list);
NeilBrown482c0832011-04-18 18:25:42 +1000200 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
201 sh->bm_seq - conf->seq_write > 0)
NeilBrown72626682005-09-09 16:23:54 -0700202 list_add_tail(&sh->lru, &conf->bitmap_list);
NeilBrown482c0832011-04-18 18:25:42 +1000203 else {
NeilBrown72626682005-09-09 16:23:54 -0700204 clear_bit(STRIPE_BIT_DELAY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 list_add_tail(&sh->lru, &conf->handle_list);
NeilBrown72626682005-09-09 16:23:54 -0700206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 md_wakeup_thread(conf->mddev->thread);
208 } else {
Dan Williams600aa102008-06-28 08:32:05 +1000209 BUG_ON(stripe_operations_active(sh));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
211 atomic_dec(&conf->preread_active_stripes);
212 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
213 md_wakeup_thread(conf->mddev->thread);
214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 atomic_dec(&conf->active_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800216 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
217 list_add_tail(&sh->lru, &conf->inactive_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 wake_up(&conf->wait_for_stripe);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -0800219 if (conf->retry_read_aligned)
220 md_wakeup_thread(conf->mddev->thread);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
223 }
224}
NeilBrownd0dabf72009-03-31 14:39:38 +1100225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226static void release_stripe(struct stripe_head *sh)
227{
228 raid5_conf_t *conf = sh->raid_conf;
229 unsigned long flags;
NeilBrown16a53ec2006-06-26 00:27:38 -0700230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 spin_lock_irqsave(&conf->device_lock, flags);
232 __release_stripe(conf, sh);
233 spin_unlock_irqrestore(&conf->device_lock, flags);
234}
235
NeilBrownfccddba2006-01-06 00:20:33 -0800236static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Dan Williams45b42332007-07-09 11:56:43 -0700238 pr_debug("remove_hash(), stripe %llu\n",
239 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
NeilBrownfccddba2006-01-06 00:20:33 -0800241 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
NeilBrown16a53ec2006-06-26 00:27:38 -0700244static inline void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
NeilBrownfccddba2006-01-06 00:20:33 -0800246 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Dan Williams45b42332007-07-09 11:56:43 -0700248 pr_debug("insert_hash(), stripe %llu\n",
249 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
NeilBrownfccddba2006-01-06 00:20:33 -0800251 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
254
255/* find an idle stripe, make sure it is unhashed, and return it. */
256static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
257{
258 struct stripe_head *sh = NULL;
259 struct list_head *first;
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (list_empty(&conf->inactive_list))
262 goto out;
263 first = conf->inactive_list.next;
264 sh = list_entry(first, struct stripe_head, lru);
265 list_del_init(first);
266 remove_hash(sh);
267 atomic_inc(&conf->active_stripes);
268out:
269 return sh;
270}
271
NeilBrowne4e11e32010-06-16 16:45:16 +1000272static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 struct page *p;
275 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000276 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
NeilBrowne4e11e32010-06-16 16:45:16 +1000278 for (i = 0; i < num ; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 p = sh->dev[i].page;
280 if (!p)
281 continue;
282 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800283 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
285}
286
NeilBrowne4e11e32010-06-16 16:45:16 +1000287static int grow_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000290 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
NeilBrowne4e11e32010-06-16 16:45:16 +1000292 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 struct page *page;
294
295 if (!(page = alloc_page(GFP_KERNEL))) {
296 return 1;
297 }
298 sh->dev[i].page = page;
299 }
300 return 0;
301}
302
NeilBrown784052e2009-03-31 15:19:07 +1100303static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrown911d4ee2009-03-31 14:39:38 +1100304static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
305 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
NeilBrownb5663ba2009-03-31 14:39:38 +1100307static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
309 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800310 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200312 BUG_ON(atomic_read(&sh->count) != 0);
313 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000314 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700315
Dan Williams45b42332007-07-09 11:56:43 -0700316 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 (unsigned long long)sh->sector);
318
319 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700320
NeilBrown86b42c72009-03-31 15:19:03 +1100321 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100322 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100324 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 sh->state = 0;
326
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800327
328 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 struct r5dev *dev = &sh->dev[i];
330
Dan Williamsd84e0f12007-01-02 13:52:30 -0700331 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700333 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700335 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000337 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100340 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342 insert_hash(conf, sh);
343}
344
NeilBrown86b42c72009-03-31 15:19:03 +1100345static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector,
346 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -0800349 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Dan Williams45b42332007-07-09 11:56:43 -0700351 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
NeilBrownfccddba2006-01-06 00:20:33 -0800352 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100353 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700355 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return NULL;
357}
358
NeilBrown674806d2010-06-16 17:17:53 +1000359/*
360 * Need to check if array has failed when deciding whether to:
361 * - start an array
362 * - remove non-faulty devices
363 * - add a spare
364 * - allow a reshape
365 * This determination is simple when no reshape is happening.
366 * However if there is a reshape, we need to carefully check
367 * both the before and after sections.
368 * This is because some failed devices may only affect one
369 * of the two sections, and some non-in_sync devices may
370 * be insync in the section most affected by failed devices.
371 */
372static int has_failed(raid5_conf_t *conf)
373{
374 int degraded;
375 int i;
376 if (conf->mddev->reshape_position == MaxSector)
377 return conf->mddev->degraded > conf->max_degraded;
378
379 rcu_read_lock();
380 degraded = 0;
381 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100382 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown674806d2010-06-16 17:17:53 +1000383 if (!rdev || test_bit(Faulty, &rdev->flags))
384 degraded++;
385 else if (test_bit(In_sync, &rdev->flags))
386 ;
387 else
388 /* not in-sync or faulty.
389 * If the reshape increases the number of devices,
390 * this is being recovered by the reshape, so
391 * this 'previous' section is not in_sync.
392 * If the number of devices is being reduced however,
393 * the device can only be part of the array if
394 * we are reverting a reshape, so this section will
395 * be in-sync.
396 */
397 if (conf->raid_disks >= conf->previous_raid_disks)
398 degraded++;
399 }
400 rcu_read_unlock();
401 if (degraded > conf->max_degraded)
402 return 1;
403 rcu_read_lock();
404 degraded = 0;
405 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100406 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown674806d2010-06-16 17:17:53 +1000407 if (!rdev || test_bit(Faulty, &rdev->flags))
408 degraded++;
409 else if (test_bit(In_sync, &rdev->flags))
410 ;
411 else
412 /* not in-sync or faulty.
413 * If reshape increases the number of devices, this
414 * section has already been recovered, else it
415 * almost certainly hasn't.
416 */
417 if (conf->raid_disks <= conf->previous_raid_disks)
418 degraded++;
419 }
420 rcu_read_unlock();
421 if (degraded > conf->max_degraded)
422 return 1;
423 return 0;
424}
425
NeilBrownb5663ba2009-03-31 14:39:38 +1100426static struct stripe_head *
427get_active_stripe(raid5_conf_t *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000428 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
430 struct stripe_head *sh;
431
Dan Williams45b42332007-07-09 11:56:43 -0700432 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 spin_lock_irq(&conf->device_lock);
435
436 do {
NeilBrown72626682005-09-09 16:23:54 -0700437 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000438 conf->quiesce == 0 || noquiesce,
NeilBrown72626682005-09-09 16:23:54 -0700439 conf->device_lock, /* nothing */);
NeilBrown86b42c72009-03-31 15:19:03 +1100440 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (!sh) {
442 if (!conf->inactive_blocked)
443 sh = get_free_stripe(conf);
444 if (noblock && sh == NULL)
445 break;
446 if (!sh) {
447 conf->inactive_blocked = 1;
448 wait_event_lock_irq(conf->wait_for_stripe,
449 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800450 (atomic_read(&conf->active_stripes)
451 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 || !conf->inactive_blocked),
453 conf->device_lock,
NeilBrown7c13edc2011-04-18 18:25:43 +1000454 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 conf->inactive_blocked = 0;
456 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100457 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 } else {
459 if (atomic_read(&sh->count)) {
NeilBrownab69ae12009-03-31 15:26:47 +1100460 BUG_ON(!list_empty(&sh->lru)
461 && !test_bit(STRIPE_EXPANDING, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 } else {
463 if (!test_bit(STRIPE_HANDLE, &sh->state))
464 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700465 if (list_empty(&sh->lru) &&
466 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700467 BUG();
468 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
470 }
471 } while (sh == NULL);
472
473 if (sh)
474 atomic_inc(&sh->count);
475
476 spin_unlock_irq(&conf->device_lock);
477 return sh;
478}
479
NeilBrown6712ecf2007-09-27 12:47:43 +0200480static void
481raid5_end_read_request(struct bio *bi, int error);
482static void
483raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700484
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000485static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700486{
487 raid5_conf_t *conf = sh->raid_conf;
488 int i, disks = sh->disks;
489
490 might_sleep();
491
492 for (i = disks; i--; ) {
493 int rw;
494 struct bio *bi;
NeilBrown3cb03002011-10-11 16:45:26 +1100495 struct md_rdev *rdev;
Tejun Heoe9c74692010-09-03 11:56:18 +0200496 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
497 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
498 rw = WRITE_FUA;
499 else
500 rw = WRITE;
501 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700502 rw = READ;
503 else
504 continue;
505
506 bi = &sh->dev[i].req;
507
508 bi->bi_rw = rw;
Namhyung Kimb0629622011-06-14 14:20:19 +1000509 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700510 bi->bi_end_io = raid5_end_write_request;
511 else
512 bi->bi_end_io = raid5_end_read_request;
513
514 rcu_read_lock();
515 rdev = rcu_dereference(conf->disks[i].rdev);
516 if (rdev && test_bit(Faulty, &rdev->flags))
517 rdev = NULL;
518 if (rdev)
519 atomic_inc(&rdev->nr_pending);
520 rcu_read_unlock();
521
NeilBrown73e92e52011-07-28 11:39:22 +1000522 /* We have already checked bad blocks for reads. Now
523 * need to check for writes.
524 */
525 while ((rw & WRITE) && rdev &&
526 test_bit(WriteErrorSeen, &rdev->flags)) {
527 sector_t first_bad;
528 int bad_sectors;
529 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
530 &first_bad, &bad_sectors);
531 if (!bad)
532 break;
533
534 if (bad < 0) {
535 set_bit(BlockedBadBlocks, &rdev->flags);
536 if (!conf->mddev->external &&
537 conf->mddev->flags) {
538 /* It is very unlikely, but we might
539 * still need to write out the
540 * bad block log - better give it
541 * a chance*/
542 md_check_recovery(conf->mddev);
543 }
544 md_wait_for_blocked_rdev(rdev, conf->mddev);
545 } else {
546 /* Acknowledged bad block - skip the write */
547 rdev_dec_pending(rdev, conf->mddev);
548 rdev = NULL;
549 }
550 }
551
Dan Williams91c00922007-01-02 13:52:30 -0700552 if (rdev) {
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000553 if (s->syncing || s->expanding || s->expanded)
Dan Williams91c00922007-01-02 13:52:30 -0700554 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
555
Dan Williams2b7497f2008-06-28 08:31:52 +1000556 set_bit(STRIPE_IO_STARTED, &sh->state);
557
Dan Williams91c00922007-01-02 13:52:30 -0700558 bi->bi_bdev = rdev->bdev;
559 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700560 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700561 bi->bi_rw, i);
562 atomic_inc(&sh->count);
563 bi->bi_sector = sh->sector + rdev->data_offset;
564 bi->bi_flags = 1 << BIO_UPTODATE;
565 bi->bi_vcnt = 1;
566 bi->bi_max_vecs = 1;
567 bi->bi_idx = 0;
568 bi->bi_io_vec = &sh->dev[i].vec;
569 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
570 bi->bi_io_vec[0].bv_offset = 0;
571 bi->bi_size = STRIPE_SIZE;
572 bi->bi_next = NULL;
Dan Williams91c00922007-01-02 13:52:30 -0700573 generic_make_request(bi);
574 } else {
Namhyung Kimb0629622011-06-14 14:20:19 +1000575 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700576 set_bit(STRIPE_DEGRADED, &sh->state);
577 pr_debug("skip op %ld on disc %d for sector %llu\n",
578 bi->bi_rw, i, (unsigned long long)sh->sector);
579 clear_bit(R5_LOCKED, &sh->dev[i].flags);
580 set_bit(STRIPE_HANDLE, &sh->state);
581 }
582 }
583}
584
585static struct dma_async_tx_descriptor *
586async_copy_data(int frombio, struct bio *bio, struct page *page,
587 sector_t sector, struct dma_async_tx_descriptor *tx)
588{
589 struct bio_vec *bvl;
590 struct page *bio_page;
591 int i;
592 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700593 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700594 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700595
596 if (bio->bi_sector >= sector)
597 page_offset = (signed)(bio->bi_sector - sector) * 512;
598 else
599 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700600
Dan Williams0403e382009-09-08 17:42:50 -0700601 if (frombio)
602 flags |= ASYNC_TX_FENCE;
603 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
604
Dan Williams91c00922007-01-02 13:52:30 -0700605 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000606 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700607 int clen;
608 int b_offset = 0;
609
610 if (page_offset < 0) {
611 b_offset = -page_offset;
612 page_offset += b_offset;
613 len -= b_offset;
614 }
615
616 if (len > 0 && page_offset + len > STRIPE_SIZE)
617 clen = STRIPE_SIZE - page_offset;
618 else
619 clen = len;
620
621 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000622 b_offset += bvl->bv_offset;
623 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700624 if (frombio)
625 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700626 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700627 else
628 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700629 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700630 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700631 /* chain the operations */
632 submit.depend_tx = tx;
633
Dan Williams91c00922007-01-02 13:52:30 -0700634 if (clen < len) /* hit end of page */
635 break;
636 page_offset += len;
637 }
638
639 return tx;
640}
641
642static void ops_complete_biofill(void *stripe_head_ref)
643{
644 struct stripe_head *sh = stripe_head_ref;
645 struct bio *return_bi = NULL;
646 raid5_conf_t *conf = sh->raid_conf;
Dan Williamse4d84902007-09-24 10:06:13 -0700647 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700648
Harvey Harrisone46b2722008-04-28 02:15:50 -0700649 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700650 (unsigned long long)sh->sector);
651
652 /* clear completed biofills */
Dan Williams83de75c2008-06-28 08:31:58 +1000653 spin_lock_irq(&conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700654 for (i = sh->disks; i--; ) {
655 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700656
657 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700658 /* and check if we need to reply to a read request,
659 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000660 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700661 */
662 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700663 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700664
Dan Williams91c00922007-01-02 13:52:30 -0700665 BUG_ON(!dev->read);
666 rbi = dev->read;
667 dev->read = NULL;
668 while (rbi && rbi->bi_sector <
669 dev->sector + STRIPE_SECTORS) {
670 rbi2 = r5_next_bio(rbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +0200671 if (!raid5_dec_bi_phys_segments(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700672 rbi->bi_next = return_bi;
673 return_bi = rbi;
674 }
Dan Williams91c00922007-01-02 13:52:30 -0700675 rbi = rbi2;
676 }
677 }
678 }
Dan Williams83de75c2008-06-28 08:31:58 +1000679 spin_unlock_irq(&conf->device_lock);
680 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700681
682 return_io(return_bi);
683
Dan Williamse4d84902007-09-24 10:06:13 -0700684 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700685 release_stripe(sh);
686}
687
688static void ops_run_biofill(struct stripe_head *sh)
689{
690 struct dma_async_tx_descriptor *tx = NULL;
691 raid5_conf_t *conf = sh->raid_conf;
Dan Williamsa08abd82009-06-03 11:43:59 -0700692 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700693 int i;
694
Harvey Harrisone46b2722008-04-28 02:15:50 -0700695 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700696 (unsigned long long)sh->sector);
697
698 for (i = sh->disks; i--; ) {
699 struct r5dev *dev = &sh->dev[i];
700 if (test_bit(R5_Wantfill, &dev->flags)) {
701 struct bio *rbi;
702 spin_lock_irq(&conf->device_lock);
703 dev->read = rbi = dev->toread;
704 dev->toread = NULL;
705 spin_unlock_irq(&conf->device_lock);
706 while (rbi && rbi->bi_sector <
707 dev->sector + STRIPE_SECTORS) {
708 tx = async_copy_data(0, rbi, dev->page,
709 dev->sector, tx);
710 rbi = r5_next_bio(rbi, dev->sector);
711 }
712 }
713 }
714
715 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700716 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
717 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700718}
719
Dan Williams4e7d2c02009-08-29 19:13:11 -0700720static void mark_target_uptodate(struct stripe_head *sh, int target)
721{
722 struct r5dev *tgt;
723
724 if (target < 0)
725 return;
726
727 tgt = &sh->dev[target];
728 set_bit(R5_UPTODATE, &tgt->flags);
729 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
730 clear_bit(R5_Wantcompute, &tgt->flags);
731}
732
Dan Williamsac6b53b2009-07-14 13:40:19 -0700733static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700734{
735 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700736
Harvey Harrisone46b2722008-04-28 02:15:50 -0700737 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700738 (unsigned long long)sh->sector);
739
Dan Williamsac6b53b2009-07-14 13:40:19 -0700740 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700741 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700742 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700743
Dan Williamsecc65c92008-06-28 08:31:57 +1000744 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
745 if (sh->check_state == check_state_compute_run)
746 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700747 set_bit(STRIPE_HANDLE, &sh->state);
748 release_stripe(sh);
749}
750
Dan Williamsd6f38f32009-07-14 11:50:52 -0700751/* return a pointer to the address conversion region of the scribble buffer */
752static addr_conv_t *to_addr_conv(struct stripe_head *sh,
753 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700754{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700755 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
756}
757
758static struct dma_async_tx_descriptor *
759ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
760{
Dan Williams91c00922007-01-02 13:52:30 -0700761 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700762 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700763 int target = sh->ops.target;
764 struct r5dev *tgt = &sh->dev[target];
765 struct page *xor_dest = tgt->page;
766 int count = 0;
767 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700768 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700769 int i;
770
771 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700772 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700773 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
774
775 for (i = disks; i--; )
776 if (i != target)
777 xor_srcs[count++] = sh->dev[i].page;
778
779 atomic_inc(&sh->count);
780
Dan Williams0403e382009-09-08 17:42:50 -0700781 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700782 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700783 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700784 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700785 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700786 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700787
Dan Williams91c00922007-01-02 13:52:30 -0700788 return tx;
789}
790
Dan Williamsac6b53b2009-07-14 13:40:19 -0700791/* set_syndrome_sources - populate source buffers for gen_syndrome
792 * @srcs - (struct page *) array of size sh->disks
793 * @sh - stripe_head to parse
794 *
795 * Populates srcs in proper layout order for the stripe and returns the
796 * 'count' of sources to be used in a call to async_gen_syndrome. The P
797 * destination buffer is recorded in srcs[count] and the Q destination
798 * is recorded in srcs[count+1]].
799 */
800static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
801{
802 int disks = sh->disks;
803 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
804 int d0_idx = raid6_d0(sh);
805 int count;
806 int i;
807
808 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100809 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700810
811 count = 0;
812 i = d0_idx;
813 do {
814 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
815
816 srcs[slot] = sh->dev[i].page;
817 i = raid6_next_disk(i, disks);
818 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700819
NeilBrowne4424fe2009-10-16 16:27:34 +1100820 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700821}
822
823static struct dma_async_tx_descriptor *
824ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
825{
826 int disks = sh->disks;
827 struct page **blocks = percpu->scribble;
828 int target;
829 int qd_idx = sh->qd_idx;
830 struct dma_async_tx_descriptor *tx;
831 struct async_submit_ctl submit;
832 struct r5dev *tgt;
833 struct page *dest;
834 int i;
835 int count;
836
837 if (sh->ops.target < 0)
838 target = sh->ops.target2;
839 else if (sh->ops.target2 < 0)
840 target = sh->ops.target;
841 else
842 /* we should only have one valid target */
843 BUG();
844 BUG_ON(target < 0);
845 pr_debug("%s: stripe %llu block: %d\n",
846 __func__, (unsigned long long)sh->sector, target);
847
848 tgt = &sh->dev[target];
849 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
850 dest = tgt->page;
851
852 atomic_inc(&sh->count);
853
854 if (target == qd_idx) {
855 count = set_syndrome_sources(blocks, sh);
856 blocks[count] = NULL; /* regenerating p is not necessary */
857 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700858 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
859 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700860 to_addr_conv(sh, percpu));
861 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
862 } else {
863 /* Compute any data- or p-drive using XOR */
864 count = 0;
865 for (i = disks; i-- ; ) {
866 if (i == target || i == qd_idx)
867 continue;
868 blocks[count++] = sh->dev[i].page;
869 }
870
Dan Williams0403e382009-09-08 17:42:50 -0700871 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
872 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700873 to_addr_conv(sh, percpu));
874 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
875 }
876
877 return tx;
878}
879
880static struct dma_async_tx_descriptor *
881ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
882{
883 int i, count, disks = sh->disks;
884 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
885 int d0_idx = raid6_d0(sh);
886 int faila = -1, failb = -1;
887 int target = sh->ops.target;
888 int target2 = sh->ops.target2;
889 struct r5dev *tgt = &sh->dev[target];
890 struct r5dev *tgt2 = &sh->dev[target2];
891 struct dma_async_tx_descriptor *tx;
892 struct page **blocks = percpu->scribble;
893 struct async_submit_ctl submit;
894
895 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
896 __func__, (unsigned long long)sh->sector, target, target2);
897 BUG_ON(target < 0 || target2 < 0);
898 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
899 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
900
Dan Williams6c910a72009-09-16 12:24:54 -0700901 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -0700902 * slot number conversion for 'faila' and 'failb'
903 */
904 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100905 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700906 count = 0;
907 i = d0_idx;
908 do {
909 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
910
911 blocks[slot] = sh->dev[i].page;
912
913 if (i == target)
914 faila = slot;
915 if (i == target2)
916 failb = slot;
917 i = raid6_next_disk(i, disks);
918 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700919
920 BUG_ON(faila == failb);
921 if (failb < faila)
922 swap(faila, failb);
923 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
924 __func__, (unsigned long long)sh->sector, faila, failb);
925
926 atomic_inc(&sh->count);
927
928 if (failb == syndrome_disks+1) {
929 /* Q disk is one of the missing disks */
930 if (faila == syndrome_disks) {
931 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -0700932 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
933 ops_complete_compute, sh,
934 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +1100935 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700936 STRIPE_SIZE, &submit);
937 } else {
938 struct page *dest;
939 int data_target;
940 int qd_idx = sh->qd_idx;
941
942 /* Missing D+Q: recompute D from P, then recompute Q */
943 if (target == qd_idx)
944 data_target = target2;
945 else
946 data_target = target;
947
948 count = 0;
949 for (i = disks; i-- ; ) {
950 if (i == data_target || i == qd_idx)
951 continue;
952 blocks[count++] = sh->dev[i].page;
953 }
954 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -0700955 init_async_submit(&submit,
956 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
957 NULL, NULL, NULL,
958 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -0700959 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
960 &submit);
961
962 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -0700963 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
964 ops_complete_compute, sh,
965 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -0700966 return async_gen_syndrome(blocks, 0, count+2,
967 STRIPE_SIZE, &submit);
968 }
Dan Williamsac6b53b2009-07-14 13:40:19 -0700969 } else {
Dan Williams6c910a72009-09-16 12:24:54 -0700970 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
971 ops_complete_compute, sh,
972 to_addr_conv(sh, percpu));
973 if (failb == syndrome_disks) {
974 /* We're missing D+P. */
975 return async_raid6_datap_recov(syndrome_disks+2,
976 STRIPE_SIZE, faila,
977 blocks, &submit);
978 } else {
979 /* We're missing D+D. */
980 return async_raid6_2data_recov(syndrome_disks+2,
981 STRIPE_SIZE, faila, failb,
982 blocks, &submit);
983 }
Dan Williamsac6b53b2009-07-14 13:40:19 -0700984 }
985}
986
987
Dan Williams91c00922007-01-02 13:52:30 -0700988static void ops_complete_prexor(void *stripe_head_ref)
989{
990 struct stripe_head *sh = stripe_head_ref;
991
Harvey Harrisone46b2722008-04-28 02:15:50 -0700992 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700993 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -0700994}
995
996static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -0700997ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
998 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -0700999{
Dan Williams91c00922007-01-02 13:52:30 -07001000 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001001 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001002 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001003 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001004
1005 /* existing parity data subtracted */
1006 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1007
Harvey Harrisone46b2722008-04-28 02:15:50 -07001008 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001009 (unsigned long long)sh->sector);
1010
1011 for (i = disks; i--; ) {
1012 struct r5dev *dev = &sh->dev[i];
1013 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001014 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001015 xor_srcs[count++] = dev->page;
1016 }
1017
Dan Williams0403e382009-09-08 17:42:50 -07001018 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001019 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001020 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001021
1022 return tx;
1023}
1024
1025static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001026ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001027{
1028 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001029 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001030
Harvey Harrisone46b2722008-04-28 02:15:50 -07001031 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001032 (unsigned long long)sh->sector);
1033
1034 for (i = disks; i--; ) {
1035 struct r5dev *dev = &sh->dev[i];
1036 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001037
Dan Williamsd8ee0722008-06-28 08:32:06 +10001038 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001039 struct bio *wbi;
1040
NeilBrowncbe47ec2011-07-26 11:20:35 +10001041 spin_lock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001042 chosen = dev->towrite;
1043 dev->towrite = NULL;
1044 BUG_ON(dev->written);
1045 wbi = dev->written = chosen;
NeilBrowncbe47ec2011-07-26 11:20:35 +10001046 spin_unlock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001047
1048 while (wbi && wbi->bi_sector <
1049 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001050 if (wbi->bi_rw & REQ_FUA)
1051 set_bit(R5_WantFUA, &dev->flags);
Dan Williams91c00922007-01-02 13:52:30 -07001052 tx = async_copy_data(1, wbi, dev->page,
1053 dev->sector, tx);
1054 wbi = r5_next_bio(wbi, dev->sector);
1055 }
1056 }
1057 }
1058
1059 return tx;
1060}
1061
Dan Williamsac6b53b2009-07-14 13:40:19 -07001062static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001063{
1064 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001065 int disks = sh->disks;
1066 int pd_idx = sh->pd_idx;
1067 int qd_idx = sh->qd_idx;
1068 int i;
Tejun Heoe9c74692010-09-03 11:56:18 +02001069 bool fua = false;
Dan Williams91c00922007-01-02 13:52:30 -07001070
Harvey Harrisone46b2722008-04-28 02:15:50 -07001071 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001072 (unsigned long long)sh->sector);
1073
Tejun Heoe9c74692010-09-03 11:56:18 +02001074 for (i = disks; i--; )
1075 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
1076
Dan Williams91c00922007-01-02 13:52:30 -07001077 for (i = disks; i--; ) {
1078 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001079
Tejun Heoe9c74692010-09-03 11:56:18 +02001080 if (dev->written || i == pd_idx || i == qd_idx) {
Dan Williams91c00922007-01-02 13:52:30 -07001081 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001082 if (fua)
1083 set_bit(R5_WantFUA, &dev->flags);
1084 }
Dan Williams91c00922007-01-02 13:52:30 -07001085 }
1086
Dan Williamsd8ee0722008-06-28 08:32:06 +10001087 if (sh->reconstruct_state == reconstruct_state_drain_run)
1088 sh->reconstruct_state = reconstruct_state_drain_result;
1089 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1090 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1091 else {
1092 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1093 sh->reconstruct_state = reconstruct_state_result;
1094 }
Dan Williams91c00922007-01-02 13:52:30 -07001095
1096 set_bit(STRIPE_HANDLE, &sh->state);
1097 release_stripe(sh);
1098}
1099
1100static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001101ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1102 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001103{
Dan Williams91c00922007-01-02 13:52:30 -07001104 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001105 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001106 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001107 int count = 0, pd_idx = sh->pd_idx, i;
1108 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001109 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001110 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001111
Harvey Harrisone46b2722008-04-28 02:15:50 -07001112 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001113 (unsigned long long)sh->sector);
1114
1115 /* check if prexor is active which means only process blocks
1116 * that are part of a read-modify-write (written)
1117 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001118 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1119 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001120 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1121 for (i = disks; i--; ) {
1122 struct r5dev *dev = &sh->dev[i];
1123 if (dev->written)
1124 xor_srcs[count++] = dev->page;
1125 }
1126 } else {
1127 xor_dest = sh->dev[pd_idx].page;
1128 for (i = disks; i--; ) {
1129 struct r5dev *dev = &sh->dev[i];
1130 if (i != pd_idx)
1131 xor_srcs[count++] = dev->page;
1132 }
1133 }
1134
Dan Williams91c00922007-01-02 13:52:30 -07001135 /* 1/ if we prexor'd then the dest is reused as a source
1136 * 2/ if we did not prexor then we are redoing the parity
1137 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1138 * for the synchronous xor case
1139 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001140 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001141 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1142
1143 atomic_inc(&sh->count);
1144
Dan Williamsac6b53b2009-07-14 13:40:19 -07001145 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001146 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001147 if (unlikely(count == 1))
1148 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1149 else
1150 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001151}
1152
Dan Williamsac6b53b2009-07-14 13:40:19 -07001153static void
1154ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1155 struct dma_async_tx_descriptor *tx)
1156{
1157 struct async_submit_ctl submit;
1158 struct page **blocks = percpu->scribble;
1159 int count;
1160
1161 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1162
1163 count = set_syndrome_sources(blocks, sh);
1164
1165 atomic_inc(&sh->count);
1166
1167 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1168 sh, to_addr_conv(sh, percpu));
1169 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001170}
1171
1172static void ops_complete_check(void *stripe_head_ref)
1173{
1174 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001175
Harvey Harrisone46b2722008-04-28 02:15:50 -07001176 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001177 (unsigned long long)sh->sector);
1178
Dan Williamsecc65c92008-06-28 08:31:57 +10001179 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001180 set_bit(STRIPE_HANDLE, &sh->state);
1181 release_stripe(sh);
1182}
1183
Dan Williamsac6b53b2009-07-14 13:40:19 -07001184static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001185{
Dan Williams91c00922007-01-02 13:52:30 -07001186 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001187 int pd_idx = sh->pd_idx;
1188 int qd_idx = sh->qd_idx;
1189 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001190 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001191 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001192 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001193 int count;
1194 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001195
Harvey Harrisone46b2722008-04-28 02:15:50 -07001196 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001197 (unsigned long long)sh->sector);
1198
Dan Williamsac6b53b2009-07-14 13:40:19 -07001199 count = 0;
1200 xor_dest = sh->dev[pd_idx].page;
1201 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001202 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001203 if (i == pd_idx || i == qd_idx)
1204 continue;
1205 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001206 }
1207
Dan Williamsd6f38f32009-07-14 11:50:52 -07001208 init_async_submit(&submit, 0, NULL, NULL, NULL,
1209 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001210 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001211 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001212
Dan Williams91c00922007-01-02 13:52:30 -07001213 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001214 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1215 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001216}
1217
Dan Williamsac6b53b2009-07-14 13:40:19 -07001218static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1219{
1220 struct page **srcs = percpu->scribble;
1221 struct async_submit_ctl submit;
1222 int count;
1223
1224 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1225 (unsigned long long)sh->sector, checkp);
1226
1227 count = set_syndrome_sources(srcs, sh);
1228 if (!checkp)
1229 srcs[count] = NULL;
1230
1231 atomic_inc(&sh->count);
1232 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1233 sh, to_addr_conv(sh, percpu));
1234 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1235 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1236}
1237
Dan Williams417b8d42009-10-16 16:25:22 +11001238static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001239{
1240 int overlap_clear = 0, i, disks = sh->disks;
1241 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001242 raid5_conf_t *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001243 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001244 struct raid5_percpu *percpu;
1245 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001246
Dan Williamsd6f38f32009-07-14 11:50:52 -07001247 cpu = get_cpu();
1248 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001249 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001250 ops_run_biofill(sh);
1251 overlap_clear++;
1252 }
1253
Dan Williams7b3a8712008-06-28 08:32:09 +10001254 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001255 if (level < 6)
1256 tx = ops_run_compute5(sh, percpu);
1257 else {
1258 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1259 tx = ops_run_compute6_1(sh, percpu);
1260 else
1261 tx = ops_run_compute6_2(sh, percpu);
1262 }
1263 /* terminate the chain if reconstruct is not set to be run */
1264 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001265 async_tx_ack(tx);
1266 }
Dan Williams91c00922007-01-02 13:52:30 -07001267
Dan Williams600aa102008-06-28 08:32:05 +10001268 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001269 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001270
Dan Williams600aa102008-06-28 08:32:05 +10001271 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001272 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001273 overlap_clear++;
1274 }
1275
Dan Williamsac6b53b2009-07-14 13:40:19 -07001276 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1277 if (level < 6)
1278 ops_run_reconstruct5(sh, percpu, tx);
1279 else
1280 ops_run_reconstruct6(sh, percpu, tx);
1281 }
Dan Williams91c00922007-01-02 13:52:30 -07001282
Dan Williamsac6b53b2009-07-14 13:40:19 -07001283 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1284 if (sh->check_state == check_state_run)
1285 ops_run_check_p(sh, percpu);
1286 else if (sh->check_state == check_state_run_q)
1287 ops_run_check_pq(sh, percpu, 0);
1288 else if (sh->check_state == check_state_run_pq)
1289 ops_run_check_pq(sh, percpu, 1);
1290 else
1291 BUG();
1292 }
Dan Williams91c00922007-01-02 13:52:30 -07001293
Dan Williams91c00922007-01-02 13:52:30 -07001294 if (overlap_clear)
1295 for (i = disks; i--; ) {
1296 struct r5dev *dev = &sh->dev[i];
1297 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1298 wake_up(&sh->raid_conf->wait_for_overlap);
1299 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001300 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001301}
1302
Dan Williams417b8d42009-10-16 16:25:22 +11001303#ifdef CONFIG_MULTICORE_RAID456
1304static void async_run_ops(void *param, async_cookie_t cookie)
1305{
1306 struct stripe_head *sh = param;
1307 unsigned long ops_request = sh->ops.request;
1308
1309 clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
1310 wake_up(&sh->ops.wait_for_ops);
1311
1312 __raid_run_ops(sh, ops_request);
1313 release_stripe(sh);
1314}
1315
1316static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1317{
1318 /* since handle_stripe can be called outside of raid5d context
1319 * we need to ensure sh->ops.request is de-staged before another
1320 * request arrives
1321 */
1322 wait_event(sh->ops.wait_for_ops,
1323 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
1324 sh->ops.request = ops_request;
1325
1326 atomic_inc(&sh->count);
1327 async_schedule(async_run_ops, sh);
1328}
1329#else
1330#define raid_run_ops __raid_run_ops
1331#endif
1332
NeilBrown3f294f42005-11-08 21:39:25 -08001333static int grow_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334{
1335 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001336 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001337 if (!sh)
1338 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001339
NeilBrown3f294f42005-11-08 21:39:25 -08001340 sh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001341 #ifdef CONFIG_MULTICORE_RAID456
1342 init_waitqueue_head(&sh->ops.wait_for_ops);
1343 #endif
NeilBrown3f294f42005-11-08 21:39:25 -08001344
NeilBrowne4e11e32010-06-16 16:45:16 +10001345 if (grow_buffers(sh)) {
1346 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001347 kmem_cache_free(conf->slab_cache, sh);
1348 return 0;
1349 }
1350 /* we just created an active stripe so... */
1351 atomic_set(&sh->count, 1);
1352 atomic_inc(&conf->active_stripes);
1353 INIT_LIST_HEAD(&sh->lru);
1354 release_stripe(sh);
1355 return 1;
1356}
1357
1358static int grow_stripes(raid5_conf_t *conf, int num)
1359{
Christoph Lametere18b8902006-12-06 20:33:20 -08001360 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001361 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
NeilBrownf4be6b42010-06-01 19:37:25 +10001363 if (conf->mddev->gendisk)
1364 sprintf(conf->cache_name[0],
1365 "raid%d-%s", conf->level, mdname(conf->mddev));
1366 else
1367 sprintf(conf->cache_name[0],
1368 "raid%d-%p", conf->level, conf->mddev);
1369 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1370
NeilBrownad01c9e2006-03-27 01:18:07 -08001371 conf->active_name = 0;
1372 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001374 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 if (!sc)
1376 return 1;
1377 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001378 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001379 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001380 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 return 0;
1383}
NeilBrown29269552006-03-27 01:18:10 -08001384
Dan Williamsd6f38f32009-07-14 11:50:52 -07001385/**
1386 * scribble_len - return the required size of the scribble region
1387 * @num - total number of disks in the array
1388 *
1389 * The size must be enough to contain:
1390 * 1/ a struct page pointer for each device in the array +2
1391 * 2/ room to convert each entry in (1) to its corresponding dma
1392 * (dma_map_page()) or page (page_address()) address.
1393 *
1394 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1395 * calculate over all devices (not just the data blocks), using zeros in place
1396 * of the P and Q blocks.
1397 */
1398static size_t scribble_len(int num)
1399{
1400 size_t len;
1401
1402 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1403
1404 return len;
1405}
1406
NeilBrownad01c9e2006-03-27 01:18:07 -08001407static int resize_stripes(raid5_conf_t *conf, int newsize)
1408{
1409 /* Make all the stripes able to hold 'newsize' devices.
1410 * New slots in each stripe get 'page' set to a new page.
1411 *
1412 * This happens in stages:
1413 * 1/ create a new kmem_cache and allocate the required number of
1414 * stripe_heads.
1415 * 2/ gather all the old stripe_heads and tranfer the pages across
1416 * to the new stripe_heads. This will have the side effect of
1417 * freezing the array as once all stripe_heads have been collected,
1418 * no IO will be possible. Old stripe heads are freed once their
1419 * pages have been transferred over, and the old kmem_cache is
1420 * freed when all stripes are done.
1421 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1422 * we simple return a failre status - no need to clean anything up.
1423 * 4/ allocate new pages for the new slots in the new stripe_heads.
1424 * If this fails, we don't bother trying the shrink the
1425 * stripe_heads down again, we just leave them as they are.
1426 * As each stripe_head is processed the new one is released into
1427 * active service.
1428 *
1429 * Once step2 is started, we cannot afford to wait for a write,
1430 * so we use GFP_NOIO allocations.
1431 */
1432 struct stripe_head *osh, *nsh;
1433 LIST_HEAD(newstripes);
1434 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001435 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001436 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001437 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001438 int i;
1439
1440 if (newsize <= conf->pool_size)
1441 return 0; /* never bother to shrink */
1442
Dan Williamsb5470dc2008-06-27 21:44:04 -07001443 err = md_allow_write(conf->mddev);
1444 if (err)
1445 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001446
NeilBrownad01c9e2006-03-27 01:18:07 -08001447 /* Step 1 */
1448 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1449 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001450 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001451 if (!sc)
1452 return -ENOMEM;
1453
1454 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001455 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001456 if (!nsh)
1457 break;
1458
NeilBrownad01c9e2006-03-27 01:18:07 -08001459 nsh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001460 #ifdef CONFIG_MULTICORE_RAID456
1461 init_waitqueue_head(&nsh->ops.wait_for_ops);
1462 #endif
NeilBrownad01c9e2006-03-27 01:18:07 -08001463
1464 list_add(&nsh->lru, &newstripes);
1465 }
1466 if (i) {
1467 /* didn't get enough, give up */
1468 while (!list_empty(&newstripes)) {
1469 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1470 list_del(&nsh->lru);
1471 kmem_cache_free(sc, nsh);
1472 }
1473 kmem_cache_destroy(sc);
1474 return -ENOMEM;
1475 }
1476 /* Step 2 - Must use GFP_NOIO now.
1477 * OK, we have enough stripes, start collecting inactive
1478 * stripes and copying them over
1479 */
1480 list_for_each_entry(nsh, &newstripes, lru) {
1481 spin_lock_irq(&conf->device_lock);
1482 wait_event_lock_irq(conf->wait_for_stripe,
1483 !list_empty(&conf->inactive_list),
1484 conf->device_lock,
NeilBrown482c0832011-04-18 18:25:42 +10001485 );
NeilBrownad01c9e2006-03-27 01:18:07 -08001486 osh = get_free_stripe(conf);
1487 spin_unlock_irq(&conf->device_lock);
1488 atomic_set(&nsh->count, 1);
1489 for(i=0; i<conf->pool_size; i++)
1490 nsh->dev[i].page = osh->dev[i].page;
1491 for( ; i<newsize; i++)
1492 nsh->dev[i].page = NULL;
1493 kmem_cache_free(conf->slab_cache, osh);
1494 }
1495 kmem_cache_destroy(conf->slab_cache);
1496
1497 /* Step 3.
1498 * At this point, we are holding all the stripes so the array
1499 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001500 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001501 */
1502 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1503 if (ndisks) {
1504 for (i=0; i<conf->raid_disks; i++)
1505 ndisks[i] = conf->disks[i];
1506 kfree(conf->disks);
1507 conf->disks = ndisks;
1508 } else
1509 err = -ENOMEM;
1510
Dan Williamsd6f38f32009-07-14 11:50:52 -07001511 get_online_cpus();
1512 conf->scribble_len = scribble_len(newsize);
1513 for_each_present_cpu(cpu) {
1514 struct raid5_percpu *percpu;
1515 void *scribble;
1516
1517 percpu = per_cpu_ptr(conf->percpu, cpu);
1518 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1519
1520 if (scribble) {
1521 kfree(percpu->scribble);
1522 percpu->scribble = scribble;
1523 } else {
1524 err = -ENOMEM;
1525 break;
1526 }
1527 }
1528 put_online_cpus();
1529
NeilBrownad01c9e2006-03-27 01:18:07 -08001530 /* Step 4, return new stripes to service */
1531 while(!list_empty(&newstripes)) {
1532 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1533 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001534
NeilBrownad01c9e2006-03-27 01:18:07 -08001535 for (i=conf->raid_disks; i < newsize; i++)
1536 if (nsh->dev[i].page == NULL) {
1537 struct page *p = alloc_page(GFP_NOIO);
1538 nsh->dev[i].page = p;
1539 if (!p)
1540 err = -ENOMEM;
1541 }
1542 release_stripe(nsh);
1543 }
1544 /* critical section pass, GFP_NOIO no longer needed */
1545
1546 conf->slab_cache = sc;
1547 conf->active_name = 1-conf->active_name;
1548 conf->pool_size = newsize;
1549 return err;
1550}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
NeilBrown3f294f42005-11-08 21:39:25 -08001552static int drop_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
1554 struct stripe_head *sh;
1555
NeilBrown3f294f42005-11-08 21:39:25 -08001556 spin_lock_irq(&conf->device_lock);
1557 sh = get_free_stripe(conf);
1558 spin_unlock_irq(&conf->device_lock);
1559 if (!sh)
1560 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001561 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001562 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001563 kmem_cache_free(conf->slab_cache, sh);
1564 atomic_dec(&conf->active_stripes);
1565 return 1;
1566}
1567
1568static void shrink_stripes(raid5_conf_t *conf)
1569{
1570 while (drop_one_stripe(conf))
1571 ;
1572
NeilBrown29fc7e32006-02-03 03:03:41 -08001573 if (conf->slab_cache)
1574 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 conf->slab_cache = NULL;
1576}
1577
NeilBrown6712ecf2007-09-27 12:47:43 +02001578static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579{
NeilBrown99c0fb52009-03-31 14:39:38 +11001580 struct stripe_head *sh = bi->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001582 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001584 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11001585 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588 for (i=0 ; i<disks; i++)
1589 if (bi == &sh->dev[i].req)
1590 break;
1591
Dan Williams45b42332007-07-09 11:56:43 -07001592 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1593 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 uptodate);
1595 if (i == disks) {
1596 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001597 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 }
1599
1600 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001602 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrownd6950432006-07-10 04:44:20 -07001603 rdev = conf->disks[i].rdev;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001604 printk_ratelimited(
1605 KERN_INFO
1606 "md/raid:%s: read error corrected"
1607 " (%lu sectors at %llu on %s)\n",
1608 mdname(conf->mddev), STRIPE_SECTORS,
1609 (unsigned long long)(sh->sector
1610 + rdev->data_offset),
1611 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001612 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001613 clear_bit(R5_ReadError, &sh->dev[i].flags);
1614 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1615 }
NeilBrownba22dcb2005-11-08 21:39:31 -08001616 if (atomic_read(&conf->disks[i].rdev->read_errors))
1617 atomic_set(&conf->disks[i].rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 } else {
NeilBrownd6950432006-07-10 04:44:20 -07001619 const char *bdn = bdevname(conf->disks[i].rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001620 int retry = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001621 rdev = conf->disks[i].rdev;
1622
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001624 atomic_inc(&rdev->read_errors);
Gabriele A. Trombetti7b0bb532010-04-28 11:51:17 +10001625 if (conf->mddev->degraded >= conf->max_degraded)
Christian Dietrich8bda4702011-07-27 11:00:36 +10001626 printk_ratelimited(
1627 KERN_WARNING
1628 "md/raid:%s: read error not correctable "
1629 "(sector %llu on %s).\n",
1630 mdname(conf->mddev),
1631 (unsigned long long)(sh->sector
1632 + rdev->data_offset),
1633 bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001634 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
NeilBrown4e5314b2005-11-08 21:39:22 -08001635 /* Oh, no!!! */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001636 printk_ratelimited(
1637 KERN_WARNING
1638 "md/raid:%s: read error NOT corrected!! "
1639 "(sector %llu on %s).\n",
1640 mdname(conf->mddev),
1641 (unsigned long long)(sh->sector
1642 + rdev->data_offset),
1643 bdn);
NeilBrownd6950432006-07-10 04:44:20 -07001644 else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001645 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001646 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001647 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001648 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001649 else
1650 retry = 1;
1651 if (retry)
1652 set_bit(R5_ReadError, &sh->dev[i].flags);
1653 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001654 clear_bit(R5_ReadError, &sh->dev[i].flags);
1655 clear_bit(R5_ReWrite, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001656 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 }
1659 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1661 set_bit(STRIPE_HANDLE, &sh->state);
1662 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663}
1664
NeilBrownd710e132008-10-13 11:55:12 +11001665static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666{
NeilBrown99c0fb52009-03-31 14:39:38 +11001667 struct stripe_head *sh = bi->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001669 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001671 sector_t first_bad;
1672 int bad_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 for (i=0 ; i<disks; i++)
1675 if (bi == &sh->dev[i].req)
1676 break;
1677
Dan Williams45b42332007-07-09 11:56:43 -07001678 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1680 uptodate);
1681 if (i == disks) {
1682 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001683 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 }
1685
NeilBrownbc2607f2011-07-28 11:39:22 +10001686 if (!uptodate) {
1687 set_bit(WriteErrorSeen, &conf->disks[i].rdev->flags);
1688 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrownb84db562011-07-28 11:39:23 +10001689 } else if (is_badblock(conf->disks[i].rdev, sh->sector, STRIPE_SECTORS,
1690 &first_bad, &bad_sectors))
1691 set_bit(R5_MadeGood, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
1693 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
1694
1695 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1696 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001697 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698}
1699
1700
NeilBrown784052e2009-03-31 15:19:07 +11001701static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
NeilBrown784052e2009-03-31 15:19:07 +11001703static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704{
1705 struct r5dev *dev = &sh->dev[i];
1706
1707 bio_init(&dev->req);
1708 dev->req.bi_io_vec = &dev->vec;
1709 dev->req.bi_vcnt++;
1710 dev->req.bi_max_vecs++;
1711 dev->vec.bv_page = dev->page;
1712 dev->vec.bv_len = STRIPE_SIZE;
1713 dev->vec.bv_offset = 0;
1714
1715 dev->req.bi_sector = sh->sector;
1716 dev->req.bi_private = sh;
1717
1718 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001719 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720}
1721
NeilBrownfd01b882011-10-11 16:47:53 +11001722static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723{
1724 char b[BDEVNAME_SIZE];
H Hartley Sweeten7b928132010-03-08 16:02:40 +11001725 raid5_conf_t *conf = mddev->private;
NeilBrown0c55e022010-05-03 14:09:02 +10001726 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
NeilBrown6f8d0c72011-05-11 14:38:44 +10001728 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1729 unsigned long flags;
1730 spin_lock_irqsave(&conf->device_lock, flags);
1731 mddev->degraded++;
1732 spin_unlock_irqrestore(&conf->device_lock, flags);
1733 /*
1734 * if recovery was running, make sure it aborts.
1735 */
1736 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 }
NeilBrownde393cd2011-07-28 11:31:48 +10001738 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001739 set_bit(Faulty, &rdev->flags);
1740 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1741 printk(KERN_ALERT
1742 "md/raid:%s: Disk failure on %s, disabling device.\n"
1743 "md/raid:%s: Operation continuing on %d devices.\n",
1744 mdname(mddev),
1745 bdevname(rdev->bdev, b),
1746 mdname(mddev),
1747 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001748}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
1750/*
1751 * Input: a 'big' sector number,
1752 * Output: index of the data and parity disk, and the sector # in them.
1753 */
NeilBrown112bf892009-03-31 14:39:38 +11001754static sector_t raid5_compute_sector(raid5_conf_t *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001755 int previous, int *dd_idx,
1756 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001758 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001759 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001761 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001762 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001764 int algorithm = previous ? conf->prev_algo
1765 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001766 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1767 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001768 int raid_disks = previous ? conf->previous_raid_disks
1769 : conf->raid_disks;
1770 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
1772 /* First compute the information on this sector */
1773
1774 /*
1775 * Compute the chunk number and the sector offset inside the chunk
1776 */
1777 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1778 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
1780 /*
1781 * Compute the stripe number
1782 */
NeilBrown35f2a592010-04-20 14:13:34 +10001783 stripe = chunk_number;
1784 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10001785 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 /*
1787 * Select the parity disk based on the user selected algorithm.
1788 */
NeilBrown84789552011-07-27 11:00:36 +10001789 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07001790 switch(conf->level) {
1791 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001792 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001793 break;
1794 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001795 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001797 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001798 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 (*dd_idx)++;
1800 break;
1801 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001802 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001803 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 (*dd_idx)++;
1805 break;
1806 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001807 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001808 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 break;
1810 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001811 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001812 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001814 case ALGORITHM_PARITY_0:
1815 pd_idx = 0;
1816 (*dd_idx)++;
1817 break;
1818 case ALGORITHM_PARITY_N:
1819 pd_idx = data_disks;
1820 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11001822 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001823 }
1824 break;
1825 case 6:
1826
NeilBrowne183eae2009-03-31 15:20:22 +11001827 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001828 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001829 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001830 qd_idx = pd_idx + 1;
1831 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001832 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001833 qd_idx = 0;
1834 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001835 (*dd_idx) += 2; /* D D P Q D */
1836 break;
1837 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001838 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001839 qd_idx = pd_idx + 1;
1840 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001841 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001842 qd_idx = 0;
1843 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001844 (*dd_idx) += 2; /* D D P Q D */
1845 break;
1846 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001847 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001848 qd_idx = (pd_idx + 1) % raid_disks;
1849 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001850 break;
1851 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001852 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001853 qd_idx = (pd_idx + 1) % raid_disks;
1854 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001855 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001856
1857 case ALGORITHM_PARITY_0:
1858 pd_idx = 0;
1859 qd_idx = 1;
1860 (*dd_idx) += 2;
1861 break;
1862 case ALGORITHM_PARITY_N:
1863 pd_idx = data_disks;
1864 qd_idx = data_disks + 1;
1865 break;
1866
1867 case ALGORITHM_ROTATING_ZERO_RESTART:
1868 /* Exactly the same as RIGHT_ASYMMETRIC, but or
1869 * of blocks for computing Q is different.
1870 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001871 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11001872 qd_idx = pd_idx + 1;
1873 if (pd_idx == raid_disks-1) {
1874 (*dd_idx)++; /* Q D D D P */
1875 qd_idx = 0;
1876 } else if (*dd_idx >= pd_idx)
1877 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001878 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001879 break;
1880
1881 case ALGORITHM_ROTATING_N_RESTART:
1882 /* Same a left_asymmetric, by first stripe is
1883 * D D D P Q rather than
1884 * Q D D D P
1885 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001886 stripe2 += 1;
1887 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11001888 qd_idx = pd_idx + 1;
1889 if (pd_idx == raid_disks-1) {
1890 (*dd_idx)++; /* Q D D D P */
1891 qd_idx = 0;
1892 } else if (*dd_idx >= pd_idx)
1893 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001894 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001895 break;
1896
1897 case ALGORITHM_ROTATING_N_CONTINUE:
1898 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001899 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11001900 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
1901 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11001902 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001903 break;
1904
1905 case ALGORITHM_LEFT_ASYMMETRIC_6:
1906 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001907 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11001908 if (*dd_idx >= pd_idx)
1909 (*dd_idx)++;
1910 qd_idx = raid_disks - 1;
1911 break;
1912
1913 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001914 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11001915 if (*dd_idx >= pd_idx)
1916 (*dd_idx)++;
1917 qd_idx = raid_disks - 1;
1918 break;
1919
1920 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001921 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11001922 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1923 qd_idx = raid_disks - 1;
1924 break;
1925
1926 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001927 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11001928 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1929 qd_idx = raid_disks - 1;
1930 break;
1931
1932 case ALGORITHM_PARITY_0_6:
1933 pd_idx = 0;
1934 (*dd_idx)++;
1935 qd_idx = raid_disks - 1;
1936 break;
1937
NeilBrown16a53ec2006-06-26 00:27:38 -07001938 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11001939 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001940 }
1941 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 }
1943
NeilBrown911d4ee2009-03-31 14:39:38 +11001944 if (sh) {
1945 sh->pd_idx = pd_idx;
1946 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001947 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11001948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 /*
1950 * Finally, compute the new sector number
1951 */
1952 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
1953 return new_sector;
1954}
1955
1956
NeilBrown784052e2009-03-31 15:19:07 +11001957static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958{
1959 raid5_conf_t *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08001960 int raid_disks = sh->disks;
1961 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001963 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1964 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11001965 int algorithm = previous ? conf->prev_algo
1966 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 sector_t stripe;
1968 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10001969 sector_t chunk_number;
1970 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11001972 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
NeilBrown16a53ec2006-06-26 00:27:38 -07001974
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 chunk_offset = sector_div(new_sector, sectors_per_chunk);
1976 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
NeilBrown16a53ec2006-06-26 00:27:38 -07001978 if (i == sh->pd_idx)
1979 return 0;
1980 switch(conf->level) {
1981 case 4: break;
1982 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001983 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 case ALGORITHM_LEFT_ASYMMETRIC:
1985 case ALGORITHM_RIGHT_ASYMMETRIC:
1986 if (i > sh->pd_idx)
1987 i--;
1988 break;
1989 case ALGORITHM_LEFT_SYMMETRIC:
1990 case ALGORITHM_RIGHT_SYMMETRIC:
1991 if (i < sh->pd_idx)
1992 i += raid_disks;
1993 i -= (sh->pd_idx + 1);
1994 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001995 case ALGORITHM_PARITY_0:
1996 i -= 1;
1997 break;
1998 case ALGORITHM_PARITY_N:
1999 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002001 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002002 }
2003 break;
2004 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002005 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002006 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002007 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002008 case ALGORITHM_LEFT_ASYMMETRIC:
2009 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002010 case ALGORITHM_ROTATING_ZERO_RESTART:
2011 case ALGORITHM_ROTATING_N_RESTART:
2012 if (sh->pd_idx == raid_disks-1)
2013 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002014 else if (i > sh->pd_idx)
2015 i -= 2; /* D D P Q D */
2016 break;
2017 case ALGORITHM_LEFT_SYMMETRIC:
2018 case ALGORITHM_RIGHT_SYMMETRIC:
2019 if (sh->pd_idx == raid_disks-1)
2020 i--; /* Q D D D P */
2021 else {
2022 /* D D P Q D */
2023 if (i < sh->pd_idx)
2024 i += raid_disks;
2025 i -= (sh->pd_idx + 2);
2026 }
2027 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002028 case ALGORITHM_PARITY_0:
2029 i -= 2;
2030 break;
2031 case ALGORITHM_PARITY_N:
2032 break;
2033 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002034 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002035 if (sh->pd_idx == 0)
2036 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002037 else {
2038 /* D D Q P D */
2039 if (i < sh->pd_idx)
2040 i += raid_disks;
2041 i -= (sh->pd_idx + 1);
2042 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002043 break;
2044 case ALGORITHM_LEFT_ASYMMETRIC_6:
2045 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2046 if (i > sh->pd_idx)
2047 i--;
2048 break;
2049 case ALGORITHM_LEFT_SYMMETRIC_6:
2050 case ALGORITHM_RIGHT_SYMMETRIC_6:
2051 if (i < sh->pd_idx)
2052 i += data_disks + 1;
2053 i -= (sh->pd_idx + 1);
2054 break;
2055 case ALGORITHM_PARITY_0_6:
2056 i -= 1;
2057 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002058 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002059 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002060 }
2061 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 }
2063
2064 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002065 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
NeilBrown112bf892009-03-31 14:39:38 +11002067 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002068 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002069 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2070 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002071 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2072 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 return 0;
2074 }
2075 return r_sector;
2076}
2077
2078
Dan Williams600aa102008-06-28 08:32:05 +10002079static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002080schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002081 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002082{
2083 int i, pd_idx = sh->pd_idx, disks = sh->disks;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002084 raid5_conf_t *conf = sh->raid_conf;
2085 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002086
Dan Williamse33129d2007-01-02 13:52:30 -07002087 if (rcw) {
2088 /* if we are not expanding this is a proper write request, and
2089 * there will be bios with new data to be drained into the
2090 * stripe cache
2091 */
2092 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10002093 sh->reconstruct_state = reconstruct_state_drain_run;
2094 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2095 } else
2096 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07002097
Dan Williamsac6b53b2009-07-14 13:40:19 -07002098 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002099
2100 for (i = disks; i--; ) {
2101 struct r5dev *dev = &sh->dev[i];
2102
2103 if (dev->towrite) {
2104 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002105 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002106 if (!expand)
2107 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002108 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002109 }
2110 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002111 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002112 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002113 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002114 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002115 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002116 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2117 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2118
Dan Williamsd8ee0722008-06-28 08:32:06 +10002119 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002120 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2121 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002122 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002123
2124 for (i = disks; i--; ) {
2125 struct r5dev *dev = &sh->dev[i];
2126 if (i == pd_idx)
2127 continue;
2128
Dan Williamse33129d2007-01-02 13:52:30 -07002129 if (dev->towrite &&
2130 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002131 test_bit(R5_Wantcompute, &dev->flags))) {
2132 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002133 set_bit(R5_LOCKED, &dev->flags);
2134 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002135 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002136 }
2137 }
2138 }
2139
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002140 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002141 * are in flight
2142 */
2143 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2144 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002145 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002146
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002147 if (level == 6) {
2148 int qd_idx = sh->qd_idx;
2149 struct r5dev *dev = &sh->dev[qd_idx];
2150
2151 set_bit(R5_LOCKED, &dev->flags);
2152 clear_bit(R5_UPTODATE, &dev->flags);
2153 s->locked++;
2154 }
2155
Dan Williams600aa102008-06-28 08:32:05 +10002156 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002157 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002158 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002159}
NeilBrown16a53ec2006-06-26 00:27:38 -07002160
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161/*
2162 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002163 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 * The bi_next chain must be in order.
2165 */
2166static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2167{
2168 struct bio **bip;
2169 raid5_conf_t *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002170 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
NeilBrowncbe47ec2011-07-26 11:20:35 +10002172 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 (unsigned long long)bi->bi_sector,
2174 (unsigned long long)sh->sector);
2175
2176
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07002178 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -07002180 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
2181 firstwrite = 1;
2182 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 bip = &sh->dev[dd_idx].toread;
2184 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2185 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2186 goto overlap;
2187 bip = & (*bip)->bi_next;
2188 }
2189 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2190 goto overlap;
2191
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002192 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 if (*bip)
2194 bi->bi_next = *bip;
2195 *bip = bi;
Jens Axboe960e7392008-08-15 10:41:18 +02002196 bi->bi_phys_segments++;
NeilBrown72626682005-09-09 16:23:54 -07002197
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 if (forwrite) {
2199 /* check if page is covered */
2200 sector_t sector = sh->dev[dd_idx].sector;
2201 for (bi=sh->dev[dd_idx].towrite;
2202 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2203 bi && bi->bi_sector <= sector;
2204 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2205 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2206 sector = bi->bi_sector + (bi->bi_size>>9);
2207 }
2208 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2209 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2210 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002211 spin_unlock_irq(&conf->device_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002212
2213 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2214 (unsigned long long)(*bip)->bi_sector,
2215 (unsigned long long)sh->sector, dd_idx);
2216
2217 if (conf->mddev->bitmap && firstwrite) {
2218 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2219 STRIPE_SECTORS, 0);
2220 sh->bm_seq = conf->seq_flush+1;
2221 set_bit(STRIPE_BIT_DELAY, &sh->state);
2222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 return 1;
2224
2225 overlap:
2226 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
2227 spin_unlock_irq(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 return 0;
2229}
2230
NeilBrown29269552006-03-27 01:18:10 -08002231static void end_reshape(raid5_conf_t *conf);
2232
NeilBrown911d4ee2009-03-31 14:39:38 +11002233static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
2234 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002235{
NeilBrown784052e2009-03-31 15:19:07 +11002236 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002237 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002238 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002239 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002240 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002241
NeilBrown112bf892009-03-31 14:39:38 +11002242 raid5_compute_sector(conf,
2243 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002244 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002245 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002246 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002247}
2248
Dan Williamsa4456852007-07-09 11:56:43 -07002249static void
Dan Williams1fe797e2008-06-28 09:16:30 +10002250handle_failed_stripe(raid5_conf_t *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002251 struct stripe_head_state *s, int disks,
2252 struct bio **return_bi)
2253{
2254 int i;
2255 for (i = disks; i--; ) {
2256 struct bio *bi;
2257 int bitmap_end = 0;
2258
2259 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002260 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002261 rcu_read_lock();
2262 rdev = rcu_dereference(conf->disks[i].rdev);
2263 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002264 atomic_inc(&rdev->nr_pending);
2265 else
2266 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002267 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002268 if (rdev) {
2269 if (!rdev_set_badblocks(
2270 rdev,
2271 sh->sector,
2272 STRIPE_SECTORS, 0))
2273 md_error(conf->mddev, rdev);
2274 rdev_dec_pending(rdev, conf->mddev);
2275 }
Dan Williamsa4456852007-07-09 11:56:43 -07002276 }
2277 spin_lock_irq(&conf->device_lock);
2278 /* fail all writes first */
2279 bi = sh->dev[i].towrite;
2280 sh->dev[i].towrite = NULL;
2281 if (bi) {
2282 s->to_write--;
2283 bitmap_end = 1;
2284 }
2285
2286 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2287 wake_up(&conf->wait_for_overlap);
2288
2289 while (bi && bi->bi_sector <
2290 sh->dev[i].sector + STRIPE_SECTORS) {
2291 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2292 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002293 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002294 md_write_end(conf->mddev);
2295 bi->bi_next = *return_bi;
2296 *return_bi = bi;
2297 }
2298 bi = nextbi;
2299 }
2300 /* and fail all 'written' */
2301 bi = sh->dev[i].written;
2302 sh->dev[i].written = NULL;
2303 if (bi) bitmap_end = 1;
2304 while (bi && bi->bi_sector <
2305 sh->dev[i].sector + STRIPE_SECTORS) {
2306 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2307 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002308 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002309 md_write_end(conf->mddev);
2310 bi->bi_next = *return_bi;
2311 *return_bi = bi;
2312 }
2313 bi = bi2;
2314 }
2315
Dan Williamsb5e98d62007-01-02 13:52:31 -07002316 /* fail any reads if this device is non-operational and
2317 * the data has not reached the cache yet.
2318 */
2319 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2320 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2321 test_bit(R5_ReadError, &sh->dev[i].flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002322 bi = sh->dev[i].toread;
2323 sh->dev[i].toread = NULL;
2324 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2325 wake_up(&conf->wait_for_overlap);
2326 if (bi) s->to_read--;
2327 while (bi && bi->bi_sector <
2328 sh->dev[i].sector + STRIPE_SECTORS) {
2329 struct bio *nextbi =
2330 r5_next_bio(bi, sh->dev[i].sector);
2331 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002332 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002333 bi->bi_next = *return_bi;
2334 *return_bi = bi;
2335 }
2336 bi = nextbi;
2337 }
2338 }
2339 spin_unlock_irq(&conf->device_lock);
2340 if (bitmap_end)
2341 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2342 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002343 /* If we were in the middle of a write the parity block might
2344 * still be locked - so just clear all R5_LOCKED flags
2345 */
2346 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002347 }
2348
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002349 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2350 if (atomic_dec_and_test(&conf->pending_full_writes))
2351 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002352}
2353
NeilBrown7f0da592011-07-28 11:39:22 +10002354static void
2355handle_failed_sync(raid5_conf_t *conf, struct stripe_head *sh,
2356 struct stripe_head_state *s)
2357{
2358 int abort = 0;
2359 int i;
2360
2361 md_done_sync(conf->mddev, STRIPE_SECTORS, 0);
2362 clear_bit(STRIPE_SYNCING, &sh->state);
2363 s->syncing = 0;
2364 /* There is nothing more to do for sync/check/repair.
2365 * For recover we need to record a bad block on all
2366 * non-sync devices, or abort the recovery
2367 */
2368 if (!test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery))
2369 return;
2370 /* During recovery devices cannot be removed, so locking and
2371 * refcounting of rdevs is not needed
2372 */
2373 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +11002374 struct md_rdev *rdev = conf->disks[i].rdev;
NeilBrown7f0da592011-07-28 11:39:22 +10002375 if (!rdev
2376 || test_bit(Faulty, &rdev->flags)
2377 || test_bit(In_sync, &rdev->flags))
2378 continue;
2379 if (!rdev_set_badblocks(rdev, sh->sector,
2380 STRIPE_SECTORS, 0))
2381 abort = 1;
2382 }
2383 if (abort) {
2384 conf->recovery_disabled = conf->mddev->recovery_disabled;
2385 set_bit(MD_RECOVERY_INTR, &conf->mddev->recovery);
2386 }
2387}
2388
NeilBrown93b3dbc2011-07-27 11:00:36 +10002389/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002390 * to be read or computed to satisfy a request.
2391 *
2392 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002393 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002394 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002395static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2396 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002397{
2398 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002399 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2400 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002401
Dan Williamsf38e1212007-01-02 13:52:30 -07002402 /* is the data in this block needed, and can we get it? */
2403 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002404 !test_bit(R5_UPTODATE, &dev->flags) &&
2405 (dev->toread ||
2406 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2407 s->syncing || s->expanding ||
NeilBrown5d35e092011-07-27 11:00:36 +10002408 (s->failed >= 1 && fdev[0]->toread) ||
2409 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002410 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2411 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2412 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002413 /* we would like to get this block, possibly by computing it,
2414 * otherwise read it if the backing disk is insync
2415 */
2416 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2417 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2418 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002419 (s->failed && (disk_idx == s->failed_num[0] ||
2420 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002421 /* have disk failed, and we're requested to fetch it;
2422 * do compute it
2423 */
2424 pr_debug("Computing stripe %llu block %d\n",
2425 (unsigned long long)sh->sector, disk_idx);
2426 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2427 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2428 set_bit(R5_Wantcompute, &dev->flags);
2429 sh->ops.target = disk_idx;
2430 sh->ops.target2 = -1; /* no 2nd target */
2431 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002432 /* Careful: from this point on 'uptodate' is in the eye
2433 * of raid_run_ops which services 'compute' operations
2434 * before writes. R5_Wantcompute flags a block that will
2435 * be R5_UPTODATE by the time it is needed for a
2436 * subsequent operation.
2437 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002438 s->uptodate++;
2439 return 1;
2440 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2441 /* Computing 2-failure is *very* expensive; only
2442 * do it if failed >= 2
2443 */
2444 int other;
2445 for (other = disks; other--; ) {
2446 if (other == disk_idx)
2447 continue;
2448 if (!test_bit(R5_UPTODATE,
2449 &sh->dev[other].flags))
2450 break;
2451 }
2452 BUG_ON(other < 0);
2453 pr_debug("Computing stripe %llu blocks %d,%d\n",
2454 (unsigned long long)sh->sector,
2455 disk_idx, other);
2456 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2457 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2458 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2459 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2460 sh->ops.target = disk_idx;
2461 sh->ops.target2 = other;
2462 s->uptodate += 2;
2463 s->req_compute = 1;
2464 return 1;
2465 } else if (test_bit(R5_Insync, &dev->flags)) {
2466 set_bit(R5_LOCKED, &dev->flags);
2467 set_bit(R5_Wantread, &dev->flags);
2468 s->locked++;
2469 pr_debug("Reading block %d (sync=%d)\n",
2470 disk_idx, s->syncing);
2471 }
2472 }
2473
2474 return 0;
2475}
2476
2477/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002478 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002479 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002480static void handle_stripe_fill(struct stripe_head *sh,
2481 struct stripe_head_state *s,
2482 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002483{
2484 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002485
2486 /* look for blocks to read/compute, skip this if a compute
2487 * is already in flight, or if the stripe contents are in the
2488 * midst of changing due to a write
2489 */
2490 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2491 !sh->reconstruct_state)
2492 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002493 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002494 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002495 set_bit(STRIPE_HANDLE, &sh->state);
2496}
2497
2498
Dan Williams1fe797e2008-06-28 09:16:30 +10002499/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002500 * any written block on an uptodate or failed drive can be returned.
2501 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2502 * never LOCKED, so we don't need to test 'failed' directly.
2503 */
Dan Williams1fe797e2008-06-28 09:16:30 +10002504static void handle_stripe_clean_event(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002505 struct stripe_head *sh, int disks, struct bio **return_bi)
2506{
2507 int i;
2508 struct r5dev *dev;
2509
2510 for (i = disks; i--; )
2511 if (sh->dev[i].written) {
2512 dev = &sh->dev[i];
2513 if (!test_bit(R5_LOCKED, &dev->flags) &&
2514 test_bit(R5_UPTODATE, &dev->flags)) {
2515 /* We can return any write requests */
2516 struct bio *wbi, *wbi2;
2517 int bitmap_end = 0;
Dan Williams45b42332007-07-09 11:56:43 -07002518 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002519 spin_lock_irq(&conf->device_lock);
2520 wbi = dev->written;
2521 dev->written = NULL;
2522 while (wbi && wbi->bi_sector <
2523 dev->sector + STRIPE_SECTORS) {
2524 wbi2 = r5_next_bio(wbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +02002525 if (!raid5_dec_bi_phys_segments(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002526 md_write_end(conf->mddev);
2527 wbi->bi_next = *return_bi;
2528 *return_bi = wbi;
2529 }
2530 wbi = wbi2;
2531 }
2532 if (dev->towrite == NULL)
2533 bitmap_end = 1;
2534 spin_unlock_irq(&conf->device_lock);
2535 if (bitmap_end)
2536 bitmap_endwrite(conf->mddev->bitmap,
2537 sh->sector,
2538 STRIPE_SECTORS,
2539 !test_bit(STRIPE_DEGRADED, &sh->state),
2540 0);
2541 }
2542 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002543
2544 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2545 if (atomic_dec_and_test(&conf->pending_full_writes))
2546 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002547}
2548
NeilBrownc8ac1802011-07-27 11:00:36 +10002549static void handle_stripe_dirtying(raid5_conf_t *conf,
2550 struct stripe_head *sh,
2551 struct stripe_head_state *s,
2552 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002553{
2554 int rmw = 0, rcw = 0, i;
NeilBrownc8ac1802011-07-27 11:00:36 +10002555 if (conf->max_degraded == 2) {
2556 /* RAID6 requires 'rcw' in current implementation
2557 * Calculate the real rcw later - for now fake it
2558 * look like rcw is cheaper
2559 */
2560 rcw = 1; rmw = 2;
2561 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002562 /* would I have to read this buffer for read_modify_write */
2563 struct r5dev *dev = &sh->dev[i];
2564 if ((dev->towrite || i == sh->pd_idx) &&
2565 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002566 !(test_bit(R5_UPTODATE, &dev->flags) ||
2567 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002568 if (test_bit(R5_Insync, &dev->flags))
2569 rmw++;
2570 else
2571 rmw += 2*disks; /* cannot read it */
2572 }
2573 /* Would I have to read this buffer for reconstruct_write */
2574 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2575 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002576 !(test_bit(R5_UPTODATE, &dev->flags) ||
2577 test_bit(R5_Wantcompute, &dev->flags))) {
2578 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002579 else
2580 rcw += 2*disks;
2581 }
2582 }
Dan Williams45b42332007-07-09 11:56:43 -07002583 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002584 (unsigned long long)sh->sector, rmw, rcw);
2585 set_bit(STRIPE_HANDLE, &sh->state);
2586 if (rmw < rcw && rmw > 0)
2587 /* prefer read-modify-write, but need to get some data */
2588 for (i = disks; i--; ) {
2589 struct r5dev *dev = &sh->dev[i];
2590 if ((dev->towrite || i == sh->pd_idx) &&
2591 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002592 !(test_bit(R5_UPTODATE, &dev->flags) ||
2593 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002594 test_bit(R5_Insync, &dev->flags)) {
2595 if (
2596 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002597 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002598 "%d for r-m-w\n", i);
2599 set_bit(R5_LOCKED, &dev->flags);
2600 set_bit(R5_Wantread, &dev->flags);
2601 s->locked++;
2602 } else {
2603 set_bit(STRIPE_DELAYED, &sh->state);
2604 set_bit(STRIPE_HANDLE, &sh->state);
2605 }
2606 }
2607 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002608 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002609 /* want reconstruct write, but need to get some data */
NeilBrownc8ac1802011-07-27 11:00:36 +10002610 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002611 for (i = disks; i--; ) {
2612 struct r5dev *dev = &sh->dev[i];
2613 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002614 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002615 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002616 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002617 test_bit(R5_Wantcompute, &dev->flags))) {
2618 rcw++;
2619 if (!test_bit(R5_Insync, &dev->flags))
2620 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002621 if (
2622 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002623 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002624 "%d for Reconstruct\n", i);
2625 set_bit(R5_LOCKED, &dev->flags);
2626 set_bit(R5_Wantread, &dev->flags);
2627 s->locked++;
2628 } else {
2629 set_bit(STRIPE_DELAYED, &sh->state);
2630 set_bit(STRIPE_HANDLE, &sh->state);
2631 }
2632 }
2633 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002634 }
Dan Williamsa4456852007-07-09 11:56:43 -07002635 /* now if nothing is locked, and if we have enough data,
2636 * we can start a write request
2637 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002638 /* since handle_stripe can be called at any time we need to handle the
2639 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002640 * subsequent call wants to start a write request. raid_run_ops only
2641 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002642 * simultaneously. If this is not the case then new writes need to be
2643 * held off until the compute completes.
2644 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002645 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2646 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2647 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002648 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002649}
2650
Dan Williamsa4456852007-07-09 11:56:43 -07002651static void handle_parity_checks5(raid5_conf_t *conf, struct stripe_head *sh,
2652 struct stripe_head_state *s, int disks)
2653{
Dan Williamsecc65c92008-06-28 08:31:57 +10002654 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002655
Dan Williamsbd2ab672008-04-10 21:29:27 -07002656 set_bit(STRIPE_HANDLE, &sh->state);
2657
Dan Williamsecc65c92008-06-28 08:31:57 +10002658 switch (sh->check_state) {
2659 case check_state_idle:
2660 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002661 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002662 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002663 sh->check_state = check_state_run;
2664 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002665 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002666 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002667 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002668 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002669 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002670 /* fall through */
2671 case check_state_compute_result:
2672 sh->check_state = check_state_idle;
2673 if (!dev)
2674 dev = &sh->dev[sh->pd_idx];
2675
2676 /* check that a write has not made the stripe insync */
2677 if (test_bit(STRIPE_INSYNC, &sh->state))
2678 break;
2679
2680 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002681 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2682 BUG_ON(s->uptodate != disks);
2683
2684 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002685 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002686 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002687
Dan Williamsa4456852007-07-09 11:56:43 -07002688 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002689 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002690 break;
2691 case check_state_run:
2692 break; /* we will be called again upon completion */
2693 case check_state_check_result:
2694 sh->check_state = check_state_idle;
2695
2696 /* if a failure occurred during the check operation, leave
2697 * STRIPE_INSYNC not set and let the stripe be handled again
2698 */
2699 if (s->failed)
2700 break;
2701
2702 /* handle a successful check operation, if parity is correct
2703 * we are done. Otherwise update the mismatch count and repair
2704 * parity if !MD_RECOVERY_CHECK
2705 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002706 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002707 /* parity is correct (on disc,
2708 * not in buffer any more)
2709 */
2710 set_bit(STRIPE_INSYNC, &sh->state);
2711 else {
2712 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2713 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2714 /* don't try to repair!! */
2715 set_bit(STRIPE_INSYNC, &sh->state);
2716 else {
2717 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002718 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002719 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2720 set_bit(R5_Wantcompute,
2721 &sh->dev[sh->pd_idx].flags);
2722 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002723 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10002724 s->uptodate++;
2725 }
2726 }
2727 break;
2728 case check_state_compute_run:
2729 break;
2730 default:
2731 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2732 __func__, sh->check_state,
2733 (unsigned long long) sh->sector);
2734 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002735 }
2736}
2737
2738
2739static void handle_parity_checks6(raid5_conf_t *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07002740 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10002741 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002742{
Dan Williamsa4456852007-07-09 11:56:43 -07002743 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11002744 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07002745 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07002746
2747 set_bit(STRIPE_HANDLE, &sh->state);
2748
2749 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002750
Dan Williamsa4456852007-07-09 11:56:43 -07002751 /* Want to check and possibly repair P and Q.
2752 * However there could be one 'failed' device, in which
2753 * case we can only check one of them, possibly using the
2754 * other to generate missing data
2755 */
2756
Dan Williamsd82dfee2009-07-14 13:40:57 -07002757 switch (sh->check_state) {
2758 case check_state_idle:
2759 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10002760 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002761 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07002762 * makes sense to check P (If anything else were failed,
2763 * we would have used P to recreate it).
2764 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002765 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07002766 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002767 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002768 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07002769 * anything, so it makes sense to check it
2770 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002771 if (sh->check_state == check_state_run)
2772 sh->check_state = check_state_run_pq;
2773 else
2774 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07002775 }
Dan Williams36d1c642009-07-14 11:48:22 -07002776
Dan Williamsd82dfee2009-07-14 13:40:57 -07002777 /* discard potentially stale zero_sum_result */
2778 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07002779
Dan Williamsd82dfee2009-07-14 13:40:57 -07002780 if (sh->check_state == check_state_run) {
2781 /* async_xor_zero_sum destroys the contents of P */
2782 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
2783 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07002784 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002785 if (sh->check_state >= check_state_run &&
2786 sh->check_state <= check_state_run_pq) {
2787 /* async_syndrome_zero_sum preserves P and Q, so
2788 * no need to mark them !uptodate here
2789 */
2790 set_bit(STRIPE_OP_CHECK, &s->ops_request);
2791 break;
2792 }
Dan Williams36d1c642009-07-14 11:48:22 -07002793
Dan Williamsd82dfee2009-07-14 13:40:57 -07002794 /* we have 2-disk failure */
2795 BUG_ON(s->failed != 2);
2796 /* fall through */
2797 case check_state_compute_result:
2798 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07002799
Dan Williamsd82dfee2009-07-14 13:40:57 -07002800 /* check that a write has not made the stripe insync */
2801 if (test_bit(STRIPE_INSYNC, &sh->state))
2802 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002803
2804 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07002805 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07002806 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002807 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07002808 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10002809 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07002810 s->locked++;
2811 set_bit(R5_LOCKED, &dev->flags);
2812 set_bit(R5_Wantwrite, &dev->flags);
2813 }
2814 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10002815 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07002816 s->locked++;
2817 set_bit(R5_LOCKED, &dev->flags);
2818 set_bit(R5_Wantwrite, &dev->flags);
2819 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002820 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002821 dev = &sh->dev[pd_idx];
2822 s->locked++;
2823 set_bit(R5_LOCKED, &dev->flags);
2824 set_bit(R5_Wantwrite, &dev->flags);
2825 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002826 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002827 dev = &sh->dev[qd_idx];
2828 s->locked++;
2829 set_bit(R5_LOCKED, &dev->flags);
2830 set_bit(R5_Wantwrite, &dev->flags);
2831 }
2832 clear_bit(STRIPE_DEGRADED, &sh->state);
2833
2834 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002835 break;
2836 case check_state_run:
2837 case check_state_run_q:
2838 case check_state_run_pq:
2839 break; /* we will be called again upon completion */
2840 case check_state_check_result:
2841 sh->check_state = check_state_idle;
2842
2843 /* handle a successful check operation, if parity is correct
2844 * we are done. Otherwise update the mismatch count and repair
2845 * parity if !MD_RECOVERY_CHECK
2846 */
2847 if (sh->ops.zero_sum_result == 0) {
2848 /* both parities are correct */
2849 if (!s->failed)
2850 set_bit(STRIPE_INSYNC, &sh->state);
2851 else {
2852 /* in contrast to the raid5 case we can validate
2853 * parity, but still have a failure to write
2854 * back
2855 */
2856 sh->check_state = check_state_compute_result;
2857 /* Returning at this point means that we may go
2858 * off and bring p and/or q uptodate again so
2859 * we make sure to check zero_sum_result again
2860 * to verify if p or q need writeback
2861 */
2862 }
2863 } else {
2864 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2865 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2866 /* don't try to repair!! */
2867 set_bit(STRIPE_INSYNC, &sh->state);
2868 else {
2869 int *target = &sh->ops.target;
2870
2871 sh->ops.target = -1;
2872 sh->ops.target2 = -1;
2873 sh->check_state = check_state_compute_run;
2874 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2875 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2876 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
2877 set_bit(R5_Wantcompute,
2878 &sh->dev[pd_idx].flags);
2879 *target = pd_idx;
2880 target = &sh->ops.target2;
2881 s->uptodate++;
2882 }
2883 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
2884 set_bit(R5_Wantcompute,
2885 &sh->dev[qd_idx].flags);
2886 *target = qd_idx;
2887 s->uptodate++;
2888 }
2889 }
2890 }
2891 break;
2892 case check_state_compute_run:
2893 break;
2894 default:
2895 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2896 __func__, sh->check_state,
2897 (unsigned long long) sh->sector);
2898 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002899 }
2900}
2901
NeilBrown86c374b2011-07-27 11:00:36 +10002902static void handle_stripe_expansion(raid5_conf_t *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07002903{
2904 int i;
2905
2906 /* We have read all the blocks in this stripe and now we need to
2907 * copy some of them into a target stripe for expand.
2908 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07002909 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002910 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2911 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11002912 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11002913 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07002914 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07002915 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07002916
NeilBrown784052e2009-03-31 15:19:07 +11002917 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11002918 sector_t s = raid5_compute_sector(conf, bn, 0,
2919 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10002920 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07002921 if (sh2 == NULL)
2922 /* so far only the early blocks of this stripe
2923 * have been requested. When later blocks
2924 * get requested, we will try again
2925 */
2926 continue;
2927 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
2928 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
2929 /* must have already done this block */
2930 release_stripe(sh2);
2931 continue;
2932 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07002933
2934 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07002935 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002936 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07002937 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07002938 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002939
Dan Williamsa4456852007-07-09 11:56:43 -07002940 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
2941 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
2942 for (j = 0; j < conf->raid_disks; j++)
2943 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10002944 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002945 !test_bit(R5_Expanded, &sh2->dev[j].flags))
2946 break;
2947 if (j == conf->raid_disks) {
2948 set_bit(STRIPE_EXPAND_READY, &sh2->state);
2949 set_bit(STRIPE_HANDLE, &sh2->state);
2950 }
2951 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002952
Dan Williamsa4456852007-07-09 11:56:43 -07002953 }
NeilBrowna2e08552007-09-11 15:23:36 -07002954 /* done submitting copies, wait for them to complete */
2955 if (tx) {
2956 async_tx_ack(tx);
2957 dma_wait_for_async_tx(tx);
2958 }
Dan Williamsa4456852007-07-09 11:56:43 -07002959}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960
Dan Williams6bfe0b42008-04-30 00:52:32 -07002961
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962/*
2963 * handle_stripe - do things to a stripe.
2964 *
2965 * We lock the stripe and then examine the state of various bits
2966 * to see what needs to be done.
2967 * Possible results:
2968 * return some read request which now have data
2969 * return some write requests which are safely on disc
2970 * schedule a read on some buffers
2971 * schedule a write of some buffers
2972 * return confirmation of parity correctness
2973 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 * buffers are taken off read_list or write_list, and bh_cache buffers
2975 * get BH_Lock set before the stripe lock is released.
2976 *
2977 */
Dan Williamsa4456852007-07-09 11:56:43 -07002978
NeilBrownacfe7262011-07-27 11:00:36 +10002979static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07002980{
NeilBrownbff61972009-03-31 14:33:13 +11002981 raid5_conf_t *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08002982 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10002983 struct r5dev *dev;
2984 int i;
NeilBrown16a53ec2006-06-26 00:27:38 -07002985
NeilBrownacfe7262011-07-27 11:00:36 +10002986 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07002987
NeilBrownacfe7262011-07-27 11:00:36 +10002988 s->syncing = test_bit(STRIPE_SYNCING, &sh->state);
2989 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2990 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
2991 s->failed_num[0] = -1;
2992 s->failed_num[1] = -1;
2993
2994 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07002995 rcu_read_lock();
NeilBrownc4c16632011-07-26 11:34:20 +10002996 spin_lock_irq(&conf->device_lock);
NeilBrown16a53ec2006-06-26 00:27:38 -07002997 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11002998 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10002999 sector_t first_bad;
3000 int bad_sectors;
3001 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003002
NeilBrown16a53ec2006-06-26 00:27:38 -07003003 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003004
Dan Williams45b42332007-07-09 11:56:43 -07003005 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown16a53ec2006-06-26 00:27:38 -07003006 i, dev->flags, dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003007 /* maybe we can reply to a read
3008 *
3009 * new wantfill requests are only permitted while
3010 * ops_complete_biofill is guaranteed to be inactive
3011 */
3012 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3013 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3014 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003015
3016 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003017 if (test_bit(R5_LOCKED, &dev->flags))
3018 s->locked++;
3019 if (test_bit(R5_UPTODATE, &dev->flags))
3020 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003021 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003022 s->compute++;
3023 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003024 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003025
NeilBrownacfe7262011-07-27 11:00:36 +10003026 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003027 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003028 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003029 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003030 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003031 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003032 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003033 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003034 }
Dan Williamsa4456852007-07-09 11:56:43 -07003035 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003036 s->written++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003037 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown31c176e2011-07-28 11:39:22 +10003038 if (rdev) {
3039 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3040 &first_bad, &bad_sectors);
3041 if (s->blocked_rdev == NULL
3042 && (test_bit(Blocked, &rdev->flags)
3043 || is_bad < 0)) {
3044 if (is_bad < 0)
3045 set_bit(BlockedBadBlocks,
3046 &rdev->flags);
3047 s->blocked_rdev = rdev;
3048 atomic_inc(&rdev->nr_pending);
3049 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003050 }
NeilBrown415e72d2010-06-17 17:25:21 +10003051 clear_bit(R5_Insync, &dev->flags);
3052 if (!rdev)
3053 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003054 else if (is_bad) {
3055 /* also not in-sync */
3056 if (!test_bit(WriteErrorSeen, &rdev->flags)) {
3057 /* treat as in-sync, but with a read error
3058 * which we can now try to correct
3059 */
3060 set_bit(R5_Insync, &dev->flags);
3061 set_bit(R5_ReadError, &dev->flags);
3062 }
3063 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003064 set_bit(R5_Insync, &dev->flags);
3065 else {
3066 /* in sync if before recovery_offset */
3067 if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
3068 set_bit(R5_Insync, &dev->flags);
3069 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003070 if (test_bit(R5_WriteError, &dev->flags)) {
3071 clear_bit(R5_Insync, &dev->flags);
3072 if (!test_bit(Faulty, &rdev->flags)) {
3073 s->handle_bad_blocks = 1;
3074 atomic_inc(&rdev->nr_pending);
3075 } else
3076 clear_bit(R5_WriteError, &dev->flags);
3077 }
NeilBrownb84db562011-07-28 11:39:23 +10003078 if (test_bit(R5_MadeGood, &dev->flags)) {
3079 if (!test_bit(Faulty, &rdev->flags)) {
3080 s->handle_bad_blocks = 1;
3081 atomic_inc(&rdev->nr_pending);
3082 } else
3083 clear_bit(R5_MadeGood, &dev->flags);
3084 }
NeilBrown415e72d2010-06-17 17:25:21 +10003085 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003086 /* The ReadError flag will just be confusing now */
3087 clear_bit(R5_ReadError, &dev->flags);
3088 clear_bit(R5_ReWrite, &dev->flags);
3089 }
NeilBrown415e72d2010-06-17 17:25:21 +10003090 if (test_bit(R5_ReadError, &dev->flags))
3091 clear_bit(R5_Insync, &dev->flags);
3092 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003093 if (s->failed < 2)
3094 s->failed_num[s->failed] = i;
3095 s->failed++;
NeilBrown415e72d2010-06-17 17:25:21 +10003096 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003097 }
NeilBrownc4c16632011-07-26 11:34:20 +10003098 spin_unlock_irq(&conf->device_lock);
NeilBrown16a53ec2006-06-26 00:27:38 -07003099 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003100}
NeilBrownf4168852007-02-28 20:11:53 -08003101
NeilBrowncc940152011-07-26 11:35:35 +10003102static void handle_stripe(struct stripe_head *sh)
3103{
3104 struct stripe_head_state s;
NeilBrown474af965fe2011-07-27 11:00:36 +10003105 raid5_conf_t *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003106 int i;
NeilBrown84789552011-07-27 11:00:36 +10003107 int prexor;
3108 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003109 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003110
3111 clear_bit(STRIPE_HANDLE, &sh->state);
3112 if (test_and_set_bit(STRIPE_ACTIVE, &sh->state)) {
3113 /* already being handled, ensure it gets handled
3114 * again when current action finishes */
3115 set_bit(STRIPE_HANDLE, &sh->state);
3116 return;
3117 }
3118
3119 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3120 set_bit(STRIPE_SYNCING, &sh->state);
3121 clear_bit(STRIPE_INSYNC, &sh->state);
3122 }
3123 clear_bit(STRIPE_DELAYED, &sh->state);
3124
3125 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3126 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3127 (unsigned long long)sh->sector, sh->state,
3128 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3129 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003130
NeilBrownacfe7262011-07-27 11:00:36 +10003131 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003132
NeilBrownbc2607f2011-07-28 11:39:22 +10003133 if (s.handle_bad_blocks) {
3134 set_bit(STRIPE_HANDLE, &sh->state);
3135 goto finish;
3136 }
3137
NeilBrown474af965fe2011-07-27 11:00:36 +10003138 if (unlikely(s.blocked_rdev)) {
3139 if (s.syncing || s.expanding || s.expanded ||
3140 s.to_write || s.written) {
3141 set_bit(STRIPE_HANDLE, &sh->state);
3142 goto finish;
3143 }
3144 /* There is nothing for the blocked_rdev to block */
3145 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3146 s.blocked_rdev = NULL;
3147 }
3148
3149 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3150 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3151 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3152 }
3153
3154 pr_debug("locked=%d uptodate=%d to_read=%d"
3155 " to_write=%d failed=%d failed_num=%d,%d\n",
3156 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3157 s.failed_num[0], s.failed_num[1]);
3158 /* check if the array has lost more than max_degraded devices and,
3159 * if so, some requests might need to be failed.
3160 */
3161 if (s.failed > conf->max_degraded && s.to_read+s.to_write+s.written)
3162 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown7f0da592011-07-28 11:39:22 +10003163 if (s.failed > conf->max_degraded && s.syncing)
3164 handle_failed_sync(conf, sh, &s);
NeilBrown474af965fe2011-07-27 11:00:36 +10003165
3166 /*
3167 * might be able to return some write requests if the parity blocks
3168 * are safe, or on a failed drive
3169 */
3170 pdev = &sh->dev[sh->pd_idx];
3171 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3172 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3173 qdev = &sh->dev[sh->qd_idx];
3174 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3175 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3176 || conf->level < 6;
3177
3178 if (s.written &&
3179 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3180 && !test_bit(R5_LOCKED, &pdev->flags)
3181 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3182 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3183 && !test_bit(R5_LOCKED, &qdev->flags)
3184 && test_bit(R5_UPTODATE, &qdev->flags)))))
3185 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3186
3187 /* Now we might consider reading some blocks, either to check/generate
3188 * parity, or to satisfy requests
3189 * or to load a block that is being partially written.
3190 */
3191 if (s.to_read || s.non_overwrite
3192 || (conf->level == 6 && s.to_write && s.failed)
3193 || (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
3194 handle_stripe_fill(sh, &s, disks);
3195
NeilBrown84789552011-07-27 11:00:36 +10003196 /* Now we check to see if any write operations have recently
3197 * completed
3198 */
3199 prexor = 0;
3200 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3201 prexor = 1;
3202 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3203 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3204 sh->reconstruct_state = reconstruct_state_idle;
3205
3206 /* All the 'written' buffers and the parity block are ready to
3207 * be written back to disk
3208 */
3209 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3210 BUG_ON(sh->qd_idx >= 0 &&
3211 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags));
3212 for (i = disks; i--; ) {
3213 struct r5dev *dev = &sh->dev[i];
3214 if (test_bit(R5_LOCKED, &dev->flags) &&
3215 (i == sh->pd_idx || i == sh->qd_idx ||
3216 dev->written)) {
3217 pr_debug("Writing block %d\n", i);
3218 set_bit(R5_Wantwrite, &dev->flags);
3219 if (prexor)
3220 continue;
3221 if (!test_bit(R5_Insync, &dev->flags) ||
3222 ((i == sh->pd_idx || i == sh->qd_idx) &&
3223 s.failed == 0))
3224 set_bit(STRIPE_INSYNC, &sh->state);
3225 }
3226 }
3227 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3228 s.dec_preread_active = 1;
3229 }
3230
3231 /* Now to consider new write requests and what else, if anything
3232 * should be read. We do not handle new writes when:
3233 * 1/ A 'write' operation (copy+xor) is already in flight.
3234 * 2/ A 'check' operation is in flight, as it may clobber the parity
3235 * block.
3236 */
3237 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3238 handle_stripe_dirtying(conf, sh, &s, disks);
3239
3240 /* maybe we need to check and possibly fix the parity for this stripe
3241 * Any reads will already have been scheduled, so we just see if enough
3242 * data is available. The parity check is held off while parity
3243 * dependent operations are in flight.
3244 */
3245 if (sh->check_state ||
3246 (s.syncing && s.locked == 0 &&
3247 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3248 !test_bit(STRIPE_INSYNC, &sh->state))) {
3249 if (conf->level == 6)
3250 handle_parity_checks6(conf, sh, &s, disks);
3251 else
3252 handle_parity_checks5(conf, sh, &s, disks);
3253 }
NeilBrownc5a31002011-07-27 11:00:36 +10003254
3255 if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
3256 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3257 clear_bit(STRIPE_SYNCING, &sh->state);
3258 }
3259
3260 /* If the failed drives are just a ReadError, then we might need
3261 * to progress the repair/check process
3262 */
3263 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3264 for (i = 0; i < s.failed; i++) {
3265 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3266 if (test_bit(R5_ReadError, &dev->flags)
3267 && !test_bit(R5_LOCKED, &dev->flags)
3268 && test_bit(R5_UPTODATE, &dev->flags)
3269 ) {
3270 if (!test_bit(R5_ReWrite, &dev->flags)) {
3271 set_bit(R5_Wantwrite, &dev->flags);
3272 set_bit(R5_ReWrite, &dev->flags);
3273 set_bit(R5_LOCKED, &dev->flags);
3274 s.locked++;
3275 } else {
3276 /* let's read it back */
3277 set_bit(R5_Wantread, &dev->flags);
3278 set_bit(R5_LOCKED, &dev->flags);
3279 s.locked++;
3280 }
3281 }
3282 }
3283
3284
NeilBrown3687c062011-07-27 11:00:36 +10003285 /* Finish reconstruct operations initiated by the expansion process */
3286 if (sh->reconstruct_state == reconstruct_state_result) {
3287 struct stripe_head *sh_src
3288 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3289 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3290 /* sh cannot be written until sh_src has been read.
3291 * so arrange for sh to be delayed a little
3292 */
3293 set_bit(STRIPE_DELAYED, &sh->state);
3294 set_bit(STRIPE_HANDLE, &sh->state);
3295 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3296 &sh_src->state))
3297 atomic_inc(&conf->preread_active_stripes);
3298 release_stripe(sh_src);
3299 goto finish;
3300 }
3301 if (sh_src)
3302 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003303
NeilBrown3687c062011-07-27 11:00:36 +10003304 sh->reconstruct_state = reconstruct_state_idle;
3305 clear_bit(STRIPE_EXPANDING, &sh->state);
3306 for (i = conf->raid_disks; i--; ) {
3307 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3308 set_bit(R5_LOCKED, &sh->dev[i].flags);
3309 s.locked++;
3310 }
3311 }
3312
3313 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3314 !sh->reconstruct_state) {
3315 /* Need to write out all blocks after computing parity */
3316 sh->disks = conf->raid_disks;
3317 stripe_set_idx(sh->sector, conf, 0, sh);
3318 schedule_reconstruction(sh, &s, 1, 1);
3319 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3320 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3321 atomic_dec(&conf->reshape_stripes);
3322 wake_up(&conf->wait_for_overlap);
3323 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3324 }
3325
3326 if (s.expanding && s.locked == 0 &&
3327 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3328 handle_stripe_expansion(conf, sh);
3329
3330finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003331 /* wait for this device to become unblocked */
NeilBrown43220aa2011-08-31 12:49:14 +10003332 if (conf->mddev->external && unlikely(s.blocked_rdev))
NeilBrownc5709ef2011-07-26 11:35:20 +10003333 md_wait_for_blocked_rdev(s.blocked_rdev, conf->mddev);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003334
NeilBrownbc2607f2011-07-28 11:39:22 +10003335 if (s.handle_bad_blocks)
3336 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003337 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003338 struct r5dev *dev = &sh->dev[i];
3339 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3340 /* We own a safe reference to the rdev */
3341 rdev = conf->disks[i].rdev;
3342 if (!rdev_set_badblocks(rdev, sh->sector,
3343 STRIPE_SECTORS, 0))
3344 md_error(conf->mddev, rdev);
3345 rdev_dec_pending(rdev, conf->mddev);
3346 }
NeilBrownb84db562011-07-28 11:39:23 +10003347 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3348 rdev = conf->disks[i].rdev;
3349 rdev_clear_badblocks(rdev, sh->sector,
3350 STRIPE_SECTORS);
3351 rdev_dec_pending(rdev, conf->mddev);
3352 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003353 }
3354
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003355 if (s.ops_request)
3356 raid_run_ops(sh, s.ops_request);
3357
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003358 ops_run_io(sh, &s);
3359
NeilBrownc5709ef2011-07-26 11:35:20 +10003360 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003361 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003362 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003363 * have actually been submitted.
3364 */
3365 atomic_dec(&conf->preread_active_stripes);
3366 if (atomic_read(&conf->preread_active_stripes) <
3367 IO_THRESHOLD)
3368 md_wakeup_thread(conf->mddev->thread);
3369 }
3370
NeilBrownc5709ef2011-07-26 11:35:20 +10003371 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003372
NeilBrownc4c16632011-07-26 11:34:20 +10003373 clear_bit(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003374}
3375
Arjan van de Ven858119e2006-01-14 13:20:43 -08003376static void raid5_activate_delayed(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377{
3378 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3379 while (!list_empty(&conf->delayed_list)) {
3380 struct list_head *l = conf->delayed_list.next;
3381 struct stripe_head *sh;
3382 sh = list_entry(l, struct stripe_head, lru);
3383 list_del_init(l);
3384 clear_bit(STRIPE_DELAYED, &sh->state);
3385 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3386 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003387 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 }
NeilBrown482c0832011-04-18 18:25:42 +10003389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390}
3391
Arjan van de Ven858119e2006-01-14 13:20:43 -08003392static void activate_bit_delay(raid5_conf_t *conf)
NeilBrown72626682005-09-09 16:23:54 -07003393{
3394 /* device_lock is held */
3395 struct list_head head;
3396 list_add(&head, &conf->bitmap_list);
3397 list_del_init(&conf->bitmap_list);
3398 while (!list_empty(&head)) {
3399 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3400 list_del_init(&sh->lru);
3401 atomic_inc(&sh->count);
3402 __release_stripe(conf, sh);
3403 }
3404}
3405
NeilBrownfd01b882011-10-11 16:47:53 +11003406int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003407{
NeilBrown070ec552009-06-16 16:54:21 +10003408 raid5_conf_t *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003409
3410 /* No difference between reads and writes. Just check
3411 * how busy the stripe_cache is
3412 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003413
NeilBrownf022b2f2006-10-03 01:15:56 -07003414 if (conf->inactive_blocked)
3415 return 1;
3416 if (conf->quiesce)
3417 return 1;
3418 if (list_empty_careful(&conf->inactive_list))
3419 return 1;
3420
3421 return 0;
3422}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003423EXPORT_SYMBOL_GPL(md_raid5_congested);
3424
3425static int raid5_congested(void *data, int bits)
3426{
NeilBrownfd01b882011-10-11 16:47:53 +11003427 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003428
3429 return mddev_congested(mddev, bits) ||
3430 md_raid5_congested(mddev, bits);
3431}
NeilBrownf022b2f2006-10-03 01:15:56 -07003432
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003433/* We want read requests to align with chunks where possible,
3434 * but write requests don't need to.
3435 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003436static int raid5_mergeable_bvec(struct request_queue *q,
3437 struct bvec_merge_data *bvm,
3438 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003439{
NeilBrownfd01b882011-10-11 16:47:53 +11003440 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003441 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003442 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003443 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003444 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003445
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003446 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003447 return biovec->bv_len; /* always allow writes to be mergeable */
3448
Andre Noll664e7c42009-06-18 08:45:27 +10003449 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3450 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003451 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3452 if (max < 0) max = 0;
3453 if (max <= biovec->bv_len && bio_sectors == 0)
3454 return biovec->bv_len;
3455 else
3456 return max;
3457}
3458
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003459
NeilBrownfd01b882011-10-11 16:47:53 +11003460static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003461{
3462 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003463 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003464 unsigned int bio_sectors = bio->bi_size >> 9;
3465
Andre Noll664e7c42009-06-18 08:45:27 +10003466 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3467 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003468 return chunk_sectors >=
3469 ((sector & (chunk_sectors - 1)) + bio_sectors);
3470}
3471
3472/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003473 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3474 * later sampled by raid5d.
3475 */
3476static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf)
3477{
3478 unsigned long flags;
3479
3480 spin_lock_irqsave(&conf->device_lock, flags);
3481
3482 bi->bi_next = conf->retry_read_aligned_list;
3483 conf->retry_read_aligned_list = bi;
3484
3485 spin_unlock_irqrestore(&conf->device_lock, flags);
3486 md_wakeup_thread(conf->mddev->thread);
3487}
3488
3489
3490static struct bio *remove_bio_from_retry(raid5_conf_t *conf)
3491{
3492 struct bio *bi;
3493
3494 bi = conf->retry_read_aligned;
3495 if (bi) {
3496 conf->retry_read_aligned = NULL;
3497 return bi;
3498 }
3499 bi = conf->retry_read_aligned_list;
3500 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003501 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003502 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003503 /*
3504 * this sets the active strip count to 1 and the processed
3505 * strip count to zero (upper 8 bits)
3506 */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003507 bi->bi_phys_segments = 1; /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003508 }
3509
3510 return bi;
3511}
3512
3513
3514/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003515 * The "raid5_align_endio" should check if the read succeeded and if it
3516 * did, call bio_endio on the original bio (having bio_put the new bio
3517 * first).
3518 * If the read failed..
3519 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003520static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003521{
3522 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003523 struct mddev *mddev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003524 raid5_conf_t *conf;
3525 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003526 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003527
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003528 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003529
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003530 rdev = (void*)raid_bi->bi_next;
3531 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003532 mddev = rdev->mddev;
3533 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003534
3535 rdev_dec_pending(rdev, conf->mddev);
3536
3537 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003538 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003539 if (atomic_dec_and_test(&conf->active_aligned_reads))
3540 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003541 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003542 }
3543
3544
Dan Williams45b42332007-07-09 11:56:43 -07003545 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003546
3547 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003548}
3549
Neil Brown387bb172007-02-08 14:20:29 -08003550static int bio_fits_rdev(struct bio *bi)
3551{
Jens Axboe165125e2007-07-24 09:28:11 +02003552 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003553
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003554 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003555 return 0;
3556 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003557 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003558 return 0;
3559
3560 if (q->merge_bvec_fn)
3561 /* it's too hard to apply the merge_bvec_fn at this stage,
3562 * just just give up
3563 */
3564 return 0;
3565
3566 return 1;
3567}
3568
3569
NeilBrownfd01b882011-10-11 16:47:53 +11003570static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003571{
NeilBrown070ec552009-06-16 16:54:21 +10003572 raid5_conf_t *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11003573 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003574 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11003575 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003576
3577 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003578 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003579 return 0;
3580 }
3581 /*
NeilBrowna167f662010-10-26 18:31:13 +11003582 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003583 */
NeilBrowna167f662010-10-26 18:31:13 +11003584 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003585 if (!align_bi)
3586 return 0;
3587 /*
3588 * set bi_end_io to a new function, and set bi_private to the
3589 * original bio.
3590 */
3591 align_bi->bi_end_io = raid5_align_endio;
3592 align_bi->bi_private = raid_bio;
3593 /*
3594 * compute position
3595 */
NeilBrown112bf892009-03-31 14:39:38 +11003596 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3597 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003598 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003599
3600 rcu_read_lock();
3601 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3602 if (rdev && test_bit(In_sync, &rdev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003603 sector_t first_bad;
3604 int bad_sectors;
3605
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003606 atomic_inc(&rdev->nr_pending);
3607 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003608 raid_bio->bi_next = (void*)rdev;
3609 align_bi->bi_bdev = rdev->bdev;
3610 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
3611 align_bi->bi_sector += rdev->data_offset;
3612
NeilBrown31c176e2011-07-28 11:39:22 +10003613 if (!bio_fits_rdev(align_bi) ||
3614 is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
3615 &first_bad, &bad_sectors)) {
3616 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08003617 bio_put(align_bi);
3618 rdev_dec_pending(rdev, mddev);
3619 return 0;
3620 }
3621
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003622 spin_lock_irq(&conf->device_lock);
3623 wait_event_lock_irq(conf->wait_for_stripe,
3624 conf->quiesce == 0,
3625 conf->device_lock, /* nothing */);
3626 atomic_inc(&conf->active_aligned_reads);
3627 spin_unlock_irq(&conf->device_lock);
3628
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003629 generic_make_request(align_bi);
3630 return 1;
3631 } else {
3632 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003633 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003634 return 0;
3635 }
3636}
3637
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003638/* __get_priority_stripe - get the next stripe to process
3639 *
3640 * Full stripe writes are allowed to pass preread active stripes up until
3641 * the bypass_threshold is exceeded. In general the bypass_count
3642 * increments when the handle_list is handled before the hold_list; however, it
3643 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3644 * stripe with in flight i/o. The bypass_count will be reset when the
3645 * head of the hold_list has changed, i.e. the head was promoted to the
3646 * handle_list.
3647 */
3648static struct stripe_head *__get_priority_stripe(raid5_conf_t *conf)
3649{
3650 struct stripe_head *sh;
3651
3652 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3653 __func__,
3654 list_empty(&conf->handle_list) ? "empty" : "busy",
3655 list_empty(&conf->hold_list) ? "empty" : "busy",
3656 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3657
3658 if (!list_empty(&conf->handle_list)) {
3659 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3660
3661 if (list_empty(&conf->hold_list))
3662 conf->bypass_count = 0;
3663 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3664 if (conf->hold_list.next == conf->last_hold)
3665 conf->bypass_count++;
3666 else {
3667 conf->last_hold = conf->hold_list.next;
3668 conf->bypass_count -= conf->bypass_threshold;
3669 if (conf->bypass_count < 0)
3670 conf->bypass_count = 0;
3671 }
3672 }
3673 } else if (!list_empty(&conf->hold_list) &&
3674 ((conf->bypass_threshold &&
3675 conf->bypass_count > conf->bypass_threshold) ||
3676 atomic_read(&conf->pending_full_writes) == 0)) {
3677 sh = list_entry(conf->hold_list.next,
3678 typeof(*sh), lru);
3679 conf->bypass_count -= conf->bypass_threshold;
3680 if (conf->bypass_count < 0)
3681 conf->bypass_count = 0;
3682 } else
3683 return NULL;
3684
3685 list_del_init(&sh->lru);
3686 atomic_inc(&sh->count);
3687 BUG_ON(atomic_read(&sh->count) != 1);
3688 return sh;
3689}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003690
NeilBrownfd01b882011-10-11 16:47:53 +11003691static int make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692{
NeilBrown070ec552009-06-16 16:54:21 +10003693 raid5_conf_t *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11003694 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 sector_t new_sector;
3696 sector_t logical_sector, last_sector;
3697 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01003698 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11003699 int remaining;
NeilBrown7c13edc2011-04-18 18:25:43 +10003700 int plugged;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701
Tejun Heoe9c74692010-09-03 11:56:18 +02003702 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
3703 md_flush_request(mddev, bi);
NeilBrowne5dcdd82005-09-09 16:23:41 -07003704 return 0;
3705 }
3706
NeilBrown3d310eb2005-06-21 17:17:26 -07003707 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07003708
NeilBrown802ba062006-12-13 00:34:13 -08003709 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003710 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11003711 chunk_aligned_read(mddev,bi))
NeilBrown99c0fb52009-03-31 14:39:38 +11003712 return 0;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003713
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
3715 last_sector = bi->bi_sector + (bi->bi_size>>9);
3716 bi->bi_next = NULL;
3717 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07003718
NeilBrown7c13edc2011-04-18 18:25:43 +10003719 plugged = mddev_check_plugged(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
3721 DEFINE_WAIT(w);
NeilBrown16a53ec2006-06-26 00:27:38 -07003722 int disks, data_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003723 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08003724
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003725 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11003726 previous = 0;
NeilBrownb0f9ec02009-03-31 15:27:18 +11003727 disks = conf->raid_disks;
NeilBrownb578d552006-03-27 01:18:12 -08003728 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11003729 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11003730 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08003731 * 64bit on a 32bit platform, and so it might be
3732 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02003733 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08003734 * the lock is dropped, so once we get a reference
3735 * to the stripe that we think it is, we will have
3736 * to check again.
3737 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003738 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11003739 if (mddev->delta_disks < 0
3740 ? logical_sector < conf->reshape_progress
3741 : logical_sector >= conf->reshape_progress) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003742 disks = conf->previous_raid_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003743 previous = 1;
3744 } else {
NeilBrownfef9c612009-03-31 15:16:46 +11003745 if (mddev->delta_disks < 0
3746 ? logical_sector < conf->reshape_safe
3747 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08003748 spin_unlock_irq(&conf->device_lock);
3749 schedule();
3750 goto retry;
3751 }
3752 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003753 spin_unlock_irq(&conf->device_lock);
3754 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003755 data_disks = disks - conf->max_degraded;
3756
NeilBrown112bf892009-03-31 14:39:38 +11003757 new_sector = raid5_compute_sector(conf, logical_sector,
3758 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003759 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10003760 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 (unsigned long long)new_sector,
3762 (unsigned long long)logical_sector);
3763
NeilBrownb5663ba2009-03-31 14:39:38 +11003764 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10003765 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11003767 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003768 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08003769 * stripe, so we must do the range check again.
3770 * Expansion could still move past after this
3771 * test, but as we are holding a reference to
3772 * 'sh', we know that if that happens,
3773 * STRIPE_EXPANDING will get set and the expansion
3774 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003775 */
3776 int must_retry = 0;
3777 spin_lock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11003778 if (mddev->delta_disks < 0
3779 ? logical_sector >= conf->reshape_progress
3780 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003781 /* mismatch, need to try again */
3782 must_retry = 1;
3783 spin_unlock_irq(&conf->device_lock);
3784 if (must_retry) {
3785 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07003786 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003787 goto retry;
3788 }
3789 }
NeilBrowne62e58a2009-07-01 13:15:35 +10003790
Namhyung Kimffd96e32011-07-18 17:38:51 +10003791 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10003792 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08003793 logical_sector < mddev->suspend_hi) {
3794 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10003795 /* As the suspend_* range is controlled by
3796 * userspace, we want an interruptible
3797 * wait.
3798 */
3799 flush_signals(current);
3800 prepare_to_wait(&conf->wait_for_overlap,
3801 &w, TASK_INTERRUPTIBLE);
3802 if (logical_sector >= mddev->suspend_lo &&
3803 logical_sector < mddev->suspend_hi)
3804 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08003805 goto retry;
3806 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003807
3808 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10003809 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003810 /* Stripe is busy expanding or
3811 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 * and wait a while
3813 */
NeilBrown482c0832011-04-18 18:25:42 +10003814 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 release_stripe(sh);
3816 schedule();
3817 goto retry;
3818 }
3819 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08003820 set_bit(STRIPE_HANDLE, &sh->state);
3821 clear_bit(STRIPE_DELAYED, &sh->state);
Tejun Heoe9c74692010-09-03 11:56:18 +02003822 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11003823 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3824 atomic_inc(&conf->preread_active_stripes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 } else {
3827 /* cannot get stripe for read-ahead, just give-up */
3828 clear_bit(BIO_UPTODATE, &bi->bi_flags);
3829 finish_wait(&conf->wait_for_overlap, &w);
3830 break;
3831 }
3832
3833 }
NeilBrown7c13edc2011-04-18 18:25:43 +10003834 if (!plugged)
3835 md_wakeup_thread(mddev->thread);
3836
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02003838 remaining = raid5_dec_bi_phys_segments(bi);
NeilBrownf6344752006-03-27 01:18:17 -08003839 spin_unlock_irq(&conf->device_lock);
3840 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841
NeilBrown16a53ec2006-06-26 00:27:38 -07003842 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02003844
Neil Brown0e13fe232008-06-28 08:31:20 +10003845 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 }
NeilBrown729a1862009-12-14 12:49:50 +11003847
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 return 0;
3849}
3850
NeilBrownfd01b882011-10-11 16:47:53 +11003851static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11003852
NeilBrownfd01b882011-10-11 16:47:53 +11003853static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854{
NeilBrown52c03292006-06-26 00:27:43 -07003855 /* reshaping is quite different to recovery/resync so it is
3856 * handled quite separately ... here.
3857 *
3858 * On each call to sync_request, we gather one chunk worth of
3859 * destination stripes and flag them as expanding.
3860 * Then we find all the source stripes and request reads.
3861 * As the reads complete, handle_stripe will copy the data
3862 * into the destination stripe and release that stripe.
3863 */
H Hartley Sweeten7b928132010-03-08 16:02:40 +11003864 raid5_conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08003866 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08003867 int raid_disks = conf->previous_raid_disks;
3868 int data_disks = raid_disks - conf->max_degraded;
3869 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07003870 int i;
3871 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11003872 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11003873 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11003874 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11003875 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07003876
NeilBrownfef9c612009-03-31 15:16:46 +11003877 if (sector_nr == 0) {
3878 /* If restarting in the middle, skip the initial sectors */
3879 if (mddev->delta_disks < 0 &&
3880 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
3881 sector_nr = raid5_size(mddev, 0, 0)
3882 - conf->reshape_progress;
NeilBrowna6397552009-08-13 10:13:00 +10003883 } else if (mddev->delta_disks >= 0 &&
NeilBrownfef9c612009-03-31 15:16:46 +11003884 conf->reshape_progress > 0)
3885 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08003886 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11003887 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11003888 mddev->curr_resync_completed = sector_nr;
3889 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11003890 *skipped = 1;
3891 return sector_nr;
3892 }
NeilBrown52c03292006-06-26 00:27:43 -07003893 }
3894
NeilBrown7a661382009-03-31 15:21:40 +11003895 /* We need to process a full chunk at a time.
3896 * If old and new chunk sizes differ, we need to process the
3897 * largest of these
3898 */
Andre Noll664e7c42009-06-18 08:45:27 +10003899 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
3900 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11003901 else
Andre Noll9d8f0362009-06-18 08:45:01 +10003902 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11003903
NeilBrown52c03292006-06-26 00:27:43 -07003904 /* we update the metadata when there is more than 3Meg
3905 * in the block range (that is rather arbitrary, should
3906 * probably be time based) or when the data about to be
3907 * copied would over-write the source of the data at
3908 * the front of the range.
NeilBrownfef9c612009-03-31 15:16:46 +11003909 * i.e. one new_stripe along from reshape_progress new_maps
3910 * to after where reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07003911 */
NeilBrownfef9c612009-03-31 15:16:46 +11003912 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08003913 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11003914 readpos = conf->reshape_progress;
3915 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11003916 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08003917 sector_div(safepos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11003918 if (mddev->delta_disks < 0) {
NeilBrowned37d832009-05-27 21:39:05 +10003919 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11003920 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11003921 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11003922 } else {
NeilBrown7a661382009-03-31 15:21:40 +11003923 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10003924 readpos -= min_t(sector_t, reshape_sectors, readpos);
3925 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11003926 }
NeilBrown52c03292006-06-26 00:27:43 -07003927
NeilBrownc8f517c2009-03-31 15:28:40 +11003928 /* 'writepos' is the most advanced device address we might write.
3929 * 'readpos' is the least advanced device address we might read.
3930 * 'safepos' is the least address recorded in the metadata as having
3931 * been reshaped.
3932 * If 'readpos' is behind 'writepos', then there is no way that we can
3933 * ensure safety in the face of a crash - that must be done by userspace
3934 * making a backup of the data. So in that case there is no particular
3935 * rush to update metadata.
3936 * Otherwise if 'safepos' is behind 'writepos', then we really need to
3937 * update the metadata to advance 'safepos' to match 'readpos' so that
3938 * we can be safe in the event of a crash.
3939 * So we insist on updating metadata if safepos is behind writepos and
3940 * readpos is beyond writepos.
3941 * In any case, update the metadata every 10 seconds.
3942 * Maybe that number should be configurable, but I'm not sure it is
3943 * worth it.... maybe it could be a multiple of safemode_delay???
3944 */
NeilBrownfef9c612009-03-31 15:16:46 +11003945 if ((mddev->delta_disks < 0
NeilBrownc8f517c2009-03-31 15:28:40 +11003946 ? (safepos > writepos && readpos < writepos)
3947 : (safepos < writepos && readpos > writepos)) ||
3948 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07003949 /* Cannot proceed until we've updated the superblock... */
3950 wait_event(conf->wait_for_overlap,
3951 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11003952 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11003953 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11003954 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07003955 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07003956 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07003957 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07003958 kthread_should_stop());
3959 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11003960 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07003961 spin_unlock_irq(&conf->device_lock);
3962 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10003963 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07003964 }
3965
NeilBrownec32a2b2009-03-31 15:17:38 +11003966 if (mddev->delta_disks < 0) {
3967 BUG_ON(conf->reshape_progress == 0);
3968 stripe_addr = writepos;
3969 BUG_ON((mddev->dev_sectors &
NeilBrown7a661382009-03-31 15:21:40 +11003970 ~((sector_t)reshape_sectors - 1))
3971 - reshape_sectors - stripe_addr
NeilBrownec32a2b2009-03-31 15:17:38 +11003972 != sector_nr);
3973 } else {
NeilBrown7a661382009-03-31 15:21:40 +11003974 BUG_ON(writepos != sector_nr + reshape_sectors);
NeilBrownec32a2b2009-03-31 15:17:38 +11003975 stripe_addr = sector_nr;
3976 }
NeilBrownab69ae12009-03-31 15:26:47 +11003977 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11003978 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07003979 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10003980 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10003981 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07003982 set_bit(STRIPE_EXPANDING, &sh->state);
3983 atomic_inc(&conf->reshape_stripes);
3984 /* If any of this stripe is beyond the end of the old
3985 * array, then we need to zero those blocks
3986 */
3987 for (j=sh->disks; j--;) {
3988 sector_t s;
3989 if (j == sh->pd_idx)
3990 continue;
NeilBrownf4168852007-02-28 20:11:53 -08003991 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11003992 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08003993 continue;
NeilBrown784052e2009-03-31 15:19:07 +11003994 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11003995 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10003996 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07003997 continue;
3998 }
3999 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4000 set_bit(R5_Expanded, &sh->dev[j].flags);
4001 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4002 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004003 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004004 set_bit(STRIPE_EXPAND_READY, &sh->state);
4005 set_bit(STRIPE_HANDLE, &sh->state);
4006 }
NeilBrownab69ae12009-03-31 15:26:47 +11004007 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004008 }
4009 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004010 if (mddev->delta_disks < 0)
NeilBrown7a661382009-03-31 15:21:40 +11004011 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004012 else
NeilBrown7a661382009-03-31 15:21:40 +11004013 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004014 spin_unlock_irq(&conf->device_lock);
4015 /* Ok, those stripe are ready. We can start scheduling
4016 * reads on the source stripes.
4017 * The source stripes are determined by mapping the first and last
4018 * block on the destination stripes.
4019 */
NeilBrown52c03292006-06-26 00:27:43 -07004020 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004021 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004022 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004023 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004024 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004025 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004026 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004027 if (last_sector >= mddev->dev_sectors)
4028 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004029 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004030 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004031 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4032 set_bit(STRIPE_HANDLE, &sh->state);
4033 release_stripe(sh);
4034 first_sector += STRIPE_SECTORS;
4035 }
NeilBrownab69ae12009-03-31 15:26:47 +11004036 /* Now that the sources are clearly marked, we can release
4037 * the destination stripes
4038 */
4039 while (!list_empty(&stripes)) {
4040 sh = list_entry(stripes.next, struct stripe_head, lru);
4041 list_del_init(&sh->lru);
4042 release_stripe(sh);
4043 }
NeilBrownc6207272008-02-06 01:39:52 -08004044 /* If this takes us to the resync_max point where we have to pause,
4045 * then we need to write out the superblock.
4046 */
NeilBrown7a661382009-03-31 15:21:40 +11004047 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004048 if ((sector_nr - mddev->curr_resync_completed) * 2
4049 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004050 /* Cannot proceed until we've updated the superblock... */
4051 wait_event(conf->wait_for_overlap,
4052 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004053 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004054 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004055 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004056 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4057 md_wakeup_thread(mddev->thread);
4058 wait_event(mddev->sb_wait,
4059 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4060 || kthread_should_stop());
4061 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004062 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004063 spin_unlock_irq(&conf->device_lock);
4064 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004065 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004066 }
NeilBrown7a661382009-03-31 15:21:40 +11004067 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004068}
4069
4070/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004071static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004072{
H Hartley Sweeten7b928132010-03-08 16:02:40 +11004073 raid5_conf_t *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004074 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004075 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004076 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004077 int still_degraded = 0;
4078 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079
NeilBrown72626682005-09-09 16:23:54 -07004080 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004082
NeilBrown29269552006-03-27 01:18:10 -08004083 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4084 end_reshape(conf);
4085 return 0;
4086 }
NeilBrown72626682005-09-09 16:23:54 -07004087
4088 if (mddev->curr_resync < max_sector) /* aborted */
4089 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4090 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004091 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004092 conf->fullsync = 0;
4093 bitmap_close_sync(mddev->bitmap);
4094
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 return 0;
4096 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004097
NeilBrown64bd6602009-08-03 10:59:58 +10004098 /* Allow raid5_quiesce to complete */
4099 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4100
NeilBrown52c03292006-06-26 00:27:43 -07004101 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4102 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004103
NeilBrownc6207272008-02-06 01:39:52 -08004104 /* No need to check resync_max as we never do more than one
4105 * stripe, and as resync_max will always be on a chunk boundary,
4106 * if the check in md_do_sync didn't fire, there is no chance
4107 * of overstepping resync_max here
4108 */
4109
NeilBrown16a53ec2006-06-26 00:27:38 -07004110 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 * to resync, then assert that we are finished, because there is
4112 * nothing we can do.
4113 */
NeilBrown3285edf2006-06-26 00:27:55 -07004114 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004115 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004116 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004117 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 return rv;
4119 }
NeilBrown72626682005-09-09 16:23:54 -07004120 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004121 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004122 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4123 /* we can skip this block, and probably more */
4124 sync_blocks /= STRIPE_SECTORS;
4125 *skipped = 1;
4126 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128
NeilBrownb47490c2008-02-06 01:39:50 -08004129
4130 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4131
NeilBrowna8c906c2009-06-09 14:39:59 +10004132 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004134 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004136 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004138 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004140 /* Need to check if array will still be degraded after recovery/resync
4141 * We don't need to check the 'failed' flag as when that gets set,
4142 * recovery aborts.
4143 */
NeilBrownf001a702009-06-09 14:30:31 +10004144 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004145 if (conf->disks[i].rdev == NULL)
4146 still_degraded = 1;
4147
4148 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4149
NeilBrown83206d62011-07-26 11:19:49 +10004150 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151
NeilBrown14425772009-10-16 15:55:25 +11004152 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 release_stripe(sh);
4154
4155 return STRIPE_SECTORS;
4156}
4157
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004158static int retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio)
4159{
4160 /* We may not be able to submit a whole bio at once as there
4161 * may not be enough stripe_heads available.
4162 * We cannot pre-allocate enough stripe_heads as we may need
4163 * more than exist in the cache (if we allow ever large chunks).
4164 * So we do one stripe head at a time and record in
4165 * ->bi_hw_segments how many have been done.
4166 *
4167 * We *know* that this entire raid_bio is in one chunk, so
4168 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4169 */
4170 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004171 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004172 sector_t sector, logical_sector, last_sector;
4173 int scnt = 0;
4174 int remaining;
4175 int handled = 0;
4176
4177 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004178 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004179 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004180 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4181
4182 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004183 logical_sector += STRIPE_SECTORS,
4184 sector += STRIPE_SECTORS,
4185 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004186
Jens Axboe960e7392008-08-15 10:41:18 +02004187 if (scnt < raid5_bi_hw_segments(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004188 /* already done this stripe */
4189 continue;
4190
NeilBrowna8c906c2009-06-09 14:39:59 +10004191 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004192
4193 if (!sh) {
4194 /* failed to get a stripe - must wait */
Jens Axboe960e7392008-08-15 10:41:18 +02004195 raid5_set_bi_hw_segments(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004196 conf->retry_read_aligned = raid_bio;
4197 return handled;
4198 }
4199
4200 set_bit(R5_ReadError, &sh->dev[dd_idx].flags);
Neil Brown387bb172007-02-08 14:20:29 -08004201 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4202 release_stripe(sh);
Jens Axboe960e7392008-08-15 10:41:18 +02004203 raid5_set_bi_hw_segments(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004204 conf->retry_read_aligned = raid_bio;
4205 return handled;
4206 }
4207
Dan Williams36d1c642009-07-14 11:48:22 -07004208 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004209 release_stripe(sh);
4210 handled++;
4211 }
4212 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004213 remaining = raid5_dec_bi_phys_segments(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004214 spin_unlock_irq(&conf->device_lock);
Neil Brown0e13fe232008-06-28 08:31:20 +10004215 if (remaining == 0)
4216 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004217 if (atomic_dec_and_test(&conf->active_aligned_reads))
4218 wake_up(&conf->wait_for_stripe);
4219 return handled;
4220}
4221
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004222
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223/*
4224 * This is our raid5 kernel thread.
4225 *
4226 * We scan the hash table for stripes which can be handled now.
4227 * During the scan, completed stripes are saved for us by the interrupt
4228 * handler, so that they will not have to wait for our next wakeup.
4229 */
NeilBrownfd01b882011-10-11 16:47:53 +11004230static void raid5d(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231{
4232 struct stripe_head *sh;
NeilBrown070ec552009-06-16 16:54:21 +10004233 raid5_conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004235 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236
Dan Williams45b42332007-07-09 11:56:43 -07004237 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238
4239 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004241 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 handled = 0;
4243 spin_lock_irq(&conf->device_lock);
4244 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004245 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246
NeilBrown7c13edc2011-04-18 18:25:43 +10004247 if (atomic_read(&mddev->plug_cnt) == 0 &&
4248 !list_empty(&conf->bitmap_list)) {
4249 /* Now is a good time to flush some bitmap updates */
4250 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004251 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004252 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004253 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004254 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004255 activate_bit_delay(conf);
4256 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004257 if (atomic_read(&mddev->plug_cnt) == 0)
4258 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004259
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004260 while ((bio = remove_bio_from_retry(conf))) {
4261 int ok;
4262 spin_unlock_irq(&conf->device_lock);
4263 ok = retry_aligned_read(conf, bio);
4264 spin_lock_irq(&conf->device_lock);
4265 if (!ok)
4266 break;
4267 handled++;
4268 }
4269
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004270 sh = __get_priority_stripe(conf);
4271
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004272 if (!sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 spin_unlock_irq(&conf->device_lock);
4275
4276 handled++;
Dan Williams417b8d42009-10-16 16:25:22 +11004277 handle_stripe(sh);
4278 release_stripe(sh);
4279 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280
NeilBrownde393cd2011-07-28 11:31:48 +10004281 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
4282 md_check_recovery(mddev);
4283
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 spin_lock_irq(&conf->device_lock);
4285 }
Dan Williams45b42332007-07-09 11:56:43 -07004286 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287
4288 spin_unlock_irq(&conf->device_lock);
4289
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004290 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004291 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292
Dan Williams45b42332007-07-09 11:56:43 -07004293 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294}
4295
NeilBrown3f294f42005-11-08 21:39:25 -08004296static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004297raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004298{
NeilBrown070ec552009-06-16 16:54:21 +10004299 raid5_conf_t *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004300 if (conf)
4301 return sprintf(page, "%d\n", conf->max_nr_stripes);
4302 else
4303 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004304}
4305
NeilBrownc41d4ac2010-06-01 19:37:24 +10004306int
NeilBrownfd01b882011-10-11 16:47:53 +11004307raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004308{
4309 raid5_conf_t *conf = mddev->private;
4310 int err;
4311
4312 if (size <= 16 || size > 32768)
4313 return -EINVAL;
4314 while (size < conf->max_nr_stripes) {
4315 if (drop_one_stripe(conf))
4316 conf->max_nr_stripes--;
4317 else
4318 break;
4319 }
4320 err = md_allow_write(mddev);
4321 if (err)
4322 return err;
4323 while (size > conf->max_nr_stripes) {
4324 if (grow_one_stripe(conf))
4325 conf->max_nr_stripes++;
4326 else break;
4327 }
4328 return 0;
4329}
4330EXPORT_SYMBOL(raid5_set_cache_size);
4331
NeilBrown3f294f42005-11-08 21:39:25 -08004332static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004333raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004334{
NeilBrown070ec552009-06-16 16:54:21 +10004335 raid5_conf_t *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004336 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004337 int err;
4338
NeilBrown3f294f42005-11-08 21:39:25 -08004339 if (len >= PAGE_SIZE)
4340 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004341 if (!conf)
4342 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004343
Dan Williams4ef197d82008-04-28 02:15:54 -07004344 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004345 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004346 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004347 if (err)
4348 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004349 return len;
4350}
NeilBrown007583c2005-11-08 21:39:30 -08004351
NeilBrown96de1e62005-11-08 21:39:39 -08004352static struct md_sysfs_entry
4353raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4354 raid5_show_stripe_cache_size,
4355 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004356
4357static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004358raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004359{
NeilBrown070ec552009-06-16 16:54:21 +10004360 raid5_conf_t *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004361 if (conf)
4362 return sprintf(page, "%d\n", conf->bypass_threshold);
4363 else
4364 return 0;
4365}
4366
4367static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004368raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004369{
NeilBrown070ec552009-06-16 16:54:21 +10004370 raid5_conf_t *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004371 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004372 if (len >= PAGE_SIZE)
4373 return -EINVAL;
4374 if (!conf)
4375 return -ENODEV;
4376
Dan Williams4ef197d82008-04-28 02:15:54 -07004377 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004378 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004379 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004380 return -EINVAL;
4381 conf->bypass_threshold = new;
4382 return len;
4383}
4384
4385static struct md_sysfs_entry
4386raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4387 S_IRUGO | S_IWUSR,
4388 raid5_show_preread_threshold,
4389 raid5_store_preread_threshold);
4390
4391static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004392stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004393{
NeilBrown070ec552009-06-16 16:54:21 +10004394 raid5_conf_t *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004395 if (conf)
4396 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4397 else
4398 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004399}
4400
NeilBrown96de1e62005-11-08 21:39:39 -08004401static struct md_sysfs_entry
4402raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004403
NeilBrown007583c2005-11-08 21:39:30 -08004404static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004405 &raid5_stripecache_size.attr,
4406 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004407 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004408 NULL,
4409};
NeilBrown007583c2005-11-08 21:39:30 -08004410static struct attribute_group raid5_attrs_group = {
4411 .name = NULL,
4412 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004413};
4414
Dan Williams80c3a6c2009-03-17 18:10:40 -07004415static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11004416raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07004417{
NeilBrown070ec552009-06-16 16:54:21 +10004418 raid5_conf_t *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004419
4420 if (!sectors)
4421 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004422 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11004423 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11004424 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004425
Andre Noll9d8f0362009-06-18 08:45:01 +10004426 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004427 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004428 return sectors * (raid_disks - conf->max_degraded);
4429}
4430
Dan Williams36d1c642009-07-14 11:48:22 -07004431static void raid5_free_percpu(raid5_conf_t *conf)
4432{
4433 struct raid5_percpu *percpu;
4434 unsigned long cpu;
4435
4436 if (!conf->percpu)
4437 return;
4438
4439 get_online_cpus();
4440 for_each_possible_cpu(cpu) {
4441 percpu = per_cpu_ptr(conf->percpu, cpu);
4442 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004443 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004444 }
4445#ifdef CONFIG_HOTPLUG_CPU
4446 unregister_cpu_notifier(&conf->cpu_notify);
4447#endif
4448 put_online_cpus();
4449
4450 free_percpu(conf->percpu);
4451}
4452
Dan Williams95fc17a2009-07-31 12:39:15 +10004453static void free_conf(raid5_conf_t *conf)
4454{
4455 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004456 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10004457 kfree(conf->disks);
4458 kfree(conf->stripe_hashtbl);
4459 kfree(conf);
4460}
4461
Dan Williams36d1c642009-07-14 11:48:22 -07004462#ifdef CONFIG_HOTPLUG_CPU
4463static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4464 void *hcpu)
4465{
4466 raid5_conf_t *conf = container_of(nfb, raid5_conf_t, cpu_notify);
4467 long cpu = (long)hcpu;
4468 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4469
4470 switch (action) {
4471 case CPU_UP_PREPARE:
4472 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07004473 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07004474 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004475 if (!percpu->scribble)
4476 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4477
4478 if (!percpu->scribble ||
4479 (conf->level == 6 && !percpu->spare_page)) {
4480 safe_put_page(percpu->spare_page);
4481 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004482 pr_err("%s: failed memory allocation for cpu%ld\n",
4483 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07004484 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07004485 }
4486 break;
4487 case CPU_DEAD:
4488 case CPU_DEAD_FROZEN:
4489 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004490 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004491 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004492 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07004493 break;
4494 default:
4495 break;
4496 }
4497 return NOTIFY_OK;
4498}
4499#endif
4500
4501static int raid5_alloc_percpu(raid5_conf_t *conf)
4502{
4503 unsigned long cpu;
4504 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09004505 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004506 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004507 int err;
4508
Dan Williams36d1c642009-07-14 11:48:22 -07004509 allcpus = alloc_percpu(struct raid5_percpu);
4510 if (!allcpus)
4511 return -ENOMEM;
4512 conf->percpu = allcpus;
4513
4514 get_online_cpus();
4515 err = 0;
4516 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07004517 if (conf->level == 6) {
4518 spare_page = alloc_page(GFP_KERNEL);
4519 if (!spare_page) {
4520 err = -ENOMEM;
4521 break;
4522 }
4523 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
4524 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11004525 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004526 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07004527 err = -ENOMEM;
4528 break;
4529 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07004530 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004531 }
4532#ifdef CONFIG_HOTPLUG_CPU
4533 conf->cpu_notify.notifier_call = raid456_cpu_notify;
4534 conf->cpu_notify.priority = 0;
4535 if (err == 0)
4536 err = register_cpu_notifier(&conf->cpu_notify);
4537#endif
4538 put_online_cpus();
4539
4540 return err;
4541}
4542
NeilBrownfd01b882011-10-11 16:47:53 +11004543static raid5_conf_t *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544{
4545 raid5_conf_t *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004546 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11004547 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 struct disk_info *disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549
NeilBrown91adb562009-03-31 14:39:39 +11004550 if (mddev->new_level != 5
4551 && mddev->new_level != 4
4552 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10004553 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004554 mdname(mddev), mddev->new_level);
4555 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 }
NeilBrown91adb562009-03-31 14:39:39 +11004557 if ((mddev->new_level == 5
4558 && !algorithm_valid_raid5(mddev->new_layout)) ||
4559 (mddev->new_level == 6
4560 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004561 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11004562 mdname(mddev), mddev->new_layout);
4563 return ERR_PTR(-EIO);
4564 }
4565 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10004566 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004567 mdname(mddev), mddev->raid_disks);
4568 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11004569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570
Andre Noll664e7c42009-06-18 08:45:27 +10004571 if (!mddev->new_chunk_sectors ||
4572 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
4573 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10004574 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
4575 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11004576 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11004577 }
4578
NeilBrown91adb562009-03-31 14:39:39 +11004579 conf = kzalloc(sizeof(raid5_conf_t), GFP_KERNEL);
4580 if (conf == NULL)
4581 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11004582 spin_lock_init(&conf->device_lock);
4583 init_waitqueue_head(&conf->wait_for_stripe);
4584 init_waitqueue_head(&conf->wait_for_overlap);
4585 INIT_LIST_HEAD(&conf->handle_list);
4586 INIT_LIST_HEAD(&conf->hold_list);
4587 INIT_LIST_HEAD(&conf->delayed_list);
4588 INIT_LIST_HEAD(&conf->bitmap_list);
4589 INIT_LIST_HEAD(&conf->inactive_list);
4590 atomic_set(&conf->active_stripes, 0);
4591 atomic_set(&conf->preread_active_stripes, 0);
4592 atomic_set(&conf->active_aligned_reads, 0);
4593 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrown91adb562009-03-31 14:39:39 +11004594
4595 conf->raid_disks = mddev->raid_disks;
4596 if (mddev->reshape_position == MaxSector)
4597 conf->previous_raid_disks = mddev->raid_disks;
4598 else
4599 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004600 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
4601 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11004602
NeilBrown5e5e3e72009-10-16 16:35:30 +11004603 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11004604 GFP_KERNEL);
4605 if (!conf->disks)
4606 goto abort;
4607
4608 conf->mddev = mddev;
4609
4610 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
4611 goto abort;
4612
Dan Williams36d1c642009-07-14 11:48:22 -07004613 conf->level = mddev->new_level;
4614 if (raid5_alloc_percpu(conf) != 0)
4615 goto abort;
4616
NeilBrown0c55e022010-05-03 14:09:02 +10004617 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11004618
4619 list_for_each_entry(rdev, &mddev->disks, same_set) {
4620 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004621 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11004622 || raid_disk < 0)
4623 continue;
4624 disk = conf->disks + raid_disk;
4625
4626 disk->rdev = rdev;
4627
4628 if (test_bit(In_sync, &rdev->flags)) {
4629 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10004630 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
4631 " disk %d\n",
4632 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05004633 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11004634 /* Cannot rely on bitmap to complete recovery */
4635 conf->fullsync = 1;
4636 }
4637
Andre Noll09c9e5f2009-06-18 08:45:55 +10004638 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11004639 conf->level = mddev->new_level;
4640 if (conf->level == 6)
4641 conf->max_degraded = 2;
4642 else
4643 conf->max_degraded = 1;
4644 conf->algorithm = mddev->new_layout;
4645 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11004646 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11004647 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10004648 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11004649 conf->prev_algo = mddev->layout;
4650 }
NeilBrown91adb562009-03-31 14:39:39 +11004651
4652 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11004653 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11004654 if (grow_stripes(conf, conf->max_nr_stripes)) {
4655 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004656 "md/raid:%s: couldn't allocate %dkB for buffers\n",
4657 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004658 goto abort;
4659 } else
NeilBrown0c55e022010-05-03 14:09:02 +10004660 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
4661 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004662
NeilBrown0da3c612009-09-23 18:09:45 +10004663 conf->thread = md_register_thread(raid5d, mddev, NULL);
NeilBrown91adb562009-03-31 14:39:39 +11004664 if (!conf->thread) {
4665 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004666 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11004667 mdname(mddev));
4668 goto abort;
4669 }
4670
4671 return conf;
4672
4673 abort:
4674 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10004675 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11004676 return ERR_PTR(-EIO);
4677 } else
4678 return ERR_PTR(-ENOMEM);
4679}
4680
NeilBrownc148ffd2009-11-13 17:47:00 +11004681
4682static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
4683{
4684 switch (algo) {
4685 case ALGORITHM_PARITY_0:
4686 if (raid_disk < max_degraded)
4687 return 1;
4688 break;
4689 case ALGORITHM_PARITY_N:
4690 if (raid_disk >= raid_disks - max_degraded)
4691 return 1;
4692 break;
4693 case ALGORITHM_PARITY_0_6:
4694 if (raid_disk == 0 ||
4695 raid_disk == raid_disks - 1)
4696 return 1;
4697 break;
4698 case ALGORITHM_LEFT_ASYMMETRIC_6:
4699 case ALGORITHM_RIGHT_ASYMMETRIC_6:
4700 case ALGORITHM_LEFT_SYMMETRIC_6:
4701 case ALGORITHM_RIGHT_SYMMETRIC_6:
4702 if (raid_disk == raid_disks - 1)
4703 return 1;
4704 }
4705 return 0;
4706}
4707
NeilBrownfd01b882011-10-11 16:47:53 +11004708static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11004709{
4710 raid5_conf_t *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10004711 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11004712 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11004713 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11004714 sector_t reshape_offset = 0;
NeilBrown91adb562009-03-31 14:39:39 +11004715
Andre Noll8c6ac862009-06-18 08:48:06 +10004716 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10004717 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10004718 " -- starting background reconstruction\n",
4719 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004720 if (mddev->reshape_position != MaxSector) {
4721 /* Check that we can continue the reshape.
4722 * Currently only disks can change, it must
4723 * increase, and we must be past the point where
4724 * a stripe over-writes itself
4725 */
4726 sector_t here_new, here_old;
4727 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11004728 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08004729
NeilBrown88ce4932009-03-31 15:24:23 +11004730 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10004731 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08004732 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08004733 mdname(mddev));
4734 return -EINVAL;
4735 }
NeilBrownf6705572006-03-27 01:18:11 -08004736 old_disks = mddev->raid_disks - mddev->delta_disks;
4737 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08004738 * further up in new geometry must map after here in old
4739 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08004740 */
4741 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10004742 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08004743 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004744 printk(KERN_ERR "md/raid:%s: reshape_position not "
4745 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004746 return -EINVAL;
4747 }
NeilBrownc148ffd2009-11-13 17:47:00 +11004748 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08004749 /* here_new is the stripe we will write to */
4750 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10004751 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08004752 (old_disks-max_degraded));
4753 /* here_old is the first stripe that we might need to read
4754 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10004755 if (mddev->delta_disks == 0) {
4756 /* We cannot be sure it is safe to start an in-place
4757 * reshape. It is only safe if user-space if monitoring
4758 * and taking constant backups.
4759 * mdadm always starts a situation like this in
4760 * readonly mode so it can take control before
4761 * allowing any writes. So just check for that.
4762 */
4763 if ((here_new * mddev->new_chunk_sectors !=
4764 here_old * mddev->chunk_sectors) ||
4765 mddev->ro == 0) {
NeilBrown0c55e022010-05-03 14:09:02 +10004766 printk(KERN_ERR "md/raid:%s: in-place reshape must be started"
4767 " in read-only mode - aborting\n",
4768 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10004769 return -EINVAL;
4770 }
4771 } else if (mddev->delta_disks < 0
4772 ? (here_new * mddev->new_chunk_sectors <=
4773 here_old * mddev->chunk_sectors)
4774 : (here_new * mddev->new_chunk_sectors >=
4775 here_old * mddev->chunk_sectors)) {
NeilBrownf6705572006-03-27 01:18:11 -08004776 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10004777 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
4778 "auto-recovery - aborting.\n",
4779 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004780 return -EINVAL;
4781 }
NeilBrown0c55e022010-05-03 14:09:02 +10004782 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
4783 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004784 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08004785 } else {
NeilBrown91adb562009-03-31 14:39:39 +11004786 BUG_ON(mddev->level != mddev->new_level);
4787 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10004788 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11004789 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08004790 }
4791
NeilBrown245f46c2009-03-31 14:39:39 +11004792 if (mddev->private == NULL)
4793 conf = setup_conf(mddev);
4794 else
4795 conf = mddev->private;
4796
NeilBrown91adb562009-03-31 14:39:39 +11004797 if (IS_ERR(conf))
4798 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08004799
NeilBrown91adb562009-03-31 14:39:39 +11004800 mddev->thread = conf->thread;
4801 conf->thread = NULL;
4802 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07004805 * 0 for a fully functional array, 1 or 2 for a degraded array.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 */
NeilBrownc148ffd2009-11-13 17:47:00 +11004807 list_for_each_entry(rdev, &mddev->disks, same_set) {
4808 if (rdev->raid_disk < 0)
4809 continue;
NeilBrown2f115882010-06-17 17:41:03 +10004810 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11004811 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10004812 continue;
4813 }
NeilBrownc148ffd2009-11-13 17:47:00 +11004814 /* This disc is not fully in-sync. However if it
4815 * just stored parity (beyond the recovery_offset),
4816 * when we don't need to be concerned about the
4817 * array being dirty.
4818 * When reshape goes 'backwards', we never have
4819 * partially completed devices, so we only need
4820 * to worry about reshape going forwards.
4821 */
4822 /* Hack because v0.91 doesn't store recovery_offset properly. */
4823 if (mddev->major_version == 0 &&
4824 mddev->minor_version > 90)
4825 rdev->recovery_offset = reshape_offset;
4826
NeilBrownc148ffd2009-11-13 17:47:00 +11004827 if (rdev->recovery_offset < reshape_offset) {
4828 /* We need to check old and new layout */
4829 if (!only_parity(rdev->raid_disk,
4830 conf->algorithm,
4831 conf->raid_disks,
4832 conf->max_degraded))
4833 continue;
4834 }
4835 if (!only_parity(rdev->raid_disk,
4836 conf->prev_algo,
4837 conf->previous_raid_disks,
4838 conf->max_degraded))
4839 continue;
4840 dirty_parity_disks++;
4841 }
NeilBrown91adb562009-03-31 14:39:39 +11004842
NeilBrown5e5e3e72009-10-16 16:35:30 +11004843 mddev->degraded = (max(conf->raid_disks, conf->previous_raid_disks)
4844 - working_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845
NeilBrown674806d2010-06-16 17:17:53 +10004846 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10004847 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07004849 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 goto abort;
4851 }
4852
NeilBrown91adb562009-03-31 14:39:39 +11004853 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004854 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11004855 mddev->resync_max_sectors = mddev->dev_sectors;
4856
NeilBrownc148ffd2009-11-13 17:47:00 +11004857 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004859 if (mddev->ok_start_degraded)
4860 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10004861 "md/raid:%s: starting dirty degraded array"
4862 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004863 mdname(mddev));
4864 else {
4865 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004866 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004867 mdname(mddev));
4868 goto abort;
4869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 }
4871
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10004873 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
4874 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11004875 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
4876 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 else
NeilBrown0c55e022010-05-03 14:09:02 +10004878 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
4879 " out of %d devices, algorithm %d\n",
4880 mdname(mddev), conf->level,
4881 mddev->raid_disks - mddev->degraded,
4882 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883
4884 print_raid5_conf(conf);
4885
NeilBrownfef9c612009-03-31 15:16:46 +11004886 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11004887 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08004888 atomic_set(&conf->reshape_stripes, 0);
4889 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4890 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4891 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4892 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4893 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10004894 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08004895 }
4896
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897
4898 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10004899 if (mddev->to_remove == &raid5_attrs_group)
4900 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10004901 else if (mddev->kobj.sd &&
4902 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08004903 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10004904 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08004905 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10004906 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
4907
4908 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10004909 int chunk_size;
NeilBrown4a5add42010-06-01 19:37:28 +10004910 /* read-ahead size must cover two whole stripes, which
4911 * is 2 * (datadisks) * chunksize where 'n' is the
4912 * number of raid devices
4913 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 int data_disks = conf->previous_raid_disks - conf->max_degraded;
4915 int stripe = data_disks *
4916 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
4917 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
4918 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10004919
4920 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10004921
4922 mddev->queue->backing_dev_info.congested_data = mddev;
4923 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10004924
4925 chunk_size = mddev->chunk_sectors << 9;
4926 blk_queue_io_min(mddev->queue, chunk_size);
4927 blk_queue_io_opt(mddev->queue, chunk_size *
4928 (conf->raid_disks - conf->max_degraded));
4929
4930 list_for_each_entry(rdev, &mddev->disks, same_set)
4931 disk_stack_limits(mddev->gendisk, rdev->bdev,
4932 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 }
4934
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 return 0;
4936abort:
NeilBrown01f96c02011-09-21 15:30:20 +10004937 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11004938 print_raid5_conf(conf);
4939 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10004941 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 return -EIO;
4943}
4944
NeilBrownfd01b882011-10-11 16:47:53 +11004945static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946{
H Hartley Sweeten7b928132010-03-08 16:02:40 +11004947 raid5_conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948
NeilBrown01f96c02011-09-21 15:30:20 +10004949 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10004950 if (mddev->queue)
4951 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10004952 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10004953 mddev->private = NULL;
4954 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 return 0;
4956}
4957
NeilBrownfd01b882011-10-11 16:47:53 +11004958static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959{
H Hartley Sweeten7b928132010-03-08 16:02:40 +11004960 raid5_conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 int i;
4962
Andre Noll9d8f0362009-06-18 08:45:01 +10004963 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
4964 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07004965 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 for (i = 0; i < conf->raid_disks; i++)
4967 seq_printf (seq, "%s",
4968 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08004969 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971}
4972
4973static void print_raid5_conf (raid5_conf_t *conf)
4974{
4975 int i;
4976 struct disk_info *tmp;
4977
NeilBrown0c55e022010-05-03 14:09:02 +10004978 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 if (!conf) {
4980 printk("(conf==NULL)\n");
4981 return;
4982 }
NeilBrown0c55e022010-05-03 14:09:02 +10004983 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
4984 conf->raid_disks,
4985 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986
4987 for (i = 0; i < conf->raid_disks; i++) {
4988 char b[BDEVNAME_SIZE];
4989 tmp = conf->disks + i;
4990 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10004991 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
4992 i, !test_bit(Faulty, &tmp->rdev->flags),
4993 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 }
4995}
4996
NeilBrownfd01b882011-10-11 16:47:53 +11004997static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998{
4999 int i;
5000 raid5_conf_t *conf = mddev->private;
5001 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005002 int count = 0;
5003 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004
5005 for (i = 0; i < conf->raid_disks; i++) {
5006 tmp = conf->disks + i;
5007 if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005008 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005009 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005010 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005011 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005012 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 }
5014 }
NeilBrown6b965622010-08-18 11:56:59 +10005015 spin_lock_irqsave(&conf->device_lock, flags);
5016 mddev->degraded -= count;
5017 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005019 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020}
5021
NeilBrownfd01b882011-10-11 16:47:53 +11005022static int raid5_remove_disk(struct mddev *mddev, int number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023{
5024 raid5_conf_t *conf = mddev->private;
5025 int err = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11005026 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 struct disk_info *p = conf->disks + number;
5028
5029 print_raid5_conf(conf);
5030 rdev = p->rdev;
5031 if (rdev) {
NeilBrownec32a2b2009-03-31 15:17:38 +11005032 if (number >= conf->raid_disks &&
5033 conf->reshape_progress == MaxSector)
5034 clear_bit(In_sync, &rdev->flags);
5035
NeilBrownb2d444d2005-11-08 21:39:31 -08005036 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 atomic_read(&rdev->nr_pending)) {
5038 err = -EBUSY;
5039 goto abort;
5040 }
NeilBrowndfc70642008-05-23 13:04:39 -07005041 /* Only remove non-faulty devices if recovery
5042 * isn't possible.
5043 */
5044 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7f0da592011-07-28 11:39:22 +10005045 mddev->recovery_disabled != conf->recovery_disabled &&
NeilBrown674806d2010-06-16 17:17:53 +10005046 !has_failed(conf) &&
NeilBrownec32a2b2009-03-31 15:17:38 +11005047 number < conf->raid_disks) {
NeilBrowndfc70642008-05-23 13:04:39 -07005048 err = -EBUSY;
5049 goto abort;
5050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07005052 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 if (atomic_read(&rdev->nr_pending)) {
5054 /* lost the race, try later */
5055 err = -EBUSY;
5056 p->rdev = rdev;
5057 }
5058 }
5059abort:
5060
5061 print_raid5_conf(conf);
5062 return err;
5063}
5064
NeilBrownfd01b882011-10-11 16:47:53 +11005065static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066{
5067 raid5_conf_t *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005068 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 int disk;
5070 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005071 int first = 0;
5072 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073
NeilBrown7f0da592011-07-28 11:39:22 +10005074 if (mddev->recovery_disabled == conf->recovery_disabled)
5075 return -EBUSY;
5076
NeilBrown674806d2010-06-16 17:17:53 +10005077 if (has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005079 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080
Neil Brown6c2fce22008-06-28 08:31:31 +10005081 if (rdev->raid_disk >= 0)
5082 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083
5084 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005085 * find the disk ... but prefer rdev->saved_raid_disk
5086 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005088 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005089 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005090 conf->disks[rdev->saved_raid_disk].rdev == NULL)
5091 disk = rdev->saved_raid_disk;
5092 else
Neil Brown6c2fce22008-06-28 08:31:31 +10005093 disk = first;
5094 for ( ; disk <= last ; disk++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 if ((p=conf->disks + disk)->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005096 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005098 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005099 if (rdev->saved_raid_disk != disk)
5100 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005101 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 break;
5103 }
5104 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005105 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106}
5107
NeilBrownfd01b882011-10-11 16:47:53 +11005108static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109{
5110 /* no resync is happening, and there is enough space
5111 * on all devices, so we can resize.
5112 * We need to make sure resync covers any new space.
5113 * If the array is shrinking we should possibly wait until
5114 * any io in the removed space completes, but it hardly seems
5115 * worth it.
5116 */
Andre Noll9d8f0362009-06-18 08:45:01 +10005117 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Dan Williams1f403622009-03-31 14:59:03 +11005118 md_set_array_sectors(mddev, raid5_size(mddev, sectors,
5119 mddev->raid_disks));
Dan Williamsb522adc2009-03-31 15:00:31 +11005120 if (mddev->array_sectors >
5121 raid5_size(mddev, sectors, mddev->raid_disks))
5122 return -EINVAL;
Andre Nollf233ea52008-07-21 17:05:22 +10005123 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005124 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005125 if (sectors > mddev->dev_sectors &&
5126 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005127 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5129 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005130 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005131 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 return 0;
5133}
5134
NeilBrownfd01b882011-10-11 16:47:53 +11005135static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005136{
5137 /* Can only proceed if there are plenty of stripe_heads.
5138 * We need a minimum of one full stripe,, and for sensible progress
5139 * it is best to have about 4 times that.
5140 * If we require 4 times, then the default 256 4K stripe_heads will
5141 * allow for chunk sizes up to 256K, which is probably OK.
5142 * If the chunk size is greater, user-space should request more
5143 * stripe_heads first.
5144 */
5145 raid5_conf_t *conf = mddev->private;
5146 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5147 > conf->max_nr_stripes ||
5148 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5149 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005150 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5151 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005152 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5153 / STRIPE_SIZE)*4);
5154 return 0;
5155 }
5156 return 1;
5157}
5158
NeilBrownfd01b882011-10-11 16:47:53 +11005159static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005160{
NeilBrown070ec552009-06-16 16:54:21 +10005161 raid5_conf_t *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005162
NeilBrown88ce4932009-03-31 15:24:23 +11005163 if (mddev->delta_disks == 0 &&
5164 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005165 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005166 return 0; /* nothing to do */
NeilBrowndba034e2008-08-05 15:54:13 +10005167 if (mddev->bitmap)
5168 /* Cannot grow a bitmap yet */
5169 return -EBUSY;
NeilBrown674806d2010-06-16 17:17:53 +10005170 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005171 return -EINVAL;
5172 if (mddev->delta_disks < 0) {
5173 /* We might be able to shrink, but the devices must
5174 * be made bigger first.
5175 * For raid6, 4 is the minimum size.
5176 * Otherwise 2 is the minimum
5177 */
5178 int min = 2;
5179 if (mddev->level == 6)
5180 min = 4;
5181 if (mddev->raid_disks + mddev->delta_disks < min)
5182 return -EINVAL;
5183 }
NeilBrown29269552006-03-27 01:18:10 -08005184
NeilBrown01ee22b2009-06-18 08:47:20 +10005185 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005186 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005187
NeilBrownec32a2b2009-03-31 15:17:38 +11005188 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
NeilBrown63c70c42006-03-27 01:18:13 -08005189}
5190
NeilBrownfd01b882011-10-11 16:47:53 +11005191static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005192{
NeilBrown070ec552009-06-16 16:54:21 +10005193 raid5_conf_t *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005194 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005195 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005196 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005197
NeilBrownf4168852007-02-28 20:11:53 -08005198 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005199 return -EBUSY;
5200
NeilBrown01ee22b2009-06-18 08:47:20 +10005201 if (!check_stripe_cache(mddev))
5202 return -ENOSPC;
5203
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005204 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown469518a2011-01-31 11:57:43 +11005205 if (!test_bit(In_sync, &rdev->flags)
5206 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005207 spares++;
NeilBrown63c70c42006-03-27 01:18:13 -08005208
NeilBrownf4168852007-02-28 20:11:53 -08005209 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005210 /* Not enough devices even to make a degraded array
5211 * of that size
5212 */
5213 return -EINVAL;
5214
NeilBrownec32a2b2009-03-31 15:17:38 +11005215 /* Refuse to reduce size of the array. Any reductions in
5216 * array size must be through explicit setting of array_size
5217 * attribute.
5218 */
5219 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5220 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10005221 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11005222 "before number of disks\n", mdname(mddev));
5223 return -EINVAL;
5224 }
5225
NeilBrownf6705572006-03-27 01:18:11 -08005226 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005227 spin_lock_irq(&conf->device_lock);
5228 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005229 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005230 conf->prev_chunk_sectors = conf->chunk_sectors;
5231 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005232 conf->prev_algo = conf->algorithm;
5233 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11005234 if (mddev->delta_disks < 0)
5235 conf->reshape_progress = raid5_size(mddev, 0, 0);
5236 else
5237 conf->reshape_progress = 0;
5238 conf->reshape_safe = conf->reshape_progress;
NeilBrown86b42c72009-03-31 15:19:03 +11005239 conf->generation++;
NeilBrown29269552006-03-27 01:18:10 -08005240 spin_unlock_irq(&conf->device_lock);
5241
5242 /* Add some new drives, as many as will fit.
5243 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10005244 * Don't add devices if we are reducing the number of
5245 * devices in the array. This is because it is not possible
5246 * to correctly record the "partially reconstructed" state of
5247 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08005248 */
NeilBrown87a8dec2011-01-31 11:57:43 +11005249 if (mddev->delta_disks >= 0) {
5250 int added_devices = 0;
5251 list_for_each_entry(rdev, &mddev->disks, same_set)
5252 if (rdev->raid_disk < 0 &&
5253 !test_bit(Faulty, &rdev->flags)) {
5254 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11005255 if (rdev->raid_disk
5256 >= conf->previous_raid_disks) {
5257 set_bit(In_sync, &rdev->flags);
5258 added_devices++;
5259 } else
5260 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10005261
5262 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11005263 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11005264 }
NeilBrown87a8dec2011-01-31 11:57:43 +11005265 } else if (rdev->raid_disk >= conf->previous_raid_disks
5266 && !test_bit(Faulty, &rdev->flags)) {
5267 /* This is a spare that was manually added */
5268 set_bit(In_sync, &rdev->flags);
5269 added_devices++;
5270 }
NeilBrown29269552006-03-27 01:18:10 -08005271
NeilBrown87a8dec2011-01-31 11:57:43 +11005272 /* When a reshape changes the number of devices,
5273 * ->degraded is measured against the larger of the
5274 * pre and post number of devices.
5275 */
NeilBrownec32a2b2009-03-31 15:17:38 +11005276 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown9eb07c22010-02-09 12:31:47 +11005277 mddev->degraded += (conf->raid_disks - conf->previous_raid_disks)
NeilBrownec32a2b2009-03-31 15:17:38 +11005278 - added_devices;
5279 spin_unlock_irqrestore(&conf->device_lock, flags);
5280 }
NeilBrown63c70c42006-03-27 01:18:13 -08005281 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10005282 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07005283 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005284
NeilBrown29269552006-03-27 01:18:10 -08005285 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5286 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5287 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5288 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5289 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005290 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08005291 if (!mddev->sync_thread) {
5292 mddev->recovery = 0;
5293 spin_lock_irq(&conf->device_lock);
5294 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005295 conf->reshape_progress = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005296 spin_unlock_irq(&conf->device_lock);
5297 return -EAGAIN;
5298 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005299 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005300 md_wakeup_thread(mddev->sync_thread);
5301 md_new_event(mddev);
5302 return 0;
5303}
NeilBrown29269552006-03-27 01:18:10 -08005304
NeilBrownec32a2b2009-03-31 15:17:38 +11005305/* This is called from the reshape thread and should make any
5306 * changes needed in 'conf'
5307 */
NeilBrown29269552006-03-27 01:18:10 -08005308static void end_reshape(raid5_conf_t *conf)
5309{
NeilBrown29269552006-03-27 01:18:10 -08005310
NeilBrownf6705572006-03-27 01:18:11 -08005311 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
Dan Williams80c3a6c2009-03-17 18:10:40 -07005312
NeilBrownf6705572006-03-27 01:18:11 -08005313 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11005314 conf->previous_raid_disks = conf->raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005315 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08005316 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005317 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07005318
5319 /* read-ahead size must cover two whole stripes, which is
5320 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5321 */
NeilBrown4a5add42010-06-01 19:37:28 +10005322 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11005323 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005324 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11005325 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07005326 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5327 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5328 }
NeilBrown29269552006-03-27 01:18:10 -08005329 }
NeilBrown29269552006-03-27 01:18:10 -08005330}
5331
NeilBrownec32a2b2009-03-31 15:17:38 +11005332/* This is called from the raid5d thread with mddev_lock held.
5333 * It makes config changes to the device.
5334 */
NeilBrownfd01b882011-10-11 16:47:53 +11005335static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11005336{
NeilBrown070ec552009-06-16 16:54:21 +10005337 raid5_conf_t *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11005338
5339 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5340
NeilBrownec32a2b2009-03-31 15:17:38 +11005341 if (mddev->delta_disks > 0) {
5342 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5343 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005344 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11005345 } else {
5346 int d;
NeilBrownec32a2b2009-03-31 15:17:38 +11005347 mddev->degraded = conf->raid_disks;
5348 for (d = 0; d < conf->raid_disks ; d++)
5349 if (conf->disks[d].rdev &&
5350 test_bit(In_sync,
5351 &conf->disks[d].rdev->flags))
5352 mddev->degraded--;
5353 for (d = conf->raid_disks ;
5354 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10005355 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11005356 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrown1a67dde2009-08-13 10:41:49 +10005357 if (rdev && raid5_remove_disk(mddev, d) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10005358 sysfs_unlink_rdev(mddev, rdev);
NeilBrown1a67dde2009-08-13 10:41:49 +10005359 rdev->raid_disk = -1;
5360 }
5361 }
NeilBrowncea9c222009-03-31 15:15:05 +11005362 }
NeilBrown88ce4932009-03-31 15:24:23 +11005363 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005364 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11005365 mddev->reshape_position = MaxSector;
5366 mddev->delta_disks = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11005367 }
5368}
5369
NeilBrownfd01b882011-10-11 16:47:53 +11005370static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07005371{
NeilBrown070ec552009-06-16 16:54:21 +10005372 raid5_conf_t *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07005373
5374 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08005375 case 2: /* resume for a suspend */
5376 wake_up(&conf->wait_for_overlap);
5377 break;
5378
NeilBrown72626682005-09-09 16:23:54 -07005379 case 1: /* stop all writes */
5380 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005381 /* '2' tells resync/reshape to pause so that all
5382 * active stripes can drain
5383 */
5384 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07005385 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005386 atomic_read(&conf->active_stripes) == 0 &&
5387 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07005388 conf->device_lock, /* nothing */);
NeilBrown64bd6602009-08-03 10:59:58 +10005389 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07005390 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005391 /* allow reshape to continue */
5392 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005393 break;
5394
5395 case 0: /* re-enable writes */
5396 spin_lock_irq(&conf->device_lock);
5397 conf->quiesce = 0;
5398 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08005399 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005400 spin_unlock_irq(&conf->device_lock);
5401 break;
5402 }
NeilBrown72626682005-09-09 16:23:54 -07005403}
NeilBrownb15c2e52006-01-06 00:20:16 -08005404
NeilBrownd562b0c2009-03-31 14:39:39 +11005405
NeilBrownfd01b882011-10-11 16:47:53 +11005406static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11005407{
NeilBrowne373ab12011-10-11 16:48:59 +11005408 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07005409 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11005410
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005411 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11005412 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10005413 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
5414 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005415 return ERR_PTR(-EINVAL);
5416 }
5417
NeilBrowne373ab12011-10-11 16:48:59 +11005418 sectors = raid0_conf->strip_zone[0].zone_end;
5419 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10005420 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005421 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11005422 mddev->new_layout = ALGORITHM_PARITY_N;
5423 mddev->new_chunk_sectors = mddev->chunk_sectors;
5424 mddev->raid_disks += 1;
5425 mddev->delta_disks = 1;
5426 /* make sure it will be not marked as dirty */
5427 mddev->recovery_cp = MaxSector;
5428
5429 return setup_conf(mddev);
5430}
5431
5432
NeilBrownfd01b882011-10-11 16:47:53 +11005433static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005434{
5435 int chunksect;
5436
5437 if (mddev->raid_disks != 2 ||
5438 mddev->degraded > 1)
5439 return ERR_PTR(-EINVAL);
5440
5441 /* Should check if there are write-behind devices? */
5442
5443 chunksect = 64*2; /* 64K by default */
5444
5445 /* The array must be an exact multiple of chunksize */
5446 while (chunksect && (mddev->array_sectors & (chunksect-1)))
5447 chunksect >>= 1;
5448
5449 if ((chunksect<<9) < STRIPE_SIZE)
5450 /* array size does not allow a suitable chunk size */
5451 return ERR_PTR(-EINVAL);
5452
5453 mddev->new_level = 5;
5454 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10005455 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11005456
5457 return setup_conf(mddev);
5458}
5459
NeilBrownfd01b882011-10-11 16:47:53 +11005460static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11005461{
5462 int new_layout;
5463
5464 switch (mddev->layout) {
5465 case ALGORITHM_LEFT_ASYMMETRIC_6:
5466 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
5467 break;
5468 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5469 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
5470 break;
5471 case ALGORITHM_LEFT_SYMMETRIC_6:
5472 new_layout = ALGORITHM_LEFT_SYMMETRIC;
5473 break;
5474 case ALGORITHM_RIGHT_SYMMETRIC_6:
5475 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
5476 break;
5477 case ALGORITHM_PARITY_0_6:
5478 new_layout = ALGORITHM_PARITY_0;
5479 break;
5480 case ALGORITHM_PARITY_N:
5481 new_layout = ALGORITHM_PARITY_N;
5482 break;
5483 default:
5484 return ERR_PTR(-EINVAL);
5485 }
5486 mddev->new_level = 5;
5487 mddev->new_layout = new_layout;
5488 mddev->delta_disks = -1;
5489 mddev->raid_disks -= 1;
5490 return setup_conf(mddev);
5491}
5492
NeilBrownd562b0c2009-03-31 14:39:39 +11005493
NeilBrownfd01b882011-10-11 16:47:53 +11005494static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11005495{
NeilBrown88ce4932009-03-31 15:24:23 +11005496 /* For a 2-drive array, the layout and chunk size can be changed
5497 * immediately as not restriping is needed.
5498 * For larger arrays we record the new value - after validation
5499 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11005500 */
NeilBrown070ec552009-06-16 16:54:21 +10005501 raid5_conf_t *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10005502 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11005503
NeilBrown597a7112009-06-18 08:47:42 +10005504 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11005505 return -EINVAL;
5506 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005507 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11005508 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005509 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11005510 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005511 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11005512 /* not factor of array size */
5513 return -EINVAL;
5514 }
5515
5516 /* They look valid */
5517
NeilBrown88ce4932009-03-31 15:24:23 +11005518 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10005519 /* can make the change immediately */
5520 if (mddev->new_layout >= 0) {
5521 conf->algorithm = mddev->new_layout;
5522 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11005523 }
5524 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10005525 conf->chunk_sectors = new_chunk ;
5526 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11005527 }
5528 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5529 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11005530 }
NeilBrown50ac1682009-06-18 08:47:55 +10005531 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11005532}
5533
NeilBrownfd01b882011-10-11 16:47:53 +11005534static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11005535{
NeilBrown597a7112009-06-18 08:47:42 +10005536 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10005537
NeilBrown597a7112009-06-18 08:47:42 +10005538 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11005539 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005540 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005541 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11005542 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005543 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11005544 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005545 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11005546 /* not factor of array size */
5547 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005548 }
NeilBrown88ce4932009-03-31 15:24:23 +11005549
5550 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10005551 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11005552}
5553
NeilBrownfd01b882011-10-11 16:47:53 +11005554static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005555{
5556 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005557 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005558 * raid1 - if there are two drives. We need to know the chunk size
5559 * raid4 - trivial - just use a raid4 layout.
5560 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005561 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005562 if (mddev->level == 0)
5563 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11005564 if (mddev->level == 1)
5565 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11005566 if (mddev->level == 4) {
5567 mddev->new_layout = ALGORITHM_PARITY_N;
5568 mddev->new_level = 5;
5569 return setup_conf(mddev);
5570 }
NeilBrownfc9739c2009-03-31 14:57:20 +11005571 if (mddev->level == 6)
5572 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11005573
5574 return ERR_PTR(-EINVAL);
5575}
5576
NeilBrownfd01b882011-10-11 16:47:53 +11005577static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11005578{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005579 /* raid4 can take over:
5580 * raid0 - if there is only one strip zone
5581 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11005582 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005583 if (mddev->level == 0)
5584 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11005585 if (mddev->level == 5 &&
5586 mddev->layout == ALGORITHM_PARITY_N) {
5587 mddev->new_layout = 0;
5588 mddev->new_level = 4;
5589 return setup_conf(mddev);
5590 }
5591 return ERR_PTR(-EINVAL);
5592}
NeilBrownd562b0c2009-03-31 14:39:39 +11005593
NeilBrown245f46c2009-03-31 14:39:39 +11005594static struct mdk_personality raid5_personality;
5595
NeilBrownfd01b882011-10-11 16:47:53 +11005596static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11005597{
5598 /* Currently can only take over a raid5. We map the
5599 * personality to an equivalent raid6 personality
5600 * with the Q block at the end.
5601 */
5602 int new_layout;
5603
5604 if (mddev->pers != &raid5_personality)
5605 return ERR_PTR(-EINVAL);
5606 if (mddev->degraded > 1)
5607 return ERR_PTR(-EINVAL);
5608 if (mddev->raid_disks > 253)
5609 return ERR_PTR(-EINVAL);
5610 if (mddev->raid_disks < 3)
5611 return ERR_PTR(-EINVAL);
5612
5613 switch (mddev->layout) {
5614 case ALGORITHM_LEFT_ASYMMETRIC:
5615 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
5616 break;
5617 case ALGORITHM_RIGHT_ASYMMETRIC:
5618 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
5619 break;
5620 case ALGORITHM_LEFT_SYMMETRIC:
5621 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
5622 break;
5623 case ALGORITHM_RIGHT_SYMMETRIC:
5624 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
5625 break;
5626 case ALGORITHM_PARITY_0:
5627 new_layout = ALGORITHM_PARITY_0_6;
5628 break;
5629 case ALGORITHM_PARITY_N:
5630 new_layout = ALGORITHM_PARITY_N;
5631 break;
5632 default:
5633 return ERR_PTR(-EINVAL);
5634 }
5635 mddev->new_level = 6;
5636 mddev->new_layout = new_layout;
5637 mddev->delta_disks = 1;
5638 mddev->raid_disks += 1;
5639 return setup_conf(mddev);
5640}
5641
5642
NeilBrown16a53ec2006-06-26 00:27:38 -07005643static struct mdk_personality raid6_personality =
5644{
5645 .name = "raid6",
5646 .level = 6,
5647 .owner = THIS_MODULE,
5648 .make_request = make_request,
5649 .run = run,
5650 .stop = stop,
5651 .status = status,
5652 .error_handler = error,
5653 .hot_add_disk = raid5_add_disk,
5654 .hot_remove_disk= raid5_remove_disk,
5655 .spare_active = raid5_spare_active,
5656 .sync_request = sync_request,
5657 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005658 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10005659 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08005660 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005661 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07005662 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11005663 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07005664};
NeilBrown2604b702006-01-06 00:20:36 -08005665static struct mdk_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666{
5667 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08005668 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 .owner = THIS_MODULE,
5670 .make_request = make_request,
5671 .run = run,
5672 .stop = stop,
5673 .status = status,
5674 .error_handler = error,
5675 .hot_add_disk = raid5_add_disk,
5676 .hot_remove_disk= raid5_remove_disk,
5677 .spare_active = raid5_spare_active,
5678 .sync_request = sync_request,
5679 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005680 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08005681 .check_reshape = raid5_check_reshape,
5682 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005683 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07005684 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11005685 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686};
5687
NeilBrown2604b702006-01-06 00:20:36 -08005688static struct mdk_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689{
NeilBrown2604b702006-01-06 00:20:36 -08005690 .name = "raid4",
5691 .level = 4,
5692 .owner = THIS_MODULE,
5693 .make_request = make_request,
5694 .run = run,
5695 .stop = stop,
5696 .status = status,
5697 .error_handler = error,
5698 .hot_add_disk = raid5_add_disk,
5699 .hot_remove_disk= raid5_remove_disk,
5700 .spare_active = raid5_spare_active,
5701 .sync_request = sync_request,
5702 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005703 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08005704 .check_reshape = raid5_check_reshape,
5705 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005706 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08005707 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11005708 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08005709};
5710
5711static int __init raid5_init(void)
5712{
NeilBrown16a53ec2006-06-26 00:27:38 -07005713 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08005714 register_md_personality(&raid5_personality);
5715 register_md_personality(&raid4_personality);
5716 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717}
5718
NeilBrown2604b702006-01-06 00:20:36 -08005719static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720{
NeilBrown16a53ec2006-06-26 00:27:38 -07005721 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08005722 unregister_md_personality(&raid5_personality);
5723 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724}
5725
5726module_init(raid5_init);
5727module_exit(raid5_exit);
5728MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11005729MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08005731MODULE_ALIAS("md-raid5");
5732MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08005733MODULE_ALIAS("md-level-5");
5734MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07005735MODULE_ALIAS("md-personality-8"); /* RAID6 */
5736MODULE_ALIAS("md-raid6");
5737MODULE_ALIAS("md-level-6");
5738
5739/* This used to be two separate modules, they were: */
5740MODULE_ALIAS("raid5");
5741MODULE_ALIAS("raid6");