blob: 01163c81e740144152d84608f7080db4a26002be [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/*
97 * The following can be used to debug the driver
98 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#define RAID5_PARANOIA 1
100#if RAID5_PARANOIA && defined(CONFIG_SMP)
101# define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
102#else
103# define CHECK_DEVLOCK()
104#endif
105
Dan Williams45b42332007-07-09 11:56:43 -0700106#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#define inline
108#define __inline__
109#endif
110
Jens Axboe960e7392008-08-15 10:41:18 +0200111/*
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200112 * We maintain a biased count of active stripes in the bottom 16 bits of
113 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
Jens Axboe960e7392008-08-15 10:41:18 +0200114 */
115static inline int raid5_bi_phys_segments(struct bio *bio)
116{
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200117 return bio->bi_phys_segments & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200118}
119
120static inline int raid5_bi_hw_segments(struct bio *bio)
121{
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200122 return (bio->bi_phys_segments >> 16) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200123}
124
125static inline int raid5_dec_bi_phys_segments(struct bio *bio)
126{
127 --bio->bi_phys_segments;
128 return raid5_bi_phys_segments(bio);
129}
130
131static inline int raid5_dec_bi_hw_segments(struct bio *bio)
132{
133 unsigned short val = raid5_bi_hw_segments(bio);
134
135 --val;
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200136 bio->bi_phys_segments = (val << 16) | raid5_bi_phys_segments(bio);
Jens Axboe960e7392008-08-15 10:41:18 +0200137 return val;
138}
139
140static inline void raid5_set_bi_hw_segments(struct bio *bio, unsigned int cnt)
141{
Namhyung Kim9b2dc8b2011-06-13 14:48:22 +0900142 bio->bi_phys_segments = raid5_bi_phys_segments(bio) | (cnt << 16);
Jens Axboe960e7392008-08-15 10:41:18 +0200143}
144
NeilBrownd0dabf72009-03-31 14:39:38 +1100145/* Find first data disk in a raid6 stripe */
146static inline int raid6_d0(struct stripe_head *sh)
147{
NeilBrown67cc2b82009-03-31 14:39:38 +1100148 if (sh->ddf_layout)
149 /* ddf always start from first device */
150 return 0;
151 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100152 if (sh->qd_idx == sh->disks - 1)
153 return 0;
154 else
155 return sh->qd_idx + 1;
156}
NeilBrown16a53ec2006-06-26 00:27:38 -0700157static inline int raid6_next_disk(int disk, int raid_disks)
158{
159 disk++;
160 return (disk < raid_disks) ? disk : 0;
161}
Dan Williamsa4456852007-07-09 11:56:43 -0700162
NeilBrownd0dabf72009-03-31 14:39:38 +1100163/* When walking through the disks in a raid5, starting at raid6_d0,
164 * We need to map each disk to a 'slot', where the data disks are slot
165 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
166 * is raid_disks-1. This help does that mapping.
167 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100168static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
169 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100170{
Dan Williams66295422009-10-19 18:09:32 -0700171 int slot = *count;
NeilBrown67cc2b82009-03-31 14:39:38 +1100172
NeilBrowne4424fe2009-10-16 16:27:34 +1100173 if (sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700174 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100175 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100176 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100177 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100178 return syndrome_disks + 1;
NeilBrowne4424fe2009-10-16 16:27:34 +1100179 if (!sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700180 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100181 return slot;
182}
183
Dan Williamsa4456852007-07-09 11:56:43 -0700184static void return_io(struct bio *return_bi)
185{
186 struct bio *bi = return_bi;
187 while (bi) {
Dan Williamsa4456852007-07-09 11:56:43 -0700188
189 return_bi = bi->bi_next;
190 bi->bi_next = NULL;
191 bi->bi_size = 0;
Neil Brown0e13fe232008-06-28 08:31:20 +1000192 bio_endio(bi, 0);
Dan Williamsa4456852007-07-09 11:56:43 -0700193 bi = return_bi;
194 }
195}
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197static void print_raid5_conf (raid5_conf_t *conf);
198
Dan Williams600aa102008-06-28 08:32:05 +1000199static int stripe_operations_active(struct stripe_head *sh)
200{
201 return sh->check_state || sh->reconstruct_state ||
202 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
203 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
204}
205
Arjan van de Ven858119e2006-01-14 13:20:43 -0800206static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208 if (atomic_dec_and_test(&sh->count)) {
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200209 BUG_ON(!list_empty(&sh->lru));
210 BUG_ON(atomic_read(&conf->active_stripes)==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (test_bit(STRIPE_HANDLE, &sh->state)) {
NeilBrown482c0832011-04-18 18:25:42 +1000212 if (test_bit(STRIPE_DELAYED, &sh->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 list_add_tail(&sh->lru, &conf->delayed_list);
NeilBrown482c0832011-04-18 18:25:42 +1000214 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
215 sh->bm_seq - conf->seq_write > 0)
NeilBrown72626682005-09-09 16:23:54 -0700216 list_add_tail(&sh->lru, &conf->bitmap_list);
NeilBrown482c0832011-04-18 18:25:42 +1000217 else {
NeilBrown72626682005-09-09 16:23:54 -0700218 clear_bit(STRIPE_BIT_DELAY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 list_add_tail(&sh->lru, &conf->handle_list);
NeilBrown72626682005-09-09 16:23:54 -0700220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 md_wakeup_thread(conf->mddev->thread);
222 } else {
Dan Williams600aa102008-06-28 08:32:05 +1000223 BUG_ON(stripe_operations_active(sh));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
225 atomic_dec(&conf->preread_active_stripes);
226 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
227 md_wakeup_thread(conf->mddev->thread);
228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 atomic_dec(&conf->active_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800230 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
231 list_add_tail(&sh->lru, &conf->inactive_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 wake_up(&conf->wait_for_stripe);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -0800233 if (conf->retry_read_aligned)
234 md_wakeup_thread(conf->mddev->thread);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
237 }
238}
NeilBrownd0dabf72009-03-31 14:39:38 +1100239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240static void release_stripe(struct stripe_head *sh)
241{
242 raid5_conf_t *conf = sh->raid_conf;
243 unsigned long flags;
NeilBrown16a53ec2006-06-26 00:27:38 -0700244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 spin_lock_irqsave(&conf->device_lock, flags);
246 __release_stripe(conf, sh);
247 spin_unlock_irqrestore(&conf->device_lock, flags);
248}
249
NeilBrownfccddba2006-01-06 00:20:33 -0800250static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Dan Williams45b42332007-07-09 11:56:43 -0700252 pr_debug("remove_hash(), stripe %llu\n",
253 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
NeilBrownfccddba2006-01-06 00:20:33 -0800255 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
NeilBrown16a53ec2006-06-26 00:27:38 -0700258static inline void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
NeilBrownfccddba2006-01-06 00:20:33 -0800260 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Dan Williams45b42332007-07-09 11:56:43 -0700262 pr_debug("insert_hash(), stripe %llu\n",
263 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 CHECK_DEVLOCK();
NeilBrownfccddba2006-01-06 00:20:33 -0800266 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
269
270/* find an idle stripe, make sure it is unhashed, and return it. */
271static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
272{
273 struct stripe_head *sh = NULL;
274 struct list_head *first;
275
276 CHECK_DEVLOCK();
277 if (list_empty(&conf->inactive_list))
278 goto out;
279 first = conf->inactive_list.next;
280 sh = list_entry(first, struct stripe_head, lru);
281 list_del_init(first);
282 remove_hash(sh);
283 atomic_inc(&conf->active_stripes);
284out:
285 return sh;
286}
287
NeilBrowne4e11e32010-06-16 16:45:16 +1000288static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 struct page *p;
291 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000292 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
NeilBrowne4e11e32010-06-16 16:45:16 +1000294 for (i = 0; i < num ; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 p = sh->dev[i].page;
296 if (!p)
297 continue;
298 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800299 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
301}
302
NeilBrowne4e11e32010-06-16 16:45:16 +1000303static int grow_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
305 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000306 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
NeilBrowne4e11e32010-06-16 16:45:16 +1000308 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 struct page *page;
310
311 if (!(page = alloc_page(GFP_KERNEL))) {
312 return 1;
313 }
314 sh->dev[i].page = page;
315 }
316 return 0;
317}
318
NeilBrown784052e2009-03-31 15:19:07 +1100319static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrown911d4ee2009-03-31 14:39:38 +1100320static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
321 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
NeilBrownb5663ba2009-03-31 14:39:38 +1100323static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800326 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200328 BUG_ON(atomic_read(&sh->count) != 0);
329 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000330 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 CHECK_DEVLOCK();
Dan Williams45b42332007-07-09 11:56:43 -0700333 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 (unsigned long long)sh->sector);
335
336 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700337
NeilBrown86b42c72009-03-31 15:19:03 +1100338 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100339 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100341 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 sh->state = 0;
343
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800344
345 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 struct r5dev *dev = &sh->dev[i];
347
Dan Williamsd84e0f12007-01-02 13:52:30 -0700348 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700350 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700352 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000354 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
356 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100357 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
359 insert_hash(conf, sh);
360}
361
NeilBrown86b42c72009-03-31 15:19:03 +1100362static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector,
363 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
365 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -0800366 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 CHECK_DEVLOCK();
Dan Williams45b42332007-07-09 11:56:43 -0700369 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
NeilBrownfccddba2006-01-06 00:20:33 -0800370 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100371 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700373 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return NULL;
375}
376
NeilBrown674806d2010-06-16 17:17:53 +1000377/*
378 * Need to check if array has failed when deciding whether to:
379 * - start an array
380 * - remove non-faulty devices
381 * - add a spare
382 * - allow a reshape
383 * This determination is simple when no reshape is happening.
384 * However if there is a reshape, we need to carefully check
385 * both the before and after sections.
386 * This is because some failed devices may only affect one
387 * of the two sections, and some non-in_sync devices may
388 * be insync in the section most affected by failed devices.
389 */
390static int has_failed(raid5_conf_t *conf)
391{
392 int degraded;
393 int i;
394 if (conf->mddev->reshape_position == MaxSector)
395 return conf->mddev->degraded > conf->max_degraded;
396
397 rcu_read_lock();
398 degraded = 0;
399 for (i = 0; i < conf->previous_raid_disks; i++) {
400 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
401 if (!rdev || test_bit(Faulty, &rdev->flags))
402 degraded++;
403 else if (test_bit(In_sync, &rdev->flags))
404 ;
405 else
406 /* not in-sync or faulty.
407 * If the reshape increases the number of devices,
408 * this is being recovered by the reshape, so
409 * this 'previous' section is not in_sync.
410 * If the number of devices is being reduced however,
411 * the device can only be part of the array if
412 * we are reverting a reshape, so this section will
413 * be in-sync.
414 */
415 if (conf->raid_disks >= conf->previous_raid_disks)
416 degraded++;
417 }
418 rcu_read_unlock();
419 if (degraded > conf->max_degraded)
420 return 1;
421 rcu_read_lock();
422 degraded = 0;
423 for (i = 0; i < conf->raid_disks; i++) {
424 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
425 if (!rdev || test_bit(Faulty, &rdev->flags))
426 degraded++;
427 else if (test_bit(In_sync, &rdev->flags))
428 ;
429 else
430 /* not in-sync or faulty.
431 * If reshape increases the number of devices, this
432 * section has already been recovered, else it
433 * almost certainly hasn't.
434 */
435 if (conf->raid_disks <= conf->previous_raid_disks)
436 degraded++;
437 }
438 rcu_read_unlock();
439 if (degraded > conf->max_degraded)
440 return 1;
441 return 0;
442}
443
NeilBrownb5663ba2009-03-31 14:39:38 +1100444static struct stripe_head *
445get_active_stripe(raid5_conf_t *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000446 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
448 struct stripe_head *sh;
449
Dan Williams45b42332007-07-09 11:56:43 -0700450 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 spin_lock_irq(&conf->device_lock);
453
454 do {
NeilBrown72626682005-09-09 16:23:54 -0700455 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000456 conf->quiesce == 0 || noquiesce,
NeilBrown72626682005-09-09 16:23:54 -0700457 conf->device_lock, /* nothing */);
NeilBrown86b42c72009-03-31 15:19:03 +1100458 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (!sh) {
460 if (!conf->inactive_blocked)
461 sh = get_free_stripe(conf);
462 if (noblock && sh == NULL)
463 break;
464 if (!sh) {
465 conf->inactive_blocked = 1;
466 wait_event_lock_irq(conf->wait_for_stripe,
467 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800468 (atomic_read(&conf->active_stripes)
469 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 || !conf->inactive_blocked),
471 conf->device_lock,
NeilBrown7c13edc2011-04-18 18:25:43 +1000472 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 conf->inactive_blocked = 0;
474 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100475 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 } else {
477 if (atomic_read(&sh->count)) {
NeilBrownab69ae12009-03-31 15:26:47 +1100478 BUG_ON(!list_empty(&sh->lru)
479 && !test_bit(STRIPE_EXPANDING, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 } else {
481 if (!test_bit(STRIPE_HANDLE, &sh->state))
482 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700483 if (list_empty(&sh->lru) &&
484 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700485 BUG();
486 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
488 }
489 } while (sh == NULL);
490
491 if (sh)
492 atomic_inc(&sh->count);
493
494 spin_unlock_irq(&conf->device_lock);
495 return sh;
496}
497
NeilBrown6712ecf2007-09-27 12:47:43 +0200498static void
499raid5_end_read_request(struct bio *bi, int error);
500static void
501raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700502
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000503static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700504{
505 raid5_conf_t *conf = sh->raid_conf;
506 int i, disks = sh->disks;
507
508 might_sleep();
509
510 for (i = disks; i--; ) {
511 int rw;
512 struct bio *bi;
513 mdk_rdev_t *rdev;
Tejun Heoe9c74692010-09-03 11:56:18 +0200514 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
515 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
516 rw = WRITE_FUA;
517 else
518 rw = WRITE;
519 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700520 rw = READ;
521 else
522 continue;
523
524 bi = &sh->dev[i].req;
525
526 bi->bi_rw = rw;
Namhyung Kimb0629622011-06-14 14:20:19 +1000527 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700528 bi->bi_end_io = raid5_end_write_request;
529 else
530 bi->bi_end_io = raid5_end_read_request;
531
532 rcu_read_lock();
533 rdev = rcu_dereference(conf->disks[i].rdev);
534 if (rdev && test_bit(Faulty, &rdev->flags))
535 rdev = NULL;
536 if (rdev)
537 atomic_inc(&rdev->nr_pending);
538 rcu_read_unlock();
539
NeilBrown73e92e52011-07-28 11:39:22 +1000540 /* We have already checked bad blocks for reads. Now
541 * need to check for writes.
542 */
543 while ((rw & WRITE) && rdev &&
544 test_bit(WriteErrorSeen, &rdev->flags)) {
545 sector_t first_bad;
546 int bad_sectors;
547 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
548 &first_bad, &bad_sectors);
549 if (!bad)
550 break;
551
552 if (bad < 0) {
553 set_bit(BlockedBadBlocks, &rdev->flags);
554 if (!conf->mddev->external &&
555 conf->mddev->flags) {
556 /* It is very unlikely, but we might
557 * still need to write out the
558 * bad block log - better give it
559 * a chance*/
560 md_check_recovery(conf->mddev);
561 }
562 md_wait_for_blocked_rdev(rdev, conf->mddev);
563 } else {
564 /* Acknowledged bad block - skip the write */
565 rdev_dec_pending(rdev, conf->mddev);
566 rdev = NULL;
567 }
568 }
569
Dan Williams91c00922007-01-02 13:52:30 -0700570 if (rdev) {
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000571 if (s->syncing || s->expanding || s->expanded)
Dan Williams91c00922007-01-02 13:52:30 -0700572 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
573
Dan Williams2b7497f2008-06-28 08:31:52 +1000574 set_bit(STRIPE_IO_STARTED, &sh->state);
575
Dan Williams91c00922007-01-02 13:52:30 -0700576 bi->bi_bdev = rdev->bdev;
577 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700578 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700579 bi->bi_rw, i);
580 atomic_inc(&sh->count);
581 bi->bi_sector = sh->sector + rdev->data_offset;
582 bi->bi_flags = 1 << BIO_UPTODATE;
583 bi->bi_vcnt = 1;
584 bi->bi_max_vecs = 1;
585 bi->bi_idx = 0;
586 bi->bi_io_vec = &sh->dev[i].vec;
587 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
588 bi->bi_io_vec[0].bv_offset = 0;
589 bi->bi_size = STRIPE_SIZE;
590 bi->bi_next = NULL;
Dan Williams91c00922007-01-02 13:52:30 -0700591 generic_make_request(bi);
592 } else {
Namhyung Kimb0629622011-06-14 14:20:19 +1000593 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700594 set_bit(STRIPE_DEGRADED, &sh->state);
595 pr_debug("skip op %ld on disc %d for sector %llu\n",
596 bi->bi_rw, i, (unsigned long long)sh->sector);
597 clear_bit(R5_LOCKED, &sh->dev[i].flags);
598 set_bit(STRIPE_HANDLE, &sh->state);
599 }
600 }
601}
602
603static struct dma_async_tx_descriptor *
604async_copy_data(int frombio, struct bio *bio, struct page *page,
605 sector_t sector, struct dma_async_tx_descriptor *tx)
606{
607 struct bio_vec *bvl;
608 struct page *bio_page;
609 int i;
610 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700611 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700612 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700613
614 if (bio->bi_sector >= sector)
615 page_offset = (signed)(bio->bi_sector - sector) * 512;
616 else
617 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700618
Dan Williams0403e382009-09-08 17:42:50 -0700619 if (frombio)
620 flags |= ASYNC_TX_FENCE;
621 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
622
Dan Williams91c00922007-01-02 13:52:30 -0700623 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000624 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700625 int clen;
626 int b_offset = 0;
627
628 if (page_offset < 0) {
629 b_offset = -page_offset;
630 page_offset += b_offset;
631 len -= b_offset;
632 }
633
634 if (len > 0 && page_offset + len > STRIPE_SIZE)
635 clen = STRIPE_SIZE - page_offset;
636 else
637 clen = len;
638
639 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000640 b_offset += bvl->bv_offset;
641 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700642 if (frombio)
643 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700644 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700645 else
646 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700647 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700648 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700649 /* chain the operations */
650 submit.depend_tx = tx;
651
Dan Williams91c00922007-01-02 13:52:30 -0700652 if (clen < len) /* hit end of page */
653 break;
654 page_offset += len;
655 }
656
657 return tx;
658}
659
660static void ops_complete_biofill(void *stripe_head_ref)
661{
662 struct stripe_head *sh = stripe_head_ref;
663 struct bio *return_bi = NULL;
664 raid5_conf_t *conf = sh->raid_conf;
Dan Williamse4d84902007-09-24 10:06:13 -0700665 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700666
Harvey Harrisone46b2722008-04-28 02:15:50 -0700667 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700668 (unsigned long long)sh->sector);
669
670 /* clear completed biofills */
Dan Williams83de75c2008-06-28 08:31:58 +1000671 spin_lock_irq(&conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700672 for (i = sh->disks; i--; ) {
673 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700674
675 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700676 /* and check if we need to reply to a read request,
677 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000678 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700679 */
680 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700681 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700682
Dan Williams91c00922007-01-02 13:52:30 -0700683 BUG_ON(!dev->read);
684 rbi = dev->read;
685 dev->read = NULL;
686 while (rbi && rbi->bi_sector <
687 dev->sector + STRIPE_SECTORS) {
688 rbi2 = r5_next_bio(rbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +0200689 if (!raid5_dec_bi_phys_segments(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700690 rbi->bi_next = return_bi;
691 return_bi = rbi;
692 }
Dan Williams91c00922007-01-02 13:52:30 -0700693 rbi = rbi2;
694 }
695 }
696 }
Dan Williams83de75c2008-06-28 08:31:58 +1000697 spin_unlock_irq(&conf->device_lock);
698 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700699
700 return_io(return_bi);
701
Dan Williamse4d84902007-09-24 10:06:13 -0700702 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700703 release_stripe(sh);
704}
705
706static void ops_run_biofill(struct stripe_head *sh)
707{
708 struct dma_async_tx_descriptor *tx = NULL;
709 raid5_conf_t *conf = sh->raid_conf;
Dan Williamsa08abd82009-06-03 11:43:59 -0700710 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700711 int i;
712
Harvey Harrisone46b2722008-04-28 02:15:50 -0700713 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700714 (unsigned long long)sh->sector);
715
716 for (i = sh->disks; i--; ) {
717 struct r5dev *dev = &sh->dev[i];
718 if (test_bit(R5_Wantfill, &dev->flags)) {
719 struct bio *rbi;
720 spin_lock_irq(&conf->device_lock);
721 dev->read = rbi = dev->toread;
722 dev->toread = NULL;
723 spin_unlock_irq(&conf->device_lock);
724 while (rbi && rbi->bi_sector <
725 dev->sector + STRIPE_SECTORS) {
726 tx = async_copy_data(0, rbi, dev->page,
727 dev->sector, tx);
728 rbi = r5_next_bio(rbi, dev->sector);
729 }
730 }
731 }
732
733 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700734 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
735 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700736}
737
Dan Williams4e7d2c02009-08-29 19:13:11 -0700738static void mark_target_uptodate(struct stripe_head *sh, int target)
739{
740 struct r5dev *tgt;
741
742 if (target < 0)
743 return;
744
745 tgt = &sh->dev[target];
746 set_bit(R5_UPTODATE, &tgt->flags);
747 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
748 clear_bit(R5_Wantcompute, &tgt->flags);
749}
750
Dan Williamsac6b53b2009-07-14 13:40:19 -0700751static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700752{
753 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700754
Harvey Harrisone46b2722008-04-28 02:15:50 -0700755 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700756 (unsigned long long)sh->sector);
757
Dan Williamsac6b53b2009-07-14 13:40:19 -0700758 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700759 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700760 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700761
Dan Williamsecc65c92008-06-28 08:31:57 +1000762 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
763 if (sh->check_state == check_state_compute_run)
764 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700765 set_bit(STRIPE_HANDLE, &sh->state);
766 release_stripe(sh);
767}
768
Dan Williamsd6f38f32009-07-14 11:50:52 -0700769/* return a pointer to the address conversion region of the scribble buffer */
770static addr_conv_t *to_addr_conv(struct stripe_head *sh,
771 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700772{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700773 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
774}
775
776static struct dma_async_tx_descriptor *
777ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
778{
Dan Williams91c00922007-01-02 13:52:30 -0700779 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700780 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700781 int target = sh->ops.target;
782 struct r5dev *tgt = &sh->dev[target];
783 struct page *xor_dest = tgt->page;
784 int count = 0;
785 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700786 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700787 int i;
788
789 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700790 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700791 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
792
793 for (i = disks; i--; )
794 if (i != target)
795 xor_srcs[count++] = sh->dev[i].page;
796
797 atomic_inc(&sh->count);
798
Dan Williams0403e382009-09-08 17:42:50 -0700799 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700800 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700801 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700802 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700803 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700804 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700805
Dan Williams91c00922007-01-02 13:52:30 -0700806 return tx;
807}
808
Dan Williamsac6b53b2009-07-14 13:40:19 -0700809/* set_syndrome_sources - populate source buffers for gen_syndrome
810 * @srcs - (struct page *) array of size sh->disks
811 * @sh - stripe_head to parse
812 *
813 * Populates srcs in proper layout order for the stripe and returns the
814 * 'count' of sources to be used in a call to async_gen_syndrome. The P
815 * destination buffer is recorded in srcs[count] and the Q destination
816 * is recorded in srcs[count+1]].
817 */
818static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
819{
820 int disks = sh->disks;
821 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
822 int d0_idx = raid6_d0(sh);
823 int count;
824 int i;
825
826 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100827 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700828
829 count = 0;
830 i = d0_idx;
831 do {
832 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
833
834 srcs[slot] = sh->dev[i].page;
835 i = raid6_next_disk(i, disks);
836 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700837
NeilBrowne4424fe2009-10-16 16:27:34 +1100838 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700839}
840
841static struct dma_async_tx_descriptor *
842ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
843{
844 int disks = sh->disks;
845 struct page **blocks = percpu->scribble;
846 int target;
847 int qd_idx = sh->qd_idx;
848 struct dma_async_tx_descriptor *tx;
849 struct async_submit_ctl submit;
850 struct r5dev *tgt;
851 struct page *dest;
852 int i;
853 int count;
854
855 if (sh->ops.target < 0)
856 target = sh->ops.target2;
857 else if (sh->ops.target2 < 0)
858 target = sh->ops.target;
859 else
860 /* we should only have one valid target */
861 BUG();
862 BUG_ON(target < 0);
863 pr_debug("%s: stripe %llu block: %d\n",
864 __func__, (unsigned long long)sh->sector, target);
865
866 tgt = &sh->dev[target];
867 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
868 dest = tgt->page;
869
870 atomic_inc(&sh->count);
871
872 if (target == qd_idx) {
873 count = set_syndrome_sources(blocks, sh);
874 blocks[count] = NULL; /* regenerating p is not necessary */
875 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700876 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
877 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700878 to_addr_conv(sh, percpu));
879 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
880 } else {
881 /* Compute any data- or p-drive using XOR */
882 count = 0;
883 for (i = disks; i-- ; ) {
884 if (i == target || i == qd_idx)
885 continue;
886 blocks[count++] = sh->dev[i].page;
887 }
888
Dan Williams0403e382009-09-08 17:42:50 -0700889 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
890 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700891 to_addr_conv(sh, percpu));
892 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
893 }
894
895 return tx;
896}
897
898static struct dma_async_tx_descriptor *
899ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
900{
901 int i, count, disks = sh->disks;
902 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
903 int d0_idx = raid6_d0(sh);
904 int faila = -1, failb = -1;
905 int target = sh->ops.target;
906 int target2 = sh->ops.target2;
907 struct r5dev *tgt = &sh->dev[target];
908 struct r5dev *tgt2 = &sh->dev[target2];
909 struct dma_async_tx_descriptor *tx;
910 struct page **blocks = percpu->scribble;
911 struct async_submit_ctl submit;
912
913 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
914 __func__, (unsigned long long)sh->sector, target, target2);
915 BUG_ON(target < 0 || target2 < 0);
916 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
917 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
918
Dan Williams6c910a72009-09-16 12:24:54 -0700919 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -0700920 * slot number conversion for 'faila' and 'failb'
921 */
922 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100923 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700924 count = 0;
925 i = d0_idx;
926 do {
927 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
928
929 blocks[slot] = sh->dev[i].page;
930
931 if (i == target)
932 faila = slot;
933 if (i == target2)
934 failb = slot;
935 i = raid6_next_disk(i, disks);
936 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700937
938 BUG_ON(faila == failb);
939 if (failb < faila)
940 swap(faila, failb);
941 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
942 __func__, (unsigned long long)sh->sector, faila, failb);
943
944 atomic_inc(&sh->count);
945
946 if (failb == syndrome_disks+1) {
947 /* Q disk is one of the missing disks */
948 if (faila == syndrome_disks) {
949 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -0700950 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
951 ops_complete_compute, sh,
952 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +1100953 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700954 STRIPE_SIZE, &submit);
955 } else {
956 struct page *dest;
957 int data_target;
958 int qd_idx = sh->qd_idx;
959
960 /* Missing D+Q: recompute D from P, then recompute Q */
961 if (target == qd_idx)
962 data_target = target2;
963 else
964 data_target = target;
965
966 count = 0;
967 for (i = disks; i-- ; ) {
968 if (i == data_target || i == qd_idx)
969 continue;
970 blocks[count++] = sh->dev[i].page;
971 }
972 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -0700973 init_async_submit(&submit,
974 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
975 NULL, NULL, NULL,
976 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -0700977 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
978 &submit);
979
980 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -0700981 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
982 ops_complete_compute, sh,
983 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -0700984 return async_gen_syndrome(blocks, 0, count+2,
985 STRIPE_SIZE, &submit);
986 }
Dan Williamsac6b53b2009-07-14 13:40:19 -0700987 } else {
Dan Williams6c910a72009-09-16 12:24:54 -0700988 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
989 ops_complete_compute, sh,
990 to_addr_conv(sh, percpu));
991 if (failb == syndrome_disks) {
992 /* We're missing D+P. */
993 return async_raid6_datap_recov(syndrome_disks+2,
994 STRIPE_SIZE, faila,
995 blocks, &submit);
996 } else {
997 /* We're missing D+D. */
998 return async_raid6_2data_recov(syndrome_disks+2,
999 STRIPE_SIZE, faila, failb,
1000 blocks, &submit);
1001 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001002 }
1003}
1004
1005
Dan Williams91c00922007-01-02 13:52:30 -07001006static void ops_complete_prexor(void *stripe_head_ref)
1007{
1008 struct stripe_head *sh = stripe_head_ref;
1009
Harvey Harrisone46b2722008-04-28 02:15:50 -07001010 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001011 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001012}
1013
1014static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001015ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1016 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001017{
Dan Williams91c00922007-01-02 13:52:30 -07001018 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001019 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001020 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001021 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001022
1023 /* existing parity data subtracted */
1024 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1025
Harvey Harrisone46b2722008-04-28 02:15:50 -07001026 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001027 (unsigned long long)sh->sector);
1028
1029 for (i = disks; i--; ) {
1030 struct r5dev *dev = &sh->dev[i];
1031 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001032 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001033 xor_srcs[count++] = dev->page;
1034 }
1035
Dan Williams0403e382009-09-08 17:42:50 -07001036 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001037 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001038 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001039
1040 return tx;
1041}
1042
1043static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001044ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001045{
1046 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001047 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001048
Harvey Harrisone46b2722008-04-28 02:15:50 -07001049 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001050 (unsigned long long)sh->sector);
1051
1052 for (i = disks; i--; ) {
1053 struct r5dev *dev = &sh->dev[i];
1054 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001055
Dan Williamsd8ee0722008-06-28 08:32:06 +10001056 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001057 struct bio *wbi;
1058
NeilBrowncbe47ec2011-07-26 11:20:35 +10001059 spin_lock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001060 chosen = dev->towrite;
1061 dev->towrite = NULL;
1062 BUG_ON(dev->written);
1063 wbi = dev->written = chosen;
NeilBrowncbe47ec2011-07-26 11:20:35 +10001064 spin_unlock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001065
1066 while (wbi && wbi->bi_sector <
1067 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001068 if (wbi->bi_rw & REQ_FUA)
1069 set_bit(R5_WantFUA, &dev->flags);
Dan Williams91c00922007-01-02 13:52:30 -07001070 tx = async_copy_data(1, wbi, dev->page,
1071 dev->sector, tx);
1072 wbi = r5_next_bio(wbi, dev->sector);
1073 }
1074 }
1075 }
1076
1077 return tx;
1078}
1079
Dan Williamsac6b53b2009-07-14 13:40:19 -07001080static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001081{
1082 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001083 int disks = sh->disks;
1084 int pd_idx = sh->pd_idx;
1085 int qd_idx = sh->qd_idx;
1086 int i;
Tejun Heoe9c74692010-09-03 11:56:18 +02001087 bool fua = false;
Dan Williams91c00922007-01-02 13:52:30 -07001088
Harvey Harrisone46b2722008-04-28 02:15:50 -07001089 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001090 (unsigned long long)sh->sector);
1091
Tejun Heoe9c74692010-09-03 11:56:18 +02001092 for (i = disks; i--; )
1093 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
1094
Dan Williams91c00922007-01-02 13:52:30 -07001095 for (i = disks; i--; ) {
1096 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001097
Tejun Heoe9c74692010-09-03 11:56:18 +02001098 if (dev->written || i == pd_idx || i == qd_idx) {
Dan Williams91c00922007-01-02 13:52:30 -07001099 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001100 if (fua)
1101 set_bit(R5_WantFUA, &dev->flags);
1102 }
Dan Williams91c00922007-01-02 13:52:30 -07001103 }
1104
Dan Williamsd8ee0722008-06-28 08:32:06 +10001105 if (sh->reconstruct_state == reconstruct_state_drain_run)
1106 sh->reconstruct_state = reconstruct_state_drain_result;
1107 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1108 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1109 else {
1110 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1111 sh->reconstruct_state = reconstruct_state_result;
1112 }
Dan Williams91c00922007-01-02 13:52:30 -07001113
1114 set_bit(STRIPE_HANDLE, &sh->state);
1115 release_stripe(sh);
1116}
1117
1118static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001119ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1120 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001121{
Dan Williams91c00922007-01-02 13:52:30 -07001122 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001123 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001124 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001125 int count = 0, pd_idx = sh->pd_idx, i;
1126 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001127 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001128 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001129
Harvey Harrisone46b2722008-04-28 02:15:50 -07001130 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001131 (unsigned long long)sh->sector);
1132
1133 /* check if prexor is active which means only process blocks
1134 * that are part of a read-modify-write (written)
1135 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001136 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1137 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001138 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1139 for (i = disks; i--; ) {
1140 struct r5dev *dev = &sh->dev[i];
1141 if (dev->written)
1142 xor_srcs[count++] = dev->page;
1143 }
1144 } else {
1145 xor_dest = sh->dev[pd_idx].page;
1146 for (i = disks; i--; ) {
1147 struct r5dev *dev = &sh->dev[i];
1148 if (i != pd_idx)
1149 xor_srcs[count++] = dev->page;
1150 }
1151 }
1152
Dan Williams91c00922007-01-02 13:52:30 -07001153 /* 1/ if we prexor'd then the dest is reused as a source
1154 * 2/ if we did not prexor then we are redoing the parity
1155 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1156 * for the synchronous xor case
1157 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001158 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001159 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1160
1161 atomic_inc(&sh->count);
1162
Dan Williamsac6b53b2009-07-14 13:40:19 -07001163 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001164 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001165 if (unlikely(count == 1))
1166 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1167 else
1168 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001169}
1170
Dan Williamsac6b53b2009-07-14 13:40:19 -07001171static void
1172ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1173 struct dma_async_tx_descriptor *tx)
1174{
1175 struct async_submit_ctl submit;
1176 struct page **blocks = percpu->scribble;
1177 int count;
1178
1179 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1180
1181 count = set_syndrome_sources(blocks, sh);
1182
1183 atomic_inc(&sh->count);
1184
1185 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1186 sh, to_addr_conv(sh, percpu));
1187 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001188}
1189
1190static void ops_complete_check(void *stripe_head_ref)
1191{
1192 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001193
Harvey Harrisone46b2722008-04-28 02:15:50 -07001194 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001195 (unsigned long long)sh->sector);
1196
Dan Williamsecc65c92008-06-28 08:31:57 +10001197 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001198 set_bit(STRIPE_HANDLE, &sh->state);
1199 release_stripe(sh);
1200}
1201
Dan Williamsac6b53b2009-07-14 13:40:19 -07001202static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001203{
Dan Williams91c00922007-01-02 13:52:30 -07001204 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001205 int pd_idx = sh->pd_idx;
1206 int qd_idx = sh->qd_idx;
1207 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001208 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001209 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001210 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001211 int count;
1212 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001213
Harvey Harrisone46b2722008-04-28 02:15:50 -07001214 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001215 (unsigned long long)sh->sector);
1216
Dan Williamsac6b53b2009-07-14 13:40:19 -07001217 count = 0;
1218 xor_dest = sh->dev[pd_idx].page;
1219 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001220 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001221 if (i == pd_idx || i == qd_idx)
1222 continue;
1223 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001224 }
1225
Dan Williamsd6f38f32009-07-14 11:50:52 -07001226 init_async_submit(&submit, 0, NULL, NULL, NULL,
1227 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001228 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001229 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001230
Dan Williams91c00922007-01-02 13:52:30 -07001231 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001232 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1233 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001234}
1235
Dan Williamsac6b53b2009-07-14 13:40:19 -07001236static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1237{
1238 struct page **srcs = percpu->scribble;
1239 struct async_submit_ctl submit;
1240 int count;
1241
1242 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1243 (unsigned long long)sh->sector, checkp);
1244
1245 count = set_syndrome_sources(srcs, sh);
1246 if (!checkp)
1247 srcs[count] = NULL;
1248
1249 atomic_inc(&sh->count);
1250 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1251 sh, to_addr_conv(sh, percpu));
1252 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1253 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1254}
1255
Dan Williams417b8d42009-10-16 16:25:22 +11001256static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001257{
1258 int overlap_clear = 0, i, disks = sh->disks;
1259 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001260 raid5_conf_t *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001261 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001262 struct raid5_percpu *percpu;
1263 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001264
Dan Williamsd6f38f32009-07-14 11:50:52 -07001265 cpu = get_cpu();
1266 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001267 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001268 ops_run_biofill(sh);
1269 overlap_clear++;
1270 }
1271
Dan Williams7b3a8712008-06-28 08:32:09 +10001272 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001273 if (level < 6)
1274 tx = ops_run_compute5(sh, percpu);
1275 else {
1276 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1277 tx = ops_run_compute6_1(sh, percpu);
1278 else
1279 tx = ops_run_compute6_2(sh, percpu);
1280 }
1281 /* terminate the chain if reconstruct is not set to be run */
1282 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001283 async_tx_ack(tx);
1284 }
Dan Williams91c00922007-01-02 13:52:30 -07001285
Dan Williams600aa102008-06-28 08:32:05 +10001286 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001287 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001288
Dan Williams600aa102008-06-28 08:32:05 +10001289 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001290 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001291 overlap_clear++;
1292 }
1293
Dan Williamsac6b53b2009-07-14 13:40:19 -07001294 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1295 if (level < 6)
1296 ops_run_reconstruct5(sh, percpu, tx);
1297 else
1298 ops_run_reconstruct6(sh, percpu, tx);
1299 }
Dan Williams91c00922007-01-02 13:52:30 -07001300
Dan Williamsac6b53b2009-07-14 13:40:19 -07001301 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1302 if (sh->check_state == check_state_run)
1303 ops_run_check_p(sh, percpu);
1304 else if (sh->check_state == check_state_run_q)
1305 ops_run_check_pq(sh, percpu, 0);
1306 else if (sh->check_state == check_state_run_pq)
1307 ops_run_check_pq(sh, percpu, 1);
1308 else
1309 BUG();
1310 }
Dan Williams91c00922007-01-02 13:52:30 -07001311
Dan Williams91c00922007-01-02 13:52:30 -07001312 if (overlap_clear)
1313 for (i = disks; i--; ) {
1314 struct r5dev *dev = &sh->dev[i];
1315 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1316 wake_up(&sh->raid_conf->wait_for_overlap);
1317 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001318 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001319}
1320
Dan Williams417b8d42009-10-16 16:25:22 +11001321#ifdef CONFIG_MULTICORE_RAID456
1322static void async_run_ops(void *param, async_cookie_t cookie)
1323{
1324 struct stripe_head *sh = param;
1325 unsigned long ops_request = sh->ops.request;
1326
1327 clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
1328 wake_up(&sh->ops.wait_for_ops);
1329
1330 __raid_run_ops(sh, ops_request);
1331 release_stripe(sh);
1332}
1333
1334static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1335{
1336 /* since handle_stripe can be called outside of raid5d context
1337 * we need to ensure sh->ops.request is de-staged before another
1338 * request arrives
1339 */
1340 wait_event(sh->ops.wait_for_ops,
1341 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
1342 sh->ops.request = ops_request;
1343
1344 atomic_inc(&sh->count);
1345 async_schedule(async_run_ops, sh);
1346}
1347#else
1348#define raid_run_ops __raid_run_ops
1349#endif
1350
NeilBrown3f294f42005-11-08 21:39:25 -08001351static int grow_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
1353 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001354 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001355 if (!sh)
1356 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001357
NeilBrown3f294f42005-11-08 21:39:25 -08001358 sh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001359 #ifdef CONFIG_MULTICORE_RAID456
1360 init_waitqueue_head(&sh->ops.wait_for_ops);
1361 #endif
NeilBrown3f294f42005-11-08 21:39:25 -08001362
NeilBrowne4e11e32010-06-16 16:45:16 +10001363 if (grow_buffers(sh)) {
1364 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001365 kmem_cache_free(conf->slab_cache, sh);
1366 return 0;
1367 }
1368 /* we just created an active stripe so... */
1369 atomic_set(&sh->count, 1);
1370 atomic_inc(&conf->active_stripes);
1371 INIT_LIST_HEAD(&sh->lru);
1372 release_stripe(sh);
1373 return 1;
1374}
1375
1376static int grow_stripes(raid5_conf_t *conf, int num)
1377{
Christoph Lametere18b8902006-12-06 20:33:20 -08001378 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001379 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
NeilBrownf4be6b42010-06-01 19:37:25 +10001381 if (conf->mddev->gendisk)
1382 sprintf(conf->cache_name[0],
1383 "raid%d-%s", conf->level, mdname(conf->mddev));
1384 else
1385 sprintf(conf->cache_name[0],
1386 "raid%d-%p", conf->level, conf->mddev);
1387 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1388
NeilBrownad01c9e2006-03-27 01:18:07 -08001389 conf->active_name = 0;
1390 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001392 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 if (!sc)
1394 return 1;
1395 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001396 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001397 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001398 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 return 0;
1401}
NeilBrown29269552006-03-27 01:18:10 -08001402
Dan Williamsd6f38f32009-07-14 11:50:52 -07001403/**
1404 * scribble_len - return the required size of the scribble region
1405 * @num - total number of disks in the array
1406 *
1407 * The size must be enough to contain:
1408 * 1/ a struct page pointer for each device in the array +2
1409 * 2/ room to convert each entry in (1) to its corresponding dma
1410 * (dma_map_page()) or page (page_address()) address.
1411 *
1412 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1413 * calculate over all devices (not just the data blocks), using zeros in place
1414 * of the P and Q blocks.
1415 */
1416static size_t scribble_len(int num)
1417{
1418 size_t len;
1419
1420 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1421
1422 return len;
1423}
1424
NeilBrownad01c9e2006-03-27 01:18:07 -08001425static int resize_stripes(raid5_conf_t *conf, int newsize)
1426{
1427 /* Make all the stripes able to hold 'newsize' devices.
1428 * New slots in each stripe get 'page' set to a new page.
1429 *
1430 * This happens in stages:
1431 * 1/ create a new kmem_cache and allocate the required number of
1432 * stripe_heads.
1433 * 2/ gather all the old stripe_heads and tranfer the pages across
1434 * to the new stripe_heads. This will have the side effect of
1435 * freezing the array as once all stripe_heads have been collected,
1436 * no IO will be possible. Old stripe heads are freed once their
1437 * pages have been transferred over, and the old kmem_cache is
1438 * freed when all stripes are done.
1439 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1440 * we simple return a failre status - no need to clean anything up.
1441 * 4/ allocate new pages for the new slots in the new stripe_heads.
1442 * If this fails, we don't bother trying the shrink the
1443 * stripe_heads down again, we just leave them as they are.
1444 * As each stripe_head is processed the new one is released into
1445 * active service.
1446 *
1447 * Once step2 is started, we cannot afford to wait for a write,
1448 * so we use GFP_NOIO allocations.
1449 */
1450 struct stripe_head *osh, *nsh;
1451 LIST_HEAD(newstripes);
1452 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001453 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001454 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001455 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001456 int i;
1457
1458 if (newsize <= conf->pool_size)
1459 return 0; /* never bother to shrink */
1460
Dan Williamsb5470dc2008-06-27 21:44:04 -07001461 err = md_allow_write(conf->mddev);
1462 if (err)
1463 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001464
NeilBrownad01c9e2006-03-27 01:18:07 -08001465 /* Step 1 */
1466 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1467 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001468 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001469 if (!sc)
1470 return -ENOMEM;
1471
1472 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001473 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001474 if (!nsh)
1475 break;
1476
NeilBrownad01c9e2006-03-27 01:18:07 -08001477 nsh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001478 #ifdef CONFIG_MULTICORE_RAID456
1479 init_waitqueue_head(&nsh->ops.wait_for_ops);
1480 #endif
NeilBrownad01c9e2006-03-27 01:18:07 -08001481
1482 list_add(&nsh->lru, &newstripes);
1483 }
1484 if (i) {
1485 /* didn't get enough, give up */
1486 while (!list_empty(&newstripes)) {
1487 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1488 list_del(&nsh->lru);
1489 kmem_cache_free(sc, nsh);
1490 }
1491 kmem_cache_destroy(sc);
1492 return -ENOMEM;
1493 }
1494 /* Step 2 - Must use GFP_NOIO now.
1495 * OK, we have enough stripes, start collecting inactive
1496 * stripes and copying them over
1497 */
1498 list_for_each_entry(nsh, &newstripes, lru) {
1499 spin_lock_irq(&conf->device_lock);
1500 wait_event_lock_irq(conf->wait_for_stripe,
1501 !list_empty(&conf->inactive_list),
1502 conf->device_lock,
NeilBrown482c0832011-04-18 18:25:42 +10001503 );
NeilBrownad01c9e2006-03-27 01:18:07 -08001504 osh = get_free_stripe(conf);
1505 spin_unlock_irq(&conf->device_lock);
1506 atomic_set(&nsh->count, 1);
1507 for(i=0; i<conf->pool_size; i++)
1508 nsh->dev[i].page = osh->dev[i].page;
1509 for( ; i<newsize; i++)
1510 nsh->dev[i].page = NULL;
1511 kmem_cache_free(conf->slab_cache, osh);
1512 }
1513 kmem_cache_destroy(conf->slab_cache);
1514
1515 /* Step 3.
1516 * At this point, we are holding all the stripes so the array
1517 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001518 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001519 */
1520 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1521 if (ndisks) {
1522 for (i=0; i<conf->raid_disks; i++)
1523 ndisks[i] = conf->disks[i];
1524 kfree(conf->disks);
1525 conf->disks = ndisks;
1526 } else
1527 err = -ENOMEM;
1528
Dan Williamsd6f38f32009-07-14 11:50:52 -07001529 get_online_cpus();
1530 conf->scribble_len = scribble_len(newsize);
1531 for_each_present_cpu(cpu) {
1532 struct raid5_percpu *percpu;
1533 void *scribble;
1534
1535 percpu = per_cpu_ptr(conf->percpu, cpu);
1536 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1537
1538 if (scribble) {
1539 kfree(percpu->scribble);
1540 percpu->scribble = scribble;
1541 } else {
1542 err = -ENOMEM;
1543 break;
1544 }
1545 }
1546 put_online_cpus();
1547
NeilBrownad01c9e2006-03-27 01:18:07 -08001548 /* Step 4, return new stripes to service */
1549 while(!list_empty(&newstripes)) {
1550 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1551 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001552
NeilBrownad01c9e2006-03-27 01:18:07 -08001553 for (i=conf->raid_disks; i < newsize; i++)
1554 if (nsh->dev[i].page == NULL) {
1555 struct page *p = alloc_page(GFP_NOIO);
1556 nsh->dev[i].page = p;
1557 if (!p)
1558 err = -ENOMEM;
1559 }
1560 release_stripe(nsh);
1561 }
1562 /* critical section pass, GFP_NOIO no longer needed */
1563
1564 conf->slab_cache = sc;
1565 conf->active_name = 1-conf->active_name;
1566 conf->pool_size = newsize;
1567 return err;
1568}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
NeilBrown3f294f42005-11-08 21:39:25 -08001570static int drop_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
1572 struct stripe_head *sh;
1573
NeilBrown3f294f42005-11-08 21:39:25 -08001574 spin_lock_irq(&conf->device_lock);
1575 sh = get_free_stripe(conf);
1576 spin_unlock_irq(&conf->device_lock);
1577 if (!sh)
1578 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001579 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001580 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001581 kmem_cache_free(conf->slab_cache, sh);
1582 atomic_dec(&conf->active_stripes);
1583 return 1;
1584}
1585
1586static void shrink_stripes(raid5_conf_t *conf)
1587{
1588 while (drop_one_stripe(conf))
1589 ;
1590
NeilBrown29fc7e32006-02-03 03:03:41 -08001591 if (conf->slab_cache)
1592 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 conf->slab_cache = NULL;
1594}
1595
NeilBrown6712ecf2007-09-27 12:47:43 +02001596static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
NeilBrown99c0fb52009-03-31 14:39:38 +11001598 struct stripe_head *sh = bi->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001600 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001602 char b[BDEVNAME_SIZE];
1603 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
1606 for (i=0 ; i<disks; i++)
1607 if (bi == &sh->dev[i].req)
1608 break;
1609
Dan Williams45b42332007-07-09 11:56:43 -07001610 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1611 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 uptodate);
1613 if (i == disks) {
1614 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001615 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 }
1617
1618 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001620 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrownd6950432006-07-10 04:44:20 -07001621 rdev = conf->disks[i].rdev;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001622 printk_ratelimited(
1623 KERN_INFO
1624 "md/raid:%s: read error corrected"
1625 " (%lu sectors at %llu on %s)\n",
1626 mdname(conf->mddev), STRIPE_SECTORS,
1627 (unsigned long long)(sh->sector
1628 + rdev->data_offset),
1629 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001630 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001631 clear_bit(R5_ReadError, &sh->dev[i].flags);
1632 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1633 }
NeilBrownba22dcb2005-11-08 21:39:31 -08001634 if (atomic_read(&conf->disks[i].rdev->read_errors))
1635 atomic_set(&conf->disks[i].rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 } else {
NeilBrownd6950432006-07-10 04:44:20 -07001637 const char *bdn = bdevname(conf->disks[i].rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001638 int retry = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001639 rdev = conf->disks[i].rdev;
1640
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001642 atomic_inc(&rdev->read_errors);
Gabriele A. Trombetti7b0bb532010-04-28 11:51:17 +10001643 if (conf->mddev->degraded >= conf->max_degraded)
Christian Dietrich8bda4702011-07-27 11:00:36 +10001644 printk_ratelimited(
1645 KERN_WARNING
1646 "md/raid:%s: read error not correctable "
1647 "(sector %llu on %s).\n",
1648 mdname(conf->mddev),
1649 (unsigned long long)(sh->sector
1650 + rdev->data_offset),
1651 bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001652 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
NeilBrown4e5314b2005-11-08 21:39:22 -08001653 /* Oh, no!!! */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001654 printk_ratelimited(
1655 KERN_WARNING
1656 "md/raid:%s: read error NOT corrected!! "
1657 "(sector %llu on %s).\n",
1658 mdname(conf->mddev),
1659 (unsigned long long)(sh->sector
1660 + rdev->data_offset),
1661 bdn);
NeilBrownd6950432006-07-10 04:44:20 -07001662 else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001663 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001664 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001665 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001666 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001667 else
1668 retry = 1;
1669 if (retry)
1670 set_bit(R5_ReadError, &sh->dev[i].flags);
1671 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001672 clear_bit(R5_ReadError, &sh->dev[i].flags);
1673 clear_bit(R5_ReWrite, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001674 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 }
1677 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1679 set_bit(STRIPE_HANDLE, &sh->state);
1680 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681}
1682
NeilBrownd710e132008-10-13 11:55:12 +11001683static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
NeilBrown99c0fb52009-03-31 14:39:38 +11001685 struct stripe_head *sh = bi->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001687 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001689 sector_t first_bad;
1690 int bad_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 for (i=0 ; i<disks; i++)
1693 if (bi == &sh->dev[i].req)
1694 break;
1695
Dan Williams45b42332007-07-09 11:56:43 -07001696 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1698 uptodate);
1699 if (i == disks) {
1700 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001701 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 }
1703
NeilBrownbc2607f2011-07-28 11:39:22 +10001704 if (!uptodate) {
1705 set_bit(WriteErrorSeen, &conf->disks[i].rdev->flags);
1706 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrownb84db562011-07-28 11:39:23 +10001707 } else if (is_badblock(conf->disks[i].rdev, sh->sector, STRIPE_SECTORS,
1708 &first_bad, &bad_sectors))
1709 set_bit(R5_MadeGood, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
1711 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
1712
1713 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1714 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001715 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716}
1717
1718
NeilBrown784052e2009-03-31 15:19:07 +11001719static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
NeilBrown784052e2009-03-31 15:19:07 +11001721static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722{
1723 struct r5dev *dev = &sh->dev[i];
1724
1725 bio_init(&dev->req);
1726 dev->req.bi_io_vec = &dev->vec;
1727 dev->req.bi_vcnt++;
1728 dev->req.bi_max_vecs++;
1729 dev->vec.bv_page = dev->page;
1730 dev->vec.bv_len = STRIPE_SIZE;
1731 dev->vec.bv_offset = 0;
1732
1733 dev->req.bi_sector = sh->sector;
1734 dev->req.bi_private = sh;
1735
1736 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001737 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738}
1739
1740static void error(mddev_t *mddev, mdk_rdev_t *rdev)
1741{
1742 char b[BDEVNAME_SIZE];
H Hartley Sweeten7b928132010-03-08 16:02:40 +11001743 raid5_conf_t *conf = mddev->private;
NeilBrown0c55e022010-05-03 14:09:02 +10001744 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
NeilBrown6f8d0c72011-05-11 14:38:44 +10001746 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1747 unsigned long flags;
1748 spin_lock_irqsave(&conf->device_lock, flags);
1749 mddev->degraded++;
1750 spin_unlock_irqrestore(&conf->device_lock, flags);
1751 /*
1752 * if recovery was running, make sure it aborts.
1753 */
1754 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 }
NeilBrownde393cd2011-07-28 11:31:48 +10001756 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001757 set_bit(Faulty, &rdev->flags);
1758 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1759 printk(KERN_ALERT
1760 "md/raid:%s: Disk failure on %s, disabling device.\n"
1761 "md/raid:%s: Operation continuing on %d devices.\n",
1762 mdname(mddev),
1763 bdevname(rdev->bdev, b),
1764 mdname(mddev),
1765 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001766}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
1768/*
1769 * Input: a 'big' sector number,
1770 * Output: index of the data and parity disk, and the sector # in them.
1771 */
NeilBrown112bf892009-03-31 14:39:38 +11001772static sector_t raid5_compute_sector(raid5_conf_t *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001773 int previous, int *dd_idx,
1774 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001776 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001777 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001779 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001780 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001782 int algorithm = previous ? conf->prev_algo
1783 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001784 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1785 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001786 int raid_disks = previous ? conf->previous_raid_disks
1787 : conf->raid_disks;
1788 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
1790 /* First compute the information on this sector */
1791
1792 /*
1793 * Compute the chunk number and the sector offset inside the chunk
1794 */
1795 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1796 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
1798 /*
1799 * Compute the stripe number
1800 */
NeilBrown35f2a592010-04-20 14:13:34 +10001801 stripe = chunk_number;
1802 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10001803 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 /*
1805 * Select the parity disk based on the user selected algorithm.
1806 */
NeilBrown84789552011-07-27 11:00:36 +10001807 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07001808 switch(conf->level) {
1809 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001810 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001811 break;
1812 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001813 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001815 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001816 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 (*dd_idx)++;
1818 break;
1819 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001820 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001821 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 (*dd_idx)++;
1823 break;
1824 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001825 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001826 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 break;
1828 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001829 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001830 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001832 case ALGORITHM_PARITY_0:
1833 pd_idx = 0;
1834 (*dd_idx)++;
1835 break;
1836 case ALGORITHM_PARITY_N:
1837 pd_idx = data_disks;
1838 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11001840 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001841 }
1842 break;
1843 case 6:
1844
NeilBrowne183eae2009-03-31 15:20:22 +11001845 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001846 case ALGORITHM_LEFT_ASYMMETRIC:
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;
1849 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001850 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001851 qd_idx = 0;
1852 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001853 (*dd_idx) += 2; /* D D P Q D */
1854 break;
1855 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001856 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001857 qd_idx = pd_idx + 1;
1858 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001859 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001860 qd_idx = 0;
1861 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001862 (*dd_idx) += 2; /* D D P Q D */
1863 break;
1864 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001865 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001866 qd_idx = (pd_idx + 1) % raid_disks;
1867 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001868 break;
1869 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001870 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001871 qd_idx = (pd_idx + 1) % raid_disks;
1872 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001873 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001874
1875 case ALGORITHM_PARITY_0:
1876 pd_idx = 0;
1877 qd_idx = 1;
1878 (*dd_idx) += 2;
1879 break;
1880 case ALGORITHM_PARITY_N:
1881 pd_idx = data_disks;
1882 qd_idx = data_disks + 1;
1883 break;
1884
1885 case ALGORITHM_ROTATING_ZERO_RESTART:
1886 /* Exactly the same as RIGHT_ASYMMETRIC, but or
1887 * of blocks for computing Q is different.
1888 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001889 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11001890 qd_idx = pd_idx + 1;
1891 if (pd_idx == raid_disks-1) {
1892 (*dd_idx)++; /* Q D D D P */
1893 qd_idx = 0;
1894 } else if (*dd_idx >= pd_idx)
1895 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001896 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001897 break;
1898
1899 case ALGORITHM_ROTATING_N_RESTART:
1900 /* Same a left_asymmetric, by first stripe is
1901 * D D D P Q rather than
1902 * Q D D D P
1903 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001904 stripe2 += 1;
1905 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11001906 qd_idx = pd_idx + 1;
1907 if (pd_idx == raid_disks-1) {
1908 (*dd_idx)++; /* Q D D D P */
1909 qd_idx = 0;
1910 } else if (*dd_idx >= pd_idx)
1911 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001912 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001913 break;
1914
1915 case ALGORITHM_ROTATING_N_CONTINUE:
1916 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001917 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11001918 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
1919 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11001920 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001921 break;
1922
1923 case ALGORITHM_LEFT_ASYMMETRIC_6:
1924 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001925 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11001926 if (*dd_idx >= pd_idx)
1927 (*dd_idx)++;
1928 qd_idx = raid_disks - 1;
1929 break;
1930
1931 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001932 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11001933 if (*dd_idx >= pd_idx)
1934 (*dd_idx)++;
1935 qd_idx = raid_disks - 1;
1936 break;
1937
1938 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001939 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11001940 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1941 qd_idx = raid_disks - 1;
1942 break;
1943
1944 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001945 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11001946 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1947 qd_idx = raid_disks - 1;
1948 break;
1949
1950 case ALGORITHM_PARITY_0_6:
1951 pd_idx = 0;
1952 (*dd_idx)++;
1953 qd_idx = raid_disks - 1;
1954 break;
1955
NeilBrown16a53ec2006-06-26 00:27:38 -07001956 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11001957 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001958 }
1959 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 }
1961
NeilBrown911d4ee2009-03-31 14:39:38 +11001962 if (sh) {
1963 sh->pd_idx = pd_idx;
1964 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001965 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11001966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 /*
1968 * Finally, compute the new sector number
1969 */
1970 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
1971 return new_sector;
1972}
1973
1974
NeilBrown784052e2009-03-31 15:19:07 +11001975static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
1977 raid5_conf_t *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08001978 int raid_disks = sh->disks;
1979 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001981 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1982 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11001983 int algorithm = previous ? conf->prev_algo
1984 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 sector_t stripe;
1986 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10001987 sector_t chunk_number;
1988 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11001990 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
NeilBrown16a53ec2006-06-26 00:27:38 -07001992
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 chunk_offset = sector_div(new_sector, sectors_per_chunk);
1994 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
NeilBrown16a53ec2006-06-26 00:27:38 -07001996 if (i == sh->pd_idx)
1997 return 0;
1998 switch(conf->level) {
1999 case 4: break;
2000 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002001 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 case ALGORITHM_LEFT_ASYMMETRIC:
2003 case ALGORITHM_RIGHT_ASYMMETRIC:
2004 if (i > sh->pd_idx)
2005 i--;
2006 break;
2007 case ALGORITHM_LEFT_SYMMETRIC:
2008 case ALGORITHM_RIGHT_SYMMETRIC:
2009 if (i < sh->pd_idx)
2010 i += raid_disks;
2011 i -= (sh->pd_idx + 1);
2012 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002013 case ALGORITHM_PARITY_0:
2014 i -= 1;
2015 break;
2016 case ALGORITHM_PARITY_N:
2017 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002019 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002020 }
2021 break;
2022 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002023 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002024 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002025 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002026 case ALGORITHM_LEFT_ASYMMETRIC:
2027 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002028 case ALGORITHM_ROTATING_ZERO_RESTART:
2029 case ALGORITHM_ROTATING_N_RESTART:
2030 if (sh->pd_idx == raid_disks-1)
2031 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002032 else if (i > sh->pd_idx)
2033 i -= 2; /* D D P Q D */
2034 break;
2035 case ALGORITHM_LEFT_SYMMETRIC:
2036 case ALGORITHM_RIGHT_SYMMETRIC:
2037 if (sh->pd_idx == raid_disks-1)
2038 i--; /* Q D D D P */
2039 else {
2040 /* D D P Q D */
2041 if (i < sh->pd_idx)
2042 i += raid_disks;
2043 i -= (sh->pd_idx + 2);
2044 }
2045 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002046 case ALGORITHM_PARITY_0:
2047 i -= 2;
2048 break;
2049 case ALGORITHM_PARITY_N:
2050 break;
2051 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002052 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002053 if (sh->pd_idx == 0)
2054 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002055 else {
2056 /* D D Q P D */
2057 if (i < sh->pd_idx)
2058 i += raid_disks;
2059 i -= (sh->pd_idx + 1);
2060 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002061 break;
2062 case ALGORITHM_LEFT_ASYMMETRIC_6:
2063 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2064 if (i > sh->pd_idx)
2065 i--;
2066 break;
2067 case ALGORITHM_LEFT_SYMMETRIC_6:
2068 case ALGORITHM_RIGHT_SYMMETRIC_6:
2069 if (i < sh->pd_idx)
2070 i += data_disks + 1;
2071 i -= (sh->pd_idx + 1);
2072 break;
2073 case ALGORITHM_PARITY_0_6:
2074 i -= 1;
2075 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002076 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002077 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002078 }
2079 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 }
2081
2082 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002083 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
NeilBrown112bf892009-03-31 14:39:38 +11002085 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002086 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002087 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2088 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002089 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2090 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 return 0;
2092 }
2093 return r_sector;
2094}
2095
2096
Dan Williams600aa102008-06-28 08:32:05 +10002097static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002098schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002099 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002100{
2101 int i, pd_idx = sh->pd_idx, disks = sh->disks;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002102 raid5_conf_t *conf = sh->raid_conf;
2103 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002104
Dan Williamse33129d2007-01-02 13:52:30 -07002105 if (rcw) {
2106 /* if we are not expanding this is a proper write request, and
2107 * there will be bios with new data to be drained into the
2108 * stripe cache
2109 */
2110 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10002111 sh->reconstruct_state = reconstruct_state_drain_run;
2112 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2113 } else
2114 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07002115
Dan Williamsac6b53b2009-07-14 13:40:19 -07002116 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002117
2118 for (i = disks; i--; ) {
2119 struct r5dev *dev = &sh->dev[i];
2120
2121 if (dev->towrite) {
2122 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002123 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002124 if (!expand)
2125 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002126 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002127 }
2128 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002129 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002130 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002131 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002132 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002133 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002134 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2135 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2136
Dan Williamsd8ee0722008-06-28 08:32:06 +10002137 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002138 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2139 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002140 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002141
2142 for (i = disks; i--; ) {
2143 struct r5dev *dev = &sh->dev[i];
2144 if (i == pd_idx)
2145 continue;
2146
Dan Williamse33129d2007-01-02 13:52:30 -07002147 if (dev->towrite &&
2148 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002149 test_bit(R5_Wantcompute, &dev->flags))) {
2150 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002151 set_bit(R5_LOCKED, &dev->flags);
2152 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002153 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002154 }
2155 }
2156 }
2157
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002158 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002159 * are in flight
2160 */
2161 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2162 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002163 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002164
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002165 if (level == 6) {
2166 int qd_idx = sh->qd_idx;
2167 struct r5dev *dev = &sh->dev[qd_idx];
2168
2169 set_bit(R5_LOCKED, &dev->flags);
2170 clear_bit(R5_UPTODATE, &dev->flags);
2171 s->locked++;
2172 }
2173
Dan Williams600aa102008-06-28 08:32:05 +10002174 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002175 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002176 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002177}
NeilBrown16a53ec2006-06-26 00:27:38 -07002178
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179/*
2180 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002181 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 * The bi_next chain must be in order.
2183 */
2184static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2185{
2186 struct bio **bip;
2187 raid5_conf_t *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002188 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
NeilBrowncbe47ec2011-07-26 11:20:35 +10002190 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 (unsigned long long)bi->bi_sector,
2192 (unsigned long long)sh->sector);
2193
2194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07002196 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -07002198 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
2199 firstwrite = 1;
2200 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 bip = &sh->dev[dd_idx].toread;
2202 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2203 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2204 goto overlap;
2205 bip = & (*bip)->bi_next;
2206 }
2207 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2208 goto overlap;
2209
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002210 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 if (*bip)
2212 bi->bi_next = *bip;
2213 *bip = bi;
Jens Axboe960e7392008-08-15 10:41:18 +02002214 bi->bi_phys_segments++;
NeilBrown72626682005-09-09 16:23:54 -07002215
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 if (forwrite) {
2217 /* check if page is covered */
2218 sector_t sector = sh->dev[dd_idx].sector;
2219 for (bi=sh->dev[dd_idx].towrite;
2220 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2221 bi && bi->bi_sector <= sector;
2222 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2223 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2224 sector = bi->bi_sector + (bi->bi_size>>9);
2225 }
2226 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2227 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2228 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002229 spin_unlock_irq(&conf->device_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002230
2231 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2232 (unsigned long long)(*bip)->bi_sector,
2233 (unsigned long long)sh->sector, dd_idx);
2234
2235 if (conf->mddev->bitmap && firstwrite) {
2236 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2237 STRIPE_SECTORS, 0);
2238 sh->bm_seq = conf->seq_flush+1;
2239 set_bit(STRIPE_BIT_DELAY, &sh->state);
2240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 return 1;
2242
2243 overlap:
2244 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
2245 spin_unlock_irq(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 return 0;
2247}
2248
NeilBrown29269552006-03-27 01:18:10 -08002249static void end_reshape(raid5_conf_t *conf);
2250
NeilBrown911d4ee2009-03-31 14:39:38 +11002251static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
2252 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002253{
NeilBrown784052e2009-03-31 15:19:07 +11002254 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002255 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002256 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002257 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002258 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002259
NeilBrown112bf892009-03-31 14:39:38 +11002260 raid5_compute_sector(conf,
2261 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002262 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002263 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002264 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002265}
2266
Dan Williamsa4456852007-07-09 11:56:43 -07002267static void
Dan Williams1fe797e2008-06-28 09:16:30 +10002268handle_failed_stripe(raid5_conf_t *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002269 struct stripe_head_state *s, int disks,
2270 struct bio **return_bi)
2271{
2272 int i;
2273 for (i = disks; i--; ) {
2274 struct bio *bi;
2275 int bitmap_end = 0;
2276
2277 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
2278 mdk_rdev_t *rdev;
2279 rcu_read_lock();
2280 rdev = rcu_dereference(conf->disks[i].rdev);
2281 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002282 atomic_inc(&rdev->nr_pending);
2283 else
2284 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002285 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002286 if (rdev) {
2287 if (!rdev_set_badblocks(
2288 rdev,
2289 sh->sector,
2290 STRIPE_SECTORS, 0))
2291 md_error(conf->mddev, rdev);
2292 rdev_dec_pending(rdev, conf->mddev);
2293 }
Dan Williamsa4456852007-07-09 11:56:43 -07002294 }
2295 spin_lock_irq(&conf->device_lock);
2296 /* fail all writes first */
2297 bi = sh->dev[i].towrite;
2298 sh->dev[i].towrite = NULL;
2299 if (bi) {
2300 s->to_write--;
2301 bitmap_end = 1;
2302 }
2303
2304 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2305 wake_up(&conf->wait_for_overlap);
2306
2307 while (bi && bi->bi_sector <
2308 sh->dev[i].sector + STRIPE_SECTORS) {
2309 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2310 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002311 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002312 md_write_end(conf->mddev);
2313 bi->bi_next = *return_bi;
2314 *return_bi = bi;
2315 }
2316 bi = nextbi;
2317 }
2318 /* and fail all 'written' */
2319 bi = sh->dev[i].written;
2320 sh->dev[i].written = NULL;
2321 if (bi) bitmap_end = 1;
2322 while (bi && bi->bi_sector <
2323 sh->dev[i].sector + STRIPE_SECTORS) {
2324 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2325 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002326 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002327 md_write_end(conf->mddev);
2328 bi->bi_next = *return_bi;
2329 *return_bi = bi;
2330 }
2331 bi = bi2;
2332 }
2333
Dan Williamsb5e98d62007-01-02 13:52:31 -07002334 /* fail any reads if this device is non-operational and
2335 * the data has not reached the cache yet.
2336 */
2337 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2338 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2339 test_bit(R5_ReadError, &sh->dev[i].flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002340 bi = sh->dev[i].toread;
2341 sh->dev[i].toread = NULL;
2342 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2343 wake_up(&conf->wait_for_overlap);
2344 if (bi) s->to_read--;
2345 while (bi && bi->bi_sector <
2346 sh->dev[i].sector + STRIPE_SECTORS) {
2347 struct bio *nextbi =
2348 r5_next_bio(bi, sh->dev[i].sector);
2349 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002350 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002351 bi->bi_next = *return_bi;
2352 *return_bi = bi;
2353 }
2354 bi = nextbi;
2355 }
2356 }
2357 spin_unlock_irq(&conf->device_lock);
2358 if (bitmap_end)
2359 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2360 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002361 /* If we were in the middle of a write the parity block might
2362 * still be locked - so just clear all R5_LOCKED flags
2363 */
2364 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002365 }
2366
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002367 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2368 if (atomic_dec_and_test(&conf->pending_full_writes))
2369 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002370}
2371
NeilBrown7f0da592011-07-28 11:39:22 +10002372static void
2373handle_failed_sync(raid5_conf_t *conf, struct stripe_head *sh,
2374 struct stripe_head_state *s)
2375{
2376 int abort = 0;
2377 int i;
2378
2379 md_done_sync(conf->mddev, STRIPE_SECTORS, 0);
2380 clear_bit(STRIPE_SYNCING, &sh->state);
2381 s->syncing = 0;
2382 /* There is nothing more to do for sync/check/repair.
2383 * For recover we need to record a bad block on all
2384 * non-sync devices, or abort the recovery
2385 */
2386 if (!test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery))
2387 return;
2388 /* During recovery devices cannot be removed, so locking and
2389 * refcounting of rdevs is not needed
2390 */
2391 for (i = 0; i < conf->raid_disks; i++) {
2392 mdk_rdev_t *rdev = conf->disks[i].rdev;
2393 if (!rdev
2394 || test_bit(Faulty, &rdev->flags)
2395 || test_bit(In_sync, &rdev->flags))
2396 continue;
2397 if (!rdev_set_badblocks(rdev, sh->sector,
2398 STRIPE_SECTORS, 0))
2399 abort = 1;
2400 }
2401 if (abort) {
2402 conf->recovery_disabled = conf->mddev->recovery_disabled;
2403 set_bit(MD_RECOVERY_INTR, &conf->mddev->recovery);
2404 }
2405}
2406
NeilBrown93b3dbc2011-07-27 11:00:36 +10002407/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002408 * to be read or computed to satisfy a request.
2409 *
2410 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002411 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002412 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002413static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2414 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002415{
2416 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002417 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2418 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002419
Dan Williamsf38e1212007-01-02 13:52:30 -07002420 /* is the data in this block needed, and can we get it? */
2421 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002422 !test_bit(R5_UPTODATE, &dev->flags) &&
2423 (dev->toread ||
2424 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2425 s->syncing || s->expanding ||
NeilBrown5d35e092011-07-27 11:00:36 +10002426 (s->failed >= 1 && fdev[0]->toread) ||
2427 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002428 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2429 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2430 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002431 /* we would like to get this block, possibly by computing it,
2432 * otherwise read it if the backing disk is insync
2433 */
2434 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2435 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2436 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002437 (s->failed && (disk_idx == s->failed_num[0] ||
2438 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002439 /* have disk failed, and we're requested to fetch it;
2440 * do compute it
2441 */
2442 pr_debug("Computing stripe %llu block %d\n",
2443 (unsigned long long)sh->sector, disk_idx);
2444 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2445 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2446 set_bit(R5_Wantcompute, &dev->flags);
2447 sh->ops.target = disk_idx;
2448 sh->ops.target2 = -1; /* no 2nd target */
2449 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002450 /* Careful: from this point on 'uptodate' is in the eye
2451 * of raid_run_ops which services 'compute' operations
2452 * before writes. R5_Wantcompute flags a block that will
2453 * be R5_UPTODATE by the time it is needed for a
2454 * subsequent operation.
2455 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002456 s->uptodate++;
2457 return 1;
2458 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2459 /* Computing 2-failure is *very* expensive; only
2460 * do it if failed >= 2
2461 */
2462 int other;
2463 for (other = disks; other--; ) {
2464 if (other == disk_idx)
2465 continue;
2466 if (!test_bit(R5_UPTODATE,
2467 &sh->dev[other].flags))
2468 break;
2469 }
2470 BUG_ON(other < 0);
2471 pr_debug("Computing stripe %llu blocks %d,%d\n",
2472 (unsigned long long)sh->sector,
2473 disk_idx, other);
2474 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2475 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2476 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2477 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2478 sh->ops.target = disk_idx;
2479 sh->ops.target2 = other;
2480 s->uptodate += 2;
2481 s->req_compute = 1;
2482 return 1;
2483 } else if (test_bit(R5_Insync, &dev->flags)) {
2484 set_bit(R5_LOCKED, &dev->flags);
2485 set_bit(R5_Wantread, &dev->flags);
2486 s->locked++;
2487 pr_debug("Reading block %d (sync=%d)\n",
2488 disk_idx, s->syncing);
2489 }
2490 }
2491
2492 return 0;
2493}
2494
2495/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002496 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002497 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002498static void handle_stripe_fill(struct stripe_head *sh,
2499 struct stripe_head_state *s,
2500 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002501{
2502 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002503
2504 /* look for blocks to read/compute, skip this if a compute
2505 * is already in flight, or if the stripe contents are in the
2506 * midst of changing due to a write
2507 */
2508 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2509 !sh->reconstruct_state)
2510 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002511 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002512 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002513 set_bit(STRIPE_HANDLE, &sh->state);
2514}
2515
2516
Dan Williams1fe797e2008-06-28 09:16:30 +10002517/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002518 * any written block on an uptodate or failed drive can be returned.
2519 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2520 * never LOCKED, so we don't need to test 'failed' directly.
2521 */
Dan Williams1fe797e2008-06-28 09:16:30 +10002522static void handle_stripe_clean_event(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002523 struct stripe_head *sh, int disks, struct bio **return_bi)
2524{
2525 int i;
2526 struct r5dev *dev;
2527
2528 for (i = disks; i--; )
2529 if (sh->dev[i].written) {
2530 dev = &sh->dev[i];
2531 if (!test_bit(R5_LOCKED, &dev->flags) &&
2532 test_bit(R5_UPTODATE, &dev->flags)) {
2533 /* We can return any write requests */
2534 struct bio *wbi, *wbi2;
2535 int bitmap_end = 0;
Dan Williams45b42332007-07-09 11:56:43 -07002536 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002537 spin_lock_irq(&conf->device_lock);
2538 wbi = dev->written;
2539 dev->written = NULL;
2540 while (wbi && wbi->bi_sector <
2541 dev->sector + STRIPE_SECTORS) {
2542 wbi2 = r5_next_bio(wbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +02002543 if (!raid5_dec_bi_phys_segments(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002544 md_write_end(conf->mddev);
2545 wbi->bi_next = *return_bi;
2546 *return_bi = wbi;
2547 }
2548 wbi = wbi2;
2549 }
2550 if (dev->towrite == NULL)
2551 bitmap_end = 1;
2552 spin_unlock_irq(&conf->device_lock);
2553 if (bitmap_end)
2554 bitmap_endwrite(conf->mddev->bitmap,
2555 sh->sector,
2556 STRIPE_SECTORS,
2557 !test_bit(STRIPE_DEGRADED, &sh->state),
2558 0);
2559 }
2560 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002561
2562 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2563 if (atomic_dec_and_test(&conf->pending_full_writes))
2564 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002565}
2566
NeilBrownc8ac1802011-07-27 11:00:36 +10002567static void handle_stripe_dirtying(raid5_conf_t *conf,
2568 struct stripe_head *sh,
2569 struct stripe_head_state *s,
2570 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002571{
2572 int rmw = 0, rcw = 0, i;
NeilBrownc8ac1802011-07-27 11:00:36 +10002573 if (conf->max_degraded == 2) {
2574 /* RAID6 requires 'rcw' in current implementation
2575 * Calculate the real rcw later - for now fake it
2576 * look like rcw is cheaper
2577 */
2578 rcw = 1; rmw = 2;
2579 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002580 /* would I have to read this buffer for read_modify_write */
2581 struct r5dev *dev = &sh->dev[i];
2582 if ((dev->towrite || i == sh->pd_idx) &&
2583 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002584 !(test_bit(R5_UPTODATE, &dev->flags) ||
2585 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002586 if (test_bit(R5_Insync, &dev->flags))
2587 rmw++;
2588 else
2589 rmw += 2*disks; /* cannot read it */
2590 }
2591 /* Would I have to read this buffer for reconstruct_write */
2592 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2593 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002594 !(test_bit(R5_UPTODATE, &dev->flags) ||
2595 test_bit(R5_Wantcompute, &dev->flags))) {
2596 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002597 else
2598 rcw += 2*disks;
2599 }
2600 }
Dan Williams45b42332007-07-09 11:56:43 -07002601 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002602 (unsigned long long)sh->sector, rmw, rcw);
2603 set_bit(STRIPE_HANDLE, &sh->state);
2604 if (rmw < rcw && rmw > 0)
2605 /* prefer read-modify-write, but need to get some data */
2606 for (i = disks; i--; ) {
2607 struct r5dev *dev = &sh->dev[i];
2608 if ((dev->towrite || i == sh->pd_idx) &&
2609 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002610 !(test_bit(R5_UPTODATE, &dev->flags) ||
2611 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002612 test_bit(R5_Insync, &dev->flags)) {
2613 if (
2614 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002615 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002616 "%d for r-m-w\n", i);
2617 set_bit(R5_LOCKED, &dev->flags);
2618 set_bit(R5_Wantread, &dev->flags);
2619 s->locked++;
2620 } else {
2621 set_bit(STRIPE_DELAYED, &sh->state);
2622 set_bit(STRIPE_HANDLE, &sh->state);
2623 }
2624 }
2625 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002626 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002627 /* want reconstruct write, but need to get some data */
NeilBrownc8ac1802011-07-27 11:00:36 +10002628 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002629 for (i = disks; i--; ) {
2630 struct r5dev *dev = &sh->dev[i];
2631 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002632 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002633 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002634 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002635 test_bit(R5_Wantcompute, &dev->flags))) {
2636 rcw++;
2637 if (!test_bit(R5_Insync, &dev->flags))
2638 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002639 if (
2640 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002641 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002642 "%d for Reconstruct\n", i);
2643 set_bit(R5_LOCKED, &dev->flags);
2644 set_bit(R5_Wantread, &dev->flags);
2645 s->locked++;
2646 } else {
2647 set_bit(STRIPE_DELAYED, &sh->state);
2648 set_bit(STRIPE_HANDLE, &sh->state);
2649 }
2650 }
2651 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002652 }
Dan Williamsa4456852007-07-09 11:56:43 -07002653 /* now if nothing is locked, and if we have enough data,
2654 * we can start a write request
2655 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002656 /* since handle_stripe can be called at any time we need to handle the
2657 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002658 * subsequent call wants to start a write request. raid_run_ops only
2659 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002660 * simultaneously. If this is not the case then new writes need to be
2661 * held off until the compute completes.
2662 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002663 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2664 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2665 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002666 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002667}
2668
Dan Williamsa4456852007-07-09 11:56:43 -07002669static void handle_parity_checks5(raid5_conf_t *conf, struct stripe_head *sh,
2670 struct stripe_head_state *s, int disks)
2671{
Dan Williamsecc65c92008-06-28 08:31:57 +10002672 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002673
Dan Williamsbd2ab672008-04-10 21:29:27 -07002674 set_bit(STRIPE_HANDLE, &sh->state);
2675
Dan Williamsecc65c92008-06-28 08:31:57 +10002676 switch (sh->check_state) {
2677 case check_state_idle:
2678 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002679 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002680 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002681 sh->check_state = check_state_run;
2682 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002683 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002684 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002685 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002686 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002687 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002688 /* fall through */
2689 case check_state_compute_result:
2690 sh->check_state = check_state_idle;
2691 if (!dev)
2692 dev = &sh->dev[sh->pd_idx];
2693
2694 /* check that a write has not made the stripe insync */
2695 if (test_bit(STRIPE_INSYNC, &sh->state))
2696 break;
2697
2698 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002699 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2700 BUG_ON(s->uptodate != disks);
2701
2702 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002703 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002704 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002705
Dan Williamsa4456852007-07-09 11:56:43 -07002706 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002707 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002708 break;
2709 case check_state_run:
2710 break; /* we will be called again upon completion */
2711 case check_state_check_result:
2712 sh->check_state = check_state_idle;
2713
2714 /* if a failure occurred during the check operation, leave
2715 * STRIPE_INSYNC not set and let the stripe be handled again
2716 */
2717 if (s->failed)
2718 break;
2719
2720 /* handle a successful check operation, if parity is correct
2721 * we are done. Otherwise update the mismatch count and repair
2722 * parity if !MD_RECOVERY_CHECK
2723 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002724 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002725 /* parity is correct (on disc,
2726 * not in buffer any more)
2727 */
2728 set_bit(STRIPE_INSYNC, &sh->state);
2729 else {
2730 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2731 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2732 /* don't try to repair!! */
2733 set_bit(STRIPE_INSYNC, &sh->state);
2734 else {
2735 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002736 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002737 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2738 set_bit(R5_Wantcompute,
2739 &sh->dev[sh->pd_idx].flags);
2740 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002741 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10002742 s->uptodate++;
2743 }
2744 }
2745 break;
2746 case check_state_compute_run:
2747 break;
2748 default:
2749 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2750 __func__, sh->check_state,
2751 (unsigned long long) sh->sector);
2752 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002753 }
2754}
2755
2756
2757static void handle_parity_checks6(raid5_conf_t *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07002758 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10002759 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002760{
Dan Williamsa4456852007-07-09 11:56:43 -07002761 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11002762 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07002763 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07002764
2765 set_bit(STRIPE_HANDLE, &sh->state);
2766
2767 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002768
Dan Williamsa4456852007-07-09 11:56:43 -07002769 /* Want to check and possibly repair P and Q.
2770 * However there could be one 'failed' device, in which
2771 * case we can only check one of them, possibly using the
2772 * other to generate missing data
2773 */
2774
Dan Williamsd82dfee2009-07-14 13:40:57 -07002775 switch (sh->check_state) {
2776 case check_state_idle:
2777 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10002778 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002779 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07002780 * makes sense to check P (If anything else were failed,
2781 * we would have used P to recreate it).
2782 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002783 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07002784 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002785 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002786 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07002787 * anything, so it makes sense to check it
2788 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002789 if (sh->check_state == check_state_run)
2790 sh->check_state = check_state_run_pq;
2791 else
2792 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07002793 }
Dan Williams36d1c642009-07-14 11:48:22 -07002794
Dan Williamsd82dfee2009-07-14 13:40:57 -07002795 /* discard potentially stale zero_sum_result */
2796 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07002797
Dan Williamsd82dfee2009-07-14 13:40:57 -07002798 if (sh->check_state == check_state_run) {
2799 /* async_xor_zero_sum destroys the contents of P */
2800 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
2801 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07002802 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002803 if (sh->check_state >= check_state_run &&
2804 sh->check_state <= check_state_run_pq) {
2805 /* async_syndrome_zero_sum preserves P and Q, so
2806 * no need to mark them !uptodate here
2807 */
2808 set_bit(STRIPE_OP_CHECK, &s->ops_request);
2809 break;
2810 }
Dan Williams36d1c642009-07-14 11:48:22 -07002811
Dan Williamsd82dfee2009-07-14 13:40:57 -07002812 /* we have 2-disk failure */
2813 BUG_ON(s->failed != 2);
2814 /* fall through */
2815 case check_state_compute_result:
2816 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07002817
Dan Williamsd82dfee2009-07-14 13:40:57 -07002818 /* check that a write has not made the stripe insync */
2819 if (test_bit(STRIPE_INSYNC, &sh->state))
2820 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002821
2822 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07002823 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07002824 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002825 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07002826 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10002827 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07002828 s->locked++;
2829 set_bit(R5_LOCKED, &dev->flags);
2830 set_bit(R5_Wantwrite, &dev->flags);
2831 }
2832 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10002833 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07002834 s->locked++;
2835 set_bit(R5_LOCKED, &dev->flags);
2836 set_bit(R5_Wantwrite, &dev->flags);
2837 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002838 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002839 dev = &sh->dev[pd_idx];
2840 s->locked++;
2841 set_bit(R5_LOCKED, &dev->flags);
2842 set_bit(R5_Wantwrite, &dev->flags);
2843 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002844 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002845 dev = &sh->dev[qd_idx];
2846 s->locked++;
2847 set_bit(R5_LOCKED, &dev->flags);
2848 set_bit(R5_Wantwrite, &dev->flags);
2849 }
2850 clear_bit(STRIPE_DEGRADED, &sh->state);
2851
2852 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002853 break;
2854 case check_state_run:
2855 case check_state_run_q:
2856 case check_state_run_pq:
2857 break; /* we will be called again upon completion */
2858 case check_state_check_result:
2859 sh->check_state = check_state_idle;
2860
2861 /* handle a successful check operation, if parity is correct
2862 * we are done. Otherwise update the mismatch count and repair
2863 * parity if !MD_RECOVERY_CHECK
2864 */
2865 if (sh->ops.zero_sum_result == 0) {
2866 /* both parities are correct */
2867 if (!s->failed)
2868 set_bit(STRIPE_INSYNC, &sh->state);
2869 else {
2870 /* in contrast to the raid5 case we can validate
2871 * parity, but still have a failure to write
2872 * back
2873 */
2874 sh->check_state = check_state_compute_result;
2875 /* Returning at this point means that we may go
2876 * off and bring p and/or q uptodate again so
2877 * we make sure to check zero_sum_result again
2878 * to verify if p or q need writeback
2879 */
2880 }
2881 } else {
2882 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2883 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2884 /* don't try to repair!! */
2885 set_bit(STRIPE_INSYNC, &sh->state);
2886 else {
2887 int *target = &sh->ops.target;
2888
2889 sh->ops.target = -1;
2890 sh->ops.target2 = -1;
2891 sh->check_state = check_state_compute_run;
2892 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2893 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2894 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
2895 set_bit(R5_Wantcompute,
2896 &sh->dev[pd_idx].flags);
2897 *target = pd_idx;
2898 target = &sh->ops.target2;
2899 s->uptodate++;
2900 }
2901 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
2902 set_bit(R5_Wantcompute,
2903 &sh->dev[qd_idx].flags);
2904 *target = qd_idx;
2905 s->uptodate++;
2906 }
2907 }
2908 }
2909 break;
2910 case check_state_compute_run:
2911 break;
2912 default:
2913 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2914 __func__, sh->check_state,
2915 (unsigned long long) sh->sector);
2916 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002917 }
2918}
2919
NeilBrown86c374b2011-07-27 11:00:36 +10002920static void handle_stripe_expansion(raid5_conf_t *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07002921{
2922 int i;
2923
2924 /* We have read all the blocks in this stripe and now we need to
2925 * copy some of them into a target stripe for expand.
2926 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07002927 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002928 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2929 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11002930 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11002931 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07002932 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07002933 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07002934
NeilBrown784052e2009-03-31 15:19:07 +11002935 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11002936 sector_t s = raid5_compute_sector(conf, bn, 0,
2937 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10002938 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07002939 if (sh2 == NULL)
2940 /* so far only the early blocks of this stripe
2941 * have been requested. When later blocks
2942 * get requested, we will try again
2943 */
2944 continue;
2945 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
2946 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
2947 /* must have already done this block */
2948 release_stripe(sh2);
2949 continue;
2950 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07002951
2952 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07002953 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002954 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07002955 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07002956 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002957
Dan Williamsa4456852007-07-09 11:56:43 -07002958 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
2959 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
2960 for (j = 0; j < conf->raid_disks; j++)
2961 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10002962 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002963 !test_bit(R5_Expanded, &sh2->dev[j].flags))
2964 break;
2965 if (j == conf->raid_disks) {
2966 set_bit(STRIPE_EXPAND_READY, &sh2->state);
2967 set_bit(STRIPE_HANDLE, &sh2->state);
2968 }
2969 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002970
Dan Williamsa4456852007-07-09 11:56:43 -07002971 }
NeilBrowna2e08552007-09-11 15:23:36 -07002972 /* done submitting copies, wait for them to complete */
2973 if (tx) {
2974 async_tx_ack(tx);
2975 dma_wait_for_async_tx(tx);
2976 }
Dan Williamsa4456852007-07-09 11:56:43 -07002977}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
Dan Williams6bfe0b42008-04-30 00:52:32 -07002979
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980/*
2981 * handle_stripe - do things to a stripe.
2982 *
2983 * We lock the stripe and then examine the state of various bits
2984 * to see what needs to be done.
2985 * Possible results:
2986 * return some read request which now have data
2987 * return some write requests which are safely on disc
2988 * schedule a read on some buffers
2989 * schedule a write of some buffers
2990 * return confirmation of parity correctness
2991 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 * buffers are taken off read_list or write_list, and bh_cache buffers
2993 * get BH_Lock set before the stripe lock is released.
2994 *
2995 */
Dan Williamsa4456852007-07-09 11:56:43 -07002996
NeilBrownacfe7262011-07-27 11:00:36 +10002997static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07002998{
NeilBrownbff61972009-03-31 14:33:13 +11002999 raid5_conf_t *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003000 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003001 struct r5dev *dev;
3002 int i;
NeilBrown16a53ec2006-06-26 00:27:38 -07003003
NeilBrownacfe7262011-07-27 11:00:36 +10003004 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003005
NeilBrownacfe7262011-07-27 11:00:36 +10003006 s->syncing = test_bit(STRIPE_SYNCING, &sh->state);
3007 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3008 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3009 s->failed_num[0] = -1;
3010 s->failed_num[1] = -1;
3011
3012 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003013 rcu_read_lock();
NeilBrownc4c16632011-07-26 11:34:20 +10003014 spin_lock_irq(&conf->device_lock);
NeilBrown16a53ec2006-06-26 00:27:38 -07003015 for (i=disks; i--; ) {
3016 mdk_rdev_t *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003017 sector_t first_bad;
3018 int bad_sectors;
3019 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003020
NeilBrown16a53ec2006-06-26 00:27:38 -07003021 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003022
Dan Williams45b42332007-07-09 11:56:43 -07003023 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown16a53ec2006-06-26 00:27:38 -07003024 i, dev->flags, dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003025 /* maybe we can reply to a read
3026 *
3027 * new wantfill requests are only permitted while
3028 * ops_complete_biofill is guaranteed to be inactive
3029 */
3030 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3031 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3032 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003033
3034 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003035 if (test_bit(R5_LOCKED, &dev->flags))
3036 s->locked++;
3037 if (test_bit(R5_UPTODATE, &dev->flags))
3038 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003039 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003040 s->compute++;
3041 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003042 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003043
NeilBrownacfe7262011-07-27 11:00:36 +10003044 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003045 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003046 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003047 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003048 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003049 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003050 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003051 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003052 }
Dan Williamsa4456852007-07-09 11:56:43 -07003053 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003054 s->written++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003055 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown31c176e2011-07-28 11:39:22 +10003056 if (rdev) {
3057 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3058 &first_bad, &bad_sectors);
3059 if (s->blocked_rdev == NULL
3060 && (test_bit(Blocked, &rdev->flags)
3061 || is_bad < 0)) {
3062 if (is_bad < 0)
3063 set_bit(BlockedBadBlocks,
3064 &rdev->flags);
3065 s->blocked_rdev = rdev;
3066 atomic_inc(&rdev->nr_pending);
3067 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003068 }
NeilBrown415e72d2010-06-17 17:25:21 +10003069 clear_bit(R5_Insync, &dev->flags);
3070 if (!rdev)
3071 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003072 else if (is_bad) {
3073 /* also not in-sync */
3074 if (!test_bit(WriteErrorSeen, &rdev->flags)) {
3075 /* treat as in-sync, but with a read error
3076 * which we can now try to correct
3077 */
3078 set_bit(R5_Insync, &dev->flags);
3079 set_bit(R5_ReadError, &dev->flags);
3080 }
3081 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003082 set_bit(R5_Insync, &dev->flags);
3083 else {
3084 /* in sync if before recovery_offset */
3085 if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
3086 set_bit(R5_Insync, &dev->flags);
3087 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003088 if (test_bit(R5_WriteError, &dev->flags)) {
3089 clear_bit(R5_Insync, &dev->flags);
3090 if (!test_bit(Faulty, &rdev->flags)) {
3091 s->handle_bad_blocks = 1;
3092 atomic_inc(&rdev->nr_pending);
3093 } else
3094 clear_bit(R5_WriteError, &dev->flags);
3095 }
NeilBrownb84db562011-07-28 11:39:23 +10003096 if (test_bit(R5_MadeGood, &dev->flags)) {
3097 if (!test_bit(Faulty, &rdev->flags)) {
3098 s->handle_bad_blocks = 1;
3099 atomic_inc(&rdev->nr_pending);
3100 } else
3101 clear_bit(R5_MadeGood, &dev->flags);
3102 }
NeilBrown415e72d2010-06-17 17:25:21 +10003103 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003104 /* The ReadError flag will just be confusing now */
3105 clear_bit(R5_ReadError, &dev->flags);
3106 clear_bit(R5_ReWrite, &dev->flags);
3107 }
NeilBrown415e72d2010-06-17 17:25:21 +10003108 if (test_bit(R5_ReadError, &dev->flags))
3109 clear_bit(R5_Insync, &dev->flags);
3110 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003111 if (s->failed < 2)
3112 s->failed_num[s->failed] = i;
3113 s->failed++;
NeilBrown415e72d2010-06-17 17:25:21 +10003114 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003115 }
NeilBrownc4c16632011-07-26 11:34:20 +10003116 spin_unlock_irq(&conf->device_lock);
NeilBrown16a53ec2006-06-26 00:27:38 -07003117 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003118}
NeilBrownf4168852007-02-28 20:11:53 -08003119
NeilBrowncc940152011-07-26 11:35:35 +10003120static void handle_stripe(struct stripe_head *sh)
3121{
3122 struct stripe_head_state s;
NeilBrown474af965fe2011-07-27 11:00:36 +10003123 raid5_conf_t *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003124 int i;
NeilBrown84789552011-07-27 11:00:36 +10003125 int prexor;
3126 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003127 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003128
3129 clear_bit(STRIPE_HANDLE, &sh->state);
3130 if (test_and_set_bit(STRIPE_ACTIVE, &sh->state)) {
3131 /* already being handled, ensure it gets handled
3132 * again when current action finishes */
3133 set_bit(STRIPE_HANDLE, &sh->state);
3134 return;
3135 }
3136
3137 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3138 set_bit(STRIPE_SYNCING, &sh->state);
3139 clear_bit(STRIPE_INSYNC, &sh->state);
3140 }
3141 clear_bit(STRIPE_DELAYED, &sh->state);
3142
3143 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3144 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3145 (unsigned long long)sh->sector, sh->state,
3146 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3147 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003148
NeilBrownacfe7262011-07-27 11:00:36 +10003149 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003150
NeilBrownbc2607f2011-07-28 11:39:22 +10003151 if (s.handle_bad_blocks) {
3152 set_bit(STRIPE_HANDLE, &sh->state);
3153 goto finish;
3154 }
3155
NeilBrown474af965fe2011-07-27 11:00:36 +10003156 if (unlikely(s.blocked_rdev)) {
3157 if (s.syncing || s.expanding || s.expanded ||
3158 s.to_write || s.written) {
3159 set_bit(STRIPE_HANDLE, &sh->state);
3160 goto finish;
3161 }
3162 /* There is nothing for the blocked_rdev to block */
3163 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3164 s.blocked_rdev = NULL;
3165 }
3166
3167 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3168 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3169 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3170 }
3171
3172 pr_debug("locked=%d uptodate=%d to_read=%d"
3173 " to_write=%d failed=%d failed_num=%d,%d\n",
3174 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3175 s.failed_num[0], s.failed_num[1]);
3176 /* check if the array has lost more than max_degraded devices and,
3177 * if so, some requests might need to be failed.
3178 */
3179 if (s.failed > conf->max_degraded && s.to_read+s.to_write+s.written)
3180 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown7f0da592011-07-28 11:39:22 +10003181 if (s.failed > conf->max_degraded && s.syncing)
3182 handle_failed_sync(conf, sh, &s);
NeilBrown474af965fe2011-07-27 11:00:36 +10003183
3184 /*
3185 * might be able to return some write requests if the parity blocks
3186 * are safe, or on a failed drive
3187 */
3188 pdev = &sh->dev[sh->pd_idx];
3189 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3190 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3191 qdev = &sh->dev[sh->qd_idx];
3192 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3193 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3194 || conf->level < 6;
3195
3196 if (s.written &&
3197 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3198 && !test_bit(R5_LOCKED, &pdev->flags)
3199 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3200 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3201 && !test_bit(R5_LOCKED, &qdev->flags)
3202 && test_bit(R5_UPTODATE, &qdev->flags)))))
3203 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3204
3205 /* Now we might consider reading some blocks, either to check/generate
3206 * parity, or to satisfy requests
3207 * or to load a block that is being partially written.
3208 */
3209 if (s.to_read || s.non_overwrite
3210 || (conf->level == 6 && s.to_write && s.failed)
3211 || (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
3212 handle_stripe_fill(sh, &s, disks);
3213
NeilBrown84789552011-07-27 11:00:36 +10003214 /* Now we check to see if any write operations have recently
3215 * completed
3216 */
3217 prexor = 0;
3218 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3219 prexor = 1;
3220 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3221 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3222 sh->reconstruct_state = reconstruct_state_idle;
3223
3224 /* All the 'written' buffers and the parity block are ready to
3225 * be written back to disk
3226 */
3227 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3228 BUG_ON(sh->qd_idx >= 0 &&
3229 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags));
3230 for (i = disks; i--; ) {
3231 struct r5dev *dev = &sh->dev[i];
3232 if (test_bit(R5_LOCKED, &dev->flags) &&
3233 (i == sh->pd_idx || i == sh->qd_idx ||
3234 dev->written)) {
3235 pr_debug("Writing block %d\n", i);
3236 set_bit(R5_Wantwrite, &dev->flags);
3237 if (prexor)
3238 continue;
3239 if (!test_bit(R5_Insync, &dev->flags) ||
3240 ((i == sh->pd_idx || i == sh->qd_idx) &&
3241 s.failed == 0))
3242 set_bit(STRIPE_INSYNC, &sh->state);
3243 }
3244 }
3245 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3246 s.dec_preread_active = 1;
3247 }
3248
3249 /* Now to consider new write requests and what else, if anything
3250 * should be read. We do not handle new writes when:
3251 * 1/ A 'write' operation (copy+xor) is already in flight.
3252 * 2/ A 'check' operation is in flight, as it may clobber the parity
3253 * block.
3254 */
3255 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3256 handle_stripe_dirtying(conf, sh, &s, disks);
3257
3258 /* maybe we need to check and possibly fix the parity for this stripe
3259 * Any reads will already have been scheduled, so we just see if enough
3260 * data is available. The parity check is held off while parity
3261 * dependent operations are in flight.
3262 */
3263 if (sh->check_state ||
3264 (s.syncing && s.locked == 0 &&
3265 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3266 !test_bit(STRIPE_INSYNC, &sh->state))) {
3267 if (conf->level == 6)
3268 handle_parity_checks6(conf, sh, &s, disks);
3269 else
3270 handle_parity_checks5(conf, sh, &s, disks);
3271 }
NeilBrownc5a31002011-07-27 11:00:36 +10003272
3273 if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
3274 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3275 clear_bit(STRIPE_SYNCING, &sh->state);
3276 }
3277
3278 /* If the failed drives are just a ReadError, then we might need
3279 * to progress the repair/check process
3280 */
3281 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3282 for (i = 0; i < s.failed; i++) {
3283 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3284 if (test_bit(R5_ReadError, &dev->flags)
3285 && !test_bit(R5_LOCKED, &dev->flags)
3286 && test_bit(R5_UPTODATE, &dev->flags)
3287 ) {
3288 if (!test_bit(R5_ReWrite, &dev->flags)) {
3289 set_bit(R5_Wantwrite, &dev->flags);
3290 set_bit(R5_ReWrite, &dev->flags);
3291 set_bit(R5_LOCKED, &dev->flags);
3292 s.locked++;
3293 } else {
3294 /* let's read it back */
3295 set_bit(R5_Wantread, &dev->flags);
3296 set_bit(R5_LOCKED, &dev->flags);
3297 s.locked++;
3298 }
3299 }
3300 }
3301
3302
NeilBrown3687c062011-07-27 11:00:36 +10003303 /* Finish reconstruct operations initiated by the expansion process */
3304 if (sh->reconstruct_state == reconstruct_state_result) {
3305 struct stripe_head *sh_src
3306 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3307 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3308 /* sh cannot be written until sh_src has been read.
3309 * so arrange for sh to be delayed a little
3310 */
3311 set_bit(STRIPE_DELAYED, &sh->state);
3312 set_bit(STRIPE_HANDLE, &sh->state);
3313 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3314 &sh_src->state))
3315 atomic_inc(&conf->preread_active_stripes);
3316 release_stripe(sh_src);
3317 goto finish;
3318 }
3319 if (sh_src)
3320 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003321
NeilBrown3687c062011-07-27 11:00:36 +10003322 sh->reconstruct_state = reconstruct_state_idle;
3323 clear_bit(STRIPE_EXPANDING, &sh->state);
3324 for (i = conf->raid_disks; i--; ) {
3325 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3326 set_bit(R5_LOCKED, &sh->dev[i].flags);
3327 s.locked++;
3328 }
3329 }
3330
3331 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3332 !sh->reconstruct_state) {
3333 /* Need to write out all blocks after computing parity */
3334 sh->disks = conf->raid_disks;
3335 stripe_set_idx(sh->sector, conf, 0, sh);
3336 schedule_reconstruction(sh, &s, 1, 1);
3337 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3338 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3339 atomic_dec(&conf->reshape_stripes);
3340 wake_up(&conf->wait_for_overlap);
3341 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3342 }
3343
3344 if (s.expanding && s.locked == 0 &&
3345 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3346 handle_stripe_expansion(conf, sh);
3347
3348finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003349 /* wait for this device to become unblocked */
NeilBrown43220aa2011-08-31 12:49:14 +10003350 if (conf->mddev->external && unlikely(s.blocked_rdev))
NeilBrownc5709ef2011-07-26 11:35:20 +10003351 md_wait_for_blocked_rdev(s.blocked_rdev, conf->mddev);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003352
NeilBrownbc2607f2011-07-28 11:39:22 +10003353 if (s.handle_bad_blocks)
3354 for (i = disks; i--; ) {
3355 mdk_rdev_t *rdev;
3356 struct r5dev *dev = &sh->dev[i];
3357 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3358 /* We own a safe reference to the rdev */
3359 rdev = conf->disks[i].rdev;
3360 if (!rdev_set_badblocks(rdev, sh->sector,
3361 STRIPE_SECTORS, 0))
3362 md_error(conf->mddev, rdev);
3363 rdev_dec_pending(rdev, conf->mddev);
3364 }
NeilBrownb84db562011-07-28 11:39:23 +10003365 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3366 rdev = conf->disks[i].rdev;
3367 rdev_clear_badblocks(rdev, sh->sector,
3368 STRIPE_SECTORS);
3369 rdev_dec_pending(rdev, conf->mddev);
3370 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003371 }
3372
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003373 if (s.ops_request)
3374 raid_run_ops(sh, s.ops_request);
3375
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003376 ops_run_io(sh, &s);
3377
NeilBrownc5709ef2011-07-26 11:35:20 +10003378 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003379 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003380 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003381 * have actually been submitted.
3382 */
3383 atomic_dec(&conf->preread_active_stripes);
3384 if (atomic_read(&conf->preread_active_stripes) <
3385 IO_THRESHOLD)
3386 md_wakeup_thread(conf->mddev->thread);
3387 }
3388
NeilBrownc5709ef2011-07-26 11:35:20 +10003389 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003390
NeilBrownc4c16632011-07-26 11:34:20 +10003391 clear_bit(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003392}
3393
Arjan van de Ven858119e2006-01-14 13:20:43 -08003394static void raid5_activate_delayed(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395{
3396 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3397 while (!list_empty(&conf->delayed_list)) {
3398 struct list_head *l = conf->delayed_list.next;
3399 struct stripe_head *sh;
3400 sh = list_entry(l, struct stripe_head, lru);
3401 list_del_init(l);
3402 clear_bit(STRIPE_DELAYED, &sh->state);
3403 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3404 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003405 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 }
NeilBrown482c0832011-04-18 18:25:42 +10003407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408}
3409
Arjan van de Ven858119e2006-01-14 13:20:43 -08003410static void activate_bit_delay(raid5_conf_t *conf)
NeilBrown72626682005-09-09 16:23:54 -07003411{
3412 /* device_lock is held */
3413 struct list_head head;
3414 list_add(&head, &conf->bitmap_list);
3415 list_del_init(&conf->bitmap_list);
3416 while (!list_empty(&head)) {
3417 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3418 list_del_init(&sh->lru);
3419 atomic_inc(&sh->count);
3420 __release_stripe(conf, sh);
3421 }
3422}
3423
NeilBrown11d8a6e2010-07-26 11:57:07 +10003424int md_raid5_congested(mddev_t *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003425{
NeilBrown070ec552009-06-16 16:54:21 +10003426 raid5_conf_t *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003427
3428 /* No difference between reads and writes. Just check
3429 * how busy the stripe_cache is
3430 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003431
NeilBrownf022b2f2006-10-03 01:15:56 -07003432 if (conf->inactive_blocked)
3433 return 1;
3434 if (conf->quiesce)
3435 return 1;
3436 if (list_empty_careful(&conf->inactive_list))
3437 return 1;
3438
3439 return 0;
3440}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003441EXPORT_SYMBOL_GPL(md_raid5_congested);
3442
3443static int raid5_congested(void *data, int bits)
3444{
3445 mddev_t *mddev = data;
3446
3447 return mddev_congested(mddev, bits) ||
3448 md_raid5_congested(mddev, bits);
3449}
NeilBrownf022b2f2006-10-03 01:15:56 -07003450
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003451/* We want read requests to align with chunks where possible,
3452 * but write requests don't need to.
3453 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003454static int raid5_mergeable_bvec(struct request_queue *q,
3455 struct bvec_merge_data *bvm,
3456 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003457{
3458 mddev_t *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003459 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003460 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003461 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003462 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003463
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003464 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003465 return biovec->bv_len; /* always allow writes to be mergeable */
3466
Andre Noll664e7c42009-06-18 08:45:27 +10003467 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3468 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003469 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3470 if (max < 0) max = 0;
3471 if (max <= biovec->bv_len && bio_sectors == 0)
3472 return biovec->bv_len;
3473 else
3474 return max;
3475}
3476
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003477
3478static int in_chunk_boundary(mddev_t *mddev, struct bio *bio)
3479{
3480 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003481 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003482 unsigned int bio_sectors = bio->bi_size >> 9;
3483
Andre Noll664e7c42009-06-18 08:45:27 +10003484 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3485 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003486 return chunk_sectors >=
3487 ((sector & (chunk_sectors - 1)) + bio_sectors);
3488}
3489
3490/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003491 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3492 * later sampled by raid5d.
3493 */
3494static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf)
3495{
3496 unsigned long flags;
3497
3498 spin_lock_irqsave(&conf->device_lock, flags);
3499
3500 bi->bi_next = conf->retry_read_aligned_list;
3501 conf->retry_read_aligned_list = bi;
3502
3503 spin_unlock_irqrestore(&conf->device_lock, flags);
3504 md_wakeup_thread(conf->mddev->thread);
3505}
3506
3507
3508static struct bio *remove_bio_from_retry(raid5_conf_t *conf)
3509{
3510 struct bio *bi;
3511
3512 bi = conf->retry_read_aligned;
3513 if (bi) {
3514 conf->retry_read_aligned = NULL;
3515 return bi;
3516 }
3517 bi = conf->retry_read_aligned_list;
3518 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003519 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003520 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003521 /*
3522 * this sets the active strip count to 1 and the processed
3523 * strip count to zero (upper 8 bits)
3524 */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003525 bi->bi_phys_segments = 1; /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003526 }
3527
3528 return bi;
3529}
3530
3531
3532/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003533 * The "raid5_align_endio" should check if the read succeeded and if it
3534 * did, call bio_endio on the original bio (having bio_put the new bio
3535 * first).
3536 * If the read failed..
3537 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003538static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003539{
3540 struct bio* raid_bi = bi->bi_private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003541 mddev_t *mddev;
3542 raid5_conf_t *conf;
3543 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
3544 mdk_rdev_t *rdev;
3545
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003546 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003547
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003548 rdev = (void*)raid_bi->bi_next;
3549 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003550 mddev = rdev->mddev;
3551 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003552
3553 rdev_dec_pending(rdev, conf->mddev);
3554
3555 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003556 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003557 if (atomic_dec_and_test(&conf->active_aligned_reads))
3558 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003559 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003560 }
3561
3562
Dan Williams45b42332007-07-09 11:56:43 -07003563 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003564
3565 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003566}
3567
Neil Brown387bb172007-02-08 14:20:29 -08003568static int bio_fits_rdev(struct bio *bi)
3569{
Jens Axboe165125e2007-07-24 09:28:11 +02003570 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003571
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003572 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003573 return 0;
3574 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003575 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003576 return 0;
3577
3578 if (q->merge_bvec_fn)
3579 /* it's too hard to apply the merge_bvec_fn at this stage,
3580 * just just give up
3581 */
3582 return 0;
3583
3584 return 1;
3585}
3586
3587
NeilBrown21a52c62010-04-01 15:02:13 +11003588static int chunk_aligned_read(mddev_t *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003589{
NeilBrown070ec552009-06-16 16:54:21 +10003590 raid5_conf_t *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11003591 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003592 struct bio* align_bi;
3593 mdk_rdev_t *rdev;
3594
3595 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003596 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003597 return 0;
3598 }
3599 /*
NeilBrowna167f662010-10-26 18:31:13 +11003600 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003601 */
NeilBrowna167f662010-10-26 18:31:13 +11003602 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003603 if (!align_bi)
3604 return 0;
3605 /*
3606 * set bi_end_io to a new function, and set bi_private to the
3607 * original bio.
3608 */
3609 align_bi->bi_end_io = raid5_align_endio;
3610 align_bi->bi_private = raid_bio;
3611 /*
3612 * compute position
3613 */
NeilBrown112bf892009-03-31 14:39:38 +11003614 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3615 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003616 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003617
3618 rcu_read_lock();
3619 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3620 if (rdev && test_bit(In_sync, &rdev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003621 sector_t first_bad;
3622 int bad_sectors;
3623
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003624 atomic_inc(&rdev->nr_pending);
3625 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003626 raid_bio->bi_next = (void*)rdev;
3627 align_bi->bi_bdev = rdev->bdev;
3628 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
3629 align_bi->bi_sector += rdev->data_offset;
3630
NeilBrown31c176e2011-07-28 11:39:22 +10003631 if (!bio_fits_rdev(align_bi) ||
3632 is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
3633 &first_bad, &bad_sectors)) {
3634 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08003635 bio_put(align_bi);
3636 rdev_dec_pending(rdev, mddev);
3637 return 0;
3638 }
3639
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003640 spin_lock_irq(&conf->device_lock);
3641 wait_event_lock_irq(conf->wait_for_stripe,
3642 conf->quiesce == 0,
3643 conf->device_lock, /* nothing */);
3644 atomic_inc(&conf->active_aligned_reads);
3645 spin_unlock_irq(&conf->device_lock);
3646
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003647 generic_make_request(align_bi);
3648 return 1;
3649 } else {
3650 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003651 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003652 return 0;
3653 }
3654}
3655
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003656/* __get_priority_stripe - get the next stripe to process
3657 *
3658 * Full stripe writes are allowed to pass preread active stripes up until
3659 * the bypass_threshold is exceeded. In general the bypass_count
3660 * increments when the handle_list is handled before the hold_list; however, it
3661 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3662 * stripe with in flight i/o. The bypass_count will be reset when the
3663 * head of the hold_list has changed, i.e. the head was promoted to the
3664 * handle_list.
3665 */
3666static struct stripe_head *__get_priority_stripe(raid5_conf_t *conf)
3667{
3668 struct stripe_head *sh;
3669
3670 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3671 __func__,
3672 list_empty(&conf->handle_list) ? "empty" : "busy",
3673 list_empty(&conf->hold_list) ? "empty" : "busy",
3674 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3675
3676 if (!list_empty(&conf->handle_list)) {
3677 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3678
3679 if (list_empty(&conf->hold_list))
3680 conf->bypass_count = 0;
3681 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3682 if (conf->hold_list.next == conf->last_hold)
3683 conf->bypass_count++;
3684 else {
3685 conf->last_hold = conf->hold_list.next;
3686 conf->bypass_count -= conf->bypass_threshold;
3687 if (conf->bypass_count < 0)
3688 conf->bypass_count = 0;
3689 }
3690 }
3691 } else if (!list_empty(&conf->hold_list) &&
3692 ((conf->bypass_threshold &&
3693 conf->bypass_count > conf->bypass_threshold) ||
3694 atomic_read(&conf->pending_full_writes) == 0)) {
3695 sh = list_entry(conf->hold_list.next,
3696 typeof(*sh), lru);
3697 conf->bypass_count -= conf->bypass_threshold;
3698 if (conf->bypass_count < 0)
3699 conf->bypass_count = 0;
3700 } else
3701 return NULL;
3702
3703 list_del_init(&sh->lru);
3704 atomic_inc(&sh->count);
3705 BUG_ON(atomic_read(&sh->count) != 1);
3706 return sh;
3707}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003708
NeilBrown21a52c62010-04-01 15:02:13 +11003709static int make_request(mddev_t *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710{
NeilBrown070ec552009-06-16 16:54:21 +10003711 raid5_conf_t *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11003712 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 sector_t new_sector;
3714 sector_t logical_sector, last_sector;
3715 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01003716 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11003717 int remaining;
NeilBrown7c13edc2011-04-18 18:25:43 +10003718 int plugged;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719
Tejun Heoe9c74692010-09-03 11:56:18 +02003720 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
3721 md_flush_request(mddev, bi);
NeilBrowne5dcdd82005-09-09 16:23:41 -07003722 return 0;
3723 }
3724
NeilBrown3d310eb2005-06-21 17:17:26 -07003725 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07003726
NeilBrown802ba062006-12-13 00:34:13 -08003727 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003728 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11003729 chunk_aligned_read(mddev,bi))
NeilBrown99c0fb52009-03-31 14:39:38 +11003730 return 0;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003731
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
3733 last_sector = bi->bi_sector + (bi->bi_size>>9);
3734 bi->bi_next = NULL;
3735 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07003736
NeilBrown7c13edc2011-04-18 18:25:43 +10003737 plugged = mddev_check_plugged(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
3739 DEFINE_WAIT(w);
NeilBrown16a53ec2006-06-26 00:27:38 -07003740 int disks, data_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003741 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08003742
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003743 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11003744 previous = 0;
NeilBrownb0f9ec02009-03-31 15:27:18 +11003745 disks = conf->raid_disks;
NeilBrownb578d552006-03-27 01:18:12 -08003746 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11003747 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11003748 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f762006-03-27 01:18:15 -08003749 * 64bit on a 32bit platform, and so it might be
3750 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02003751 * Of course reshape_progress could change after
NeilBrowndf8e7f762006-03-27 01:18:15 -08003752 * the lock is dropped, so once we get a reference
3753 * to the stripe that we think it is, we will have
3754 * to check again.
3755 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003756 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11003757 if (mddev->delta_disks < 0
3758 ? logical_sector < conf->reshape_progress
3759 : logical_sector >= conf->reshape_progress) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003760 disks = conf->previous_raid_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003761 previous = 1;
3762 } else {
NeilBrownfef9c612009-03-31 15:16:46 +11003763 if (mddev->delta_disks < 0
3764 ? logical_sector < conf->reshape_safe
3765 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08003766 spin_unlock_irq(&conf->device_lock);
3767 schedule();
3768 goto retry;
3769 }
3770 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003771 spin_unlock_irq(&conf->device_lock);
3772 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003773 data_disks = disks - conf->max_degraded;
3774
NeilBrown112bf892009-03-31 14:39:38 +11003775 new_sector = raid5_compute_sector(conf, logical_sector,
3776 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003777 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10003778 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 (unsigned long long)new_sector,
3780 (unsigned long long)logical_sector);
3781
NeilBrownb5663ba2009-03-31 14:39:38 +11003782 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10003783 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11003785 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003786 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f762006-03-27 01:18:15 -08003787 * stripe, so we must do the range check again.
3788 * Expansion could still move past after this
3789 * test, but as we are holding a reference to
3790 * 'sh', we know that if that happens,
3791 * STRIPE_EXPANDING will get set and the expansion
3792 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003793 */
3794 int must_retry = 0;
3795 spin_lock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11003796 if (mddev->delta_disks < 0
3797 ? logical_sector >= conf->reshape_progress
3798 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003799 /* mismatch, need to try again */
3800 must_retry = 1;
3801 spin_unlock_irq(&conf->device_lock);
3802 if (must_retry) {
3803 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07003804 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003805 goto retry;
3806 }
3807 }
NeilBrowne62e58a2009-07-01 13:15:35 +10003808
Namhyung Kimffd96e32011-07-18 17:38:51 +10003809 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10003810 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08003811 logical_sector < mddev->suspend_hi) {
3812 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10003813 /* As the suspend_* range is controlled by
3814 * userspace, we want an interruptible
3815 * wait.
3816 */
3817 flush_signals(current);
3818 prepare_to_wait(&conf->wait_for_overlap,
3819 &w, TASK_INTERRUPTIBLE);
3820 if (logical_sector >= mddev->suspend_lo &&
3821 logical_sector < mddev->suspend_hi)
3822 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08003823 goto retry;
3824 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003825
3826 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10003827 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003828 /* Stripe is busy expanding or
3829 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 * and wait a while
3831 */
NeilBrown482c0832011-04-18 18:25:42 +10003832 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 release_stripe(sh);
3834 schedule();
3835 goto retry;
3836 }
3837 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08003838 set_bit(STRIPE_HANDLE, &sh->state);
3839 clear_bit(STRIPE_DELAYED, &sh->state);
Tejun Heoe9c74692010-09-03 11:56:18 +02003840 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11003841 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3842 atomic_inc(&conf->preread_active_stripes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 } else {
3845 /* cannot get stripe for read-ahead, just give-up */
3846 clear_bit(BIO_UPTODATE, &bi->bi_flags);
3847 finish_wait(&conf->wait_for_overlap, &w);
3848 break;
3849 }
3850
3851 }
NeilBrown7c13edc2011-04-18 18:25:43 +10003852 if (!plugged)
3853 md_wakeup_thread(mddev->thread);
3854
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02003856 remaining = raid5_dec_bi_phys_segments(bi);
NeilBrownf6344752006-03-27 01:18:17 -08003857 spin_unlock_irq(&conf->device_lock);
3858 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859
NeilBrown16a53ec2006-06-26 00:27:38 -07003860 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02003862
Neil Brown0e13fe232008-06-28 08:31:20 +10003863 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 }
NeilBrown729a1862009-12-14 12:49:50 +11003865
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 return 0;
3867}
3868
Dan Williamsb522adc2009-03-31 15:00:31 +11003869static sector_t raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks);
3870
NeilBrown52c03292006-06-26 00:27:43 -07003871static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872{
NeilBrown52c03292006-06-26 00:27:43 -07003873 /* reshaping is quite different to recovery/resync so it is
3874 * handled quite separately ... here.
3875 *
3876 * On each call to sync_request, we gather one chunk worth of
3877 * destination stripes and flag them as expanding.
3878 * Then we find all the source stripes and request reads.
3879 * As the reads complete, handle_stripe will copy the data
3880 * into the destination stripe and release that stripe.
3881 */
H Hartley Sweeten7b928132010-03-08 16:02:40 +11003882 raid5_conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08003884 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08003885 int raid_disks = conf->previous_raid_disks;
3886 int data_disks = raid_disks - conf->max_degraded;
3887 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07003888 int i;
3889 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11003890 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11003891 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11003892 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11003893 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07003894
NeilBrownfef9c612009-03-31 15:16:46 +11003895 if (sector_nr == 0) {
3896 /* If restarting in the middle, skip the initial sectors */
3897 if (mddev->delta_disks < 0 &&
3898 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
3899 sector_nr = raid5_size(mddev, 0, 0)
3900 - conf->reshape_progress;
NeilBrowna6397552009-08-13 10:13:00 +10003901 } else if (mddev->delta_disks >= 0 &&
NeilBrownfef9c612009-03-31 15:16:46 +11003902 conf->reshape_progress > 0)
3903 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08003904 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11003905 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11003906 mddev->curr_resync_completed = sector_nr;
3907 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11003908 *skipped = 1;
3909 return sector_nr;
3910 }
NeilBrown52c03292006-06-26 00:27:43 -07003911 }
3912
NeilBrown7a661382009-03-31 15:21:40 +11003913 /* We need to process a full chunk at a time.
3914 * If old and new chunk sizes differ, we need to process the
3915 * largest of these
3916 */
Andre Noll664e7c42009-06-18 08:45:27 +10003917 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
3918 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11003919 else
Andre Noll9d8f0362009-06-18 08:45:01 +10003920 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11003921
NeilBrown52c03292006-06-26 00:27:43 -07003922 /* we update the metadata when there is more than 3Meg
3923 * in the block range (that is rather arbitrary, should
3924 * probably be time based) or when the data about to be
3925 * copied would over-write the source of the data at
3926 * the front of the range.
NeilBrownfef9c612009-03-31 15:16:46 +11003927 * i.e. one new_stripe along from reshape_progress new_maps
3928 * to after where reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07003929 */
NeilBrownfef9c612009-03-31 15:16:46 +11003930 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08003931 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11003932 readpos = conf->reshape_progress;
3933 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11003934 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08003935 sector_div(safepos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11003936 if (mddev->delta_disks < 0) {
NeilBrowned37d832009-05-27 21:39:05 +10003937 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11003938 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11003939 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11003940 } else {
NeilBrown7a661382009-03-31 15:21:40 +11003941 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10003942 readpos -= min_t(sector_t, reshape_sectors, readpos);
3943 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11003944 }
NeilBrown52c03292006-06-26 00:27:43 -07003945
NeilBrownc8f517c2009-03-31 15:28:40 +11003946 /* 'writepos' is the most advanced device address we might write.
3947 * 'readpos' is the least advanced device address we might read.
3948 * 'safepos' is the least address recorded in the metadata as having
3949 * been reshaped.
3950 * If 'readpos' is behind 'writepos', then there is no way that we can
3951 * ensure safety in the face of a crash - that must be done by userspace
3952 * making a backup of the data. So in that case there is no particular
3953 * rush to update metadata.
3954 * Otherwise if 'safepos' is behind 'writepos', then we really need to
3955 * update the metadata to advance 'safepos' to match 'readpos' so that
3956 * we can be safe in the event of a crash.
3957 * So we insist on updating metadata if safepos is behind writepos and
3958 * readpos is beyond writepos.
3959 * In any case, update the metadata every 10 seconds.
3960 * Maybe that number should be configurable, but I'm not sure it is
3961 * worth it.... maybe it could be a multiple of safemode_delay???
3962 */
NeilBrownfef9c612009-03-31 15:16:46 +11003963 if ((mddev->delta_disks < 0
NeilBrownc8f517c2009-03-31 15:28:40 +11003964 ? (safepos > writepos && readpos < writepos)
3965 : (safepos < writepos && readpos > writepos)) ||
3966 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07003967 /* Cannot proceed until we've updated the superblock... */
3968 wait_event(conf->wait_for_overlap,
3969 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11003970 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11003971 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11003972 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07003973 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07003974 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07003975 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07003976 kthread_should_stop());
3977 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11003978 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07003979 spin_unlock_irq(&conf->device_lock);
3980 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10003981 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07003982 }
3983
NeilBrownec32a2b2009-03-31 15:17:38 +11003984 if (mddev->delta_disks < 0) {
3985 BUG_ON(conf->reshape_progress == 0);
3986 stripe_addr = writepos;
3987 BUG_ON((mddev->dev_sectors &
NeilBrown7a661382009-03-31 15:21:40 +11003988 ~((sector_t)reshape_sectors - 1))
3989 - reshape_sectors - stripe_addr
NeilBrownec32a2b2009-03-31 15:17:38 +11003990 != sector_nr);
3991 } else {
NeilBrown7a661382009-03-31 15:21:40 +11003992 BUG_ON(writepos != sector_nr + reshape_sectors);
NeilBrownec32a2b2009-03-31 15:17:38 +11003993 stripe_addr = sector_nr;
3994 }
NeilBrownab69ae12009-03-31 15:26:47 +11003995 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11003996 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07003997 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10003998 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10003999 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004000 set_bit(STRIPE_EXPANDING, &sh->state);
4001 atomic_inc(&conf->reshape_stripes);
4002 /* If any of this stripe is beyond the end of the old
4003 * array, then we need to zero those blocks
4004 */
4005 for (j=sh->disks; j--;) {
4006 sector_t s;
4007 if (j == sh->pd_idx)
4008 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004009 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004010 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004011 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004012 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004013 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004014 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004015 continue;
4016 }
4017 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4018 set_bit(R5_Expanded, &sh->dev[j].flags);
4019 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4020 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004021 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004022 set_bit(STRIPE_EXPAND_READY, &sh->state);
4023 set_bit(STRIPE_HANDLE, &sh->state);
4024 }
NeilBrownab69ae12009-03-31 15:26:47 +11004025 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004026 }
4027 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004028 if (mddev->delta_disks < 0)
NeilBrown7a661382009-03-31 15:21:40 +11004029 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004030 else
NeilBrown7a661382009-03-31 15:21:40 +11004031 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004032 spin_unlock_irq(&conf->device_lock);
4033 /* Ok, those stripe are ready. We can start scheduling
4034 * reads on the source stripes.
4035 * The source stripes are determined by mapping the first and last
4036 * block on the destination stripes.
4037 */
NeilBrown52c03292006-06-26 00:27:43 -07004038 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004039 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004040 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004041 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004042 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004043 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004044 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004045 if (last_sector >= mddev->dev_sectors)
4046 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004047 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004048 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004049 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4050 set_bit(STRIPE_HANDLE, &sh->state);
4051 release_stripe(sh);
4052 first_sector += STRIPE_SECTORS;
4053 }
NeilBrownab69ae12009-03-31 15:26:47 +11004054 /* Now that the sources are clearly marked, we can release
4055 * the destination stripes
4056 */
4057 while (!list_empty(&stripes)) {
4058 sh = list_entry(stripes.next, struct stripe_head, lru);
4059 list_del_init(&sh->lru);
4060 release_stripe(sh);
4061 }
NeilBrownc6207272008-02-06 01:39:52 -08004062 /* If this takes us to the resync_max point where we have to pause,
4063 * then we need to write out the superblock.
4064 */
NeilBrown7a661382009-03-31 15:21:40 +11004065 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004066 if ((sector_nr - mddev->curr_resync_completed) * 2
4067 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004068 /* Cannot proceed until we've updated the superblock... */
4069 wait_event(conf->wait_for_overlap,
4070 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004071 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004072 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004073 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004074 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4075 md_wakeup_thread(mddev->thread);
4076 wait_event(mddev->sb_wait,
4077 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4078 || kthread_should_stop());
4079 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004080 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004081 spin_unlock_irq(&conf->device_lock);
4082 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004083 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004084 }
NeilBrown7a661382009-03-31 15:21:40 +11004085 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004086}
4087
4088/* FIXME go_faster isn't used */
4089static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
4090{
H Hartley Sweeten7b928132010-03-08 16:02:40 +11004091 raid5_conf_t *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004092 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004093 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004094 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004095 int still_degraded = 0;
4096 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097
NeilBrown72626682005-09-09 16:23:54 -07004098 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004100
NeilBrown29269552006-03-27 01:18:10 -08004101 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4102 end_reshape(conf);
4103 return 0;
4104 }
NeilBrown72626682005-09-09 16:23:54 -07004105
4106 if (mddev->curr_resync < max_sector) /* aborted */
4107 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4108 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004109 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004110 conf->fullsync = 0;
4111 bitmap_close_sync(mddev->bitmap);
4112
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 return 0;
4114 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004115
NeilBrown64bd6602009-08-03 10:59:58 +10004116 /* Allow raid5_quiesce to complete */
4117 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4118
NeilBrown52c03292006-06-26 00:27:43 -07004119 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4120 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004121
NeilBrownc6207272008-02-06 01:39:52 -08004122 /* No need to check resync_max as we never do more than one
4123 * stripe, and as resync_max will always be on a chunk boundary,
4124 * if the check in md_do_sync didn't fire, there is no chance
4125 * of overstepping resync_max here
4126 */
4127
NeilBrown16a53ec2006-06-26 00:27:38 -07004128 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 * to resync, then assert that we are finished, because there is
4130 * nothing we can do.
4131 */
NeilBrown3285edf2006-06-26 00:27:55 -07004132 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004133 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004134 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004135 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 return rv;
4137 }
NeilBrown72626682005-09-09 16:23:54 -07004138 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004139 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004140 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4141 /* we can skip this block, and probably more */
4142 sync_blocks /= STRIPE_SECTORS;
4143 *skipped = 1;
4144 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146
NeilBrownb47490c2008-02-06 01:39:50 -08004147
4148 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4149
NeilBrowna8c906c2009-06-09 14:39:59 +10004150 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004152 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004154 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004156 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004158 /* Need to check if array will still be degraded after recovery/resync
4159 * We don't need to check the 'failed' flag as when that gets set,
4160 * recovery aborts.
4161 */
NeilBrownf001a702009-06-09 14:30:31 +10004162 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004163 if (conf->disks[i].rdev == NULL)
4164 still_degraded = 1;
4165
4166 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4167
NeilBrown83206d62011-07-26 11:19:49 +10004168 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169
NeilBrown14425772009-10-16 15:55:25 +11004170 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 release_stripe(sh);
4172
4173 return STRIPE_SECTORS;
4174}
4175
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004176static int retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio)
4177{
4178 /* We may not be able to submit a whole bio at once as there
4179 * may not be enough stripe_heads available.
4180 * We cannot pre-allocate enough stripe_heads as we may need
4181 * more than exist in the cache (if we allow ever large chunks).
4182 * So we do one stripe head at a time and record in
4183 * ->bi_hw_segments how many have been done.
4184 *
4185 * We *know* that this entire raid_bio is in one chunk, so
4186 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4187 */
4188 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004189 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004190 sector_t sector, logical_sector, last_sector;
4191 int scnt = 0;
4192 int remaining;
4193 int handled = 0;
4194
4195 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004196 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004197 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004198 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4199
4200 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004201 logical_sector += STRIPE_SECTORS,
4202 sector += STRIPE_SECTORS,
4203 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004204
Jens Axboe960e7392008-08-15 10:41:18 +02004205 if (scnt < raid5_bi_hw_segments(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004206 /* already done this stripe */
4207 continue;
4208
NeilBrowna8c906c2009-06-09 14:39:59 +10004209 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004210
4211 if (!sh) {
4212 /* failed to get a stripe - must wait */
Jens Axboe960e7392008-08-15 10:41:18 +02004213 raid5_set_bi_hw_segments(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004214 conf->retry_read_aligned = raid_bio;
4215 return handled;
4216 }
4217
4218 set_bit(R5_ReadError, &sh->dev[dd_idx].flags);
Neil Brown387bb172007-02-08 14:20:29 -08004219 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4220 release_stripe(sh);
Jens Axboe960e7392008-08-15 10:41:18 +02004221 raid5_set_bi_hw_segments(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004222 conf->retry_read_aligned = raid_bio;
4223 return handled;
4224 }
4225
Dan Williams36d1c642009-07-14 11:48:22 -07004226 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004227 release_stripe(sh);
4228 handled++;
4229 }
4230 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004231 remaining = raid5_dec_bi_phys_segments(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004232 spin_unlock_irq(&conf->device_lock);
Neil Brown0e13fe232008-06-28 08:31:20 +10004233 if (remaining == 0)
4234 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004235 if (atomic_dec_and_test(&conf->active_aligned_reads))
4236 wake_up(&conf->wait_for_stripe);
4237 return handled;
4238}
4239
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004240
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241/*
4242 * This is our raid5 kernel thread.
4243 *
4244 * We scan the hash table for stripes which can be handled now.
4245 * During the scan, completed stripes are saved for us by the interrupt
4246 * handler, so that they will not have to wait for our next wakeup.
4247 */
NeilBrown6ed30032008-02-06 01:40:00 -08004248static void raid5d(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249{
4250 struct stripe_head *sh;
NeilBrown070ec552009-06-16 16:54:21 +10004251 raid5_conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004253 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254
Dan Williams45b42332007-07-09 11:56:43 -07004255 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256
4257 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004259 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 handled = 0;
4261 spin_lock_irq(&conf->device_lock);
4262 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004263 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264
NeilBrown7c13edc2011-04-18 18:25:43 +10004265 if (atomic_read(&mddev->plug_cnt) == 0 &&
4266 !list_empty(&conf->bitmap_list)) {
4267 /* Now is a good time to flush some bitmap updates */
4268 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004269 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004270 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004271 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004272 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004273 activate_bit_delay(conf);
4274 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004275 if (atomic_read(&mddev->plug_cnt) == 0)
4276 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004277
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004278 while ((bio = remove_bio_from_retry(conf))) {
4279 int ok;
4280 spin_unlock_irq(&conf->device_lock);
4281 ok = retry_aligned_read(conf, bio);
4282 spin_lock_irq(&conf->device_lock);
4283 if (!ok)
4284 break;
4285 handled++;
4286 }
4287
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004288 sh = __get_priority_stripe(conf);
4289
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004290 if (!sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 spin_unlock_irq(&conf->device_lock);
4293
4294 handled++;
Dan Williams417b8d42009-10-16 16:25:22 +11004295 handle_stripe(sh);
4296 release_stripe(sh);
4297 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298
NeilBrownde393cd2011-07-28 11:31:48 +10004299 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
4300 md_check_recovery(mddev);
4301
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 spin_lock_irq(&conf->device_lock);
4303 }
Dan Williams45b42332007-07-09 11:56:43 -07004304 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305
4306 spin_unlock_irq(&conf->device_lock);
4307
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004308 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004309 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310
Dan Williams45b42332007-07-09 11:56:43 -07004311 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312}
4313
NeilBrown3f294f42005-11-08 21:39:25 -08004314static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08004315raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004316{
NeilBrown070ec552009-06-16 16:54:21 +10004317 raid5_conf_t *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004318 if (conf)
4319 return sprintf(page, "%d\n", conf->max_nr_stripes);
4320 else
4321 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004322}
4323
NeilBrownc41d4ac2010-06-01 19:37:24 +10004324int
4325raid5_set_cache_size(mddev_t *mddev, int size)
4326{
4327 raid5_conf_t *conf = mddev->private;
4328 int err;
4329
4330 if (size <= 16 || size > 32768)
4331 return -EINVAL;
4332 while (size < conf->max_nr_stripes) {
4333 if (drop_one_stripe(conf))
4334 conf->max_nr_stripes--;
4335 else
4336 break;
4337 }
4338 err = md_allow_write(mddev);
4339 if (err)
4340 return err;
4341 while (size > conf->max_nr_stripes) {
4342 if (grow_one_stripe(conf))
4343 conf->max_nr_stripes++;
4344 else break;
4345 }
4346 return 0;
4347}
4348EXPORT_SYMBOL(raid5_set_cache_size);
4349
NeilBrown3f294f42005-11-08 21:39:25 -08004350static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08004351raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004352{
NeilBrown070ec552009-06-16 16:54:21 +10004353 raid5_conf_t *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004354 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004355 int err;
4356
NeilBrown3f294f42005-11-08 21:39:25 -08004357 if (len >= PAGE_SIZE)
4358 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004359 if (!conf)
4360 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004361
Dan Williams4ef197d82008-04-28 02:15:54 -07004362 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004363 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004364 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004365 if (err)
4366 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004367 return len;
4368}
NeilBrown007583c2005-11-08 21:39:30 -08004369
NeilBrown96de1e62005-11-08 21:39:39 -08004370static struct md_sysfs_entry
4371raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4372 raid5_show_stripe_cache_size,
4373 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004374
4375static ssize_t
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004376raid5_show_preread_threshold(mddev_t *mddev, char *page)
4377{
NeilBrown070ec552009-06-16 16:54:21 +10004378 raid5_conf_t *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004379 if (conf)
4380 return sprintf(page, "%d\n", conf->bypass_threshold);
4381 else
4382 return 0;
4383}
4384
4385static ssize_t
4386raid5_store_preread_threshold(mddev_t *mddev, const char *page, size_t len)
4387{
NeilBrown070ec552009-06-16 16:54:21 +10004388 raid5_conf_t *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004389 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004390 if (len >= PAGE_SIZE)
4391 return -EINVAL;
4392 if (!conf)
4393 return -ENODEV;
4394
Dan Williams4ef197d82008-04-28 02:15:54 -07004395 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004396 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004397 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004398 return -EINVAL;
4399 conf->bypass_threshold = new;
4400 return len;
4401}
4402
4403static struct md_sysfs_entry
4404raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4405 S_IRUGO | S_IWUSR,
4406 raid5_show_preread_threshold,
4407 raid5_store_preread_threshold);
4408
4409static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08004410stripe_cache_active_show(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004411{
NeilBrown070ec552009-06-16 16:54:21 +10004412 raid5_conf_t *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004413 if (conf)
4414 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4415 else
4416 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004417}
4418
NeilBrown96de1e62005-11-08 21:39:39 -08004419static struct md_sysfs_entry
4420raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004421
NeilBrown007583c2005-11-08 21:39:30 -08004422static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004423 &raid5_stripecache_size.attr,
4424 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004425 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004426 NULL,
4427};
NeilBrown007583c2005-11-08 21:39:30 -08004428static struct attribute_group raid5_attrs_group = {
4429 .name = NULL,
4430 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004431};
4432
Dan Williams80c3a6c2009-03-17 18:10:40 -07004433static sector_t
4434raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks)
4435{
NeilBrown070ec552009-06-16 16:54:21 +10004436 raid5_conf_t *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004437
4438 if (!sectors)
4439 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004440 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11004441 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11004442 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004443
Andre Noll9d8f0362009-06-18 08:45:01 +10004444 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004445 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004446 return sectors * (raid_disks - conf->max_degraded);
4447}
4448
Dan Williams36d1c642009-07-14 11:48:22 -07004449static void raid5_free_percpu(raid5_conf_t *conf)
4450{
4451 struct raid5_percpu *percpu;
4452 unsigned long cpu;
4453
4454 if (!conf->percpu)
4455 return;
4456
4457 get_online_cpus();
4458 for_each_possible_cpu(cpu) {
4459 percpu = per_cpu_ptr(conf->percpu, cpu);
4460 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004461 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004462 }
4463#ifdef CONFIG_HOTPLUG_CPU
4464 unregister_cpu_notifier(&conf->cpu_notify);
4465#endif
4466 put_online_cpus();
4467
4468 free_percpu(conf->percpu);
4469}
4470
Dan Williams95fc17a2009-07-31 12:39:15 +10004471static void free_conf(raid5_conf_t *conf)
4472{
4473 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004474 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10004475 kfree(conf->disks);
4476 kfree(conf->stripe_hashtbl);
4477 kfree(conf);
4478}
4479
Dan Williams36d1c642009-07-14 11:48:22 -07004480#ifdef CONFIG_HOTPLUG_CPU
4481static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4482 void *hcpu)
4483{
4484 raid5_conf_t *conf = container_of(nfb, raid5_conf_t, cpu_notify);
4485 long cpu = (long)hcpu;
4486 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4487
4488 switch (action) {
4489 case CPU_UP_PREPARE:
4490 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07004491 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07004492 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004493 if (!percpu->scribble)
4494 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4495
4496 if (!percpu->scribble ||
4497 (conf->level == 6 && !percpu->spare_page)) {
4498 safe_put_page(percpu->spare_page);
4499 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004500 pr_err("%s: failed memory allocation for cpu%ld\n",
4501 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07004502 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07004503 }
4504 break;
4505 case CPU_DEAD:
4506 case CPU_DEAD_FROZEN:
4507 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004508 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004509 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004510 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07004511 break;
4512 default:
4513 break;
4514 }
4515 return NOTIFY_OK;
4516}
4517#endif
4518
4519static int raid5_alloc_percpu(raid5_conf_t *conf)
4520{
4521 unsigned long cpu;
4522 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09004523 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004524 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004525 int err;
4526
Dan Williams36d1c642009-07-14 11:48:22 -07004527 allcpus = alloc_percpu(struct raid5_percpu);
4528 if (!allcpus)
4529 return -ENOMEM;
4530 conf->percpu = allcpus;
4531
4532 get_online_cpus();
4533 err = 0;
4534 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07004535 if (conf->level == 6) {
4536 spare_page = alloc_page(GFP_KERNEL);
4537 if (!spare_page) {
4538 err = -ENOMEM;
4539 break;
4540 }
4541 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
4542 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11004543 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004544 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07004545 err = -ENOMEM;
4546 break;
4547 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07004548 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004549 }
4550#ifdef CONFIG_HOTPLUG_CPU
4551 conf->cpu_notify.notifier_call = raid456_cpu_notify;
4552 conf->cpu_notify.priority = 0;
4553 if (err == 0)
4554 err = register_cpu_notifier(&conf->cpu_notify);
4555#endif
4556 put_online_cpus();
4557
4558 return err;
4559}
4560
NeilBrown91adb562009-03-31 14:39:39 +11004561static raid5_conf_t *setup_conf(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562{
4563 raid5_conf_t *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004564 int raid_disk, memory, max_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 mdk_rdev_t *rdev;
4566 struct disk_info *disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567
NeilBrown91adb562009-03-31 14:39:39 +11004568 if (mddev->new_level != 5
4569 && mddev->new_level != 4
4570 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10004571 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004572 mdname(mddev), mddev->new_level);
4573 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 }
NeilBrown91adb562009-03-31 14:39:39 +11004575 if ((mddev->new_level == 5
4576 && !algorithm_valid_raid5(mddev->new_layout)) ||
4577 (mddev->new_level == 6
4578 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004579 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11004580 mdname(mddev), mddev->new_layout);
4581 return ERR_PTR(-EIO);
4582 }
4583 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10004584 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004585 mdname(mddev), mddev->raid_disks);
4586 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11004587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588
Andre Noll664e7c42009-06-18 08:45:27 +10004589 if (!mddev->new_chunk_sectors ||
4590 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
4591 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10004592 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
4593 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11004594 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11004595 }
4596
NeilBrown91adb562009-03-31 14:39:39 +11004597 conf = kzalloc(sizeof(raid5_conf_t), GFP_KERNEL);
4598 if (conf == NULL)
4599 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11004600 spin_lock_init(&conf->device_lock);
4601 init_waitqueue_head(&conf->wait_for_stripe);
4602 init_waitqueue_head(&conf->wait_for_overlap);
4603 INIT_LIST_HEAD(&conf->handle_list);
4604 INIT_LIST_HEAD(&conf->hold_list);
4605 INIT_LIST_HEAD(&conf->delayed_list);
4606 INIT_LIST_HEAD(&conf->bitmap_list);
4607 INIT_LIST_HEAD(&conf->inactive_list);
4608 atomic_set(&conf->active_stripes, 0);
4609 atomic_set(&conf->preread_active_stripes, 0);
4610 atomic_set(&conf->active_aligned_reads, 0);
4611 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrown91adb562009-03-31 14:39:39 +11004612
4613 conf->raid_disks = mddev->raid_disks;
4614 if (mddev->reshape_position == MaxSector)
4615 conf->previous_raid_disks = mddev->raid_disks;
4616 else
4617 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004618 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
4619 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11004620
NeilBrown5e5e3e72009-10-16 16:35:30 +11004621 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11004622 GFP_KERNEL);
4623 if (!conf->disks)
4624 goto abort;
4625
4626 conf->mddev = mddev;
4627
4628 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
4629 goto abort;
4630
Dan Williams36d1c642009-07-14 11:48:22 -07004631 conf->level = mddev->new_level;
4632 if (raid5_alloc_percpu(conf) != 0)
4633 goto abort;
4634
NeilBrown0c55e022010-05-03 14:09:02 +10004635 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11004636
4637 list_for_each_entry(rdev, &mddev->disks, same_set) {
4638 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004639 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11004640 || raid_disk < 0)
4641 continue;
4642 disk = conf->disks + raid_disk;
4643
4644 disk->rdev = rdev;
4645
4646 if (test_bit(In_sync, &rdev->flags)) {
4647 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10004648 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
4649 " disk %d\n",
4650 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05004651 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11004652 /* Cannot rely on bitmap to complete recovery */
4653 conf->fullsync = 1;
4654 }
4655
Andre Noll09c9e5f2009-06-18 08:45:55 +10004656 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11004657 conf->level = mddev->new_level;
4658 if (conf->level == 6)
4659 conf->max_degraded = 2;
4660 else
4661 conf->max_degraded = 1;
4662 conf->algorithm = mddev->new_layout;
4663 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11004664 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11004665 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10004666 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11004667 conf->prev_algo = mddev->layout;
4668 }
NeilBrown91adb562009-03-31 14:39:39 +11004669
4670 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11004671 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11004672 if (grow_stripes(conf, conf->max_nr_stripes)) {
4673 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004674 "md/raid:%s: couldn't allocate %dkB for buffers\n",
4675 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004676 goto abort;
4677 } else
NeilBrown0c55e022010-05-03 14:09:02 +10004678 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
4679 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004680
NeilBrown0da3c612009-09-23 18:09:45 +10004681 conf->thread = md_register_thread(raid5d, mddev, NULL);
NeilBrown91adb562009-03-31 14:39:39 +11004682 if (!conf->thread) {
4683 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004684 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11004685 mdname(mddev));
4686 goto abort;
4687 }
4688
4689 return conf;
4690
4691 abort:
4692 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10004693 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11004694 return ERR_PTR(-EIO);
4695 } else
4696 return ERR_PTR(-ENOMEM);
4697}
4698
NeilBrownc148ffd2009-11-13 17:47:00 +11004699
4700static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
4701{
4702 switch (algo) {
4703 case ALGORITHM_PARITY_0:
4704 if (raid_disk < max_degraded)
4705 return 1;
4706 break;
4707 case ALGORITHM_PARITY_N:
4708 if (raid_disk >= raid_disks - max_degraded)
4709 return 1;
4710 break;
4711 case ALGORITHM_PARITY_0_6:
4712 if (raid_disk == 0 ||
4713 raid_disk == raid_disks - 1)
4714 return 1;
4715 break;
4716 case ALGORITHM_LEFT_ASYMMETRIC_6:
4717 case ALGORITHM_RIGHT_ASYMMETRIC_6:
4718 case ALGORITHM_LEFT_SYMMETRIC_6:
4719 case ALGORITHM_RIGHT_SYMMETRIC_6:
4720 if (raid_disk == raid_disks - 1)
4721 return 1;
4722 }
4723 return 0;
4724}
4725
NeilBrown91adb562009-03-31 14:39:39 +11004726static int run(mddev_t *mddev)
4727{
4728 raid5_conf_t *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10004729 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11004730 int dirty_parity_disks = 0;
NeilBrown91adb562009-03-31 14:39:39 +11004731 mdk_rdev_t *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11004732 sector_t reshape_offset = 0;
NeilBrown91adb562009-03-31 14:39:39 +11004733
Andre Noll8c6ac862009-06-18 08:48:06 +10004734 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10004735 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10004736 " -- starting background reconstruction\n",
4737 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004738 if (mddev->reshape_position != MaxSector) {
4739 /* Check that we can continue the reshape.
4740 * Currently only disks can change, it must
4741 * increase, and we must be past the point where
4742 * a stripe over-writes itself
4743 */
4744 sector_t here_new, here_old;
4745 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11004746 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08004747
NeilBrown88ce4932009-03-31 15:24:23 +11004748 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10004749 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08004750 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08004751 mdname(mddev));
4752 return -EINVAL;
4753 }
NeilBrownf6705572006-03-27 01:18:11 -08004754 old_disks = mddev->raid_disks - mddev->delta_disks;
4755 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08004756 * further up in new geometry must map after here in old
4757 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08004758 */
4759 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10004760 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08004761 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004762 printk(KERN_ERR "md/raid:%s: reshape_position not "
4763 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004764 return -EINVAL;
4765 }
NeilBrownc148ffd2009-11-13 17:47:00 +11004766 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08004767 /* here_new is the stripe we will write to */
4768 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10004769 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08004770 (old_disks-max_degraded));
4771 /* here_old is the first stripe that we might need to read
4772 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10004773 if (mddev->delta_disks == 0) {
4774 /* We cannot be sure it is safe to start an in-place
4775 * reshape. It is only safe if user-space if monitoring
4776 * and taking constant backups.
4777 * mdadm always starts a situation like this in
4778 * readonly mode so it can take control before
4779 * allowing any writes. So just check for that.
4780 */
4781 if ((here_new * mddev->new_chunk_sectors !=
4782 here_old * mddev->chunk_sectors) ||
4783 mddev->ro == 0) {
NeilBrown0c55e022010-05-03 14:09:02 +10004784 printk(KERN_ERR "md/raid:%s: in-place reshape must be started"
4785 " in read-only mode - aborting\n",
4786 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10004787 return -EINVAL;
4788 }
4789 } else if (mddev->delta_disks < 0
4790 ? (here_new * mddev->new_chunk_sectors <=
4791 here_old * mddev->chunk_sectors)
4792 : (here_new * mddev->new_chunk_sectors >=
4793 here_old * mddev->chunk_sectors)) {
NeilBrownf6705572006-03-27 01:18:11 -08004794 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10004795 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
4796 "auto-recovery - aborting.\n",
4797 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004798 return -EINVAL;
4799 }
NeilBrown0c55e022010-05-03 14:09:02 +10004800 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
4801 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004802 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08004803 } else {
NeilBrown91adb562009-03-31 14:39:39 +11004804 BUG_ON(mddev->level != mddev->new_level);
4805 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10004806 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11004807 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08004808 }
4809
NeilBrown245f46c2009-03-31 14:39:39 +11004810 if (mddev->private == NULL)
4811 conf = setup_conf(mddev);
4812 else
4813 conf = mddev->private;
4814
NeilBrown91adb562009-03-31 14:39:39 +11004815 if (IS_ERR(conf))
4816 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08004817
NeilBrown91adb562009-03-31 14:39:39 +11004818 mddev->thread = conf->thread;
4819 conf->thread = NULL;
4820 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07004823 * 0 for a fully functional array, 1 or 2 for a degraded array.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 */
NeilBrownc148ffd2009-11-13 17:47:00 +11004825 list_for_each_entry(rdev, &mddev->disks, same_set) {
4826 if (rdev->raid_disk < 0)
4827 continue;
NeilBrown2f115882010-06-17 17:41:03 +10004828 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11004829 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10004830 continue;
4831 }
NeilBrownc148ffd2009-11-13 17:47:00 +11004832 /* This disc is not fully in-sync. However if it
4833 * just stored parity (beyond the recovery_offset),
4834 * when we don't need to be concerned about the
4835 * array being dirty.
4836 * When reshape goes 'backwards', we never have
4837 * partially completed devices, so we only need
4838 * to worry about reshape going forwards.
4839 */
4840 /* Hack because v0.91 doesn't store recovery_offset properly. */
4841 if (mddev->major_version == 0 &&
4842 mddev->minor_version > 90)
4843 rdev->recovery_offset = reshape_offset;
4844
NeilBrownc148ffd2009-11-13 17:47:00 +11004845 if (rdev->recovery_offset < reshape_offset) {
4846 /* We need to check old and new layout */
4847 if (!only_parity(rdev->raid_disk,
4848 conf->algorithm,
4849 conf->raid_disks,
4850 conf->max_degraded))
4851 continue;
4852 }
4853 if (!only_parity(rdev->raid_disk,
4854 conf->prev_algo,
4855 conf->previous_raid_disks,
4856 conf->max_degraded))
4857 continue;
4858 dirty_parity_disks++;
4859 }
NeilBrown91adb562009-03-31 14:39:39 +11004860
NeilBrown5e5e3e72009-10-16 16:35:30 +11004861 mddev->degraded = (max(conf->raid_disks, conf->previous_raid_disks)
4862 - working_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863
NeilBrown674806d2010-06-16 17:17:53 +10004864 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10004865 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07004867 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 goto abort;
4869 }
4870
NeilBrown91adb562009-03-31 14:39:39 +11004871 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004872 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11004873 mddev->resync_max_sectors = mddev->dev_sectors;
4874
NeilBrownc148ffd2009-11-13 17:47:00 +11004875 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004877 if (mddev->ok_start_degraded)
4878 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10004879 "md/raid:%s: starting dirty degraded array"
4880 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004881 mdname(mddev));
4882 else {
4883 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004884 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004885 mdname(mddev));
4886 goto abort;
4887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 }
4889
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10004891 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
4892 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11004893 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
4894 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 else
NeilBrown0c55e022010-05-03 14:09:02 +10004896 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
4897 " out of %d devices, algorithm %d\n",
4898 mdname(mddev), conf->level,
4899 mddev->raid_disks - mddev->degraded,
4900 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901
4902 print_raid5_conf(conf);
4903
NeilBrownfef9c612009-03-31 15:16:46 +11004904 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11004905 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08004906 atomic_set(&conf->reshape_stripes, 0);
4907 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4908 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4909 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4910 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4911 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10004912 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08004913 }
4914
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915
4916 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10004917 if (mddev->to_remove == &raid5_attrs_group)
4918 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10004919 else if (mddev->kobj.sd &&
4920 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08004921 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10004922 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08004923 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10004924 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
4925
4926 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10004927 int chunk_size;
NeilBrown4a5add42010-06-01 19:37:28 +10004928 /* read-ahead size must cover two whole stripes, which
4929 * is 2 * (datadisks) * chunksize where 'n' is the
4930 * number of raid devices
4931 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 int data_disks = conf->previous_raid_disks - conf->max_degraded;
4933 int stripe = data_disks *
4934 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
4935 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
4936 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10004937
4938 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10004939
4940 mddev->queue->backing_dev_info.congested_data = mddev;
4941 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10004942
4943 chunk_size = mddev->chunk_sectors << 9;
4944 blk_queue_io_min(mddev->queue, chunk_size);
4945 blk_queue_io_opt(mddev->queue, chunk_size *
4946 (conf->raid_disks - conf->max_degraded));
4947
4948 list_for_each_entry(rdev, &mddev->disks, same_set)
4949 disk_stack_limits(mddev->gendisk, rdev->bdev,
4950 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 }
4952
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 return 0;
4954abort:
NeilBrown01f96c02011-09-21 15:30:20 +10004955 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11004956 print_raid5_conf(conf);
4957 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10004959 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 return -EIO;
4961}
4962
NeilBrown3f294f42005-11-08 21:39:25 -08004963static int stop(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964{
H Hartley Sweeten7b928132010-03-08 16:02:40 +11004965 raid5_conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966
NeilBrown01f96c02011-09-21 15:30:20 +10004967 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10004968 if (mddev->queue)
4969 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10004970 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10004971 mddev->private = NULL;
4972 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 return 0;
4974}
4975
Dan Williams45b42332007-07-09 11:56:43 -07004976#ifdef DEBUG
NeilBrownd710e132008-10-13 11:55:12 +11004977static void print_sh(struct seq_file *seq, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978{
4979 int i;
4980
NeilBrown16a53ec2006-06-26 00:27:38 -07004981 seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
4982 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
4983 seq_printf(seq, "sh %llu, count %d.\n",
4984 (unsigned long long)sh->sector, atomic_read(&sh->count));
4985 seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004986 for (i = 0; i < sh->disks; i++) {
NeilBrown16a53ec2006-06-26 00:27:38 -07004987 seq_printf(seq, "(cache%d: %p %ld) ",
4988 i, sh->dev[i].page, sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004990 seq_printf(seq, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991}
4992
NeilBrownd710e132008-10-13 11:55:12 +11004993static void printall(struct seq_file *seq, raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994{
4995 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -08004996 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 int i;
4998
4999 spin_lock_irq(&conf->device_lock);
5000 for (i = 0; i < NR_HASH; i++) {
NeilBrownfccddba2006-01-06 00:20:33 -08005001 hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 if (sh->raid_conf != conf)
5003 continue;
NeilBrown16a53ec2006-06-26 00:27:38 -07005004 print_sh(seq, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 }
5006 }
5007 spin_unlock_irq(&conf->device_lock);
5008}
5009#endif
5010
NeilBrownd710e132008-10-13 11:55:12 +11005011static void status(struct seq_file *seq, mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012{
H Hartley Sweeten7b928132010-03-08 16:02:40 +11005013 raid5_conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 int i;
5015
Andre Noll9d8f0362009-06-18 08:45:01 +10005016 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5017 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005018 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 for (i = 0; i < conf->raid_disks; i++)
5020 seq_printf (seq, "%s",
5021 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005022 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 seq_printf (seq, "]");
Dan Williams45b42332007-07-09 11:56:43 -07005024#ifdef DEBUG
NeilBrown16a53ec2006-06-26 00:27:38 -07005025 seq_printf (seq, "\n");
5026 printall(seq, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027#endif
5028}
5029
5030static void print_raid5_conf (raid5_conf_t *conf)
5031{
5032 int i;
5033 struct disk_info *tmp;
5034
NeilBrown0c55e022010-05-03 14:09:02 +10005035 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 if (!conf) {
5037 printk("(conf==NULL)\n");
5038 return;
5039 }
NeilBrown0c55e022010-05-03 14:09:02 +10005040 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5041 conf->raid_disks,
5042 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043
5044 for (i = 0; i < conf->raid_disks; i++) {
5045 char b[BDEVNAME_SIZE];
5046 tmp = conf->disks + i;
5047 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005048 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5049 i, !test_bit(Faulty, &tmp->rdev->flags),
5050 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 }
5052}
5053
5054static int raid5_spare_active(mddev_t *mddev)
5055{
5056 int i;
5057 raid5_conf_t *conf = mddev->private;
5058 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005059 int count = 0;
5060 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061
5062 for (i = 0; i < conf->raid_disks; i++) {
5063 tmp = conf->disks + i;
5064 if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005065 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005066 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005067 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005068 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005069 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 }
5071 }
NeilBrown6b965622010-08-18 11:56:59 +10005072 spin_lock_irqsave(&conf->device_lock, flags);
5073 mddev->degraded -= count;
5074 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005076 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077}
5078
5079static int raid5_remove_disk(mddev_t *mddev, int number)
5080{
5081 raid5_conf_t *conf = mddev->private;
5082 int err = 0;
5083 mdk_rdev_t *rdev;
5084 struct disk_info *p = conf->disks + number;
5085
5086 print_raid5_conf(conf);
5087 rdev = p->rdev;
5088 if (rdev) {
NeilBrownec32a2b2009-03-31 15:17:38 +11005089 if (number >= conf->raid_disks &&
5090 conf->reshape_progress == MaxSector)
5091 clear_bit(In_sync, &rdev->flags);
5092
NeilBrownb2d444d2005-11-08 21:39:31 -08005093 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094 atomic_read(&rdev->nr_pending)) {
5095 err = -EBUSY;
5096 goto abort;
5097 }
NeilBrowndfc70642008-05-23 13:04:39 -07005098 /* Only remove non-faulty devices if recovery
5099 * isn't possible.
5100 */
5101 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7f0da592011-07-28 11:39:22 +10005102 mddev->recovery_disabled != conf->recovery_disabled &&
NeilBrown674806d2010-06-16 17:17:53 +10005103 !has_failed(conf) &&
NeilBrownec32a2b2009-03-31 15:17:38 +11005104 number < conf->raid_disks) {
NeilBrowndfc70642008-05-23 13:04:39 -07005105 err = -EBUSY;
5106 goto abort;
5107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07005109 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 if (atomic_read(&rdev->nr_pending)) {
5111 /* lost the race, try later */
5112 err = -EBUSY;
5113 p->rdev = rdev;
5114 }
5115 }
5116abort:
5117
5118 print_raid5_conf(conf);
5119 return err;
5120}
5121
5122static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
5123{
5124 raid5_conf_t *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005125 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 int disk;
5127 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005128 int first = 0;
5129 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130
NeilBrown7f0da592011-07-28 11:39:22 +10005131 if (mddev->recovery_disabled == conf->recovery_disabled)
5132 return -EBUSY;
5133
NeilBrown674806d2010-06-16 17:17:53 +10005134 if (has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005136 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137
Neil Brown6c2fce22008-06-28 08:31:31 +10005138 if (rdev->raid_disk >= 0)
5139 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140
5141 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005142 * find the disk ... but prefer rdev->saved_raid_disk
5143 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005145 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005146 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005147 conf->disks[rdev->saved_raid_disk].rdev == NULL)
5148 disk = rdev->saved_raid_disk;
5149 else
Neil Brown6c2fce22008-06-28 08:31:31 +10005150 disk = first;
5151 for ( ; disk <= last ; disk++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 if ((p=conf->disks + disk)->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005153 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005155 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005156 if (rdev->saved_raid_disk != disk)
5157 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005158 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 break;
5160 }
5161 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005162 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163}
5164
5165static int raid5_resize(mddev_t *mddev, sector_t sectors)
5166{
5167 /* no resync is happening, and there is enough space
5168 * on all devices, so we can resize.
5169 * We need to make sure resync covers any new space.
5170 * If the array is shrinking we should possibly wait until
5171 * any io in the removed space completes, but it hardly seems
5172 * worth it.
5173 */
Andre Noll9d8f0362009-06-18 08:45:01 +10005174 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Dan Williams1f403622009-03-31 14:59:03 +11005175 md_set_array_sectors(mddev, raid5_size(mddev, sectors,
5176 mddev->raid_disks));
Dan Williamsb522adc2009-03-31 15:00:31 +11005177 if (mddev->array_sectors >
5178 raid5_size(mddev, sectors, mddev->raid_disks))
5179 return -EINVAL;
Andre Nollf233ea52008-07-21 17:05:22 +10005180 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005181 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005182 if (sectors > mddev->dev_sectors &&
5183 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005184 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5186 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005187 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005188 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189 return 0;
5190}
5191
NeilBrown01ee22b2009-06-18 08:47:20 +10005192static int check_stripe_cache(mddev_t *mddev)
5193{
5194 /* Can only proceed if there are plenty of stripe_heads.
5195 * We need a minimum of one full stripe,, and for sensible progress
5196 * it is best to have about 4 times that.
5197 * If we require 4 times, then the default 256 4K stripe_heads will
5198 * allow for chunk sizes up to 256K, which is probably OK.
5199 * If the chunk size is greater, user-space should request more
5200 * stripe_heads first.
5201 */
5202 raid5_conf_t *conf = mddev->private;
5203 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5204 > conf->max_nr_stripes ||
5205 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5206 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005207 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5208 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005209 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5210 / STRIPE_SIZE)*4);
5211 return 0;
5212 }
5213 return 1;
5214}
5215
NeilBrown50ac1682009-06-18 08:47:55 +10005216static int check_reshape(mddev_t *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005217{
NeilBrown070ec552009-06-16 16:54:21 +10005218 raid5_conf_t *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005219
NeilBrown88ce4932009-03-31 15:24:23 +11005220 if (mddev->delta_disks == 0 &&
5221 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005222 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005223 return 0; /* nothing to do */
NeilBrowndba034e2008-08-05 15:54:13 +10005224 if (mddev->bitmap)
5225 /* Cannot grow a bitmap yet */
5226 return -EBUSY;
NeilBrown674806d2010-06-16 17:17:53 +10005227 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005228 return -EINVAL;
5229 if (mddev->delta_disks < 0) {
5230 /* We might be able to shrink, but the devices must
5231 * be made bigger first.
5232 * For raid6, 4 is the minimum size.
5233 * Otherwise 2 is the minimum
5234 */
5235 int min = 2;
5236 if (mddev->level == 6)
5237 min = 4;
5238 if (mddev->raid_disks + mddev->delta_disks < min)
5239 return -EINVAL;
5240 }
NeilBrown29269552006-03-27 01:18:10 -08005241
NeilBrown01ee22b2009-06-18 08:47:20 +10005242 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005243 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005244
NeilBrownec32a2b2009-03-31 15:17:38 +11005245 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
NeilBrown63c70c42006-03-27 01:18:13 -08005246}
5247
5248static int raid5_start_reshape(mddev_t *mddev)
5249{
NeilBrown070ec552009-06-16 16:54:21 +10005250 raid5_conf_t *conf = mddev->private;
NeilBrown63c70c42006-03-27 01:18:13 -08005251 mdk_rdev_t *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005252 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005253 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005254
NeilBrownf4168852007-02-28 20:11:53 -08005255 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005256 return -EBUSY;
5257
NeilBrown01ee22b2009-06-18 08:47:20 +10005258 if (!check_stripe_cache(mddev))
5259 return -ENOSPC;
5260
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005261 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown469518a2011-01-31 11:57:43 +11005262 if (!test_bit(In_sync, &rdev->flags)
5263 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005264 spares++;
NeilBrown63c70c42006-03-27 01:18:13 -08005265
NeilBrownf4168852007-02-28 20:11:53 -08005266 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005267 /* Not enough devices even to make a degraded array
5268 * of that size
5269 */
5270 return -EINVAL;
5271
NeilBrownec32a2b2009-03-31 15:17:38 +11005272 /* Refuse to reduce size of the array. Any reductions in
5273 * array size must be through explicit setting of array_size
5274 * attribute.
5275 */
5276 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5277 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10005278 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11005279 "before number of disks\n", mdname(mddev));
5280 return -EINVAL;
5281 }
5282
NeilBrownf6705572006-03-27 01:18:11 -08005283 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005284 spin_lock_irq(&conf->device_lock);
5285 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005286 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005287 conf->prev_chunk_sectors = conf->chunk_sectors;
5288 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005289 conf->prev_algo = conf->algorithm;
5290 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11005291 if (mddev->delta_disks < 0)
5292 conf->reshape_progress = raid5_size(mddev, 0, 0);
5293 else
5294 conf->reshape_progress = 0;
5295 conf->reshape_safe = conf->reshape_progress;
NeilBrown86b42c72009-03-31 15:19:03 +11005296 conf->generation++;
NeilBrown29269552006-03-27 01:18:10 -08005297 spin_unlock_irq(&conf->device_lock);
5298
5299 /* Add some new drives, as many as will fit.
5300 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10005301 * Don't add devices if we are reducing the number of
5302 * devices in the array. This is because it is not possible
5303 * to correctly record the "partially reconstructed" state of
5304 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08005305 */
NeilBrown87a8dec2011-01-31 11:57:43 +11005306 if (mddev->delta_disks >= 0) {
5307 int added_devices = 0;
5308 list_for_each_entry(rdev, &mddev->disks, same_set)
5309 if (rdev->raid_disk < 0 &&
5310 !test_bit(Faulty, &rdev->flags)) {
5311 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11005312 if (rdev->raid_disk
5313 >= conf->previous_raid_disks) {
5314 set_bit(In_sync, &rdev->flags);
5315 added_devices++;
5316 } else
5317 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10005318
5319 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11005320 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11005321 }
NeilBrown87a8dec2011-01-31 11:57:43 +11005322 } else if (rdev->raid_disk >= conf->previous_raid_disks
5323 && !test_bit(Faulty, &rdev->flags)) {
5324 /* This is a spare that was manually added */
5325 set_bit(In_sync, &rdev->flags);
5326 added_devices++;
5327 }
NeilBrown29269552006-03-27 01:18:10 -08005328
NeilBrown87a8dec2011-01-31 11:57:43 +11005329 /* When a reshape changes the number of devices,
5330 * ->degraded is measured against the larger of the
5331 * pre and post number of devices.
5332 */
NeilBrownec32a2b2009-03-31 15:17:38 +11005333 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown9eb07c22010-02-09 12:31:47 +11005334 mddev->degraded += (conf->raid_disks - conf->previous_raid_disks)
NeilBrownec32a2b2009-03-31 15:17:38 +11005335 - added_devices;
5336 spin_unlock_irqrestore(&conf->device_lock, flags);
5337 }
NeilBrown63c70c42006-03-27 01:18:13 -08005338 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10005339 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07005340 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005341
NeilBrown29269552006-03-27 01:18:10 -08005342 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5343 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5344 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5345 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5346 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005347 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08005348 if (!mddev->sync_thread) {
5349 mddev->recovery = 0;
5350 spin_lock_irq(&conf->device_lock);
5351 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005352 conf->reshape_progress = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005353 spin_unlock_irq(&conf->device_lock);
5354 return -EAGAIN;
5355 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005356 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005357 md_wakeup_thread(mddev->sync_thread);
5358 md_new_event(mddev);
5359 return 0;
5360}
NeilBrown29269552006-03-27 01:18:10 -08005361
NeilBrownec32a2b2009-03-31 15:17:38 +11005362/* This is called from the reshape thread and should make any
5363 * changes needed in 'conf'
5364 */
NeilBrown29269552006-03-27 01:18:10 -08005365static void end_reshape(raid5_conf_t *conf)
5366{
NeilBrown29269552006-03-27 01:18:10 -08005367
NeilBrownf6705572006-03-27 01:18:11 -08005368 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
Dan Williams80c3a6c2009-03-17 18:10:40 -07005369
NeilBrownf6705572006-03-27 01:18:11 -08005370 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11005371 conf->previous_raid_disks = conf->raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005372 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08005373 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005374 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07005375
5376 /* read-ahead size must cover two whole stripes, which is
5377 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5378 */
NeilBrown4a5add42010-06-01 19:37:28 +10005379 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11005380 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005381 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11005382 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07005383 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5384 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5385 }
NeilBrown29269552006-03-27 01:18:10 -08005386 }
NeilBrown29269552006-03-27 01:18:10 -08005387}
5388
NeilBrownec32a2b2009-03-31 15:17:38 +11005389/* This is called from the raid5d thread with mddev_lock held.
5390 * It makes config changes to the device.
5391 */
NeilBrowncea9c222009-03-31 15:15:05 +11005392static void raid5_finish_reshape(mddev_t *mddev)
5393{
NeilBrown070ec552009-06-16 16:54:21 +10005394 raid5_conf_t *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11005395
5396 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5397
NeilBrownec32a2b2009-03-31 15:17:38 +11005398 if (mddev->delta_disks > 0) {
5399 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5400 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005401 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11005402 } else {
5403 int d;
NeilBrownec32a2b2009-03-31 15:17:38 +11005404 mddev->degraded = conf->raid_disks;
5405 for (d = 0; d < conf->raid_disks ; d++)
5406 if (conf->disks[d].rdev &&
5407 test_bit(In_sync,
5408 &conf->disks[d].rdev->flags))
5409 mddev->degraded--;
5410 for (d = conf->raid_disks ;
5411 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10005412 d++) {
5413 mdk_rdev_t *rdev = conf->disks[d].rdev;
5414 if (rdev && raid5_remove_disk(mddev, d) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10005415 sysfs_unlink_rdev(mddev, rdev);
NeilBrown1a67dde2009-08-13 10:41:49 +10005416 rdev->raid_disk = -1;
5417 }
5418 }
NeilBrowncea9c222009-03-31 15:15:05 +11005419 }
NeilBrown88ce4932009-03-31 15:24:23 +11005420 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005421 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11005422 mddev->reshape_position = MaxSector;
5423 mddev->delta_disks = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11005424 }
5425}
5426
NeilBrown72626682005-09-09 16:23:54 -07005427static void raid5_quiesce(mddev_t *mddev, int state)
5428{
NeilBrown070ec552009-06-16 16:54:21 +10005429 raid5_conf_t *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07005430
5431 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08005432 case 2: /* resume for a suspend */
5433 wake_up(&conf->wait_for_overlap);
5434 break;
5435
NeilBrown72626682005-09-09 16:23:54 -07005436 case 1: /* stop all writes */
5437 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005438 /* '2' tells resync/reshape to pause so that all
5439 * active stripes can drain
5440 */
5441 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07005442 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005443 atomic_read(&conf->active_stripes) == 0 &&
5444 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07005445 conf->device_lock, /* nothing */);
NeilBrown64bd6602009-08-03 10:59:58 +10005446 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07005447 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005448 /* allow reshape to continue */
5449 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005450 break;
5451
5452 case 0: /* re-enable writes */
5453 spin_lock_irq(&conf->device_lock);
5454 conf->quiesce = 0;
5455 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08005456 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005457 spin_unlock_irq(&conf->device_lock);
5458 break;
5459 }
NeilBrown72626682005-09-09 16:23:54 -07005460}
NeilBrownb15c2e52006-01-06 00:20:16 -08005461
NeilBrownd562b0c2009-03-31 14:39:39 +11005462
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005463static void *raid45_takeover_raid0(mddev_t *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11005464{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005465 struct raid0_private_data *raid0_priv = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07005466 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11005467
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005468 /* for raid0 takeover only one zone is supported */
5469 if (raid0_priv->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10005470 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
5471 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005472 return ERR_PTR(-EINVAL);
5473 }
5474
NeilBrown3b71bd92011-04-20 15:38:18 +10005475 sectors = raid0_priv->strip_zone[0].zone_end;
5476 sector_div(sectors, raid0_priv->strip_zone[0].nb_dev);
5477 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005478 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11005479 mddev->new_layout = ALGORITHM_PARITY_N;
5480 mddev->new_chunk_sectors = mddev->chunk_sectors;
5481 mddev->raid_disks += 1;
5482 mddev->delta_disks = 1;
5483 /* make sure it will be not marked as dirty */
5484 mddev->recovery_cp = MaxSector;
5485
5486 return setup_conf(mddev);
5487}
5488
5489
NeilBrownd562b0c2009-03-31 14:39:39 +11005490static void *raid5_takeover_raid1(mddev_t *mddev)
5491{
5492 int chunksect;
5493
5494 if (mddev->raid_disks != 2 ||
5495 mddev->degraded > 1)
5496 return ERR_PTR(-EINVAL);
5497
5498 /* Should check if there are write-behind devices? */
5499
5500 chunksect = 64*2; /* 64K by default */
5501
5502 /* The array must be an exact multiple of chunksize */
5503 while (chunksect && (mddev->array_sectors & (chunksect-1)))
5504 chunksect >>= 1;
5505
5506 if ((chunksect<<9) < STRIPE_SIZE)
5507 /* array size does not allow a suitable chunk size */
5508 return ERR_PTR(-EINVAL);
5509
5510 mddev->new_level = 5;
5511 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10005512 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11005513
5514 return setup_conf(mddev);
5515}
5516
NeilBrownfc9739c2009-03-31 14:57:20 +11005517static void *raid5_takeover_raid6(mddev_t *mddev)
5518{
5519 int new_layout;
5520
5521 switch (mddev->layout) {
5522 case ALGORITHM_LEFT_ASYMMETRIC_6:
5523 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
5524 break;
5525 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5526 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
5527 break;
5528 case ALGORITHM_LEFT_SYMMETRIC_6:
5529 new_layout = ALGORITHM_LEFT_SYMMETRIC;
5530 break;
5531 case ALGORITHM_RIGHT_SYMMETRIC_6:
5532 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
5533 break;
5534 case ALGORITHM_PARITY_0_6:
5535 new_layout = ALGORITHM_PARITY_0;
5536 break;
5537 case ALGORITHM_PARITY_N:
5538 new_layout = ALGORITHM_PARITY_N;
5539 break;
5540 default:
5541 return ERR_PTR(-EINVAL);
5542 }
5543 mddev->new_level = 5;
5544 mddev->new_layout = new_layout;
5545 mddev->delta_disks = -1;
5546 mddev->raid_disks -= 1;
5547 return setup_conf(mddev);
5548}
5549
NeilBrownd562b0c2009-03-31 14:39:39 +11005550
NeilBrown50ac1682009-06-18 08:47:55 +10005551static int raid5_check_reshape(mddev_t *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11005552{
NeilBrown88ce4932009-03-31 15:24:23 +11005553 /* For a 2-drive array, the layout and chunk size can be changed
5554 * immediately as not restriping is needed.
5555 * For larger arrays we record the new value - after validation
5556 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11005557 */
NeilBrown070ec552009-06-16 16:54:21 +10005558 raid5_conf_t *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10005559 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11005560
NeilBrown597a7112009-06-18 08:47:42 +10005561 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11005562 return -EINVAL;
5563 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005564 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11005565 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005566 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11005567 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005568 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11005569 /* not factor of array size */
5570 return -EINVAL;
5571 }
5572
5573 /* They look valid */
5574
NeilBrown88ce4932009-03-31 15:24:23 +11005575 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10005576 /* can make the change immediately */
5577 if (mddev->new_layout >= 0) {
5578 conf->algorithm = mddev->new_layout;
5579 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11005580 }
5581 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10005582 conf->chunk_sectors = new_chunk ;
5583 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11005584 }
5585 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5586 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11005587 }
NeilBrown50ac1682009-06-18 08:47:55 +10005588 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11005589}
5590
NeilBrown50ac1682009-06-18 08:47:55 +10005591static int raid6_check_reshape(mddev_t *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11005592{
NeilBrown597a7112009-06-18 08:47:42 +10005593 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10005594
NeilBrown597a7112009-06-18 08:47:42 +10005595 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11005596 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005597 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005598 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11005599 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005600 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11005601 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005602 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11005603 /* not factor of array size */
5604 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005605 }
NeilBrown88ce4932009-03-31 15:24:23 +11005606
5607 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10005608 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11005609}
5610
NeilBrownd562b0c2009-03-31 14:39:39 +11005611static void *raid5_takeover(mddev_t *mddev)
5612{
5613 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005614 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005615 * raid1 - if there are two drives. We need to know the chunk size
5616 * raid4 - trivial - just use a raid4 layout.
5617 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005618 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005619 if (mddev->level == 0)
5620 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11005621 if (mddev->level == 1)
5622 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11005623 if (mddev->level == 4) {
5624 mddev->new_layout = ALGORITHM_PARITY_N;
5625 mddev->new_level = 5;
5626 return setup_conf(mddev);
5627 }
NeilBrownfc9739c2009-03-31 14:57:20 +11005628 if (mddev->level == 6)
5629 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11005630
5631 return ERR_PTR(-EINVAL);
5632}
5633
NeilBrowna78d38a2010-03-22 16:53:49 +11005634static void *raid4_takeover(mddev_t *mddev)
5635{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005636 /* raid4 can take over:
5637 * raid0 - if there is only one strip zone
5638 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11005639 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005640 if (mddev->level == 0)
5641 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11005642 if (mddev->level == 5 &&
5643 mddev->layout == ALGORITHM_PARITY_N) {
5644 mddev->new_layout = 0;
5645 mddev->new_level = 4;
5646 return setup_conf(mddev);
5647 }
5648 return ERR_PTR(-EINVAL);
5649}
NeilBrownd562b0c2009-03-31 14:39:39 +11005650
NeilBrown245f46c2009-03-31 14:39:39 +11005651static struct mdk_personality raid5_personality;
5652
5653static void *raid6_takeover(mddev_t *mddev)
5654{
5655 /* Currently can only take over a raid5. We map the
5656 * personality to an equivalent raid6 personality
5657 * with the Q block at the end.
5658 */
5659 int new_layout;
5660
5661 if (mddev->pers != &raid5_personality)
5662 return ERR_PTR(-EINVAL);
5663 if (mddev->degraded > 1)
5664 return ERR_PTR(-EINVAL);
5665 if (mddev->raid_disks > 253)
5666 return ERR_PTR(-EINVAL);
5667 if (mddev->raid_disks < 3)
5668 return ERR_PTR(-EINVAL);
5669
5670 switch (mddev->layout) {
5671 case ALGORITHM_LEFT_ASYMMETRIC:
5672 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
5673 break;
5674 case ALGORITHM_RIGHT_ASYMMETRIC:
5675 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
5676 break;
5677 case ALGORITHM_LEFT_SYMMETRIC:
5678 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
5679 break;
5680 case ALGORITHM_RIGHT_SYMMETRIC:
5681 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
5682 break;
5683 case ALGORITHM_PARITY_0:
5684 new_layout = ALGORITHM_PARITY_0_6;
5685 break;
5686 case ALGORITHM_PARITY_N:
5687 new_layout = ALGORITHM_PARITY_N;
5688 break;
5689 default:
5690 return ERR_PTR(-EINVAL);
5691 }
5692 mddev->new_level = 6;
5693 mddev->new_layout = new_layout;
5694 mddev->delta_disks = 1;
5695 mddev->raid_disks += 1;
5696 return setup_conf(mddev);
5697}
5698
5699
NeilBrown16a53ec2006-06-26 00:27:38 -07005700static struct mdk_personality raid6_personality =
5701{
5702 .name = "raid6",
5703 .level = 6,
5704 .owner = THIS_MODULE,
5705 .make_request = make_request,
5706 .run = run,
5707 .stop = stop,
5708 .status = status,
5709 .error_handler = error,
5710 .hot_add_disk = raid5_add_disk,
5711 .hot_remove_disk= raid5_remove_disk,
5712 .spare_active = raid5_spare_active,
5713 .sync_request = sync_request,
5714 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005715 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10005716 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08005717 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005718 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07005719 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11005720 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07005721};
NeilBrown2604b702006-01-06 00:20:36 -08005722static struct mdk_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723{
5724 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08005725 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726 .owner = THIS_MODULE,
5727 .make_request = make_request,
5728 .run = run,
5729 .stop = stop,
5730 .status = status,
5731 .error_handler = error,
5732 .hot_add_disk = raid5_add_disk,
5733 .hot_remove_disk= raid5_remove_disk,
5734 .spare_active = raid5_spare_active,
5735 .sync_request = sync_request,
5736 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005737 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08005738 .check_reshape = raid5_check_reshape,
5739 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005740 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07005741 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11005742 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743};
5744
NeilBrown2604b702006-01-06 00:20:36 -08005745static struct mdk_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746{
NeilBrown2604b702006-01-06 00:20:36 -08005747 .name = "raid4",
5748 .level = 4,
5749 .owner = THIS_MODULE,
5750 .make_request = make_request,
5751 .run = run,
5752 .stop = stop,
5753 .status = status,
5754 .error_handler = error,
5755 .hot_add_disk = raid5_add_disk,
5756 .hot_remove_disk= raid5_remove_disk,
5757 .spare_active = raid5_spare_active,
5758 .sync_request = sync_request,
5759 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005760 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08005761 .check_reshape = raid5_check_reshape,
5762 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005763 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08005764 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11005765 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08005766};
5767
5768static int __init raid5_init(void)
5769{
NeilBrown16a53ec2006-06-26 00:27:38 -07005770 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08005771 register_md_personality(&raid5_personality);
5772 register_md_personality(&raid4_personality);
5773 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774}
5775
NeilBrown2604b702006-01-06 00:20:36 -08005776static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777{
NeilBrown16a53ec2006-06-26 00:27:38 -07005778 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08005779 unregister_md_personality(&raid5_personality);
5780 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781}
5782
5783module_init(raid5_init);
5784module_exit(raid5_exit);
5785MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11005786MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08005788MODULE_ALIAS("md-raid5");
5789MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08005790MODULE_ALIAS("md-level-5");
5791MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07005792MODULE_ALIAS("md-personality-8"); /* RAID6 */
5793MODULE_ALIAS("md-raid6");
5794MODULE_ALIAS("md-level-6");
5795
5796/* This used to be two separate modules, they were: */
5797MODULE_ALIAS("raid5");
5798MODULE_ALIAS("raid6");