blob: 858fdbb7eb07a24ceb18a4d6214218c9205089e7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * raid5.c : Multiple Devices driver for Linux
3 * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
4 * Copyright (C) 1999, 2000 Ingo Molnar
NeilBrown16a53ec2006-06-26 00:27:38 -07005 * Copyright (C) 2002, 2003 H. Peter Anvin
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
NeilBrown16a53ec2006-06-26 00:27:38 -07007 * RAID-4/5/6 management functions.
8 * Thanks to Penguin Computing for making the RAID-6 development possible
9 * by donating a test server!
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
NeilBrownae3c20c2006-07-10 04:44:17 -070021/*
22 * BITMAP UNPLUGGING:
23 *
24 * The sequencing for updating the bitmap reliably is a little
25 * subtle (and I got it wrong the first time) so it deserves some
26 * explanation.
27 *
28 * We group bitmap updates into batches. Each batch has a number.
29 * We may write out several batches at once, but that isn't very important.
NeilBrown7c13edc2011-04-18 18:25:43 +100030 * conf->seq_write is the number of the last batch successfully written.
31 * conf->seq_flush is the number of the last batch that was closed to
NeilBrownae3c20c2006-07-10 04:44:17 -070032 * new additions.
33 * When we discover that we will need to write to any block in a stripe
34 * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq
NeilBrown7c13edc2011-04-18 18:25:43 +100035 * the number of the batch it will be in. This is seq_flush+1.
NeilBrownae3c20c2006-07-10 04:44:17 -070036 * When we are ready to do a write, if that batch hasn't been written yet,
37 * we plug the array and queue the stripe for later.
38 * When an unplug happens, we increment bm_flush, thus closing the current
39 * batch.
40 * When we notice that bm_flush > bm_write, we write out all pending updates
41 * to the bitmap, and advance bm_write to where bm_flush was.
42 * This may occasionally write a bit out twice, but is sure never to
43 * miss any bits.
44 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
NeilBrownbff61972009-03-31 14:33:13 +110046#include <linux/blkdev.h>
NeilBrownf6705572006-03-27 01:18:11 -080047#include <linux/kthread.h>
Dan Williamsf701d582009-03-31 15:09:39 +110048#include <linux/raid/pq.h>
Dan Williams91c00922007-01-02 13:52:30 -070049#include <linux/async_tx.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040050#include <linux/module.h>
Dan Williams07a3b412009-08-29 19:13:13 -070051#include <linux/async.h>
NeilBrownbff61972009-03-31 14:33:13 +110052#include <linux/seq_file.h>
Dan Williams36d1c642009-07-14 11:48:22 -070053#include <linux/cpu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090054#include <linux/slab.h>
Christian Dietrich8bda4702011-07-27 11:00:36 +100055#include <linux/ratelimit.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110056#include "md.h"
NeilBrownbff61972009-03-31 14:33:13 +110057#include "raid5.h"
Trela Maciej54071b32010-03-08 16:02:42 +110058#include "raid0.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110059#include "bitmap.h"
NeilBrown72626682005-09-09 16:23:54 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/*
62 * Stripe cache
63 */
64
65#define NR_STRIPES 256
66#define STRIPE_SIZE PAGE_SIZE
67#define STRIPE_SHIFT (PAGE_SHIFT - 9)
68#define STRIPE_SECTORS (STRIPE_SIZE>>9)
69#define IO_THRESHOLD 1
Dan Williams8b3e6cd2008-04-28 02:15:53 -070070#define BYPASS_THRESHOLD 1
NeilBrownfccddba2006-01-06 00:20:33 -080071#define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define HASH_MASK (NR_HASH - 1)
73
NeilBrownd1688a62011-10-11 16:49:52 +110074static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
NeilBrowndb298e12011-10-07 14:23:00 +110075{
76 int hash = (sect >> STRIPE_SHIFT) & HASH_MASK;
77 return &conf->stripe_hashtbl[hash];
78}
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80/* bio's attached to a stripe+device for I/O are linked together in bi_sector
81 * order without overlap. There may be several bio's per stripe+device, and
82 * a bio could span several devices.
83 * When walking this list for a particular stripe+device, we must never proceed
84 * beyond a bio that extends past this device, as the next bio might no longer
85 * be valid.
NeilBrowndb298e12011-10-07 14:23:00 +110086 * This function is used to determine the 'next' bio in the list, given the sector
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * of the current stripe+device
88 */
NeilBrowndb298e12011-10-07 14:23:00 +110089static inline struct bio *r5_next_bio(struct bio *bio, sector_t sector)
90{
91 int sectors = bio->bi_size >> 9;
92 if (bio->bi_sector + sectors < sector + STRIPE_SECTORS)
93 return bio->bi_next;
94 else
95 return NULL;
96}
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Jens Axboe960e7392008-08-15 10:41:18 +020098/*
Jens Axboe5b99c2f2008-08-15 10:56:11 +020099 * We maintain a biased count of active stripes in the bottom 16 bits of
100 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
Jens Axboe960e7392008-08-15 10:41:18 +0200101 */
102static inline int raid5_bi_phys_segments(struct bio *bio)
103{
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200104 return bio->bi_phys_segments & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200105}
106
107static inline int raid5_bi_hw_segments(struct bio *bio)
108{
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200109 return (bio->bi_phys_segments >> 16) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200110}
111
112static inline int raid5_dec_bi_phys_segments(struct bio *bio)
113{
114 --bio->bi_phys_segments;
115 return raid5_bi_phys_segments(bio);
116}
117
118static inline int raid5_dec_bi_hw_segments(struct bio *bio)
119{
120 unsigned short val = raid5_bi_hw_segments(bio);
121
122 --val;
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200123 bio->bi_phys_segments = (val << 16) | raid5_bi_phys_segments(bio);
Jens Axboe960e7392008-08-15 10:41:18 +0200124 return val;
125}
126
127static inline void raid5_set_bi_hw_segments(struct bio *bio, unsigned int cnt)
128{
Namhyung Kim9b2dc8b2011-06-13 14:48:22 +0900129 bio->bi_phys_segments = raid5_bi_phys_segments(bio) | (cnt << 16);
Jens Axboe960e7392008-08-15 10:41:18 +0200130}
131
NeilBrownd0dabf72009-03-31 14:39:38 +1100132/* Find first data disk in a raid6 stripe */
133static inline int raid6_d0(struct stripe_head *sh)
134{
NeilBrown67cc2b82009-03-31 14:39:38 +1100135 if (sh->ddf_layout)
136 /* ddf always start from first device */
137 return 0;
138 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100139 if (sh->qd_idx == sh->disks - 1)
140 return 0;
141 else
142 return sh->qd_idx + 1;
143}
NeilBrown16a53ec2006-06-26 00:27:38 -0700144static inline int raid6_next_disk(int disk, int raid_disks)
145{
146 disk++;
147 return (disk < raid_disks) ? disk : 0;
148}
Dan Williamsa4456852007-07-09 11:56:43 -0700149
NeilBrownd0dabf72009-03-31 14:39:38 +1100150/* When walking through the disks in a raid5, starting at raid6_d0,
151 * We need to map each disk to a 'slot', where the data disks are slot
152 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
153 * is raid_disks-1. This help does that mapping.
154 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100155static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
156 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100157{
Dan Williams66295422009-10-19 18:09:32 -0700158 int slot = *count;
NeilBrown67cc2b82009-03-31 14:39:38 +1100159
NeilBrowne4424fe2009-10-16 16:27:34 +1100160 if (sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700161 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100162 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100163 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100164 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100165 return syndrome_disks + 1;
NeilBrowne4424fe2009-10-16 16:27:34 +1100166 if (!sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700167 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100168 return slot;
169}
170
Dan Williamsa4456852007-07-09 11:56:43 -0700171static void return_io(struct bio *return_bi)
172{
173 struct bio *bi = return_bi;
174 while (bi) {
Dan Williamsa4456852007-07-09 11:56:43 -0700175
176 return_bi = bi->bi_next;
177 bi->bi_next = NULL;
178 bi->bi_size = 0;
Neil Brown0e13fe232008-06-28 08:31:20 +1000179 bio_endio(bi, 0);
Dan Williamsa4456852007-07-09 11:56:43 -0700180 bi = return_bi;
181 }
182}
183
NeilBrownd1688a62011-10-11 16:49:52 +1100184static void print_raid5_conf (struct r5conf *conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Dan Williams600aa102008-06-28 08:32:05 +1000186static int stripe_operations_active(struct stripe_head *sh)
187{
188 return sh->check_state || sh->reconstruct_state ||
189 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
190 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
191}
192
NeilBrownd1688a62011-10-11 16:49:52 +1100193static void __release_stripe(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 if (atomic_dec_and_test(&sh->count)) {
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200196 BUG_ON(!list_empty(&sh->lru));
197 BUG_ON(atomic_read(&conf->active_stripes)==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (test_bit(STRIPE_HANDLE, &sh->state)) {
NeilBrown482c0832011-04-18 18:25:42 +1000199 if (test_bit(STRIPE_DELAYED, &sh->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 list_add_tail(&sh->lru, &conf->delayed_list);
NeilBrown482c0832011-04-18 18:25:42 +1000201 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
202 sh->bm_seq - conf->seq_write > 0)
NeilBrown72626682005-09-09 16:23:54 -0700203 list_add_tail(&sh->lru, &conf->bitmap_list);
NeilBrown482c0832011-04-18 18:25:42 +1000204 else {
NeilBrown72626682005-09-09 16:23:54 -0700205 clear_bit(STRIPE_BIT_DELAY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 list_add_tail(&sh->lru, &conf->handle_list);
NeilBrown72626682005-09-09 16:23:54 -0700207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 md_wakeup_thread(conf->mddev->thread);
209 } else {
Dan Williams600aa102008-06-28 08:32:05 +1000210 BUG_ON(stripe_operations_active(sh));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
212 atomic_dec(&conf->preread_active_stripes);
213 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
214 md_wakeup_thread(conf->mddev->thread);
215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 atomic_dec(&conf->active_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800217 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
218 list_add_tail(&sh->lru, &conf->inactive_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 wake_up(&conf->wait_for_stripe);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -0800220 if (conf->retry_read_aligned)
221 md_wakeup_thread(conf->mddev->thread);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
224 }
225}
NeilBrownd0dabf72009-03-31 14:39:38 +1100226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227static void release_stripe(struct stripe_head *sh)
228{
NeilBrownd1688a62011-10-11 16:49:52 +1100229 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 unsigned long flags;
NeilBrown16a53ec2006-06-26 00:27:38 -0700231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 spin_lock_irqsave(&conf->device_lock, flags);
233 __release_stripe(conf, sh);
234 spin_unlock_irqrestore(&conf->device_lock, flags);
235}
236
NeilBrownfccddba2006-01-06 00:20:33 -0800237static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Dan Williams45b42332007-07-09 11:56:43 -0700239 pr_debug("remove_hash(), stripe %llu\n",
240 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
NeilBrownfccddba2006-01-06 00:20:33 -0800242 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
NeilBrownd1688a62011-10-11 16:49:52 +1100245static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
NeilBrownfccddba2006-01-06 00:20:33 -0800247 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Dan Williams45b42332007-07-09 11:56:43 -0700249 pr_debug("insert_hash(), stripe %llu\n",
250 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
NeilBrownfccddba2006-01-06 00:20:33 -0800252 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
255
256/* find an idle stripe, make sure it is unhashed, and return it. */
NeilBrownd1688a62011-10-11 16:49:52 +1100257static struct stripe_head *get_free_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 struct stripe_head *sh = NULL;
260 struct list_head *first;
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (list_empty(&conf->inactive_list))
263 goto out;
264 first = conf->inactive_list.next;
265 sh = list_entry(first, struct stripe_head, lru);
266 list_del_init(first);
267 remove_hash(sh);
268 atomic_inc(&conf->active_stripes);
269out:
270 return sh;
271}
272
NeilBrowne4e11e32010-06-16 16:45:16 +1000273static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 struct page *p;
276 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000277 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
NeilBrowne4e11e32010-06-16 16:45:16 +1000279 for (i = 0; i < num ; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 p = sh->dev[i].page;
281 if (!p)
282 continue;
283 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800284 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286}
287
NeilBrowne4e11e32010-06-16 16:45:16 +1000288static int grow_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000291 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
NeilBrowne4e11e32010-06-16 16:45:16 +1000293 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 struct page *page;
295
296 if (!(page = alloc_page(GFP_KERNEL))) {
297 return 1;
298 }
299 sh->dev[i].page = page;
300 }
301 return 0;
302}
303
NeilBrown784052e2009-03-31 15:19:07 +1100304static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrownd1688a62011-10-11 16:49:52 +1100305static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100306 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
NeilBrownb5663ba2009-03-31 14:39:38 +1100308static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
NeilBrownd1688a62011-10-11 16:49:52 +1100310 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800311 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200313 BUG_ON(atomic_read(&sh->count) != 0);
314 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000315 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700316
Dan Williams45b42332007-07-09 11:56:43 -0700317 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 (unsigned long long)sh->sector);
319
320 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700321
NeilBrown86b42c72009-03-31 15:19:03 +1100322 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100323 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100325 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 sh->state = 0;
327
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800328
329 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 struct r5dev *dev = &sh->dev[i];
331
Dan Williamsd84e0f12007-01-02 13:52:30 -0700332 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700334 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700336 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000338 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100341 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343 insert_hash(conf, sh);
344}
345
NeilBrownd1688a62011-10-11 16:49:52 +1100346static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100347 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -0800350 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Dan Williams45b42332007-07-09 11:56:43 -0700352 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
NeilBrownfccddba2006-01-06 00:20:33 -0800353 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100354 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700356 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return NULL;
358}
359
NeilBrown674806d2010-06-16 17:17:53 +1000360/*
361 * Need to check if array has failed when deciding whether to:
362 * - start an array
363 * - remove non-faulty devices
364 * - add a spare
365 * - allow a reshape
366 * This determination is simple when no reshape is happening.
367 * However if there is a reshape, we need to carefully check
368 * both the before and after sections.
369 * This is because some failed devices may only affect one
370 * of the two sections, and some non-in_sync devices may
371 * be insync in the section most affected by failed devices.
372 */
NeilBrownd1688a62011-10-11 16:49:52 +1100373static int has_failed(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000374{
375 int degraded;
376 int i;
377 if (conf->mddev->reshape_position == MaxSector)
378 return conf->mddev->degraded > conf->max_degraded;
379
380 rcu_read_lock();
381 degraded = 0;
382 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100383 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown674806d2010-06-16 17:17:53 +1000384 if (!rdev || test_bit(Faulty, &rdev->flags))
385 degraded++;
386 else if (test_bit(In_sync, &rdev->flags))
387 ;
388 else
389 /* not in-sync or faulty.
390 * If the reshape increases the number of devices,
391 * this is being recovered by the reshape, so
392 * this 'previous' section is not in_sync.
393 * If the number of devices is being reduced however,
394 * the device can only be part of the array if
395 * we are reverting a reshape, so this section will
396 * be in-sync.
397 */
398 if (conf->raid_disks >= conf->previous_raid_disks)
399 degraded++;
400 }
401 rcu_read_unlock();
402 if (degraded > conf->max_degraded)
403 return 1;
404 rcu_read_lock();
405 degraded = 0;
406 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100407 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown674806d2010-06-16 17:17:53 +1000408 if (!rdev || test_bit(Faulty, &rdev->flags))
409 degraded++;
410 else if (test_bit(In_sync, &rdev->flags))
411 ;
412 else
413 /* not in-sync or faulty.
414 * If reshape increases the number of devices, this
415 * section has already been recovered, else it
416 * almost certainly hasn't.
417 */
418 if (conf->raid_disks <= conf->previous_raid_disks)
419 degraded++;
420 }
421 rcu_read_unlock();
422 if (degraded > conf->max_degraded)
423 return 1;
424 return 0;
425}
426
NeilBrownb5663ba2009-03-31 14:39:38 +1100427static struct stripe_head *
NeilBrownd1688a62011-10-11 16:49:52 +1100428get_active_stripe(struct r5conf *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000429 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
431 struct stripe_head *sh;
432
Dan Williams45b42332007-07-09 11:56:43 -0700433 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 spin_lock_irq(&conf->device_lock);
436
437 do {
NeilBrown72626682005-09-09 16:23:54 -0700438 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000439 conf->quiesce == 0 || noquiesce,
NeilBrown72626682005-09-09 16:23:54 -0700440 conf->device_lock, /* nothing */);
NeilBrown86b42c72009-03-31 15:19:03 +1100441 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 if (!sh) {
443 if (!conf->inactive_blocked)
444 sh = get_free_stripe(conf);
445 if (noblock && sh == NULL)
446 break;
447 if (!sh) {
448 conf->inactive_blocked = 1;
449 wait_event_lock_irq(conf->wait_for_stripe,
450 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800451 (atomic_read(&conf->active_stripes)
452 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 || !conf->inactive_blocked),
454 conf->device_lock,
NeilBrown7c13edc2011-04-18 18:25:43 +1000455 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 conf->inactive_blocked = 0;
457 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100458 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 } else {
460 if (atomic_read(&sh->count)) {
NeilBrownab69ae12009-03-31 15:26:47 +1100461 BUG_ON(!list_empty(&sh->lru)
462 && !test_bit(STRIPE_EXPANDING, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 } else {
464 if (!test_bit(STRIPE_HANDLE, &sh->state))
465 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700466 if (list_empty(&sh->lru) &&
467 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700468 BUG();
469 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
471 }
472 } while (sh == NULL);
473
474 if (sh)
475 atomic_inc(&sh->count);
476
477 spin_unlock_irq(&conf->device_lock);
478 return sh;
479}
480
NeilBrown6712ecf2007-09-27 12:47:43 +0200481static void
482raid5_end_read_request(struct bio *bi, int error);
483static void
484raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700485
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000486static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700487{
NeilBrownd1688a62011-10-11 16:49:52 +1100488 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700489 int i, disks = sh->disks;
490
491 might_sleep();
492
493 for (i = disks; i--; ) {
494 int rw;
495 struct bio *bi;
NeilBrown3cb03002011-10-11 16:45:26 +1100496 struct md_rdev *rdev;
Tejun Heoe9c74692010-09-03 11:56:18 +0200497 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
498 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
499 rw = WRITE_FUA;
500 else
501 rw = WRITE;
502 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700503 rw = READ;
504 else
505 continue;
506
507 bi = &sh->dev[i].req;
508
509 bi->bi_rw = rw;
Namhyung Kimb0629622011-06-14 14:20:19 +1000510 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700511 bi->bi_end_io = raid5_end_write_request;
512 else
513 bi->bi_end_io = raid5_end_read_request;
514
515 rcu_read_lock();
516 rdev = rcu_dereference(conf->disks[i].rdev);
517 if (rdev && test_bit(Faulty, &rdev->flags))
518 rdev = NULL;
519 if (rdev)
520 atomic_inc(&rdev->nr_pending);
521 rcu_read_unlock();
522
NeilBrown73e92e52011-07-28 11:39:22 +1000523 /* We have already checked bad blocks for reads. Now
524 * need to check for writes.
525 */
526 while ((rw & WRITE) && rdev &&
527 test_bit(WriteErrorSeen, &rdev->flags)) {
528 sector_t first_bad;
529 int bad_sectors;
530 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
531 &first_bad, &bad_sectors);
532 if (!bad)
533 break;
534
535 if (bad < 0) {
536 set_bit(BlockedBadBlocks, &rdev->flags);
537 if (!conf->mddev->external &&
538 conf->mddev->flags) {
539 /* It is very unlikely, but we might
540 * still need to write out the
541 * bad block log - better give it
542 * a chance*/
543 md_check_recovery(conf->mddev);
544 }
545 md_wait_for_blocked_rdev(rdev, conf->mddev);
546 } else {
547 /* Acknowledged bad block - skip the write */
548 rdev_dec_pending(rdev, conf->mddev);
549 rdev = NULL;
550 }
551 }
552
Dan Williams91c00922007-01-02 13:52:30 -0700553 if (rdev) {
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000554 if (s->syncing || s->expanding || s->expanded)
Dan Williams91c00922007-01-02 13:52:30 -0700555 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
556
Dan Williams2b7497f2008-06-28 08:31:52 +1000557 set_bit(STRIPE_IO_STARTED, &sh->state);
558
Dan Williams91c00922007-01-02 13:52:30 -0700559 bi->bi_bdev = rdev->bdev;
560 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700561 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700562 bi->bi_rw, i);
563 atomic_inc(&sh->count);
564 bi->bi_sector = sh->sector + rdev->data_offset;
565 bi->bi_flags = 1 << BIO_UPTODATE;
566 bi->bi_vcnt = 1;
567 bi->bi_max_vecs = 1;
568 bi->bi_idx = 0;
569 bi->bi_io_vec = &sh->dev[i].vec;
570 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
571 bi->bi_io_vec[0].bv_offset = 0;
572 bi->bi_size = STRIPE_SIZE;
573 bi->bi_next = NULL;
Dan Williams91c00922007-01-02 13:52:30 -0700574 generic_make_request(bi);
575 } else {
Namhyung Kimb0629622011-06-14 14:20:19 +1000576 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700577 set_bit(STRIPE_DEGRADED, &sh->state);
578 pr_debug("skip op %ld on disc %d for sector %llu\n",
579 bi->bi_rw, i, (unsigned long long)sh->sector);
580 clear_bit(R5_LOCKED, &sh->dev[i].flags);
581 set_bit(STRIPE_HANDLE, &sh->state);
582 }
583 }
584}
585
586static struct dma_async_tx_descriptor *
587async_copy_data(int frombio, struct bio *bio, struct page *page,
588 sector_t sector, struct dma_async_tx_descriptor *tx)
589{
590 struct bio_vec *bvl;
591 struct page *bio_page;
592 int i;
593 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700594 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700595 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700596
597 if (bio->bi_sector >= sector)
598 page_offset = (signed)(bio->bi_sector - sector) * 512;
599 else
600 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700601
Dan Williams0403e382009-09-08 17:42:50 -0700602 if (frombio)
603 flags |= ASYNC_TX_FENCE;
604 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
605
Dan Williams91c00922007-01-02 13:52:30 -0700606 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000607 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700608 int clen;
609 int b_offset = 0;
610
611 if (page_offset < 0) {
612 b_offset = -page_offset;
613 page_offset += b_offset;
614 len -= b_offset;
615 }
616
617 if (len > 0 && page_offset + len > STRIPE_SIZE)
618 clen = STRIPE_SIZE - page_offset;
619 else
620 clen = len;
621
622 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000623 b_offset += bvl->bv_offset;
624 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700625 if (frombio)
626 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700627 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700628 else
629 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700630 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700631 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700632 /* chain the operations */
633 submit.depend_tx = tx;
634
Dan Williams91c00922007-01-02 13:52:30 -0700635 if (clen < len) /* hit end of page */
636 break;
637 page_offset += len;
638 }
639
640 return tx;
641}
642
643static void ops_complete_biofill(void *stripe_head_ref)
644{
645 struct stripe_head *sh = stripe_head_ref;
646 struct bio *return_bi = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +1100647 struct r5conf *conf = sh->raid_conf;
Dan Williamse4d84902007-09-24 10:06:13 -0700648 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700649
Harvey Harrisone46b2722008-04-28 02:15:50 -0700650 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700651 (unsigned long long)sh->sector);
652
653 /* clear completed biofills */
Dan Williams83de75c2008-06-28 08:31:58 +1000654 spin_lock_irq(&conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700655 for (i = sh->disks; i--; ) {
656 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700657
658 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700659 /* and check if we need to reply to a read request,
660 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000661 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700662 */
663 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700664 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700665
Dan Williams91c00922007-01-02 13:52:30 -0700666 BUG_ON(!dev->read);
667 rbi = dev->read;
668 dev->read = NULL;
669 while (rbi && rbi->bi_sector <
670 dev->sector + STRIPE_SECTORS) {
671 rbi2 = r5_next_bio(rbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +0200672 if (!raid5_dec_bi_phys_segments(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700673 rbi->bi_next = return_bi;
674 return_bi = rbi;
675 }
Dan Williams91c00922007-01-02 13:52:30 -0700676 rbi = rbi2;
677 }
678 }
679 }
Dan Williams83de75c2008-06-28 08:31:58 +1000680 spin_unlock_irq(&conf->device_lock);
681 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700682
683 return_io(return_bi);
684
Dan Williamse4d84902007-09-24 10:06:13 -0700685 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700686 release_stripe(sh);
687}
688
689static void ops_run_biofill(struct stripe_head *sh)
690{
691 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +1100692 struct r5conf *conf = sh->raid_conf;
Dan Williamsa08abd82009-06-03 11:43:59 -0700693 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700694 int i;
695
Harvey Harrisone46b2722008-04-28 02:15:50 -0700696 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700697 (unsigned long long)sh->sector);
698
699 for (i = sh->disks; i--; ) {
700 struct r5dev *dev = &sh->dev[i];
701 if (test_bit(R5_Wantfill, &dev->flags)) {
702 struct bio *rbi;
703 spin_lock_irq(&conf->device_lock);
704 dev->read = rbi = dev->toread;
705 dev->toread = NULL;
706 spin_unlock_irq(&conf->device_lock);
707 while (rbi && rbi->bi_sector <
708 dev->sector + STRIPE_SECTORS) {
709 tx = async_copy_data(0, rbi, dev->page,
710 dev->sector, tx);
711 rbi = r5_next_bio(rbi, dev->sector);
712 }
713 }
714 }
715
716 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700717 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
718 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700719}
720
Dan Williams4e7d2c02009-08-29 19:13:11 -0700721static void mark_target_uptodate(struct stripe_head *sh, int target)
722{
723 struct r5dev *tgt;
724
725 if (target < 0)
726 return;
727
728 tgt = &sh->dev[target];
729 set_bit(R5_UPTODATE, &tgt->flags);
730 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
731 clear_bit(R5_Wantcompute, &tgt->flags);
732}
733
Dan Williamsac6b53b2009-07-14 13:40:19 -0700734static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700735{
736 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700737
Harvey Harrisone46b2722008-04-28 02:15:50 -0700738 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700739 (unsigned long long)sh->sector);
740
Dan Williamsac6b53b2009-07-14 13:40:19 -0700741 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700742 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700743 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700744
Dan Williamsecc65c92008-06-28 08:31:57 +1000745 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
746 if (sh->check_state == check_state_compute_run)
747 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700748 set_bit(STRIPE_HANDLE, &sh->state);
749 release_stripe(sh);
750}
751
Dan Williamsd6f38f32009-07-14 11:50:52 -0700752/* return a pointer to the address conversion region of the scribble buffer */
753static addr_conv_t *to_addr_conv(struct stripe_head *sh,
754 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700755{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700756 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
757}
758
759static struct dma_async_tx_descriptor *
760ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
761{
Dan Williams91c00922007-01-02 13:52:30 -0700762 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700763 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700764 int target = sh->ops.target;
765 struct r5dev *tgt = &sh->dev[target];
766 struct page *xor_dest = tgt->page;
767 int count = 0;
768 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700769 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700770 int i;
771
772 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700773 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700774 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
775
776 for (i = disks; i--; )
777 if (i != target)
778 xor_srcs[count++] = sh->dev[i].page;
779
780 atomic_inc(&sh->count);
781
Dan Williams0403e382009-09-08 17:42:50 -0700782 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700783 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700784 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700785 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700786 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700787 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700788
Dan Williams91c00922007-01-02 13:52:30 -0700789 return tx;
790}
791
Dan Williamsac6b53b2009-07-14 13:40:19 -0700792/* set_syndrome_sources - populate source buffers for gen_syndrome
793 * @srcs - (struct page *) array of size sh->disks
794 * @sh - stripe_head to parse
795 *
796 * Populates srcs in proper layout order for the stripe and returns the
797 * 'count' of sources to be used in a call to async_gen_syndrome. The P
798 * destination buffer is recorded in srcs[count] and the Q destination
799 * is recorded in srcs[count+1]].
800 */
801static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
802{
803 int disks = sh->disks;
804 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
805 int d0_idx = raid6_d0(sh);
806 int count;
807 int i;
808
809 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100810 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700811
812 count = 0;
813 i = d0_idx;
814 do {
815 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
816
817 srcs[slot] = sh->dev[i].page;
818 i = raid6_next_disk(i, disks);
819 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700820
NeilBrowne4424fe2009-10-16 16:27:34 +1100821 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700822}
823
824static struct dma_async_tx_descriptor *
825ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
826{
827 int disks = sh->disks;
828 struct page **blocks = percpu->scribble;
829 int target;
830 int qd_idx = sh->qd_idx;
831 struct dma_async_tx_descriptor *tx;
832 struct async_submit_ctl submit;
833 struct r5dev *tgt;
834 struct page *dest;
835 int i;
836 int count;
837
838 if (sh->ops.target < 0)
839 target = sh->ops.target2;
840 else if (sh->ops.target2 < 0)
841 target = sh->ops.target;
842 else
843 /* we should only have one valid target */
844 BUG();
845 BUG_ON(target < 0);
846 pr_debug("%s: stripe %llu block: %d\n",
847 __func__, (unsigned long long)sh->sector, target);
848
849 tgt = &sh->dev[target];
850 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
851 dest = tgt->page;
852
853 atomic_inc(&sh->count);
854
855 if (target == qd_idx) {
856 count = set_syndrome_sources(blocks, sh);
857 blocks[count] = NULL; /* regenerating p is not necessary */
858 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700859 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
860 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700861 to_addr_conv(sh, percpu));
862 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
863 } else {
864 /* Compute any data- or p-drive using XOR */
865 count = 0;
866 for (i = disks; i-- ; ) {
867 if (i == target || i == qd_idx)
868 continue;
869 blocks[count++] = sh->dev[i].page;
870 }
871
Dan Williams0403e382009-09-08 17:42:50 -0700872 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
873 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700874 to_addr_conv(sh, percpu));
875 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
876 }
877
878 return tx;
879}
880
881static struct dma_async_tx_descriptor *
882ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
883{
884 int i, count, disks = sh->disks;
885 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
886 int d0_idx = raid6_d0(sh);
887 int faila = -1, failb = -1;
888 int target = sh->ops.target;
889 int target2 = sh->ops.target2;
890 struct r5dev *tgt = &sh->dev[target];
891 struct r5dev *tgt2 = &sh->dev[target2];
892 struct dma_async_tx_descriptor *tx;
893 struct page **blocks = percpu->scribble;
894 struct async_submit_ctl submit;
895
896 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
897 __func__, (unsigned long long)sh->sector, target, target2);
898 BUG_ON(target < 0 || target2 < 0);
899 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
900 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
901
Dan Williams6c910a72009-09-16 12:24:54 -0700902 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -0700903 * slot number conversion for 'faila' and 'failb'
904 */
905 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100906 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700907 count = 0;
908 i = d0_idx;
909 do {
910 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
911
912 blocks[slot] = sh->dev[i].page;
913
914 if (i == target)
915 faila = slot;
916 if (i == target2)
917 failb = slot;
918 i = raid6_next_disk(i, disks);
919 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700920
921 BUG_ON(faila == failb);
922 if (failb < faila)
923 swap(faila, failb);
924 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
925 __func__, (unsigned long long)sh->sector, faila, failb);
926
927 atomic_inc(&sh->count);
928
929 if (failb == syndrome_disks+1) {
930 /* Q disk is one of the missing disks */
931 if (faila == syndrome_disks) {
932 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -0700933 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
934 ops_complete_compute, sh,
935 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +1100936 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700937 STRIPE_SIZE, &submit);
938 } else {
939 struct page *dest;
940 int data_target;
941 int qd_idx = sh->qd_idx;
942
943 /* Missing D+Q: recompute D from P, then recompute Q */
944 if (target == qd_idx)
945 data_target = target2;
946 else
947 data_target = target;
948
949 count = 0;
950 for (i = disks; i-- ; ) {
951 if (i == data_target || i == qd_idx)
952 continue;
953 blocks[count++] = sh->dev[i].page;
954 }
955 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -0700956 init_async_submit(&submit,
957 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
958 NULL, NULL, NULL,
959 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -0700960 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
961 &submit);
962
963 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -0700964 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
965 ops_complete_compute, sh,
966 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -0700967 return async_gen_syndrome(blocks, 0, count+2,
968 STRIPE_SIZE, &submit);
969 }
Dan Williamsac6b53b2009-07-14 13:40:19 -0700970 } else {
Dan Williams6c910a72009-09-16 12:24:54 -0700971 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
972 ops_complete_compute, sh,
973 to_addr_conv(sh, percpu));
974 if (failb == syndrome_disks) {
975 /* We're missing D+P. */
976 return async_raid6_datap_recov(syndrome_disks+2,
977 STRIPE_SIZE, faila,
978 blocks, &submit);
979 } else {
980 /* We're missing D+D. */
981 return async_raid6_2data_recov(syndrome_disks+2,
982 STRIPE_SIZE, faila, failb,
983 blocks, &submit);
984 }
Dan Williamsac6b53b2009-07-14 13:40:19 -0700985 }
986}
987
988
Dan Williams91c00922007-01-02 13:52:30 -0700989static void ops_complete_prexor(void *stripe_head_ref)
990{
991 struct stripe_head *sh = stripe_head_ref;
992
Harvey Harrisone46b2722008-04-28 02:15:50 -0700993 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700994 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -0700995}
996
997static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -0700998ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
999 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001000{
Dan Williams91c00922007-01-02 13:52:30 -07001001 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001002 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001003 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001004 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001005
1006 /* existing parity data subtracted */
1007 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1008
Harvey Harrisone46b2722008-04-28 02:15:50 -07001009 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001010 (unsigned long long)sh->sector);
1011
1012 for (i = disks; i--; ) {
1013 struct r5dev *dev = &sh->dev[i];
1014 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001015 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001016 xor_srcs[count++] = dev->page;
1017 }
1018
Dan Williams0403e382009-09-08 17:42:50 -07001019 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001020 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001021 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001022
1023 return tx;
1024}
1025
1026static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001027ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001028{
1029 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001030 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001031
Harvey Harrisone46b2722008-04-28 02:15:50 -07001032 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001033 (unsigned long long)sh->sector);
1034
1035 for (i = disks; i--; ) {
1036 struct r5dev *dev = &sh->dev[i];
1037 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001038
Dan Williamsd8ee0722008-06-28 08:32:06 +10001039 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001040 struct bio *wbi;
1041
NeilBrowncbe47ec2011-07-26 11:20:35 +10001042 spin_lock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001043 chosen = dev->towrite;
1044 dev->towrite = NULL;
1045 BUG_ON(dev->written);
1046 wbi = dev->written = chosen;
NeilBrowncbe47ec2011-07-26 11:20:35 +10001047 spin_unlock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001048
1049 while (wbi && wbi->bi_sector <
1050 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001051 if (wbi->bi_rw & REQ_FUA)
1052 set_bit(R5_WantFUA, &dev->flags);
Dan Williams91c00922007-01-02 13:52:30 -07001053 tx = async_copy_data(1, wbi, dev->page,
1054 dev->sector, tx);
1055 wbi = r5_next_bio(wbi, dev->sector);
1056 }
1057 }
1058 }
1059
1060 return tx;
1061}
1062
Dan Williamsac6b53b2009-07-14 13:40:19 -07001063static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001064{
1065 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001066 int disks = sh->disks;
1067 int pd_idx = sh->pd_idx;
1068 int qd_idx = sh->qd_idx;
1069 int i;
Tejun Heoe9c74692010-09-03 11:56:18 +02001070 bool fua = false;
Dan Williams91c00922007-01-02 13:52:30 -07001071
Harvey Harrisone46b2722008-04-28 02:15:50 -07001072 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001073 (unsigned long long)sh->sector);
1074
Tejun Heoe9c74692010-09-03 11:56:18 +02001075 for (i = disks; i--; )
1076 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
1077
Dan Williams91c00922007-01-02 13:52:30 -07001078 for (i = disks; i--; ) {
1079 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001080
Tejun Heoe9c74692010-09-03 11:56:18 +02001081 if (dev->written || i == pd_idx || i == qd_idx) {
Dan Williams91c00922007-01-02 13:52:30 -07001082 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001083 if (fua)
1084 set_bit(R5_WantFUA, &dev->flags);
1085 }
Dan Williams91c00922007-01-02 13:52:30 -07001086 }
1087
Dan Williamsd8ee0722008-06-28 08:32:06 +10001088 if (sh->reconstruct_state == reconstruct_state_drain_run)
1089 sh->reconstruct_state = reconstruct_state_drain_result;
1090 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1091 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1092 else {
1093 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1094 sh->reconstruct_state = reconstruct_state_result;
1095 }
Dan Williams91c00922007-01-02 13:52:30 -07001096
1097 set_bit(STRIPE_HANDLE, &sh->state);
1098 release_stripe(sh);
1099}
1100
1101static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001102ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1103 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001104{
Dan Williams91c00922007-01-02 13:52:30 -07001105 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001106 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001107 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001108 int count = 0, pd_idx = sh->pd_idx, i;
1109 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001110 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001111 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001112
Harvey Harrisone46b2722008-04-28 02:15:50 -07001113 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001114 (unsigned long long)sh->sector);
1115
1116 /* check if prexor is active which means only process blocks
1117 * that are part of a read-modify-write (written)
1118 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001119 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1120 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001121 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1122 for (i = disks; i--; ) {
1123 struct r5dev *dev = &sh->dev[i];
1124 if (dev->written)
1125 xor_srcs[count++] = dev->page;
1126 }
1127 } else {
1128 xor_dest = sh->dev[pd_idx].page;
1129 for (i = disks; i--; ) {
1130 struct r5dev *dev = &sh->dev[i];
1131 if (i != pd_idx)
1132 xor_srcs[count++] = dev->page;
1133 }
1134 }
1135
Dan Williams91c00922007-01-02 13:52:30 -07001136 /* 1/ if we prexor'd then the dest is reused as a source
1137 * 2/ if we did not prexor then we are redoing the parity
1138 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1139 * for the synchronous xor case
1140 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001141 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001142 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1143
1144 atomic_inc(&sh->count);
1145
Dan Williamsac6b53b2009-07-14 13:40:19 -07001146 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001147 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001148 if (unlikely(count == 1))
1149 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1150 else
1151 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001152}
1153
Dan Williamsac6b53b2009-07-14 13:40:19 -07001154static void
1155ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1156 struct dma_async_tx_descriptor *tx)
1157{
1158 struct async_submit_ctl submit;
1159 struct page **blocks = percpu->scribble;
1160 int count;
1161
1162 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1163
1164 count = set_syndrome_sources(blocks, sh);
1165
1166 atomic_inc(&sh->count);
1167
1168 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1169 sh, to_addr_conv(sh, percpu));
1170 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001171}
1172
1173static void ops_complete_check(void *stripe_head_ref)
1174{
1175 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001176
Harvey Harrisone46b2722008-04-28 02:15:50 -07001177 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001178 (unsigned long long)sh->sector);
1179
Dan Williamsecc65c92008-06-28 08:31:57 +10001180 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001181 set_bit(STRIPE_HANDLE, &sh->state);
1182 release_stripe(sh);
1183}
1184
Dan Williamsac6b53b2009-07-14 13:40:19 -07001185static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001186{
Dan Williams91c00922007-01-02 13:52:30 -07001187 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001188 int pd_idx = sh->pd_idx;
1189 int qd_idx = sh->qd_idx;
1190 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001191 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001192 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001193 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001194 int count;
1195 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001196
Harvey Harrisone46b2722008-04-28 02:15:50 -07001197 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001198 (unsigned long long)sh->sector);
1199
Dan Williamsac6b53b2009-07-14 13:40:19 -07001200 count = 0;
1201 xor_dest = sh->dev[pd_idx].page;
1202 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001203 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001204 if (i == pd_idx || i == qd_idx)
1205 continue;
1206 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001207 }
1208
Dan Williamsd6f38f32009-07-14 11:50:52 -07001209 init_async_submit(&submit, 0, NULL, NULL, NULL,
1210 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001211 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001212 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001213
Dan Williams91c00922007-01-02 13:52:30 -07001214 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001215 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1216 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001217}
1218
Dan Williamsac6b53b2009-07-14 13:40:19 -07001219static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1220{
1221 struct page **srcs = percpu->scribble;
1222 struct async_submit_ctl submit;
1223 int count;
1224
1225 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1226 (unsigned long long)sh->sector, checkp);
1227
1228 count = set_syndrome_sources(srcs, sh);
1229 if (!checkp)
1230 srcs[count] = NULL;
1231
1232 atomic_inc(&sh->count);
1233 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1234 sh, to_addr_conv(sh, percpu));
1235 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1236 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1237}
1238
Dan Williams417b8d42009-10-16 16:25:22 +11001239static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001240{
1241 int overlap_clear = 0, i, disks = sh->disks;
1242 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001243 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001244 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001245 struct raid5_percpu *percpu;
1246 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001247
Dan Williamsd6f38f32009-07-14 11:50:52 -07001248 cpu = get_cpu();
1249 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001250 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001251 ops_run_biofill(sh);
1252 overlap_clear++;
1253 }
1254
Dan Williams7b3a8712008-06-28 08:32:09 +10001255 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001256 if (level < 6)
1257 tx = ops_run_compute5(sh, percpu);
1258 else {
1259 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1260 tx = ops_run_compute6_1(sh, percpu);
1261 else
1262 tx = ops_run_compute6_2(sh, percpu);
1263 }
1264 /* terminate the chain if reconstruct is not set to be run */
1265 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001266 async_tx_ack(tx);
1267 }
Dan Williams91c00922007-01-02 13:52:30 -07001268
Dan Williams600aa102008-06-28 08:32:05 +10001269 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001270 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001271
Dan Williams600aa102008-06-28 08:32:05 +10001272 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001273 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001274 overlap_clear++;
1275 }
1276
Dan Williamsac6b53b2009-07-14 13:40:19 -07001277 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1278 if (level < 6)
1279 ops_run_reconstruct5(sh, percpu, tx);
1280 else
1281 ops_run_reconstruct6(sh, percpu, tx);
1282 }
Dan Williams91c00922007-01-02 13:52:30 -07001283
Dan Williamsac6b53b2009-07-14 13:40:19 -07001284 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1285 if (sh->check_state == check_state_run)
1286 ops_run_check_p(sh, percpu);
1287 else if (sh->check_state == check_state_run_q)
1288 ops_run_check_pq(sh, percpu, 0);
1289 else if (sh->check_state == check_state_run_pq)
1290 ops_run_check_pq(sh, percpu, 1);
1291 else
1292 BUG();
1293 }
Dan Williams91c00922007-01-02 13:52:30 -07001294
Dan Williams91c00922007-01-02 13:52:30 -07001295 if (overlap_clear)
1296 for (i = disks; i--; ) {
1297 struct r5dev *dev = &sh->dev[i];
1298 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1299 wake_up(&sh->raid_conf->wait_for_overlap);
1300 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001301 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001302}
1303
Dan Williams417b8d42009-10-16 16:25:22 +11001304#ifdef CONFIG_MULTICORE_RAID456
1305static void async_run_ops(void *param, async_cookie_t cookie)
1306{
1307 struct stripe_head *sh = param;
1308 unsigned long ops_request = sh->ops.request;
1309
1310 clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
1311 wake_up(&sh->ops.wait_for_ops);
1312
1313 __raid_run_ops(sh, ops_request);
1314 release_stripe(sh);
1315}
1316
1317static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1318{
1319 /* since handle_stripe can be called outside of raid5d context
1320 * we need to ensure sh->ops.request is de-staged before another
1321 * request arrives
1322 */
1323 wait_event(sh->ops.wait_for_ops,
1324 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
1325 sh->ops.request = ops_request;
1326
1327 atomic_inc(&sh->count);
1328 async_schedule(async_run_ops, sh);
1329}
1330#else
1331#define raid_run_ops __raid_run_ops
1332#endif
1333
NeilBrownd1688a62011-10-11 16:49:52 +11001334static int grow_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
1336 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001337 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001338 if (!sh)
1339 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001340
NeilBrown3f294f42005-11-08 21:39:25 -08001341 sh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001342 #ifdef CONFIG_MULTICORE_RAID456
1343 init_waitqueue_head(&sh->ops.wait_for_ops);
1344 #endif
NeilBrown3f294f42005-11-08 21:39:25 -08001345
NeilBrowne4e11e32010-06-16 16:45:16 +10001346 if (grow_buffers(sh)) {
1347 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001348 kmem_cache_free(conf->slab_cache, sh);
1349 return 0;
1350 }
1351 /* we just created an active stripe so... */
1352 atomic_set(&sh->count, 1);
1353 atomic_inc(&conf->active_stripes);
1354 INIT_LIST_HEAD(&sh->lru);
1355 release_stripe(sh);
1356 return 1;
1357}
1358
NeilBrownd1688a62011-10-11 16:49:52 +11001359static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001360{
Christoph Lametere18b8902006-12-06 20:33:20 -08001361 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001362 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
NeilBrownf4be6b42010-06-01 19:37:25 +10001364 if (conf->mddev->gendisk)
1365 sprintf(conf->cache_name[0],
1366 "raid%d-%s", conf->level, mdname(conf->mddev));
1367 else
1368 sprintf(conf->cache_name[0],
1369 "raid%d-%p", conf->level, conf->mddev);
1370 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1371
NeilBrownad01c9e2006-03-27 01:18:07 -08001372 conf->active_name = 0;
1373 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001375 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 if (!sc)
1377 return 1;
1378 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001379 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001380 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001381 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 return 0;
1384}
NeilBrown29269552006-03-27 01:18:10 -08001385
Dan Williamsd6f38f32009-07-14 11:50:52 -07001386/**
1387 * scribble_len - return the required size of the scribble region
1388 * @num - total number of disks in the array
1389 *
1390 * The size must be enough to contain:
1391 * 1/ a struct page pointer for each device in the array +2
1392 * 2/ room to convert each entry in (1) to its corresponding dma
1393 * (dma_map_page()) or page (page_address()) address.
1394 *
1395 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1396 * calculate over all devices (not just the data blocks), using zeros in place
1397 * of the P and Q blocks.
1398 */
1399static size_t scribble_len(int num)
1400{
1401 size_t len;
1402
1403 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1404
1405 return len;
1406}
1407
NeilBrownd1688a62011-10-11 16:49:52 +11001408static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001409{
1410 /* Make all the stripes able to hold 'newsize' devices.
1411 * New slots in each stripe get 'page' set to a new page.
1412 *
1413 * This happens in stages:
1414 * 1/ create a new kmem_cache and allocate the required number of
1415 * stripe_heads.
1416 * 2/ gather all the old stripe_heads and tranfer the pages across
1417 * to the new stripe_heads. This will have the side effect of
1418 * freezing the array as once all stripe_heads have been collected,
1419 * no IO will be possible. Old stripe heads are freed once their
1420 * pages have been transferred over, and the old kmem_cache is
1421 * freed when all stripes are done.
1422 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1423 * we simple return a failre status - no need to clean anything up.
1424 * 4/ allocate new pages for the new slots in the new stripe_heads.
1425 * If this fails, we don't bother trying the shrink the
1426 * stripe_heads down again, we just leave them as they are.
1427 * As each stripe_head is processed the new one is released into
1428 * active service.
1429 *
1430 * Once step2 is started, we cannot afford to wait for a write,
1431 * so we use GFP_NOIO allocations.
1432 */
1433 struct stripe_head *osh, *nsh;
1434 LIST_HEAD(newstripes);
1435 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001436 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001437 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001438 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001439 int i;
1440
1441 if (newsize <= conf->pool_size)
1442 return 0; /* never bother to shrink */
1443
Dan Williamsb5470dc2008-06-27 21:44:04 -07001444 err = md_allow_write(conf->mddev);
1445 if (err)
1446 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001447
NeilBrownad01c9e2006-03-27 01:18:07 -08001448 /* Step 1 */
1449 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1450 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001451 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001452 if (!sc)
1453 return -ENOMEM;
1454
1455 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001456 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001457 if (!nsh)
1458 break;
1459
NeilBrownad01c9e2006-03-27 01:18:07 -08001460 nsh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001461 #ifdef CONFIG_MULTICORE_RAID456
1462 init_waitqueue_head(&nsh->ops.wait_for_ops);
1463 #endif
NeilBrownad01c9e2006-03-27 01:18:07 -08001464
1465 list_add(&nsh->lru, &newstripes);
1466 }
1467 if (i) {
1468 /* didn't get enough, give up */
1469 while (!list_empty(&newstripes)) {
1470 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1471 list_del(&nsh->lru);
1472 kmem_cache_free(sc, nsh);
1473 }
1474 kmem_cache_destroy(sc);
1475 return -ENOMEM;
1476 }
1477 /* Step 2 - Must use GFP_NOIO now.
1478 * OK, we have enough stripes, start collecting inactive
1479 * stripes and copying them over
1480 */
1481 list_for_each_entry(nsh, &newstripes, lru) {
1482 spin_lock_irq(&conf->device_lock);
1483 wait_event_lock_irq(conf->wait_for_stripe,
1484 !list_empty(&conf->inactive_list),
1485 conf->device_lock,
NeilBrown482c0832011-04-18 18:25:42 +10001486 );
NeilBrownad01c9e2006-03-27 01:18:07 -08001487 osh = get_free_stripe(conf);
1488 spin_unlock_irq(&conf->device_lock);
1489 atomic_set(&nsh->count, 1);
1490 for(i=0; i<conf->pool_size; i++)
1491 nsh->dev[i].page = osh->dev[i].page;
1492 for( ; i<newsize; i++)
1493 nsh->dev[i].page = NULL;
1494 kmem_cache_free(conf->slab_cache, osh);
1495 }
1496 kmem_cache_destroy(conf->slab_cache);
1497
1498 /* Step 3.
1499 * At this point, we are holding all the stripes so the array
1500 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001501 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001502 */
1503 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1504 if (ndisks) {
1505 for (i=0; i<conf->raid_disks; i++)
1506 ndisks[i] = conf->disks[i];
1507 kfree(conf->disks);
1508 conf->disks = ndisks;
1509 } else
1510 err = -ENOMEM;
1511
Dan Williamsd6f38f32009-07-14 11:50:52 -07001512 get_online_cpus();
1513 conf->scribble_len = scribble_len(newsize);
1514 for_each_present_cpu(cpu) {
1515 struct raid5_percpu *percpu;
1516 void *scribble;
1517
1518 percpu = per_cpu_ptr(conf->percpu, cpu);
1519 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1520
1521 if (scribble) {
1522 kfree(percpu->scribble);
1523 percpu->scribble = scribble;
1524 } else {
1525 err = -ENOMEM;
1526 break;
1527 }
1528 }
1529 put_online_cpus();
1530
NeilBrownad01c9e2006-03-27 01:18:07 -08001531 /* Step 4, return new stripes to service */
1532 while(!list_empty(&newstripes)) {
1533 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1534 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001535
NeilBrownad01c9e2006-03-27 01:18:07 -08001536 for (i=conf->raid_disks; i < newsize; i++)
1537 if (nsh->dev[i].page == NULL) {
1538 struct page *p = alloc_page(GFP_NOIO);
1539 nsh->dev[i].page = p;
1540 if (!p)
1541 err = -ENOMEM;
1542 }
1543 release_stripe(nsh);
1544 }
1545 /* critical section pass, GFP_NOIO no longer needed */
1546
1547 conf->slab_cache = sc;
1548 conf->active_name = 1-conf->active_name;
1549 conf->pool_size = newsize;
1550 return err;
1551}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
NeilBrownd1688a62011-10-11 16:49:52 +11001553static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
1555 struct stripe_head *sh;
1556
NeilBrown3f294f42005-11-08 21:39:25 -08001557 spin_lock_irq(&conf->device_lock);
1558 sh = get_free_stripe(conf);
1559 spin_unlock_irq(&conf->device_lock);
1560 if (!sh)
1561 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001562 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001563 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001564 kmem_cache_free(conf->slab_cache, sh);
1565 atomic_dec(&conf->active_stripes);
1566 return 1;
1567}
1568
NeilBrownd1688a62011-10-11 16:49:52 +11001569static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001570{
1571 while (drop_one_stripe(conf))
1572 ;
1573
NeilBrown29fc7e32006-02-03 03:03:41 -08001574 if (conf->slab_cache)
1575 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 conf->slab_cache = NULL;
1577}
1578
NeilBrown6712ecf2007-09-27 12:47:43 +02001579static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
NeilBrown99c0fb52009-03-31 14:39:38 +11001581 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001582 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001583 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001585 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11001586 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
1589 for (i=0 ; i<disks; i++)
1590 if (bi == &sh->dev[i].req)
1591 break;
1592
Dan Williams45b42332007-07-09 11:56:43 -07001593 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1594 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 uptodate);
1596 if (i == disks) {
1597 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001598 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 }
1600
1601 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001603 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrownd6950432006-07-10 04:44:20 -07001604 rdev = conf->disks[i].rdev;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001605 printk_ratelimited(
1606 KERN_INFO
1607 "md/raid:%s: read error corrected"
1608 " (%lu sectors at %llu on %s)\n",
1609 mdname(conf->mddev), STRIPE_SECTORS,
1610 (unsigned long long)(sh->sector
1611 + rdev->data_offset),
1612 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001613 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001614 clear_bit(R5_ReadError, &sh->dev[i].flags);
1615 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1616 }
NeilBrownba22dcb2005-11-08 21:39:31 -08001617 if (atomic_read(&conf->disks[i].rdev->read_errors))
1618 atomic_set(&conf->disks[i].rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 } else {
NeilBrownd6950432006-07-10 04:44:20 -07001620 const char *bdn = bdevname(conf->disks[i].rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001621 int retry = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001622 rdev = conf->disks[i].rdev;
1623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001625 atomic_inc(&rdev->read_errors);
Gabriele A. Trombetti7b0bb532010-04-28 11:51:17 +10001626 if (conf->mddev->degraded >= conf->max_degraded)
Christian Dietrich8bda4702011-07-27 11:00:36 +10001627 printk_ratelimited(
1628 KERN_WARNING
1629 "md/raid:%s: read error not correctable "
1630 "(sector %llu on %s).\n",
1631 mdname(conf->mddev),
1632 (unsigned long long)(sh->sector
1633 + rdev->data_offset),
1634 bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001635 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
NeilBrown4e5314b2005-11-08 21:39:22 -08001636 /* Oh, no!!! */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001637 printk_ratelimited(
1638 KERN_WARNING
1639 "md/raid:%s: read error NOT corrected!! "
1640 "(sector %llu on %s).\n",
1641 mdname(conf->mddev),
1642 (unsigned long long)(sh->sector
1643 + rdev->data_offset),
1644 bdn);
NeilBrownd6950432006-07-10 04:44:20 -07001645 else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001646 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001647 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001648 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001649 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001650 else
1651 retry = 1;
1652 if (retry)
1653 set_bit(R5_ReadError, &sh->dev[i].flags);
1654 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001655 clear_bit(R5_ReadError, &sh->dev[i].flags);
1656 clear_bit(R5_ReWrite, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001657 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 }
1660 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1662 set_bit(STRIPE_HANDLE, &sh->state);
1663 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664}
1665
NeilBrownd710e132008-10-13 11:55:12 +11001666static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667{
NeilBrown99c0fb52009-03-31 14:39:38 +11001668 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001669 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001670 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001672 sector_t first_bad;
1673 int bad_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 for (i=0 ; i<disks; i++)
1676 if (bi == &sh->dev[i].req)
1677 break;
1678
Dan Williams45b42332007-07-09 11:56:43 -07001679 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1681 uptodate);
1682 if (i == disks) {
1683 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001684 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 }
1686
NeilBrownbc2607f2011-07-28 11:39:22 +10001687 if (!uptodate) {
1688 set_bit(WriteErrorSeen, &conf->disks[i].rdev->flags);
1689 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrownb84db562011-07-28 11:39:23 +10001690 } else if (is_badblock(conf->disks[i].rdev, sh->sector, STRIPE_SECTORS,
1691 &first_bad, &bad_sectors))
1692 set_bit(R5_MadeGood, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
1694 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
1695
1696 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1697 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001698 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699}
1700
1701
NeilBrown784052e2009-03-31 15:19:07 +11001702static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
NeilBrown784052e2009-03-31 15:19:07 +11001704static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705{
1706 struct r5dev *dev = &sh->dev[i];
1707
1708 bio_init(&dev->req);
1709 dev->req.bi_io_vec = &dev->vec;
1710 dev->req.bi_vcnt++;
1711 dev->req.bi_max_vecs++;
1712 dev->vec.bv_page = dev->page;
1713 dev->vec.bv_len = STRIPE_SIZE;
1714 dev->vec.bv_offset = 0;
1715
1716 dev->req.bi_sector = sh->sector;
1717 dev->req.bi_private = sh;
1718
1719 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001720 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721}
1722
NeilBrownfd01b882011-10-11 16:47:53 +11001723static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724{
1725 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11001726 struct r5conf *conf = mddev->private;
NeilBrown0c55e022010-05-03 14:09:02 +10001727 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
NeilBrown6f8d0c72011-05-11 14:38:44 +10001729 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1730 unsigned long flags;
1731 spin_lock_irqsave(&conf->device_lock, flags);
1732 mddev->degraded++;
1733 spin_unlock_irqrestore(&conf->device_lock, flags);
1734 /*
1735 * if recovery was running, make sure it aborts.
1736 */
1737 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 }
NeilBrownde393cd2011-07-28 11:31:48 +10001739 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001740 set_bit(Faulty, &rdev->flags);
1741 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1742 printk(KERN_ALERT
1743 "md/raid:%s: Disk failure on %s, disabling device.\n"
1744 "md/raid:%s: Operation continuing on %d devices.\n",
1745 mdname(mddev),
1746 bdevname(rdev->bdev, b),
1747 mdname(mddev),
1748 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001749}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
1751/*
1752 * Input: a 'big' sector number,
1753 * Output: index of the data and parity disk, and the sector # in them.
1754 */
NeilBrownd1688a62011-10-11 16:49:52 +11001755static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001756 int previous, int *dd_idx,
1757 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001759 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001760 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001762 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001763 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001765 int algorithm = previous ? conf->prev_algo
1766 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001767 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1768 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001769 int raid_disks = previous ? conf->previous_raid_disks
1770 : conf->raid_disks;
1771 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
1773 /* First compute the information on this sector */
1774
1775 /*
1776 * Compute the chunk number and the sector offset inside the chunk
1777 */
1778 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1779 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
1781 /*
1782 * Compute the stripe number
1783 */
NeilBrown35f2a592010-04-20 14:13:34 +10001784 stripe = chunk_number;
1785 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10001786 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 /*
1788 * Select the parity disk based on the user selected algorithm.
1789 */
NeilBrown84789552011-07-27 11:00:36 +10001790 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07001791 switch(conf->level) {
1792 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001793 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001794 break;
1795 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001796 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001798 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001799 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 (*dd_idx)++;
1801 break;
1802 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001803 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001804 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 (*dd_idx)++;
1806 break;
1807 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001808 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001809 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 break;
1811 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001812 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001813 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001815 case ALGORITHM_PARITY_0:
1816 pd_idx = 0;
1817 (*dd_idx)++;
1818 break;
1819 case ALGORITHM_PARITY_N:
1820 pd_idx = data_disks;
1821 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11001823 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001824 }
1825 break;
1826 case 6:
1827
NeilBrowne183eae2009-03-31 15:20:22 +11001828 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001829 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001830 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001831 qd_idx = pd_idx + 1;
1832 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001833 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001834 qd_idx = 0;
1835 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001836 (*dd_idx) += 2; /* D D P Q D */
1837 break;
1838 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001839 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001840 qd_idx = pd_idx + 1;
1841 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001842 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001843 qd_idx = 0;
1844 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001845 (*dd_idx) += 2; /* D D P Q D */
1846 break;
1847 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001848 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001849 qd_idx = (pd_idx + 1) % raid_disks;
1850 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001851 break;
1852 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001853 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001854 qd_idx = (pd_idx + 1) % raid_disks;
1855 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001856 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001857
1858 case ALGORITHM_PARITY_0:
1859 pd_idx = 0;
1860 qd_idx = 1;
1861 (*dd_idx) += 2;
1862 break;
1863 case ALGORITHM_PARITY_N:
1864 pd_idx = data_disks;
1865 qd_idx = data_disks + 1;
1866 break;
1867
1868 case ALGORITHM_ROTATING_ZERO_RESTART:
1869 /* Exactly the same as RIGHT_ASYMMETRIC, but or
1870 * of blocks for computing Q is different.
1871 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001872 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11001873 qd_idx = pd_idx + 1;
1874 if (pd_idx == raid_disks-1) {
1875 (*dd_idx)++; /* Q D D D P */
1876 qd_idx = 0;
1877 } else if (*dd_idx >= pd_idx)
1878 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001879 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001880 break;
1881
1882 case ALGORITHM_ROTATING_N_RESTART:
1883 /* Same a left_asymmetric, by first stripe is
1884 * D D D P Q rather than
1885 * Q D D D P
1886 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001887 stripe2 += 1;
1888 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11001889 qd_idx = pd_idx + 1;
1890 if (pd_idx == raid_disks-1) {
1891 (*dd_idx)++; /* Q D D D P */
1892 qd_idx = 0;
1893 } else if (*dd_idx >= pd_idx)
1894 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001895 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001896 break;
1897
1898 case ALGORITHM_ROTATING_N_CONTINUE:
1899 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001900 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11001901 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
1902 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11001903 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001904 break;
1905
1906 case ALGORITHM_LEFT_ASYMMETRIC_6:
1907 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001908 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11001909 if (*dd_idx >= pd_idx)
1910 (*dd_idx)++;
1911 qd_idx = raid_disks - 1;
1912 break;
1913
1914 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001915 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11001916 if (*dd_idx >= pd_idx)
1917 (*dd_idx)++;
1918 qd_idx = raid_disks - 1;
1919 break;
1920
1921 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001922 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11001923 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1924 qd_idx = raid_disks - 1;
1925 break;
1926
1927 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001928 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11001929 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1930 qd_idx = raid_disks - 1;
1931 break;
1932
1933 case ALGORITHM_PARITY_0_6:
1934 pd_idx = 0;
1935 (*dd_idx)++;
1936 qd_idx = raid_disks - 1;
1937 break;
1938
NeilBrown16a53ec2006-06-26 00:27:38 -07001939 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11001940 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001941 }
1942 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 }
1944
NeilBrown911d4ee2009-03-31 14:39:38 +11001945 if (sh) {
1946 sh->pd_idx = pd_idx;
1947 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001948 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11001949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 /*
1951 * Finally, compute the new sector number
1952 */
1953 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
1954 return new_sector;
1955}
1956
1957
NeilBrown784052e2009-03-31 15:19:07 +11001958static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
NeilBrownd1688a62011-10-11 16:49:52 +11001960 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08001961 int raid_disks = sh->disks;
1962 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001964 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1965 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11001966 int algorithm = previous ? conf->prev_algo
1967 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 sector_t stripe;
1969 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10001970 sector_t chunk_number;
1971 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11001973 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
NeilBrown16a53ec2006-06-26 00:27:38 -07001975
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 chunk_offset = sector_div(new_sector, sectors_per_chunk);
1977 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
NeilBrown16a53ec2006-06-26 00:27:38 -07001979 if (i == sh->pd_idx)
1980 return 0;
1981 switch(conf->level) {
1982 case 4: break;
1983 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001984 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 case ALGORITHM_LEFT_ASYMMETRIC:
1986 case ALGORITHM_RIGHT_ASYMMETRIC:
1987 if (i > sh->pd_idx)
1988 i--;
1989 break;
1990 case ALGORITHM_LEFT_SYMMETRIC:
1991 case ALGORITHM_RIGHT_SYMMETRIC:
1992 if (i < sh->pd_idx)
1993 i += raid_disks;
1994 i -= (sh->pd_idx + 1);
1995 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001996 case ALGORITHM_PARITY_0:
1997 i -= 1;
1998 break;
1999 case ALGORITHM_PARITY_N:
2000 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002002 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002003 }
2004 break;
2005 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002006 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002007 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002008 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002009 case ALGORITHM_LEFT_ASYMMETRIC:
2010 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002011 case ALGORITHM_ROTATING_ZERO_RESTART:
2012 case ALGORITHM_ROTATING_N_RESTART:
2013 if (sh->pd_idx == raid_disks-1)
2014 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002015 else if (i > sh->pd_idx)
2016 i -= 2; /* D D P Q D */
2017 break;
2018 case ALGORITHM_LEFT_SYMMETRIC:
2019 case ALGORITHM_RIGHT_SYMMETRIC:
2020 if (sh->pd_idx == raid_disks-1)
2021 i--; /* Q D D D P */
2022 else {
2023 /* D D P Q D */
2024 if (i < sh->pd_idx)
2025 i += raid_disks;
2026 i -= (sh->pd_idx + 2);
2027 }
2028 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002029 case ALGORITHM_PARITY_0:
2030 i -= 2;
2031 break;
2032 case ALGORITHM_PARITY_N:
2033 break;
2034 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002035 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002036 if (sh->pd_idx == 0)
2037 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002038 else {
2039 /* D D Q P D */
2040 if (i < sh->pd_idx)
2041 i += raid_disks;
2042 i -= (sh->pd_idx + 1);
2043 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002044 break;
2045 case ALGORITHM_LEFT_ASYMMETRIC_6:
2046 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2047 if (i > sh->pd_idx)
2048 i--;
2049 break;
2050 case ALGORITHM_LEFT_SYMMETRIC_6:
2051 case ALGORITHM_RIGHT_SYMMETRIC_6:
2052 if (i < sh->pd_idx)
2053 i += data_disks + 1;
2054 i -= (sh->pd_idx + 1);
2055 break;
2056 case ALGORITHM_PARITY_0_6:
2057 i -= 1;
2058 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002059 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002060 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002061 }
2062 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 }
2064
2065 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002066 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
NeilBrown112bf892009-03-31 14:39:38 +11002068 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002069 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002070 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2071 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002072 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2073 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 return 0;
2075 }
2076 return r_sector;
2077}
2078
2079
Dan Williams600aa102008-06-28 08:32:05 +10002080static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002081schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002082 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002083{
2084 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002085 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002086 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002087
Dan Williamse33129d2007-01-02 13:52:30 -07002088 if (rcw) {
2089 /* if we are not expanding this is a proper write request, and
2090 * there will be bios with new data to be drained into the
2091 * stripe cache
2092 */
2093 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10002094 sh->reconstruct_state = reconstruct_state_drain_run;
2095 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2096 } else
2097 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07002098
Dan Williamsac6b53b2009-07-14 13:40:19 -07002099 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002100
2101 for (i = disks; i--; ) {
2102 struct r5dev *dev = &sh->dev[i];
2103
2104 if (dev->towrite) {
2105 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002106 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002107 if (!expand)
2108 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002109 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002110 }
2111 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002112 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002113 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002114 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002115 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002116 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002117 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2118 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2119
Dan Williamsd8ee0722008-06-28 08:32:06 +10002120 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002121 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2122 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002123 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002124
2125 for (i = disks; i--; ) {
2126 struct r5dev *dev = &sh->dev[i];
2127 if (i == pd_idx)
2128 continue;
2129
Dan Williamse33129d2007-01-02 13:52:30 -07002130 if (dev->towrite &&
2131 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002132 test_bit(R5_Wantcompute, &dev->flags))) {
2133 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002134 set_bit(R5_LOCKED, &dev->flags);
2135 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002136 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002137 }
2138 }
2139 }
2140
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002141 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002142 * are in flight
2143 */
2144 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2145 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002146 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002147
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002148 if (level == 6) {
2149 int qd_idx = sh->qd_idx;
2150 struct r5dev *dev = &sh->dev[qd_idx];
2151
2152 set_bit(R5_LOCKED, &dev->flags);
2153 clear_bit(R5_UPTODATE, &dev->flags);
2154 s->locked++;
2155 }
2156
Dan Williams600aa102008-06-28 08:32:05 +10002157 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002158 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002159 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002160}
NeilBrown16a53ec2006-06-26 00:27:38 -07002161
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162/*
2163 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002164 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 * The bi_next chain must be in order.
2166 */
2167static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2168{
2169 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002170 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002171 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
NeilBrowncbe47ec2011-07-26 11:20:35 +10002173 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 (unsigned long long)bi->bi_sector,
2175 (unsigned long long)sh->sector);
2176
2177
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07002179 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -07002181 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
2182 firstwrite = 1;
2183 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 bip = &sh->dev[dd_idx].toread;
2185 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2186 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2187 goto overlap;
2188 bip = & (*bip)->bi_next;
2189 }
2190 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2191 goto overlap;
2192
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002193 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 if (*bip)
2195 bi->bi_next = *bip;
2196 *bip = bi;
Jens Axboe960e7392008-08-15 10:41:18 +02002197 bi->bi_phys_segments++;
NeilBrown72626682005-09-09 16:23:54 -07002198
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 if (forwrite) {
2200 /* check if page is covered */
2201 sector_t sector = sh->dev[dd_idx].sector;
2202 for (bi=sh->dev[dd_idx].towrite;
2203 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2204 bi && bi->bi_sector <= sector;
2205 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2206 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2207 sector = bi->bi_sector + (bi->bi_size>>9);
2208 }
2209 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2210 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2211 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002212 spin_unlock_irq(&conf->device_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002213
2214 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2215 (unsigned long long)(*bip)->bi_sector,
2216 (unsigned long long)sh->sector, dd_idx);
2217
2218 if (conf->mddev->bitmap && firstwrite) {
2219 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2220 STRIPE_SECTORS, 0);
2221 sh->bm_seq = conf->seq_flush+1;
2222 set_bit(STRIPE_BIT_DELAY, &sh->state);
2223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 return 1;
2225
2226 overlap:
2227 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
2228 spin_unlock_irq(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 return 0;
2230}
2231
NeilBrownd1688a62011-10-11 16:49:52 +11002232static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002233
NeilBrownd1688a62011-10-11 16:49:52 +11002234static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002235 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002236{
NeilBrown784052e2009-03-31 15:19:07 +11002237 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002238 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002239 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002240 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002241 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002242
NeilBrown112bf892009-03-31 14:39:38 +11002243 raid5_compute_sector(conf,
2244 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002245 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002246 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002247 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002248}
2249
Dan Williamsa4456852007-07-09 11:56:43 -07002250static void
NeilBrownd1688a62011-10-11 16:49:52 +11002251handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002252 struct stripe_head_state *s, int disks,
2253 struct bio **return_bi)
2254{
2255 int i;
2256 for (i = disks; i--; ) {
2257 struct bio *bi;
2258 int bitmap_end = 0;
2259
2260 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002261 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002262 rcu_read_lock();
2263 rdev = rcu_dereference(conf->disks[i].rdev);
2264 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002265 atomic_inc(&rdev->nr_pending);
2266 else
2267 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002268 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002269 if (rdev) {
2270 if (!rdev_set_badblocks(
2271 rdev,
2272 sh->sector,
2273 STRIPE_SECTORS, 0))
2274 md_error(conf->mddev, rdev);
2275 rdev_dec_pending(rdev, conf->mddev);
2276 }
Dan Williamsa4456852007-07-09 11:56:43 -07002277 }
2278 spin_lock_irq(&conf->device_lock);
2279 /* fail all writes first */
2280 bi = sh->dev[i].towrite;
2281 sh->dev[i].towrite = NULL;
2282 if (bi) {
2283 s->to_write--;
2284 bitmap_end = 1;
2285 }
2286
2287 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2288 wake_up(&conf->wait_for_overlap);
2289
2290 while (bi && bi->bi_sector <
2291 sh->dev[i].sector + STRIPE_SECTORS) {
2292 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2293 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002294 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002295 md_write_end(conf->mddev);
2296 bi->bi_next = *return_bi;
2297 *return_bi = bi;
2298 }
2299 bi = nextbi;
2300 }
2301 /* and fail all 'written' */
2302 bi = sh->dev[i].written;
2303 sh->dev[i].written = NULL;
2304 if (bi) bitmap_end = 1;
2305 while (bi && bi->bi_sector <
2306 sh->dev[i].sector + STRIPE_SECTORS) {
2307 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2308 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002309 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002310 md_write_end(conf->mddev);
2311 bi->bi_next = *return_bi;
2312 *return_bi = bi;
2313 }
2314 bi = bi2;
2315 }
2316
Dan Williamsb5e98d62007-01-02 13:52:31 -07002317 /* fail any reads if this device is non-operational and
2318 * the data has not reached the cache yet.
2319 */
2320 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2321 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2322 test_bit(R5_ReadError, &sh->dev[i].flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002323 bi = sh->dev[i].toread;
2324 sh->dev[i].toread = NULL;
2325 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2326 wake_up(&conf->wait_for_overlap);
2327 if (bi) s->to_read--;
2328 while (bi && bi->bi_sector <
2329 sh->dev[i].sector + STRIPE_SECTORS) {
2330 struct bio *nextbi =
2331 r5_next_bio(bi, sh->dev[i].sector);
2332 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002333 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002334 bi->bi_next = *return_bi;
2335 *return_bi = bi;
2336 }
2337 bi = nextbi;
2338 }
2339 }
2340 spin_unlock_irq(&conf->device_lock);
2341 if (bitmap_end)
2342 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2343 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002344 /* If we were in the middle of a write the parity block might
2345 * still be locked - so just clear all R5_LOCKED flags
2346 */
2347 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002348 }
2349
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002350 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2351 if (atomic_dec_and_test(&conf->pending_full_writes))
2352 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002353}
2354
NeilBrown7f0da592011-07-28 11:39:22 +10002355static void
NeilBrownd1688a62011-10-11 16:49:52 +11002356handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002357 struct stripe_head_state *s)
2358{
2359 int abort = 0;
2360 int i;
2361
2362 md_done_sync(conf->mddev, STRIPE_SECTORS, 0);
2363 clear_bit(STRIPE_SYNCING, &sh->state);
2364 s->syncing = 0;
2365 /* There is nothing more to do for sync/check/repair.
2366 * For recover we need to record a bad block on all
2367 * non-sync devices, or abort the recovery
2368 */
2369 if (!test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery))
2370 return;
2371 /* During recovery devices cannot be removed, so locking and
2372 * refcounting of rdevs is not needed
2373 */
2374 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +11002375 struct md_rdev *rdev = conf->disks[i].rdev;
NeilBrown7f0da592011-07-28 11:39:22 +10002376 if (!rdev
2377 || test_bit(Faulty, &rdev->flags)
2378 || test_bit(In_sync, &rdev->flags))
2379 continue;
2380 if (!rdev_set_badblocks(rdev, sh->sector,
2381 STRIPE_SECTORS, 0))
2382 abort = 1;
2383 }
2384 if (abort) {
2385 conf->recovery_disabled = conf->mddev->recovery_disabled;
2386 set_bit(MD_RECOVERY_INTR, &conf->mddev->recovery);
2387 }
2388}
2389
NeilBrown93b3dbc2011-07-27 11:00:36 +10002390/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002391 * to be read or computed to satisfy a request.
2392 *
2393 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002394 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002395 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002396static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2397 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002398{
2399 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002400 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2401 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002402
Dan Williamsf38e1212007-01-02 13:52:30 -07002403 /* is the data in this block needed, and can we get it? */
2404 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002405 !test_bit(R5_UPTODATE, &dev->flags) &&
2406 (dev->toread ||
2407 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2408 s->syncing || s->expanding ||
NeilBrown5d35e092011-07-27 11:00:36 +10002409 (s->failed >= 1 && fdev[0]->toread) ||
2410 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002411 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2412 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2413 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002414 /* we would like to get this block, possibly by computing it,
2415 * otherwise read it if the backing disk is insync
2416 */
2417 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2418 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2419 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002420 (s->failed && (disk_idx == s->failed_num[0] ||
2421 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002422 /* have disk failed, and we're requested to fetch it;
2423 * do compute it
2424 */
2425 pr_debug("Computing stripe %llu block %d\n",
2426 (unsigned long long)sh->sector, disk_idx);
2427 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2428 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2429 set_bit(R5_Wantcompute, &dev->flags);
2430 sh->ops.target = disk_idx;
2431 sh->ops.target2 = -1; /* no 2nd target */
2432 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002433 /* Careful: from this point on 'uptodate' is in the eye
2434 * of raid_run_ops which services 'compute' operations
2435 * before writes. R5_Wantcompute flags a block that will
2436 * be R5_UPTODATE by the time it is needed for a
2437 * subsequent operation.
2438 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002439 s->uptodate++;
2440 return 1;
2441 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2442 /* Computing 2-failure is *very* expensive; only
2443 * do it if failed >= 2
2444 */
2445 int other;
2446 for (other = disks; other--; ) {
2447 if (other == disk_idx)
2448 continue;
2449 if (!test_bit(R5_UPTODATE,
2450 &sh->dev[other].flags))
2451 break;
2452 }
2453 BUG_ON(other < 0);
2454 pr_debug("Computing stripe %llu blocks %d,%d\n",
2455 (unsigned long long)sh->sector,
2456 disk_idx, other);
2457 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2458 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2459 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2460 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2461 sh->ops.target = disk_idx;
2462 sh->ops.target2 = other;
2463 s->uptodate += 2;
2464 s->req_compute = 1;
2465 return 1;
2466 } else if (test_bit(R5_Insync, &dev->flags)) {
2467 set_bit(R5_LOCKED, &dev->flags);
2468 set_bit(R5_Wantread, &dev->flags);
2469 s->locked++;
2470 pr_debug("Reading block %d (sync=%d)\n",
2471 disk_idx, s->syncing);
2472 }
2473 }
2474
2475 return 0;
2476}
2477
2478/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002479 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002480 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002481static void handle_stripe_fill(struct stripe_head *sh,
2482 struct stripe_head_state *s,
2483 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002484{
2485 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002486
2487 /* look for blocks to read/compute, skip this if a compute
2488 * is already in flight, or if the stripe contents are in the
2489 * midst of changing due to a write
2490 */
2491 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2492 !sh->reconstruct_state)
2493 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002494 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002495 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002496 set_bit(STRIPE_HANDLE, &sh->state);
2497}
2498
2499
Dan Williams1fe797e2008-06-28 09:16:30 +10002500/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002501 * any written block on an uptodate or failed drive can be returned.
2502 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2503 * never LOCKED, so we don't need to test 'failed' directly.
2504 */
NeilBrownd1688a62011-10-11 16:49:52 +11002505static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002506 struct stripe_head *sh, int disks, struct bio **return_bi)
2507{
2508 int i;
2509 struct r5dev *dev;
2510
2511 for (i = disks; i--; )
2512 if (sh->dev[i].written) {
2513 dev = &sh->dev[i];
2514 if (!test_bit(R5_LOCKED, &dev->flags) &&
2515 test_bit(R5_UPTODATE, &dev->flags)) {
2516 /* We can return any write requests */
2517 struct bio *wbi, *wbi2;
2518 int bitmap_end = 0;
Dan Williams45b42332007-07-09 11:56:43 -07002519 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002520 spin_lock_irq(&conf->device_lock);
2521 wbi = dev->written;
2522 dev->written = NULL;
2523 while (wbi && wbi->bi_sector <
2524 dev->sector + STRIPE_SECTORS) {
2525 wbi2 = r5_next_bio(wbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +02002526 if (!raid5_dec_bi_phys_segments(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002527 md_write_end(conf->mddev);
2528 wbi->bi_next = *return_bi;
2529 *return_bi = wbi;
2530 }
2531 wbi = wbi2;
2532 }
2533 if (dev->towrite == NULL)
2534 bitmap_end = 1;
2535 spin_unlock_irq(&conf->device_lock);
2536 if (bitmap_end)
2537 bitmap_endwrite(conf->mddev->bitmap,
2538 sh->sector,
2539 STRIPE_SECTORS,
2540 !test_bit(STRIPE_DEGRADED, &sh->state),
2541 0);
2542 }
2543 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002544
2545 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2546 if (atomic_dec_and_test(&conf->pending_full_writes))
2547 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002548}
2549
NeilBrownd1688a62011-10-11 16:49:52 +11002550static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002551 struct stripe_head *sh,
2552 struct stripe_head_state *s,
2553 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002554{
2555 int rmw = 0, rcw = 0, i;
NeilBrownc8ac1802011-07-27 11:00:36 +10002556 if (conf->max_degraded == 2) {
2557 /* RAID6 requires 'rcw' in current implementation
2558 * Calculate the real rcw later - for now fake it
2559 * look like rcw is cheaper
2560 */
2561 rcw = 1; rmw = 2;
2562 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002563 /* would I have to read this buffer for read_modify_write */
2564 struct r5dev *dev = &sh->dev[i];
2565 if ((dev->towrite || i == sh->pd_idx) &&
2566 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002567 !(test_bit(R5_UPTODATE, &dev->flags) ||
2568 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002569 if (test_bit(R5_Insync, &dev->flags))
2570 rmw++;
2571 else
2572 rmw += 2*disks; /* cannot read it */
2573 }
2574 /* Would I have to read this buffer for reconstruct_write */
2575 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2576 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002577 !(test_bit(R5_UPTODATE, &dev->flags) ||
2578 test_bit(R5_Wantcompute, &dev->flags))) {
2579 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002580 else
2581 rcw += 2*disks;
2582 }
2583 }
Dan Williams45b42332007-07-09 11:56:43 -07002584 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002585 (unsigned long long)sh->sector, rmw, rcw);
2586 set_bit(STRIPE_HANDLE, &sh->state);
2587 if (rmw < rcw && rmw > 0)
2588 /* prefer read-modify-write, but need to get some data */
2589 for (i = disks; i--; ) {
2590 struct r5dev *dev = &sh->dev[i];
2591 if ((dev->towrite || i == sh->pd_idx) &&
2592 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002593 !(test_bit(R5_UPTODATE, &dev->flags) ||
2594 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002595 test_bit(R5_Insync, &dev->flags)) {
2596 if (
2597 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002598 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002599 "%d for r-m-w\n", i);
2600 set_bit(R5_LOCKED, &dev->flags);
2601 set_bit(R5_Wantread, &dev->flags);
2602 s->locked++;
2603 } else {
2604 set_bit(STRIPE_DELAYED, &sh->state);
2605 set_bit(STRIPE_HANDLE, &sh->state);
2606 }
2607 }
2608 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002609 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002610 /* want reconstruct write, but need to get some data */
NeilBrownc8ac1802011-07-27 11:00:36 +10002611 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002612 for (i = disks; i--; ) {
2613 struct r5dev *dev = &sh->dev[i];
2614 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002615 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002616 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002617 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002618 test_bit(R5_Wantcompute, &dev->flags))) {
2619 rcw++;
2620 if (!test_bit(R5_Insync, &dev->flags))
2621 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002622 if (
2623 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002624 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002625 "%d for Reconstruct\n", i);
2626 set_bit(R5_LOCKED, &dev->flags);
2627 set_bit(R5_Wantread, &dev->flags);
2628 s->locked++;
2629 } else {
2630 set_bit(STRIPE_DELAYED, &sh->state);
2631 set_bit(STRIPE_HANDLE, &sh->state);
2632 }
2633 }
2634 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002635 }
Dan Williamsa4456852007-07-09 11:56:43 -07002636 /* now if nothing is locked, and if we have enough data,
2637 * we can start a write request
2638 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002639 /* since handle_stripe can be called at any time we need to handle the
2640 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002641 * subsequent call wants to start a write request. raid_run_ops only
2642 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002643 * simultaneously. If this is not the case then new writes need to be
2644 * held off until the compute completes.
2645 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002646 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2647 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2648 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002649 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002650}
2651
NeilBrownd1688a62011-10-11 16:49:52 +11002652static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002653 struct stripe_head_state *s, int disks)
2654{
Dan Williamsecc65c92008-06-28 08:31:57 +10002655 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002656
Dan Williamsbd2ab672008-04-10 21:29:27 -07002657 set_bit(STRIPE_HANDLE, &sh->state);
2658
Dan Williamsecc65c92008-06-28 08:31:57 +10002659 switch (sh->check_state) {
2660 case check_state_idle:
2661 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002662 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002663 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002664 sh->check_state = check_state_run;
2665 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002666 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002667 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002668 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002669 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002670 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002671 /* fall through */
2672 case check_state_compute_result:
2673 sh->check_state = check_state_idle;
2674 if (!dev)
2675 dev = &sh->dev[sh->pd_idx];
2676
2677 /* check that a write has not made the stripe insync */
2678 if (test_bit(STRIPE_INSYNC, &sh->state))
2679 break;
2680
2681 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002682 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2683 BUG_ON(s->uptodate != disks);
2684
2685 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002686 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002687 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002688
Dan Williamsa4456852007-07-09 11:56:43 -07002689 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002690 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002691 break;
2692 case check_state_run:
2693 break; /* we will be called again upon completion */
2694 case check_state_check_result:
2695 sh->check_state = check_state_idle;
2696
2697 /* if a failure occurred during the check operation, leave
2698 * STRIPE_INSYNC not set and let the stripe be handled again
2699 */
2700 if (s->failed)
2701 break;
2702
2703 /* handle a successful check operation, if parity is correct
2704 * we are done. Otherwise update the mismatch count and repair
2705 * parity if !MD_RECOVERY_CHECK
2706 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002707 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002708 /* parity is correct (on disc,
2709 * not in buffer any more)
2710 */
2711 set_bit(STRIPE_INSYNC, &sh->state);
2712 else {
2713 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2714 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2715 /* don't try to repair!! */
2716 set_bit(STRIPE_INSYNC, &sh->state);
2717 else {
2718 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002719 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002720 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2721 set_bit(R5_Wantcompute,
2722 &sh->dev[sh->pd_idx].flags);
2723 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002724 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10002725 s->uptodate++;
2726 }
2727 }
2728 break;
2729 case check_state_compute_run:
2730 break;
2731 default:
2732 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2733 __func__, sh->check_state,
2734 (unsigned long long) sh->sector);
2735 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002736 }
2737}
2738
2739
NeilBrownd1688a62011-10-11 16:49:52 +11002740static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07002741 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10002742 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002743{
Dan Williamsa4456852007-07-09 11:56:43 -07002744 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11002745 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07002746 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07002747
2748 set_bit(STRIPE_HANDLE, &sh->state);
2749
2750 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002751
Dan Williamsa4456852007-07-09 11:56:43 -07002752 /* Want to check and possibly repair P and Q.
2753 * However there could be one 'failed' device, in which
2754 * case we can only check one of them, possibly using the
2755 * other to generate missing data
2756 */
2757
Dan Williamsd82dfee2009-07-14 13:40:57 -07002758 switch (sh->check_state) {
2759 case check_state_idle:
2760 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10002761 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002762 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07002763 * makes sense to check P (If anything else were failed,
2764 * we would have used P to recreate it).
2765 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002766 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07002767 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002768 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002769 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07002770 * anything, so it makes sense to check it
2771 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002772 if (sh->check_state == check_state_run)
2773 sh->check_state = check_state_run_pq;
2774 else
2775 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07002776 }
Dan Williams36d1c642009-07-14 11:48:22 -07002777
Dan Williamsd82dfee2009-07-14 13:40:57 -07002778 /* discard potentially stale zero_sum_result */
2779 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07002780
Dan Williamsd82dfee2009-07-14 13:40:57 -07002781 if (sh->check_state == check_state_run) {
2782 /* async_xor_zero_sum destroys the contents of P */
2783 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
2784 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07002785 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002786 if (sh->check_state >= check_state_run &&
2787 sh->check_state <= check_state_run_pq) {
2788 /* async_syndrome_zero_sum preserves P and Q, so
2789 * no need to mark them !uptodate here
2790 */
2791 set_bit(STRIPE_OP_CHECK, &s->ops_request);
2792 break;
2793 }
Dan Williams36d1c642009-07-14 11:48:22 -07002794
Dan Williamsd82dfee2009-07-14 13:40:57 -07002795 /* we have 2-disk failure */
2796 BUG_ON(s->failed != 2);
2797 /* fall through */
2798 case check_state_compute_result:
2799 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07002800
Dan Williamsd82dfee2009-07-14 13:40:57 -07002801 /* check that a write has not made the stripe insync */
2802 if (test_bit(STRIPE_INSYNC, &sh->state))
2803 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002804
2805 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07002806 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07002807 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002808 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07002809 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10002810 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07002811 s->locked++;
2812 set_bit(R5_LOCKED, &dev->flags);
2813 set_bit(R5_Wantwrite, &dev->flags);
2814 }
2815 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10002816 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07002817 s->locked++;
2818 set_bit(R5_LOCKED, &dev->flags);
2819 set_bit(R5_Wantwrite, &dev->flags);
2820 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002821 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002822 dev = &sh->dev[pd_idx];
2823 s->locked++;
2824 set_bit(R5_LOCKED, &dev->flags);
2825 set_bit(R5_Wantwrite, &dev->flags);
2826 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002827 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002828 dev = &sh->dev[qd_idx];
2829 s->locked++;
2830 set_bit(R5_LOCKED, &dev->flags);
2831 set_bit(R5_Wantwrite, &dev->flags);
2832 }
2833 clear_bit(STRIPE_DEGRADED, &sh->state);
2834
2835 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002836 break;
2837 case check_state_run:
2838 case check_state_run_q:
2839 case check_state_run_pq:
2840 break; /* we will be called again upon completion */
2841 case check_state_check_result:
2842 sh->check_state = check_state_idle;
2843
2844 /* handle a successful check operation, if parity is correct
2845 * we are done. Otherwise update the mismatch count and repair
2846 * parity if !MD_RECOVERY_CHECK
2847 */
2848 if (sh->ops.zero_sum_result == 0) {
2849 /* both parities are correct */
2850 if (!s->failed)
2851 set_bit(STRIPE_INSYNC, &sh->state);
2852 else {
2853 /* in contrast to the raid5 case we can validate
2854 * parity, but still have a failure to write
2855 * back
2856 */
2857 sh->check_state = check_state_compute_result;
2858 /* Returning at this point means that we may go
2859 * off and bring p and/or q uptodate again so
2860 * we make sure to check zero_sum_result again
2861 * to verify if p or q need writeback
2862 */
2863 }
2864 } else {
2865 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2866 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2867 /* don't try to repair!! */
2868 set_bit(STRIPE_INSYNC, &sh->state);
2869 else {
2870 int *target = &sh->ops.target;
2871
2872 sh->ops.target = -1;
2873 sh->ops.target2 = -1;
2874 sh->check_state = check_state_compute_run;
2875 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2876 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2877 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
2878 set_bit(R5_Wantcompute,
2879 &sh->dev[pd_idx].flags);
2880 *target = pd_idx;
2881 target = &sh->ops.target2;
2882 s->uptodate++;
2883 }
2884 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
2885 set_bit(R5_Wantcompute,
2886 &sh->dev[qd_idx].flags);
2887 *target = qd_idx;
2888 s->uptodate++;
2889 }
2890 }
2891 }
2892 break;
2893 case check_state_compute_run:
2894 break;
2895 default:
2896 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2897 __func__, sh->check_state,
2898 (unsigned long long) sh->sector);
2899 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002900 }
2901}
2902
NeilBrownd1688a62011-10-11 16:49:52 +11002903static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07002904{
2905 int i;
2906
2907 /* We have read all the blocks in this stripe and now we need to
2908 * copy some of them into a target stripe for expand.
2909 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07002910 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002911 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2912 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11002913 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11002914 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07002915 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07002916 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07002917
NeilBrown784052e2009-03-31 15:19:07 +11002918 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11002919 sector_t s = raid5_compute_sector(conf, bn, 0,
2920 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10002921 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07002922 if (sh2 == NULL)
2923 /* so far only the early blocks of this stripe
2924 * have been requested. When later blocks
2925 * get requested, we will try again
2926 */
2927 continue;
2928 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
2929 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
2930 /* must have already done this block */
2931 release_stripe(sh2);
2932 continue;
2933 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07002934
2935 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07002936 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002937 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07002938 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07002939 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002940
Dan Williamsa4456852007-07-09 11:56:43 -07002941 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
2942 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
2943 for (j = 0; j < conf->raid_disks; j++)
2944 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10002945 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002946 !test_bit(R5_Expanded, &sh2->dev[j].flags))
2947 break;
2948 if (j == conf->raid_disks) {
2949 set_bit(STRIPE_EXPAND_READY, &sh2->state);
2950 set_bit(STRIPE_HANDLE, &sh2->state);
2951 }
2952 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002953
Dan Williamsa4456852007-07-09 11:56:43 -07002954 }
NeilBrowna2e08552007-09-11 15:23:36 -07002955 /* done submitting copies, wait for them to complete */
2956 if (tx) {
2957 async_tx_ack(tx);
2958 dma_wait_for_async_tx(tx);
2959 }
Dan Williamsa4456852007-07-09 11:56:43 -07002960}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
Dan Williams6bfe0b42008-04-30 00:52:32 -07002962
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963/*
2964 * handle_stripe - do things to a stripe.
2965 *
2966 * We lock the stripe and then examine the state of various bits
2967 * to see what needs to be done.
2968 * Possible results:
2969 * return some read request which now have data
2970 * return some write requests which are safely on disc
2971 * schedule a read on some buffers
2972 * schedule a write of some buffers
2973 * return confirmation of parity correctness
2974 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 * buffers are taken off read_list or write_list, and bh_cache buffers
2976 * get BH_Lock set before the stripe lock is released.
2977 *
2978 */
Dan Williamsa4456852007-07-09 11:56:43 -07002979
NeilBrownacfe7262011-07-27 11:00:36 +10002980static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07002981{
NeilBrownd1688a62011-10-11 16:49:52 +11002982 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08002983 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10002984 struct r5dev *dev;
2985 int i;
NeilBrown16a53ec2006-06-26 00:27:38 -07002986
NeilBrownacfe7262011-07-27 11:00:36 +10002987 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07002988
NeilBrownacfe7262011-07-27 11:00:36 +10002989 s->syncing = test_bit(STRIPE_SYNCING, &sh->state);
2990 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2991 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
2992 s->failed_num[0] = -1;
2993 s->failed_num[1] = -1;
2994
2995 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07002996 rcu_read_lock();
NeilBrownc4c16632011-07-26 11:34:20 +10002997 spin_lock_irq(&conf->device_lock);
NeilBrown16a53ec2006-06-26 00:27:38 -07002998 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11002999 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003000 sector_t first_bad;
3001 int bad_sectors;
3002 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003003
NeilBrown16a53ec2006-06-26 00:27:38 -07003004 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003005
Dan Williams45b42332007-07-09 11:56:43 -07003006 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown16a53ec2006-06-26 00:27:38 -07003007 i, dev->flags, dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003008 /* maybe we can reply to a read
3009 *
3010 * new wantfill requests are only permitted while
3011 * ops_complete_biofill is guaranteed to be inactive
3012 */
3013 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3014 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3015 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003016
3017 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003018 if (test_bit(R5_LOCKED, &dev->flags))
3019 s->locked++;
3020 if (test_bit(R5_UPTODATE, &dev->flags))
3021 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003022 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003023 s->compute++;
3024 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003025 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003026
NeilBrownacfe7262011-07-27 11:00:36 +10003027 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003028 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003029 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003030 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003031 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003032 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003033 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003034 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003035 }
Dan Williamsa4456852007-07-09 11:56:43 -07003036 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003037 s->written++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003038 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown9283d8c2011-12-08 16:27:57 +11003039 if (rdev && test_bit(Faulty, &rdev->flags))
3040 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003041 if (rdev) {
3042 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3043 &first_bad, &bad_sectors);
3044 if (s->blocked_rdev == NULL
3045 && (test_bit(Blocked, &rdev->flags)
3046 || is_bad < 0)) {
3047 if (is_bad < 0)
3048 set_bit(BlockedBadBlocks,
3049 &rdev->flags);
3050 s->blocked_rdev = rdev;
3051 atomic_inc(&rdev->nr_pending);
3052 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003053 }
NeilBrown415e72d2010-06-17 17:25:21 +10003054 clear_bit(R5_Insync, &dev->flags);
3055 if (!rdev)
3056 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003057 else if (is_bad) {
3058 /* also not in-sync */
3059 if (!test_bit(WriteErrorSeen, &rdev->flags)) {
3060 /* treat as in-sync, but with a read error
3061 * which we can now try to correct
3062 */
3063 set_bit(R5_Insync, &dev->flags);
3064 set_bit(R5_ReadError, &dev->flags);
3065 }
3066 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003067 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003068 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003069 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003070 set_bit(R5_Insync, &dev->flags);
3071 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3072 test_bit(R5_Expanded, &dev->flags))
3073 /* If we've reshaped into here, we assume it is Insync.
3074 * We will shortly update recovery_offset to make
3075 * it official.
3076 */
3077 set_bit(R5_Insync, &dev->flags);
3078
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003079 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003080 clear_bit(R5_Insync, &dev->flags);
3081 if (!test_bit(Faulty, &rdev->flags)) {
3082 s->handle_bad_blocks = 1;
3083 atomic_inc(&rdev->nr_pending);
3084 } else
3085 clear_bit(R5_WriteError, &dev->flags);
3086 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003087 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003088 if (!test_bit(Faulty, &rdev->flags)) {
3089 s->handle_bad_blocks = 1;
3090 atomic_inc(&rdev->nr_pending);
3091 } else
3092 clear_bit(R5_MadeGood, &dev->flags);
3093 }
NeilBrown415e72d2010-06-17 17:25:21 +10003094 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003095 /* The ReadError flag will just be confusing now */
3096 clear_bit(R5_ReadError, &dev->flags);
3097 clear_bit(R5_ReWrite, &dev->flags);
3098 }
NeilBrown415e72d2010-06-17 17:25:21 +10003099 if (test_bit(R5_ReadError, &dev->flags))
3100 clear_bit(R5_Insync, &dev->flags);
3101 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003102 if (s->failed < 2)
3103 s->failed_num[s->failed] = i;
3104 s->failed++;
NeilBrown415e72d2010-06-17 17:25:21 +10003105 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003106 }
NeilBrownc4c16632011-07-26 11:34:20 +10003107 spin_unlock_irq(&conf->device_lock);
NeilBrown16a53ec2006-06-26 00:27:38 -07003108 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003109}
NeilBrownf4168852007-02-28 20:11:53 -08003110
NeilBrowncc940152011-07-26 11:35:35 +10003111static void handle_stripe(struct stripe_head *sh)
3112{
3113 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003114 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003115 int i;
NeilBrown84789552011-07-27 11:00:36 +10003116 int prexor;
3117 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003118 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003119
3120 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003121 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003122 /* already being handled, ensure it gets handled
3123 * again when current action finishes */
3124 set_bit(STRIPE_HANDLE, &sh->state);
3125 return;
3126 }
3127
3128 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3129 set_bit(STRIPE_SYNCING, &sh->state);
3130 clear_bit(STRIPE_INSYNC, &sh->state);
3131 }
3132 clear_bit(STRIPE_DELAYED, &sh->state);
3133
3134 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3135 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3136 (unsigned long long)sh->sector, sh->state,
3137 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3138 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003139
NeilBrownacfe7262011-07-27 11:00:36 +10003140 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003141
NeilBrownbc2607f2011-07-28 11:39:22 +10003142 if (s.handle_bad_blocks) {
3143 set_bit(STRIPE_HANDLE, &sh->state);
3144 goto finish;
3145 }
3146
NeilBrown474af965fe2011-07-27 11:00:36 +10003147 if (unlikely(s.blocked_rdev)) {
3148 if (s.syncing || s.expanding || s.expanded ||
3149 s.to_write || s.written) {
3150 set_bit(STRIPE_HANDLE, &sh->state);
3151 goto finish;
3152 }
3153 /* There is nothing for the blocked_rdev to block */
3154 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3155 s.blocked_rdev = NULL;
3156 }
3157
3158 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3159 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3160 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3161 }
3162
3163 pr_debug("locked=%d uptodate=%d to_read=%d"
3164 " to_write=%d failed=%d failed_num=%d,%d\n",
3165 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3166 s.failed_num[0], s.failed_num[1]);
3167 /* check if the array has lost more than max_degraded devices and,
3168 * if so, some requests might need to be failed.
3169 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003170 if (s.failed > conf->max_degraded) {
3171 sh->check_state = 0;
3172 sh->reconstruct_state = 0;
3173 if (s.to_read+s.to_write+s.written)
3174 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
3175 if (s.syncing)
3176 handle_failed_sync(conf, sh, &s);
3177 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003178
3179 /*
3180 * might be able to return some write requests if the parity blocks
3181 * are safe, or on a failed drive
3182 */
3183 pdev = &sh->dev[sh->pd_idx];
3184 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3185 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3186 qdev = &sh->dev[sh->qd_idx];
3187 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3188 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3189 || conf->level < 6;
3190
3191 if (s.written &&
3192 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3193 && !test_bit(R5_LOCKED, &pdev->flags)
3194 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3195 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3196 && !test_bit(R5_LOCKED, &qdev->flags)
3197 && test_bit(R5_UPTODATE, &qdev->flags)))))
3198 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3199
3200 /* Now we might consider reading some blocks, either to check/generate
3201 * parity, or to satisfy requests
3202 * or to load a block that is being partially written.
3203 */
3204 if (s.to_read || s.non_overwrite
3205 || (conf->level == 6 && s.to_write && s.failed)
3206 || (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
3207 handle_stripe_fill(sh, &s, disks);
3208
NeilBrown84789552011-07-27 11:00:36 +10003209 /* Now we check to see if any write operations have recently
3210 * completed
3211 */
3212 prexor = 0;
3213 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3214 prexor = 1;
3215 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3216 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3217 sh->reconstruct_state = reconstruct_state_idle;
3218
3219 /* All the 'written' buffers and the parity block are ready to
3220 * be written back to disk
3221 */
3222 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3223 BUG_ON(sh->qd_idx >= 0 &&
3224 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags));
3225 for (i = disks; i--; ) {
3226 struct r5dev *dev = &sh->dev[i];
3227 if (test_bit(R5_LOCKED, &dev->flags) &&
3228 (i == sh->pd_idx || i == sh->qd_idx ||
3229 dev->written)) {
3230 pr_debug("Writing block %d\n", i);
3231 set_bit(R5_Wantwrite, &dev->flags);
3232 if (prexor)
3233 continue;
3234 if (!test_bit(R5_Insync, &dev->flags) ||
3235 ((i == sh->pd_idx || i == sh->qd_idx) &&
3236 s.failed == 0))
3237 set_bit(STRIPE_INSYNC, &sh->state);
3238 }
3239 }
3240 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3241 s.dec_preread_active = 1;
3242 }
3243
3244 /* Now to consider new write requests and what else, if anything
3245 * should be read. We do not handle new writes when:
3246 * 1/ A 'write' operation (copy+xor) is already in flight.
3247 * 2/ A 'check' operation is in flight, as it may clobber the parity
3248 * block.
3249 */
3250 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3251 handle_stripe_dirtying(conf, sh, &s, disks);
3252
3253 /* maybe we need to check and possibly fix the parity for this stripe
3254 * Any reads will already have been scheduled, so we just see if enough
3255 * data is available. The parity check is held off while parity
3256 * dependent operations are in flight.
3257 */
3258 if (sh->check_state ||
3259 (s.syncing && s.locked == 0 &&
3260 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3261 !test_bit(STRIPE_INSYNC, &sh->state))) {
3262 if (conf->level == 6)
3263 handle_parity_checks6(conf, sh, &s, disks);
3264 else
3265 handle_parity_checks5(conf, sh, &s, disks);
3266 }
NeilBrownc5a31002011-07-27 11:00:36 +10003267
3268 if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
3269 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3270 clear_bit(STRIPE_SYNCING, &sh->state);
3271 }
3272
3273 /* If the failed drives are just a ReadError, then we might need
3274 * to progress the repair/check process
3275 */
3276 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3277 for (i = 0; i < s.failed; i++) {
3278 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3279 if (test_bit(R5_ReadError, &dev->flags)
3280 && !test_bit(R5_LOCKED, &dev->flags)
3281 && test_bit(R5_UPTODATE, &dev->flags)
3282 ) {
3283 if (!test_bit(R5_ReWrite, &dev->flags)) {
3284 set_bit(R5_Wantwrite, &dev->flags);
3285 set_bit(R5_ReWrite, &dev->flags);
3286 set_bit(R5_LOCKED, &dev->flags);
3287 s.locked++;
3288 } else {
3289 /* let's read it back */
3290 set_bit(R5_Wantread, &dev->flags);
3291 set_bit(R5_LOCKED, &dev->flags);
3292 s.locked++;
3293 }
3294 }
3295 }
3296
3297
NeilBrown3687c062011-07-27 11:00:36 +10003298 /* Finish reconstruct operations initiated by the expansion process */
3299 if (sh->reconstruct_state == reconstruct_state_result) {
3300 struct stripe_head *sh_src
3301 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3302 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3303 /* sh cannot be written until sh_src has been read.
3304 * so arrange for sh to be delayed a little
3305 */
3306 set_bit(STRIPE_DELAYED, &sh->state);
3307 set_bit(STRIPE_HANDLE, &sh->state);
3308 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3309 &sh_src->state))
3310 atomic_inc(&conf->preread_active_stripes);
3311 release_stripe(sh_src);
3312 goto finish;
3313 }
3314 if (sh_src)
3315 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003316
NeilBrown3687c062011-07-27 11:00:36 +10003317 sh->reconstruct_state = reconstruct_state_idle;
3318 clear_bit(STRIPE_EXPANDING, &sh->state);
3319 for (i = conf->raid_disks; i--; ) {
3320 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3321 set_bit(R5_LOCKED, &sh->dev[i].flags);
3322 s.locked++;
3323 }
3324 }
3325
3326 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3327 !sh->reconstruct_state) {
3328 /* Need to write out all blocks after computing parity */
3329 sh->disks = conf->raid_disks;
3330 stripe_set_idx(sh->sector, conf, 0, sh);
3331 schedule_reconstruction(sh, &s, 1, 1);
3332 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3333 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3334 atomic_dec(&conf->reshape_stripes);
3335 wake_up(&conf->wait_for_overlap);
3336 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3337 }
3338
3339 if (s.expanding && s.locked == 0 &&
3340 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3341 handle_stripe_expansion(conf, sh);
3342
3343finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003344 /* wait for this device to become unblocked */
NeilBrown43220aa2011-08-31 12:49:14 +10003345 if (conf->mddev->external && unlikely(s.blocked_rdev))
NeilBrownc5709ef2011-07-26 11:35:20 +10003346 md_wait_for_blocked_rdev(s.blocked_rdev, conf->mddev);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003347
NeilBrownbc2607f2011-07-28 11:39:22 +10003348 if (s.handle_bad_blocks)
3349 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003350 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003351 struct r5dev *dev = &sh->dev[i];
3352 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3353 /* We own a safe reference to the rdev */
3354 rdev = conf->disks[i].rdev;
3355 if (!rdev_set_badblocks(rdev, sh->sector,
3356 STRIPE_SECTORS, 0))
3357 md_error(conf->mddev, rdev);
3358 rdev_dec_pending(rdev, conf->mddev);
3359 }
NeilBrownb84db562011-07-28 11:39:23 +10003360 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3361 rdev = conf->disks[i].rdev;
3362 rdev_clear_badblocks(rdev, sh->sector,
3363 STRIPE_SECTORS);
3364 rdev_dec_pending(rdev, conf->mddev);
3365 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003366 }
3367
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003368 if (s.ops_request)
3369 raid_run_ops(sh, s.ops_request);
3370
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003371 ops_run_io(sh, &s);
3372
NeilBrownc5709ef2011-07-26 11:35:20 +10003373 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003374 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003375 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003376 * have actually been submitted.
3377 */
3378 atomic_dec(&conf->preread_active_stripes);
3379 if (atomic_read(&conf->preread_active_stripes) <
3380 IO_THRESHOLD)
3381 md_wakeup_thread(conf->mddev->thread);
3382 }
3383
NeilBrownc5709ef2011-07-26 11:35:20 +10003384 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003385
Dan Williams257a4b42011-11-08 16:22:06 +11003386 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003387}
3388
NeilBrownd1688a62011-10-11 16:49:52 +11003389static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390{
3391 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3392 while (!list_empty(&conf->delayed_list)) {
3393 struct list_head *l = conf->delayed_list.next;
3394 struct stripe_head *sh;
3395 sh = list_entry(l, struct stripe_head, lru);
3396 list_del_init(l);
3397 clear_bit(STRIPE_DELAYED, &sh->state);
3398 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3399 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003400 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 }
NeilBrown482c0832011-04-18 18:25:42 +10003402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403}
3404
NeilBrownd1688a62011-10-11 16:49:52 +11003405static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003406{
3407 /* device_lock is held */
3408 struct list_head head;
3409 list_add(&head, &conf->bitmap_list);
3410 list_del_init(&conf->bitmap_list);
3411 while (!list_empty(&head)) {
3412 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3413 list_del_init(&sh->lru);
3414 atomic_inc(&sh->count);
3415 __release_stripe(conf, sh);
3416 }
3417}
3418
NeilBrownfd01b882011-10-11 16:47:53 +11003419int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003420{
NeilBrownd1688a62011-10-11 16:49:52 +11003421 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003422
3423 /* No difference between reads and writes. Just check
3424 * how busy the stripe_cache is
3425 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003426
NeilBrownf022b2f2006-10-03 01:15:56 -07003427 if (conf->inactive_blocked)
3428 return 1;
3429 if (conf->quiesce)
3430 return 1;
3431 if (list_empty_careful(&conf->inactive_list))
3432 return 1;
3433
3434 return 0;
3435}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003436EXPORT_SYMBOL_GPL(md_raid5_congested);
3437
3438static int raid5_congested(void *data, int bits)
3439{
NeilBrownfd01b882011-10-11 16:47:53 +11003440 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003441
3442 return mddev_congested(mddev, bits) ||
3443 md_raid5_congested(mddev, bits);
3444}
NeilBrownf022b2f2006-10-03 01:15:56 -07003445
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003446/* We want read requests to align with chunks where possible,
3447 * but write requests don't need to.
3448 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003449static int raid5_mergeable_bvec(struct request_queue *q,
3450 struct bvec_merge_data *bvm,
3451 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003452{
NeilBrownfd01b882011-10-11 16:47:53 +11003453 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003454 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003455 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003456 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003457 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003458
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003459 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003460 return biovec->bv_len; /* always allow writes to be mergeable */
3461
Andre Noll664e7c42009-06-18 08:45:27 +10003462 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3463 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003464 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3465 if (max < 0) max = 0;
3466 if (max <= biovec->bv_len && bio_sectors == 0)
3467 return biovec->bv_len;
3468 else
3469 return max;
3470}
3471
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003472
NeilBrownfd01b882011-10-11 16:47:53 +11003473static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003474{
3475 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003476 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003477 unsigned int bio_sectors = bio->bi_size >> 9;
3478
Andre Noll664e7c42009-06-18 08:45:27 +10003479 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3480 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003481 return chunk_sectors >=
3482 ((sector & (chunk_sectors - 1)) + bio_sectors);
3483}
3484
3485/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003486 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3487 * later sampled by raid5d.
3488 */
NeilBrownd1688a62011-10-11 16:49:52 +11003489static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003490{
3491 unsigned long flags;
3492
3493 spin_lock_irqsave(&conf->device_lock, flags);
3494
3495 bi->bi_next = conf->retry_read_aligned_list;
3496 conf->retry_read_aligned_list = bi;
3497
3498 spin_unlock_irqrestore(&conf->device_lock, flags);
3499 md_wakeup_thread(conf->mddev->thread);
3500}
3501
3502
NeilBrownd1688a62011-10-11 16:49:52 +11003503static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003504{
3505 struct bio *bi;
3506
3507 bi = conf->retry_read_aligned;
3508 if (bi) {
3509 conf->retry_read_aligned = NULL;
3510 return bi;
3511 }
3512 bi = conf->retry_read_aligned_list;
3513 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003514 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003515 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003516 /*
3517 * this sets the active strip count to 1 and the processed
3518 * strip count to zero (upper 8 bits)
3519 */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003520 bi->bi_phys_segments = 1; /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003521 }
3522
3523 return bi;
3524}
3525
3526
3527/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003528 * The "raid5_align_endio" should check if the read succeeded and if it
3529 * did, call bio_endio on the original bio (having bio_put the new bio
3530 * first).
3531 * If the read failed..
3532 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003533static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003534{
3535 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003536 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003537 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003538 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003539 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003540
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003541 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003542
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003543 rdev = (void*)raid_bi->bi_next;
3544 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003545 mddev = rdev->mddev;
3546 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003547
3548 rdev_dec_pending(rdev, conf->mddev);
3549
3550 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003551 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003552 if (atomic_dec_and_test(&conf->active_aligned_reads))
3553 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003554 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003555 }
3556
3557
Dan Williams45b42332007-07-09 11:56:43 -07003558 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003559
3560 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003561}
3562
Neil Brown387bb172007-02-08 14:20:29 -08003563static int bio_fits_rdev(struct bio *bi)
3564{
Jens Axboe165125e2007-07-24 09:28:11 +02003565 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003566
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003567 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003568 return 0;
3569 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003570 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003571 return 0;
3572
3573 if (q->merge_bvec_fn)
3574 /* it's too hard to apply the merge_bvec_fn at this stage,
3575 * just just give up
3576 */
3577 return 0;
3578
3579 return 1;
3580}
3581
3582
NeilBrownfd01b882011-10-11 16:47:53 +11003583static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003584{
NeilBrownd1688a62011-10-11 16:49:52 +11003585 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11003586 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003587 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11003588 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003589
3590 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003591 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003592 return 0;
3593 }
3594 /*
NeilBrowna167f662010-10-26 18:31:13 +11003595 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003596 */
NeilBrowna167f662010-10-26 18:31:13 +11003597 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003598 if (!align_bi)
3599 return 0;
3600 /*
3601 * set bi_end_io to a new function, and set bi_private to the
3602 * original bio.
3603 */
3604 align_bi->bi_end_io = raid5_align_endio;
3605 align_bi->bi_private = raid_bio;
3606 /*
3607 * compute position
3608 */
NeilBrown112bf892009-03-31 14:39:38 +11003609 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3610 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003611 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003612
3613 rcu_read_lock();
3614 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3615 if (rdev && test_bit(In_sync, &rdev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003616 sector_t first_bad;
3617 int bad_sectors;
3618
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003619 atomic_inc(&rdev->nr_pending);
3620 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003621 raid_bio->bi_next = (void*)rdev;
3622 align_bi->bi_bdev = rdev->bdev;
3623 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
3624 align_bi->bi_sector += rdev->data_offset;
3625
NeilBrown31c176e2011-07-28 11:39:22 +10003626 if (!bio_fits_rdev(align_bi) ||
3627 is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
3628 &first_bad, &bad_sectors)) {
3629 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08003630 bio_put(align_bi);
3631 rdev_dec_pending(rdev, mddev);
3632 return 0;
3633 }
3634
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003635 spin_lock_irq(&conf->device_lock);
3636 wait_event_lock_irq(conf->wait_for_stripe,
3637 conf->quiesce == 0,
3638 conf->device_lock, /* nothing */);
3639 atomic_inc(&conf->active_aligned_reads);
3640 spin_unlock_irq(&conf->device_lock);
3641
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003642 generic_make_request(align_bi);
3643 return 1;
3644 } else {
3645 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003646 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003647 return 0;
3648 }
3649}
3650
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003651/* __get_priority_stripe - get the next stripe to process
3652 *
3653 * Full stripe writes are allowed to pass preread active stripes up until
3654 * the bypass_threshold is exceeded. In general the bypass_count
3655 * increments when the handle_list is handled before the hold_list; however, it
3656 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3657 * stripe with in flight i/o. The bypass_count will be reset when the
3658 * head of the hold_list has changed, i.e. the head was promoted to the
3659 * handle_list.
3660 */
NeilBrownd1688a62011-10-11 16:49:52 +11003661static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003662{
3663 struct stripe_head *sh;
3664
3665 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3666 __func__,
3667 list_empty(&conf->handle_list) ? "empty" : "busy",
3668 list_empty(&conf->hold_list) ? "empty" : "busy",
3669 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3670
3671 if (!list_empty(&conf->handle_list)) {
3672 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3673
3674 if (list_empty(&conf->hold_list))
3675 conf->bypass_count = 0;
3676 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3677 if (conf->hold_list.next == conf->last_hold)
3678 conf->bypass_count++;
3679 else {
3680 conf->last_hold = conf->hold_list.next;
3681 conf->bypass_count -= conf->bypass_threshold;
3682 if (conf->bypass_count < 0)
3683 conf->bypass_count = 0;
3684 }
3685 }
3686 } else if (!list_empty(&conf->hold_list) &&
3687 ((conf->bypass_threshold &&
3688 conf->bypass_count > conf->bypass_threshold) ||
3689 atomic_read(&conf->pending_full_writes) == 0)) {
3690 sh = list_entry(conf->hold_list.next,
3691 typeof(*sh), lru);
3692 conf->bypass_count -= conf->bypass_threshold;
3693 if (conf->bypass_count < 0)
3694 conf->bypass_count = 0;
3695 } else
3696 return NULL;
3697
3698 list_del_init(&sh->lru);
3699 atomic_inc(&sh->count);
3700 BUG_ON(atomic_read(&sh->count) != 1);
3701 return sh;
3702}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003703
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07003704static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705{
NeilBrownd1688a62011-10-11 16:49:52 +11003706 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11003707 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 sector_t new_sector;
3709 sector_t logical_sector, last_sector;
3710 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01003711 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11003712 int remaining;
NeilBrown7c13edc2011-04-18 18:25:43 +10003713 int plugged;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714
Tejun Heoe9c74692010-09-03 11:56:18 +02003715 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
3716 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02003717 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07003718 }
3719
NeilBrown3d310eb2005-06-21 17:17:26 -07003720 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07003721
NeilBrown802ba062006-12-13 00:34:13 -08003722 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003723 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11003724 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02003725 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003726
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
3728 last_sector = bi->bi_sector + (bi->bi_size>>9);
3729 bi->bi_next = NULL;
3730 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07003731
NeilBrown7c13edc2011-04-18 18:25:43 +10003732 plugged = mddev_check_plugged(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
3734 DEFINE_WAIT(w);
NeilBrown16a53ec2006-06-26 00:27:38 -07003735 int disks, data_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003736 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08003737
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003738 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11003739 previous = 0;
NeilBrownb0f9ec02009-03-31 15:27:18 +11003740 disks = conf->raid_disks;
NeilBrownb578d552006-03-27 01:18:12 -08003741 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11003742 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11003743 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08003744 * 64bit on a 32bit platform, and so it might be
3745 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02003746 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08003747 * the lock is dropped, so once we get a reference
3748 * to the stripe that we think it is, we will have
3749 * to check again.
3750 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003751 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11003752 if (mddev->delta_disks < 0
3753 ? logical_sector < conf->reshape_progress
3754 : logical_sector >= conf->reshape_progress) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003755 disks = conf->previous_raid_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003756 previous = 1;
3757 } else {
NeilBrownfef9c612009-03-31 15:16:46 +11003758 if (mddev->delta_disks < 0
3759 ? logical_sector < conf->reshape_safe
3760 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08003761 spin_unlock_irq(&conf->device_lock);
3762 schedule();
3763 goto retry;
3764 }
3765 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003766 spin_unlock_irq(&conf->device_lock);
3767 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003768 data_disks = disks - conf->max_degraded;
3769
NeilBrown112bf892009-03-31 14:39:38 +11003770 new_sector = raid5_compute_sector(conf, logical_sector,
3771 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003772 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10003773 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 (unsigned long long)new_sector,
3775 (unsigned long long)logical_sector);
3776
NeilBrownb5663ba2009-03-31 14:39:38 +11003777 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10003778 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11003780 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003781 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08003782 * stripe, so we must do the range check again.
3783 * Expansion could still move past after this
3784 * test, but as we are holding a reference to
3785 * 'sh', we know that if that happens,
3786 * STRIPE_EXPANDING will get set and the expansion
3787 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003788 */
3789 int must_retry = 0;
3790 spin_lock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11003791 if (mddev->delta_disks < 0
3792 ? logical_sector >= conf->reshape_progress
3793 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003794 /* mismatch, need to try again */
3795 must_retry = 1;
3796 spin_unlock_irq(&conf->device_lock);
3797 if (must_retry) {
3798 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07003799 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003800 goto retry;
3801 }
3802 }
NeilBrowne62e58a2009-07-01 13:15:35 +10003803
Namhyung Kimffd96e32011-07-18 17:38:51 +10003804 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10003805 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08003806 logical_sector < mddev->suspend_hi) {
3807 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10003808 /* As the suspend_* range is controlled by
3809 * userspace, we want an interruptible
3810 * wait.
3811 */
3812 flush_signals(current);
3813 prepare_to_wait(&conf->wait_for_overlap,
3814 &w, TASK_INTERRUPTIBLE);
3815 if (logical_sector >= mddev->suspend_lo &&
3816 logical_sector < mddev->suspend_hi)
3817 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08003818 goto retry;
3819 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003820
3821 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10003822 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003823 /* Stripe is busy expanding or
3824 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 * and wait a while
3826 */
NeilBrown482c0832011-04-18 18:25:42 +10003827 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 release_stripe(sh);
3829 schedule();
3830 goto retry;
3831 }
3832 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08003833 set_bit(STRIPE_HANDLE, &sh->state);
3834 clear_bit(STRIPE_DELAYED, &sh->state);
Tejun Heoe9c74692010-09-03 11:56:18 +02003835 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11003836 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3837 atomic_inc(&conf->preread_active_stripes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 } else {
3840 /* cannot get stripe for read-ahead, just give-up */
3841 clear_bit(BIO_UPTODATE, &bi->bi_flags);
3842 finish_wait(&conf->wait_for_overlap, &w);
3843 break;
3844 }
3845
3846 }
NeilBrown7c13edc2011-04-18 18:25:43 +10003847 if (!plugged)
3848 md_wakeup_thread(mddev->thread);
3849
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02003851 remaining = raid5_dec_bi_phys_segments(bi);
NeilBrownf6344752006-03-27 01:18:17 -08003852 spin_unlock_irq(&conf->device_lock);
3853 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854
NeilBrown16a53ec2006-06-26 00:27:38 -07003855 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02003857
Neil Brown0e13fe232008-06-28 08:31:20 +10003858 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860}
3861
NeilBrownfd01b882011-10-11 16:47:53 +11003862static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11003863
NeilBrownfd01b882011-10-11 16:47:53 +11003864static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865{
NeilBrown52c03292006-06-26 00:27:43 -07003866 /* reshaping is quite different to recovery/resync so it is
3867 * handled quite separately ... here.
3868 *
3869 * On each call to sync_request, we gather one chunk worth of
3870 * destination stripes and flag them as expanding.
3871 * Then we find all the source stripes and request reads.
3872 * As the reads complete, handle_stripe will copy the data
3873 * into the destination stripe and release that stripe.
3874 */
NeilBrownd1688a62011-10-11 16:49:52 +11003875 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08003877 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08003878 int raid_disks = conf->previous_raid_disks;
3879 int data_disks = raid_disks - conf->max_degraded;
3880 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07003881 int i;
3882 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11003883 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11003884 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11003885 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11003886 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07003887
NeilBrownfef9c612009-03-31 15:16:46 +11003888 if (sector_nr == 0) {
3889 /* If restarting in the middle, skip the initial sectors */
3890 if (mddev->delta_disks < 0 &&
3891 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
3892 sector_nr = raid5_size(mddev, 0, 0)
3893 - conf->reshape_progress;
NeilBrowna6397552009-08-13 10:13:00 +10003894 } else if (mddev->delta_disks >= 0 &&
NeilBrownfef9c612009-03-31 15:16:46 +11003895 conf->reshape_progress > 0)
3896 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08003897 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11003898 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11003899 mddev->curr_resync_completed = sector_nr;
3900 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11003901 *skipped = 1;
3902 return sector_nr;
3903 }
NeilBrown52c03292006-06-26 00:27:43 -07003904 }
3905
NeilBrown7a661382009-03-31 15:21:40 +11003906 /* We need to process a full chunk at a time.
3907 * If old and new chunk sizes differ, we need to process the
3908 * largest of these
3909 */
Andre Noll664e7c42009-06-18 08:45:27 +10003910 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
3911 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11003912 else
Andre Noll9d8f0362009-06-18 08:45:01 +10003913 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11003914
NeilBrown52c03292006-06-26 00:27:43 -07003915 /* we update the metadata when there is more than 3Meg
3916 * in the block range (that is rather arbitrary, should
3917 * probably be time based) or when the data about to be
3918 * copied would over-write the source of the data at
3919 * the front of the range.
NeilBrownfef9c612009-03-31 15:16:46 +11003920 * i.e. one new_stripe along from reshape_progress new_maps
3921 * to after where reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07003922 */
NeilBrownfef9c612009-03-31 15:16:46 +11003923 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08003924 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11003925 readpos = conf->reshape_progress;
3926 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11003927 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08003928 sector_div(safepos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11003929 if (mddev->delta_disks < 0) {
NeilBrowned37d832009-05-27 21:39:05 +10003930 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11003931 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11003932 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11003933 } else {
NeilBrown7a661382009-03-31 15:21:40 +11003934 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10003935 readpos -= min_t(sector_t, reshape_sectors, readpos);
3936 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11003937 }
NeilBrown52c03292006-06-26 00:27:43 -07003938
NeilBrownc8f517c2009-03-31 15:28:40 +11003939 /* 'writepos' is the most advanced device address we might write.
3940 * 'readpos' is the least advanced device address we might read.
3941 * 'safepos' is the least address recorded in the metadata as having
3942 * been reshaped.
3943 * If 'readpos' is behind 'writepos', then there is no way that we can
3944 * ensure safety in the face of a crash - that must be done by userspace
3945 * making a backup of the data. So in that case there is no particular
3946 * rush to update metadata.
3947 * Otherwise if 'safepos' is behind 'writepos', then we really need to
3948 * update the metadata to advance 'safepos' to match 'readpos' so that
3949 * we can be safe in the event of a crash.
3950 * So we insist on updating metadata if safepos is behind writepos and
3951 * readpos is beyond writepos.
3952 * In any case, update the metadata every 10 seconds.
3953 * Maybe that number should be configurable, but I'm not sure it is
3954 * worth it.... maybe it could be a multiple of safemode_delay???
3955 */
NeilBrownfef9c612009-03-31 15:16:46 +11003956 if ((mddev->delta_disks < 0
NeilBrownc8f517c2009-03-31 15:28:40 +11003957 ? (safepos > writepos && readpos < writepos)
3958 : (safepos < writepos && readpos > writepos)) ||
3959 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07003960 /* Cannot proceed until we've updated the superblock... */
3961 wait_event(conf->wait_for_overlap,
3962 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11003963 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11003964 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11003965 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07003966 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07003967 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07003968 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07003969 kthread_should_stop());
3970 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11003971 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07003972 spin_unlock_irq(&conf->device_lock);
3973 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10003974 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07003975 }
3976
NeilBrownec32a2b2009-03-31 15:17:38 +11003977 if (mddev->delta_disks < 0) {
3978 BUG_ON(conf->reshape_progress == 0);
3979 stripe_addr = writepos;
3980 BUG_ON((mddev->dev_sectors &
NeilBrown7a661382009-03-31 15:21:40 +11003981 ~((sector_t)reshape_sectors - 1))
3982 - reshape_sectors - stripe_addr
NeilBrownec32a2b2009-03-31 15:17:38 +11003983 != sector_nr);
3984 } else {
NeilBrown7a661382009-03-31 15:21:40 +11003985 BUG_ON(writepos != sector_nr + reshape_sectors);
NeilBrownec32a2b2009-03-31 15:17:38 +11003986 stripe_addr = sector_nr;
3987 }
NeilBrownab69ae12009-03-31 15:26:47 +11003988 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11003989 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07003990 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10003991 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10003992 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07003993 set_bit(STRIPE_EXPANDING, &sh->state);
3994 atomic_inc(&conf->reshape_stripes);
3995 /* If any of this stripe is beyond the end of the old
3996 * array, then we need to zero those blocks
3997 */
3998 for (j=sh->disks; j--;) {
3999 sector_t s;
4000 if (j == sh->pd_idx)
4001 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004002 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004003 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004004 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004005 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004006 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004007 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004008 continue;
4009 }
4010 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4011 set_bit(R5_Expanded, &sh->dev[j].flags);
4012 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4013 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004014 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004015 set_bit(STRIPE_EXPAND_READY, &sh->state);
4016 set_bit(STRIPE_HANDLE, &sh->state);
4017 }
NeilBrownab69ae12009-03-31 15:26:47 +11004018 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004019 }
4020 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004021 if (mddev->delta_disks < 0)
NeilBrown7a661382009-03-31 15:21:40 +11004022 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004023 else
NeilBrown7a661382009-03-31 15:21:40 +11004024 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004025 spin_unlock_irq(&conf->device_lock);
4026 /* Ok, those stripe are ready. We can start scheduling
4027 * reads on the source stripes.
4028 * The source stripes are determined by mapping the first and last
4029 * block on the destination stripes.
4030 */
NeilBrown52c03292006-06-26 00:27:43 -07004031 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004032 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004033 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004034 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004035 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004036 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004037 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004038 if (last_sector >= mddev->dev_sectors)
4039 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004040 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004041 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004042 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4043 set_bit(STRIPE_HANDLE, &sh->state);
4044 release_stripe(sh);
4045 first_sector += STRIPE_SECTORS;
4046 }
NeilBrownab69ae12009-03-31 15:26:47 +11004047 /* Now that the sources are clearly marked, we can release
4048 * the destination stripes
4049 */
4050 while (!list_empty(&stripes)) {
4051 sh = list_entry(stripes.next, struct stripe_head, lru);
4052 list_del_init(&sh->lru);
4053 release_stripe(sh);
4054 }
NeilBrownc6207272008-02-06 01:39:52 -08004055 /* If this takes us to the resync_max point where we have to pause,
4056 * then we need to write out the superblock.
4057 */
NeilBrown7a661382009-03-31 15:21:40 +11004058 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004059 if ((sector_nr - mddev->curr_resync_completed) * 2
4060 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004061 /* Cannot proceed until we've updated the superblock... */
4062 wait_event(conf->wait_for_overlap,
4063 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004064 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004065 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004066 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004067 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4068 md_wakeup_thread(mddev->thread);
4069 wait_event(mddev->sb_wait,
4070 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4071 || kthread_should_stop());
4072 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004073 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004074 spin_unlock_irq(&conf->device_lock);
4075 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004076 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004077 }
NeilBrown7a661382009-03-31 15:21:40 +11004078 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004079}
4080
4081/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004082static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004083{
NeilBrownd1688a62011-10-11 16:49:52 +11004084 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004085 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004086 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004087 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004088 int still_degraded = 0;
4089 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090
NeilBrown72626682005-09-09 16:23:54 -07004091 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004093
NeilBrown29269552006-03-27 01:18:10 -08004094 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4095 end_reshape(conf);
4096 return 0;
4097 }
NeilBrown72626682005-09-09 16:23:54 -07004098
4099 if (mddev->curr_resync < max_sector) /* aborted */
4100 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4101 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004102 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004103 conf->fullsync = 0;
4104 bitmap_close_sync(mddev->bitmap);
4105
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 return 0;
4107 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004108
NeilBrown64bd6602009-08-03 10:59:58 +10004109 /* Allow raid5_quiesce to complete */
4110 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4111
NeilBrown52c03292006-06-26 00:27:43 -07004112 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4113 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004114
NeilBrownc6207272008-02-06 01:39:52 -08004115 /* No need to check resync_max as we never do more than one
4116 * stripe, and as resync_max will always be on a chunk boundary,
4117 * if the check in md_do_sync didn't fire, there is no chance
4118 * of overstepping resync_max here
4119 */
4120
NeilBrown16a53ec2006-06-26 00:27:38 -07004121 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 * to resync, then assert that we are finished, because there is
4123 * nothing we can do.
4124 */
NeilBrown3285edf2006-06-26 00:27:55 -07004125 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004126 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004127 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004128 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 return rv;
4130 }
NeilBrown72626682005-09-09 16:23:54 -07004131 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004132 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004133 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4134 /* we can skip this block, and probably more */
4135 sync_blocks /= STRIPE_SECTORS;
4136 *skipped = 1;
4137 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139
NeilBrownb47490c2008-02-06 01:39:50 -08004140
4141 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4142
NeilBrowna8c906c2009-06-09 14:39:59 +10004143 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004145 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004147 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004149 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004151 /* Need to check if array will still be degraded after recovery/resync
4152 * We don't need to check the 'failed' flag as when that gets set,
4153 * recovery aborts.
4154 */
NeilBrownf001a702009-06-09 14:30:31 +10004155 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004156 if (conf->disks[i].rdev == NULL)
4157 still_degraded = 1;
4158
4159 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4160
NeilBrown83206d62011-07-26 11:19:49 +10004161 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162
NeilBrown14425772009-10-16 15:55:25 +11004163 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 release_stripe(sh);
4165
4166 return STRIPE_SECTORS;
4167}
4168
NeilBrownd1688a62011-10-11 16:49:52 +11004169static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004170{
4171 /* We may not be able to submit a whole bio at once as there
4172 * may not be enough stripe_heads available.
4173 * We cannot pre-allocate enough stripe_heads as we may need
4174 * more than exist in the cache (if we allow ever large chunks).
4175 * So we do one stripe head at a time and record in
4176 * ->bi_hw_segments how many have been done.
4177 *
4178 * We *know* that this entire raid_bio is in one chunk, so
4179 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4180 */
4181 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004182 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004183 sector_t sector, logical_sector, last_sector;
4184 int scnt = 0;
4185 int remaining;
4186 int handled = 0;
4187
4188 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004189 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004190 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004191 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4192
4193 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004194 logical_sector += STRIPE_SECTORS,
4195 sector += STRIPE_SECTORS,
4196 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004197
Jens Axboe960e7392008-08-15 10:41:18 +02004198 if (scnt < raid5_bi_hw_segments(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004199 /* already done this stripe */
4200 continue;
4201
NeilBrowna8c906c2009-06-09 14:39:59 +10004202 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004203
4204 if (!sh) {
4205 /* failed to get a stripe - must wait */
Jens Axboe960e7392008-08-15 10:41:18 +02004206 raid5_set_bi_hw_segments(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004207 conf->retry_read_aligned = raid_bio;
4208 return handled;
4209 }
4210
4211 set_bit(R5_ReadError, &sh->dev[dd_idx].flags);
Neil Brown387bb172007-02-08 14:20:29 -08004212 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4213 release_stripe(sh);
Jens Axboe960e7392008-08-15 10:41:18 +02004214 raid5_set_bi_hw_segments(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004215 conf->retry_read_aligned = raid_bio;
4216 return handled;
4217 }
4218
Dan Williams36d1c642009-07-14 11:48:22 -07004219 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004220 release_stripe(sh);
4221 handled++;
4222 }
4223 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004224 remaining = raid5_dec_bi_phys_segments(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004225 spin_unlock_irq(&conf->device_lock);
Neil Brown0e13fe232008-06-28 08:31:20 +10004226 if (remaining == 0)
4227 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004228 if (atomic_dec_and_test(&conf->active_aligned_reads))
4229 wake_up(&conf->wait_for_stripe);
4230 return handled;
4231}
4232
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004233
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234/*
4235 * This is our raid5 kernel thread.
4236 *
4237 * We scan the hash table for stripes which can be handled now.
4238 * During the scan, completed stripes are saved for us by the interrupt
4239 * handler, so that they will not have to wait for our next wakeup.
4240 */
NeilBrownfd01b882011-10-11 16:47:53 +11004241static void raid5d(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242{
4243 struct stripe_head *sh;
NeilBrownd1688a62011-10-11 16:49:52 +11004244 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004246 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247
Dan Williams45b42332007-07-09 11:56:43 -07004248 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249
4250 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004252 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 handled = 0;
4254 spin_lock_irq(&conf->device_lock);
4255 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004256 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257
NeilBrown7c13edc2011-04-18 18:25:43 +10004258 if (atomic_read(&mddev->plug_cnt) == 0 &&
4259 !list_empty(&conf->bitmap_list)) {
4260 /* Now is a good time to flush some bitmap updates */
4261 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004262 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004263 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004264 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004265 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004266 activate_bit_delay(conf);
4267 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004268 if (atomic_read(&mddev->plug_cnt) == 0)
4269 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004270
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004271 while ((bio = remove_bio_from_retry(conf))) {
4272 int ok;
4273 spin_unlock_irq(&conf->device_lock);
4274 ok = retry_aligned_read(conf, bio);
4275 spin_lock_irq(&conf->device_lock);
4276 if (!ok)
4277 break;
4278 handled++;
4279 }
4280
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004281 sh = __get_priority_stripe(conf);
4282
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004283 if (!sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 spin_unlock_irq(&conf->device_lock);
4286
4287 handled++;
Dan Williams417b8d42009-10-16 16:25:22 +11004288 handle_stripe(sh);
4289 release_stripe(sh);
4290 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291
NeilBrownde393cd2011-07-28 11:31:48 +10004292 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
4293 md_check_recovery(mddev);
4294
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 spin_lock_irq(&conf->device_lock);
4296 }
Dan Williams45b42332007-07-09 11:56:43 -07004297 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298
4299 spin_unlock_irq(&conf->device_lock);
4300
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004301 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004302 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303
Dan Williams45b42332007-07-09 11:56:43 -07004304 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305}
4306
NeilBrown3f294f42005-11-08 21:39:25 -08004307static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004308raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004309{
NeilBrownd1688a62011-10-11 16:49:52 +11004310 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004311 if (conf)
4312 return sprintf(page, "%d\n", conf->max_nr_stripes);
4313 else
4314 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004315}
4316
NeilBrownc41d4ac2010-06-01 19:37:24 +10004317int
NeilBrownfd01b882011-10-11 16:47:53 +11004318raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004319{
NeilBrownd1688a62011-10-11 16:49:52 +11004320 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004321 int err;
4322
4323 if (size <= 16 || size > 32768)
4324 return -EINVAL;
4325 while (size < conf->max_nr_stripes) {
4326 if (drop_one_stripe(conf))
4327 conf->max_nr_stripes--;
4328 else
4329 break;
4330 }
4331 err = md_allow_write(mddev);
4332 if (err)
4333 return err;
4334 while (size > conf->max_nr_stripes) {
4335 if (grow_one_stripe(conf))
4336 conf->max_nr_stripes++;
4337 else break;
4338 }
4339 return 0;
4340}
4341EXPORT_SYMBOL(raid5_set_cache_size);
4342
NeilBrown3f294f42005-11-08 21:39:25 -08004343static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004344raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004345{
NeilBrownd1688a62011-10-11 16:49:52 +11004346 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004347 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004348 int err;
4349
NeilBrown3f294f42005-11-08 21:39:25 -08004350 if (len >= PAGE_SIZE)
4351 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004352 if (!conf)
4353 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004354
Dan Williams4ef197d82008-04-28 02:15:54 -07004355 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004356 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004357 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004358 if (err)
4359 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004360 return len;
4361}
NeilBrown007583c2005-11-08 21:39:30 -08004362
NeilBrown96de1e62005-11-08 21:39:39 -08004363static struct md_sysfs_entry
4364raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4365 raid5_show_stripe_cache_size,
4366 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004367
4368static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004369raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004370{
NeilBrownd1688a62011-10-11 16:49:52 +11004371 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004372 if (conf)
4373 return sprintf(page, "%d\n", conf->bypass_threshold);
4374 else
4375 return 0;
4376}
4377
4378static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004379raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004380{
NeilBrownd1688a62011-10-11 16:49:52 +11004381 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004382 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004383 if (len >= PAGE_SIZE)
4384 return -EINVAL;
4385 if (!conf)
4386 return -ENODEV;
4387
Dan Williams4ef197d82008-04-28 02:15:54 -07004388 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004389 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004390 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004391 return -EINVAL;
4392 conf->bypass_threshold = new;
4393 return len;
4394}
4395
4396static struct md_sysfs_entry
4397raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4398 S_IRUGO | S_IWUSR,
4399 raid5_show_preread_threshold,
4400 raid5_store_preread_threshold);
4401
4402static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004403stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004404{
NeilBrownd1688a62011-10-11 16:49:52 +11004405 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004406 if (conf)
4407 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4408 else
4409 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004410}
4411
NeilBrown96de1e62005-11-08 21:39:39 -08004412static struct md_sysfs_entry
4413raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004414
NeilBrown007583c2005-11-08 21:39:30 -08004415static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004416 &raid5_stripecache_size.attr,
4417 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004418 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004419 NULL,
4420};
NeilBrown007583c2005-11-08 21:39:30 -08004421static struct attribute_group raid5_attrs_group = {
4422 .name = NULL,
4423 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004424};
4425
Dan Williams80c3a6c2009-03-17 18:10:40 -07004426static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11004427raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07004428{
NeilBrownd1688a62011-10-11 16:49:52 +11004429 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004430
4431 if (!sectors)
4432 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004433 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11004434 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11004435 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004436
Andre Noll9d8f0362009-06-18 08:45:01 +10004437 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004438 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004439 return sectors * (raid_disks - conf->max_degraded);
4440}
4441
NeilBrownd1688a62011-10-11 16:49:52 +11004442static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004443{
4444 struct raid5_percpu *percpu;
4445 unsigned long cpu;
4446
4447 if (!conf->percpu)
4448 return;
4449
4450 get_online_cpus();
4451 for_each_possible_cpu(cpu) {
4452 percpu = per_cpu_ptr(conf->percpu, cpu);
4453 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004454 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004455 }
4456#ifdef CONFIG_HOTPLUG_CPU
4457 unregister_cpu_notifier(&conf->cpu_notify);
4458#endif
4459 put_online_cpus();
4460
4461 free_percpu(conf->percpu);
4462}
4463
NeilBrownd1688a62011-10-11 16:49:52 +11004464static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10004465{
4466 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004467 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10004468 kfree(conf->disks);
4469 kfree(conf->stripe_hashtbl);
4470 kfree(conf);
4471}
4472
Dan Williams36d1c642009-07-14 11:48:22 -07004473#ifdef CONFIG_HOTPLUG_CPU
4474static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4475 void *hcpu)
4476{
NeilBrownd1688a62011-10-11 16:49:52 +11004477 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07004478 long cpu = (long)hcpu;
4479 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4480
4481 switch (action) {
4482 case CPU_UP_PREPARE:
4483 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07004484 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07004485 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004486 if (!percpu->scribble)
4487 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4488
4489 if (!percpu->scribble ||
4490 (conf->level == 6 && !percpu->spare_page)) {
4491 safe_put_page(percpu->spare_page);
4492 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004493 pr_err("%s: failed memory allocation for cpu%ld\n",
4494 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07004495 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07004496 }
4497 break;
4498 case CPU_DEAD:
4499 case CPU_DEAD_FROZEN:
4500 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004501 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004502 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004503 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07004504 break;
4505 default:
4506 break;
4507 }
4508 return NOTIFY_OK;
4509}
4510#endif
4511
NeilBrownd1688a62011-10-11 16:49:52 +11004512static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004513{
4514 unsigned long cpu;
4515 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09004516 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004517 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004518 int err;
4519
Dan Williams36d1c642009-07-14 11:48:22 -07004520 allcpus = alloc_percpu(struct raid5_percpu);
4521 if (!allcpus)
4522 return -ENOMEM;
4523 conf->percpu = allcpus;
4524
4525 get_online_cpus();
4526 err = 0;
4527 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07004528 if (conf->level == 6) {
4529 spare_page = alloc_page(GFP_KERNEL);
4530 if (!spare_page) {
4531 err = -ENOMEM;
4532 break;
4533 }
4534 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
4535 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11004536 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004537 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07004538 err = -ENOMEM;
4539 break;
4540 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07004541 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004542 }
4543#ifdef CONFIG_HOTPLUG_CPU
4544 conf->cpu_notify.notifier_call = raid456_cpu_notify;
4545 conf->cpu_notify.priority = 0;
4546 if (err == 0)
4547 err = register_cpu_notifier(&conf->cpu_notify);
4548#endif
4549 put_online_cpus();
4550
4551 return err;
4552}
4553
NeilBrownd1688a62011-10-11 16:49:52 +11004554static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555{
NeilBrownd1688a62011-10-11 16:49:52 +11004556 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004557 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11004558 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 struct disk_info *disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560
NeilBrown91adb562009-03-31 14:39:39 +11004561 if (mddev->new_level != 5
4562 && mddev->new_level != 4
4563 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10004564 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004565 mdname(mddev), mddev->new_level);
4566 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 }
NeilBrown91adb562009-03-31 14:39:39 +11004568 if ((mddev->new_level == 5
4569 && !algorithm_valid_raid5(mddev->new_layout)) ||
4570 (mddev->new_level == 6
4571 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004572 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11004573 mdname(mddev), mddev->new_layout);
4574 return ERR_PTR(-EIO);
4575 }
4576 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10004577 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004578 mdname(mddev), mddev->raid_disks);
4579 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11004580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581
Andre Noll664e7c42009-06-18 08:45:27 +10004582 if (!mddev->new_chunk_sectors ||
4583 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
4584 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10004585 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
4586 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11004587 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11004588 }
4589
NeilBrownd1688a62011-10-11 16:49:52 +11004590 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11004591 if (conf == NULL)
4592 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11004593 spin_lock_init(&conf->device_lock);
4594 init_waitqueue_head(&conf->wait_for_stripe);
4595 init_waitqueue_head(&conf->wait_for_overlap);
4596 INIT_LIST_HEAD(&conf->handle_list);
4597 INIT_LIST_HEAD(&conf->hold_list);
4598 INIT_LIST_HEAD(&conf->delayed_list);
4599 INIT_LIST_HEAD(&conf->bitmap_list);
4600 INIT_LIST_HEAD(&conf->inactive_list);
4601 atomic_set(&conf->active_stripes, 0);
4602 atomic_set(&conf->preread_active_stripes, 0);
4603 atomic_set(&conf->active_aligned_reads, 0);
4604 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11004605 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11004606
4607 conf->raid_disks = mddev->raid_disks;
4608 if (mddev->reshape_position == MaxSector)
4609 conf->previous_raid_disks = mddev->raid_disks;
4610 else
4611 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004612 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
4613 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11004614
NeilBrown5e5e3e72009-10-16 16:35:30 +11004615 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11004616 GFP_KERNEL);
4617 if (!conf->disks)
4618 goto abort;
4619
4620 conf->mddev = mddev;
4621
4622 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
4623 goto abort;
4624
Dan Williams36d1c642009-07-14 11:48:22 -07004625 conf->level = mddev->new_level;
4626 if (raid5_alloc_percpu(conf) != 0)
4627 goto abort;
4628
NeilBrown0c55e022010-05-03 14:09:02 +10004629 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11004630
4631 list_for_each_entry(rdev, &mddev->disks, same_set) {
4632 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004633 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11004634 || raid_disk < 0)
4635 continue;
4636 disk = conf->disks + raid_disk;
4637
4638 disk->rdev = rdev;
4639
4640 if (test_bit(In_sync, &rdev->flags)) {
4641 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10004642 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
4643 " disk %d\n",
4644 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05004645 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11004646 /* Cannot rely on bitmap to complete recovery */
4647 conf->fullsync = 1;
4648 }
4649
Andre Noll09c9e5f2009-06-18 08:45:55 +10004650 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11004651 conf->level = mddev->new_level;
4652 if (conf->level == 6)
4653 conf->max_degraded = 2;
4654 else
4655 conf->max_degraded = 1;
4656 conf->algorithm = mddev->new_layout;
4657 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11004658 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11004659 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10004660 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11004661 conf->prev_algo = mddev->layout;
4662 }
NeilBrown91adb562009-03-31 14:39:39 +11004663
4664 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11004665 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11004666 if (grow_stripes(conf, conf->max_nr_stripes)) {
4667 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004668 "md/raid:%s: couldn't allocate %dkB for buffers\n",
4669 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004670 goto abort;
4671 } else
NeilBrown0c55e022010-05-03 14:09:02 +10004672 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
4673 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004674
NeilBrown0da3c612009-09-23 18:09:45 +10004675 conf->thread = md_register_thread(raid5d, mddev, NULL);
NeilBrown91adb562009-03-31 14:39:39 +11004676 if (!conf->thread) {
4677 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004678 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11004679 mdname(mddev));
4680 goto abort;
4681 }
4682
4683 return conf;
4684
4685 abort:
4686 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10004687 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11004688 return ERR_PTR(-EIO);
4689 } else
4690 return ERR_PTR(-ENOMEM);
4691}
4692
NeilBrownc148ffd2009-11-13 17:47:00 +11004693
4694static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
4695{
4696 switch (algo) {
4697 case ALGORITHM_PARITY_0:
4698 if (raid_disk < max_degraded)
4699 return 1;
4700 break;
4701 case ALGORITHM_PARITY_N:
4702 if (raid_disk >= raid_disks - max_degraded)
4703 return 1;
4704 break;
4705 case ALGORITHM_PARITY_0_6:
4706 if (raid_disk == 0 ||
4707 raid_disk == raid_disks - 1)
4708 return 1;
4709 break;
4710 case ALGORITHM_LEFT_ASYMMETRIC_6:
4711 case ALGORITHM_RIGHT_ASYMMETRIC_6:
4712 case ALGORITHM_LEFT_SYMMETRIC_6:
4713 case ALGORITHM_RIGHT_SYMMETRIC_6:
4714 if (raid_disk == raid_disks - 1)
4715 return 1;
4716 }
4717 return 0;
4718}
4719
NeilBrownfd01b882011-10-11 16:47:53 +11004720static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11004721{
NeilBrownd1688a62011-10-11 16:49:52 +11004722 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10004723 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11004724 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11004725 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11004726 sector_t reshape_offset = 0;
NeilBrown91adb562009-03-31 14:39:39 +11004727
Andre Noll8c6ac862009-06-18 08:48:06 +10004728 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10004729 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10004730 " -- starting background reconstruction\n",
4731 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004732 if (mddev->reshape_position != MaxSector) {
4733 /* Check that we can continue the reshape.
4734 * Currently only disks can change, it must
4735 * increase, and we must be past the point where
4736 * a stripe over-writes itself
4737 */
4738 sector_t here_new, here_old;
4739 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11004740 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08004741
NeilBrown88ce4932009-03-31 15:24:23 +11004742 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10004743 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08004744 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08004745 mdname(mddev));
4746 return -EINVAL;
4747 }
NeilBrownf6705572006-03-27 01:18:11 -08004748 old_disks = mddev->raid_disks - mddev->delta_disks;
4749 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08004750 * further up in new geometry must map after here in old
4751 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08004752 */
4753 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10004754 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08004755 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004756 printk(KERN_ERR "md/raid:%s: reshape_position not "
4757 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004758 return -EINVAL;
4759 }
NeilBrownc148ffd2009-11-13 17:47:00 +11004760 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08004761 /* here_new is the stripe we will write to */
4762 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10004763 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08004764 (old_disks-max_degraded));
4765 /* here_old is the first stripe that we might need to read
4766 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10004767 if (mddev->delta_disks == 0) {
4768 /* We cannot be sure it is safe to start an in-place
4769 * reshape. It is only safe if user-space if monitoring
4770 * and taking constant backups.
4771 * mdadm always starts a situation like this in
4772 * readonly mode so it can take control before
4773 * allowing any writes. So just check for that.
4774 */
4775 if ((here_new * mddev->new_chunk_sectors !=
4776 here_old * mddev->chunk_sectors) ||
4777 mddev->ro == 0) {
NeilBrown0c55e022010-05-03 14:09:02 +10004778 printk(KERN_ERR "md/raid:%s: in-place reshape must be started"
4779 " in read-only mode - aborting\n",
4780 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10004781 return -EINVAL;
4782 }
4783 } else if (mddev->delta_disks < 0
4784 ? (here_new * mddev->new_chunk_sectors <=
4785 here_old * mddev->chunk_sectors)
4786 : (here_new * mddev->new_chunk_sectors >=
4787 here_old * mddev->chunk_sectors)) {
NeilBrownf6705572006-03-27 01:18:11 -08004788 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10004789 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
4790 "auto-recovery - aborting.\n",
4791 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004792 return -EINVAL;
4793 }
NeilBrown0c55e022010-05-03 14:09:02 +10004794 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
4795 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004796 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08004797 } else {
NeilBrown91adb562009-03-31 14:39:39 +11004798 BUG_ON(mddev->level != mddev->new_level);
4799 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10004800 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11004801 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08004802 }
4803
NeilBrown245f46c2009-03-31 14:39:39 +11004804 if (mddev->private == NULL)
4805 conf = setup_conf(mddev);
4806 else
4807 conf = mddev->private;
4808
NeilBrown91adb562009-03-31 14:39:39 +11004809 if (IS_ERR(conf))
4810 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08004811
NeilBrown91adb562009-03-31 14:39:39 +11004812 mddev->thread = conf->thread;
4813 conf->thread = NULL;
4814 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07004817 * 0 for a fully functional array, 1 or 2 for a degraded array.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 */
NeilBrownc148ffd2009-11-13 17:47:00 +11004819 list_for_each_entry(rdev, &mddev->disks, same_set) {
4820 if (rdev->raid_disk < 0)
4821 continue;
NeilBrown2f115882010-06-17 17:41:03 +10004822 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11004823 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10004824 continue;
4825 }
NeilBrownc148ffd2009-11-13 17:47:00 +11004826 /* This disc is not fully in-sync. However if it
4827 * just stored parity (beyond the recovery_offset),
4828 * when we don't need to be concerned about the
4829 * array being dirty.
4830 * When reshape goes 'backwards', we never have
4831 * partially completed devices, so we only need
4832 * to worry about reshape going forwards.
4833 */
4834 /* Hack because v0.91 doesn't store recovery_offset properly. */
4835 if (mddev->major_version == 0 &&
4836 mddev->minor_version > 90)
4837 rdev->recovery_offset = reshape_offset;
4838
NeilBrownc148ffd2009-11-13 17:47:00 +11004839 if (rdev->recovery_offset < reshape_offset) {
4840 /* We need to check old and new layout */
4841 if (!only_parity(rdev->raid_disk,
4842 conf->algorithm,
4843 conf->raid_disks,
4844 conf->max_degraded))
4845 continue;
4846 }
4847 if (!only_parity(rdev->raid_disk,
4848 conf->prev_algo,
4849 conf->previous_raid_disks,
4850 conf->max_degraded))
4851 continue;
4852 dirty_parity_disks++;
4853 }
NeilBrown91adb562009-03-31 14:39:39 +11004854
NeilBrown5e5e3e72009-10-16 16:35:30 +11004855 mddev->degraded = (max(conf->raid_disks, conf->previous_raid_disks)
4856 - working_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857
NeilBrown674806d2010-06-16 17:17:53 +10004858 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10004859 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07004861 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 goto abort;
4863 }
4864
NeilBrown91adb562009-03-31 14:39:39 +11004865 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004866 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11004867 mddev->resync_max_sectors = mddev->dev_sectors;
4868
NeilBrownc148ffd2009-11-13 17:47:00 +11004869 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004871 if (mddev->ok_start_degraded)
4872 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10004873 "md/raid:%s: starting dirty degraded array"
4874 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004875 mdname(mddev));
4876 else {
4877 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004878 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004879 mdname(mddev));
4880 goto abort;
4881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 }
4883
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10004885 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
4886 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11004887 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
4888 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 else
NeilBrown0c55e022010-05-03 14:09:02 +10004890 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
4891 " out of %d devices, algorithm %d\n",
4892 mdname(mddev), conf->level,
4893 mddev->raid_disks - mddev->degraded,
4894 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895
4896 print_raid5_conf(conf);
4897
NeilBrownfef9c612009-03-31 15:16:46 +11004898 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11004899 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08004900 atomic_set(&conf->reshape_stripes, 0);
4901 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4902 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4903 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4904 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4905 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10004906 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08004907 }
4908
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909
4910 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10004911 if (mddev->to_remove == &raid5_attrs_group)
4912 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10004913 else if (mddev->kobj.sd &&
4914 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08004915 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10004916 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08004917 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10004918 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
4919
4920 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10004921 int chunk_size;
NeilBrown4a5add42010-06-01 19:37:28 +10004922 /* read-ahead size must cover two whole stripes, which
4923 * is 2 * (datadisks) * chunksize where 'n' is the
4924 * number of raid devices
4925 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 int data_disks = conf->previous_raid_disks - conf->max_degraded;
4927 int stripe = data_disks *
4928 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
4929 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
4930 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10004931
4932 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10004933
4934 mddev->queue->backing_dev_info.congested_data = mddev;
4935 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10004936
4937 chunk_size = mddev->chunk_sectors << 9;
4938 blk_queue_io_min(mddev->queue, chunk_size);
4939 blk_queue_io_opt(mddev->queue, chunk_size *
4940 (conf->raid_disks - conf->max_degraded));
4941
4942 list_for_each_entry(rdev, &mddev->disks, same_set)
4943 disk_stack_limits(mddev->gendisk, rdev->bdev,
4944 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 }
4946
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 return 0;
4948abort:
NeilBrown01f96c02011-09-21 15:30:20 +10004949 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11004950 print_raid5_conf(conf);
4951 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10004953 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 return -EIO;
4955}
4956
NeilBrownfd01b882011-10-11 16:47:53 +11004957static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958{
NeilBrownd1688a62011-10-11 16:49:52 +11004959 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960
NeilBrown01f96c02011-09-21 15:30:20 +10004961 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10004962 if (mddev->queue)
4963 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10004964 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10004965 mddev->private = NULL;
4966 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 return 0;
4968}
4969
NeilBrownfd01b882011-10-11 16:47:53 +11004970static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971{
NeilBrownd1688a62011-10-11 16:49:52 +11004972 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 int i;
4974
Andre Noll9d8f0362009-06-18 08:45:01 +10004975 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
4976 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07004977 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 for (i = 0; i < conf->raid_disks; i++)
4979 seq_printf (seq, "%s",
4980 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08004981 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983}
4984
NeilBrownd1688a62011-10-11 16:49:52 +11004985static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986{
4987 int i;
4988 struct disk_info *tmp;
4989
NeilBrown0c55e022010-05-03 14:09:02 +10004990 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 if (!conf) {
4992 printk("(conf==NULL)\n");
4993 return;
4994 }
NeilBrown0c55e022010-05-03 14:09:02 +10004995 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
4996 conf->raid_disks,
4997 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998
4999 for (i = 0; i < conf->raid_disks; i++) {
5000 char b[BDEVNAME_SIZE];
5001 tmp = conf->disks + i;
5002 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005003 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5004 i, !test_bit(Faulty, &tmp->rdev->flags),
5005 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 }
5007}
5008
NeilBrownfd01b882011-10-11 16:47:53 +11005009static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010{
5011 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005012 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005014 int count = 0;
5015 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016
5017 for (i = 0; i < conf->raid_disks; i++) {
5018 tmp = conf->disks + i;
5019 if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005020 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005021 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005022 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005023 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005024 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 }
5026 }
NeilBrown6b965622010-08-18 11:56:59 +10005027 spin_lock_irqsave(&conf->device_lock, flags);
5028 mddev->degraded -= count;
5029 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005031 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032}
5033
NeilBrownfd01b882011-10-11 16:47:53 +11005034static int raid5_remove_disk(struct mddev *mddev, int number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035{
NeilBrownd1688a62011-10-11 16:49:52 +11005036 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 int err = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11005038 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039 struct disk_info *p = conf->disks + number;
5040
5041 print_raid5_conf(conf);
5042 rdev = p->rdev;
5043 if (rdev) {
NeilBrownec32a2b2009-03-31 15:17:38 +11005044 if (number >= conf->raid_disks &&
5045 conf->reshape_progress == MaxSector)
5046 clear_bit(In_sync, &rdev->flags);
5047
NeilBrownb2d444d2005-11-08 21:39:31 -08005048 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049 atomic_read(&rdev->nr_pending)) {
5050 err = -EBUSY;
5051 goto abort;
5052 }
NeilBrowndfc70642008-05-23 13:04:39 -07005053 /* Only remove non-faulty devices if recovery
5054 * isn't possible.
5055 */
5056 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7f0da592011-07-28 11:39:22 +10005057 mddev->recovery_disabled != conf->recovery_disabled &&
NeilBrown674806d2010-06-16 17:17:53 +10005058 !has_failed(conf) &&
NeilBrownec32a2b2009-03-31 15:17:38 +11005059 number < conf->raid_disks) {
NeilBrowndfc70642008-05-23 13:04:39 -07005060 err = -EBUSY;
5061 goto abort;
5062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07005064 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065 if (atomic_read(&rdev->nr_pending)) {
5066 /* lost the race, try later */
5067 err = -EBUSY;
5068 p->rdev = rdev;
5069 }
5070 }
5071abort:
5072
5073 print_raid5_conf(conf);
5074 return err;
5075}
5076
NeilBrownfd01b882011-10-11 16:47:53 +11005077static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078{
NeilBrownd1688a62011-10-11 16:49:52 +11005079 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005080 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 int disk;
5082 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005083 int first = 0;
5084 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085
NeilBrown7f0da592011-07-28 11:39:22 +10005086 if (mddev->recovery_disabled == conf->recovery_disabled)
5087 return -EBUSY;
5088
NeilBrown674806d2010-06-16 17:17:53 +10005089 if (has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005091 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092
Neil Brown6c2fce22008-06-28 08:31:31 +10005093 if (rdev->raid_disk >= 0)
5094 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095
5096 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005097 * find the disk ... but prefer rdev->saved_raid_disk
5098 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005100 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005101 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005102 conf->disks[rdev->saved_raid_disk].rdev == NULL)
5103 disk = rdev->saved_raid_disk;
5104 else
Neil Brown6c2fce22008-06-28 08:31:31 +10005105 disk = first;
5106 for ( ; disk <= last ; disk++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 if ((p=conf->disks + disk)->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005108 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005110 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005111 if (rdev->saved_raid_disk != disk)
5112 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005113 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 break;
5115 }
5116 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005117 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118}
5119
NeilBrownfd01b882011-10-11 16:47:53 +11005120static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121{
5122 /* no resync is happening, and there is enough space
5123 * on all devices, so we can resize.
5124 * We need to make sure resync covers any new space.
5125 * If the array is shrinking we should possibly wait until
5126 * any io in the removed space completes, but it hardly seems
5127 * worth it.
5128 */
Andre Noll9d8f0362009-06-18 08:45:01 +10005129 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Dan Williams1f403622009-03-31 14:59:03 +11005130 md_set_array_sectors(mddev, raid5_size(mddev, sectors,
5131 mddev->raid_disks));
Dan Williamsb522adc2009-03-31 15:00:31 +11005132 if (mddev->array_sectors >
5133 raid5_size(mddev, sectors, mddev->raid_disks))
5134 return -EINVAL;
Andre Nollf233ea52008-07-21 17:05:22 +10005135 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005136 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005137 if (sectors > mddev->dev_sectors &&
5138 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005139 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5141 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005142 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005143 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 return 0;
5145}
5146
NeilBrownfd01b882011-10-11 16:47:53 +11005147static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005148{
5149 /* Can only proceed if there are plenty of stripe_heads.
5150 * We need a minimum of one full stripe,, and for sensible progress
5151 * it is best to have about 4 times that.
5152 * If we require 4 times, then the default 256 4K stripe_heads will
5153 * allow for chunk sizes up to 256K, which is probably OK.
5154 * If the chunk size is greater, user-space should request more
5155 * stripe_heads first.
5156 */
NeilBrownd1688a62011-10-11 16:49:52 +11005157 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005158 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5159 > conf->max_nr_stripes ||
5160 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5161 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005162 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5163 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005164 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5165 / STRIPE_SIZE)*4);
5166 return 0;
5167 }
5168 return 1;
5169}
5170
NeilBrownfd01b882011-10-11 16:47:53 +11005171static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005172{
NeilBrownd1688a62011-10-11 16:49:52 +11005173 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005174
NeilBrown88ce4932009-03-31 15:24:23 +11005175 if (mddev->delta_disks == 0 &&
5176 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005177 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005178 return 0; /* nothing to do */
NeilBrowndba034e2008-08-05 15:54:13 +10005179 if (mddev->bitmap)
5180 /* Cannot grow a bitmap yet */
5181 return -EBUSY;
NeilBrown674806d2010-06-16 17:17:53 +10005182 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005183 return -EINVAL;
5184 if (mddev->delta_disks < 0) {
5185 /* We might be able to shrink, but the devices must
5186 * be made bigger first.
5187 * For raid6, 4 is the minimum size.
5188 * Otherwise 2 is the minimum
5189 */
5190 int min = 2;
5191 if (mddev->level == 6)
5192 min = 4;
5193 if (mddev->raid_disks + mddev->delta_disks < min)
5194 return -EINVAL;
5195 }
NeilBrown29269552006-03-27 01:18:10 -08005196
NeilBrown01ee22b2009-06-18 08:47:20 +10005197 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005198 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005199
NeilBrownec32a2b2009-03-31 15:17:38 +11005200 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
NeilBrown63c70c42006-03-27 01:18:13 -08005201}
5202
NeilBrownfd01b882011-10-11 16:47:53 +11005203static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005204{
NeilBrownd1688a62011-10-11 16:49:52 +11005205 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005206 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005207 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005208 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005209
NeilBrownf4168852007-02-28 20:11:53 -08005210 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005211 return -EBUSY;
5212
NeilBrown01ee22b2009-06-18 08:47:20 +10005213 if (!check_stripe_cache(mddev))
5214 return -ENOSPC;
5215
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005216 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown469518a2011-01-31 11:57:43 +11005217 if (!test_bit(In_sync, &rdev->flags)
5218 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005219 spares++;
NeilBrown63c70c42006-03-27 01:18:13 -08005220
NeilBrownf4168852007-02-28 20:11:53 -08005221 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005222 /* Not enough devices even to make a degraded array
5223 * of that size
5224 */
5225 return -EINVAL;
5226
NeilBrownec32a2b2009-03-31 15:17:38 +11005227 /* Refuse to reduce size of the array. Any reductions in
5228 * array size must be through explicit setting of array_size
5229 * attribute.
5230 */
5231 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5232 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10005233 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11005234 "before number of disks\n", mdname(mddev));
5235 return -EINVAL;
5236 }
5237
NeilBrownf6705572006-03-27 01:18:11 -08005238 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005239 spin_lock_irq(&conf->device_lock);
5240 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005241 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005242 conf->prev_chunk_sectors = conf->chunk_sectors;
5243 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005244 conf->prev_algo = conf->algorithm;
5245 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11005246 if (mddev->delta_disks < 0)
5247 conf->reshape_progress = raid5_size(mddev, 0, 0);
5248 else
5249 conf->reshape_progress = 0;
5250 conf->reshape_safe = conf->reshape_progress;
NeilBrown86b42c72009-03-31 15:19:03 +11005251 conf->generation++;
NeilBrown29269552006-03-27 01:18:10 -08005252 spin_unlock_irq(&conf->device_lock);
5253
5254 /* Add some new drives, as many as will fit.
5255 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10005256 * Don't add devices if we are reducing the number of
5257 * devices in the array. This is because it is not possible
5258 * to correctly record the "partially reconstructed" state of
5259 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08005260 */
NeilBrown87a8dec2011-01-31 11:57:43 +11005261 if (mddev->delta_disks >= 0) {
5262 int added_devices = 0;
5263 list_for_each_entry(rdev, &mddev->disks, same_set)
5264 if (rdev->raid_disk < 0 &&
5265 !test_bit(Faulty, &rdev->flags)) {
5266 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11005267 if (rdev->raid_disk
5268 >= conf->previous_raid_disks) {
5269 set_bit(In_sync, &rdev->flags);
5270 added_devices++;
5271 } else
5272 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10005273
5274 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11005275 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11005276 }
NeilBrown87a8dec2011-01-31 11:57:43 +11005277 } else if (rdev->raid_disk >= conf->previous_raid_disks
5278 && !test_bit(Faulty, &rdev->flags)) {
5279 /* This is a spare that was manually added */
5280 set_bit(In_sync, &rdev->flags);
5281 added_devices++;
5282 }
NeilBrown29269552006-03-27 01:18:10 -08005283
NeilBrown87a8dec2011-01-31 11:57:43 +11005284 /* When a reshape changes the number of devices,
5285 * ->degraded is measured against the larger of the
5286 * pre and post number of devices.
5287 */
NeilBrownec32a2b2009-03-31 15:17:38 +11005288 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown9eb07c22010-02-09 12:31:47 +11005289 mddev->degraded += (conf->raid_disks - conf->previous_raid_disks)
NeilBrownec32a2b2009-03-31 15:17:38 +11005290 - added_devices;
5291 spin_unlock_irqrestore(&conf->device_lock, flags);
5292 }
NeilBrown63c70c42006-03-27 01:18:13 -08005293 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10005294 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07005295 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005296
NeilBrown29269552006-03-27 01:18:10 -08005297 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5298 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5299 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5300 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5301 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005302 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08005303 if (!mddev->sync_thread) {
5304 mddev->recovery = 0;
5305 spin_lock_irq(&conf->device_lock);
5306 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005307 conf->reshape_progress = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005308 spin_unlock_irq(&conf->device_lock);
5309 return -EAGAIN;
5310 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005311 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005312 md_wakeup_thread(mddev->sync_thread);
5313 md_new_event(mddev);
5314 return 0;
5315}
NeilBrown29269552006-03-27 01:18:10 -08005316
NeilBrownec32a2b2009-03-31 15:17:38 +11005317/* This is called from the reshape thread and should make any
5318 * changes needed in 'conf'
5319 */
NeilBrownd1688a62011-10-11 16:49:52 +11005320static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08005321{
NeilBrown29269552006-03-27 01:18:10 -08005322
NeilBrownf6705572006-03-27 01:18:11 -08005323 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
Dan Williams80c3a6c2009-03-17 18:10:40 -07005324
NeilBrownf6705572006-03-27 01:18:11 -08005325 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11005326 conf->previous_raid_disks = conf->raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005327 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08005328 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005329 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07005330
5331 /* read-ahead size must cover two whole stripes, which is
5332 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5333 */
NeilBrown4a5add42010-06-01 19:37:28 +10005334 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11005335 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005336 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11005337 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07005338 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5339 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5340 }
NeilBrown29269552006-03-27 01:18:10 -08005341 }
NeilBrown29269552006-03-27 01:18:10 -08005342}
5343
NeilBrownec32a2b2009-03-31 15:17:38 +11005344/* This is called from the raid5d thread with mddev_lock held.
5345 * It makes config changes to the device.
5346 */
NeilBrownfd01b882011-10-11 16:47:53 +11005347static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11005348{
NeilBrownd1688a62011-10-11 16:49:52 +11005349 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11005350
5351 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5352
NeilBrownec32a2b2009-03-31 15:17:38 +11005353 if (mddev->delta_disks > 0) {
5354 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5355 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005356 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11005357 } else {
5358 int d;
NeilBrownec32a2b2009-03-31 15:17:38 +11005359 mddev->degraded = conf->raid_disks;
5360 for (d = 0; d < conf->raid_disks ; d++)
5361 if (conf->disks[d].rdev &&
5362 test_bit(In_sync,
5363 &conf->disks[d].rdev->flags))
5364 mddev->degraded--;
5365 for (d = conf->raid_disks ;
5366 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10005367 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11005368 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrown1a67dde2009-08-13 10:41:49 +10005369 if (rdev && raid5_remove_disk(mddev, d) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10005370 sysfs_unlink_rdev(mddev, rdev);
NeilBrown1a67dde2009-08-13 10:41:49 +10005371 rdev->raid_disk = -1;
5372 }
5373 }
NeilBrowncea9c222009-03-31 15:15:05 +11005374 }
NeilBrown88ce4932009-03-31 15:24:23 +11005375 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005376 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11005377 mddev->reshape_position = MaxSector;
5378 mddev->delta_disks = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11005379 }
5380}
5381
NeilBrownfd01b882011-10-11 16:47:53 +11005382static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07005383{
NeilBrownd1688a62011-10-11 16:49:52 +11005384 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07005385
5386 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08005387 case 2: /* resume for a suspend */
5388 wake_up(&conf->wait_for_overlap);
5389 break;
5390
NeilBrown72626682005-09-09 16:23:54 -07005391 case 1: /* stop all writes */
5392 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005393 /* '2' tells resync/reshape to pause so that all
5394 * active stripes can drain
5395 */
5396 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07005397 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005398 atomic_read(&conf->active_stripes) == 0 &&
5399 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07005400 conf->device_lock, /* nothing */);
NeilBrown64bd6602009-08-03 10:59:58 +10005401 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07005402 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005403 /* allow reshape to continue */
5404 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005405 break;
5406
5407 case 0: /* re-enable writes */
5408 spin_lock_irq(&conf->device_lock);
5409 conf->quiesce = 0;
5410 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08005411 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005412 spin_unlock_irq(&conf->device_lock);
5413 break;
5414 }
NeilBrown72626682005-09-09 16:23:54 -07005415}
NeilBrownb15c2e52006-01-06 00:20:16 -08005416
NeilBrownd562b0c2009-03-31 14:39:39 +11005417
NeilBrownfd01b882011-10-11 16:47:53 +11005418static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11005419{
NeilBrowne373ab12011-10-11 16:48:59 +11005420 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07005421 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11005422
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005423 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11005424 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10005425 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
5426 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005427 return ERR_PTR(-EINVAL);
5428 }
5429
NeilBrowne373ab12011-10-11 16:48:59 +11005430 sectors = raid0_conf->strip_zone[0].zone_end;
5431 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10005432 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005433 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11005434 mddev->new_layout = ALGORITHM_PARITY_N;
5435 mddev->new_chunk_sectors = mddev->chunk_sectors;
5436 mddev->raid_disks += 1;
5437 mddev->delta_disks = 1;
5438 /* make sure it will be not marked as dirty */
5439 mddev->recovery_cp = MaxSector;
5440
5441 return setup_conf(mddev);
5442}
5443
5444
NeilBrownfd01b882011-10-11 16:47:53 +11005445static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005446{
5447 int chunksect;
5448
5449 if (mddev->raid_disks != 2 ||
5450 mddev->degraded > 1)
5451 return ERR_PTR(-EINVAL);
5452
5453 /* Should check if there are write-behind devices? */
5454
5455 chunksect = 64*2; /* 64K by default */
5456
5457 /* The array must be an exact multiple of chunksize */
5458 while (chunksect && (mddev->array_sectors & (chunksect-1)))
5459 chunksect >>= 1;
5460
5461 if ((chunksect<<9) < STRIPE_SIZE)
5462 /* array size does not allow a suitable chunk size */
5463 return ERR_PTR(-EINVAL);
5464
5465 mddev->new_level = 5;
5466 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10005467 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11005468
5469 return setup_conf(mddev);
5470}
5471
NeilBrownfd01b882011-10-11 16:47:53 +11005472static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11005473{
5474 int new_layout;
5475
5476 switch (mddev->layout) {
5477 case ALGORITHM_LEFT_ASYMMETRIC_6:
5478 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
5479 break;
5480 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5481 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
5482 break;
5483 case ALGORITHM_LEFT_SYMMETRIC_6:
5484 new_layout = ALGORITHM_LEFT_SYMMETRIC;
5485 break;
5486 case ALGORITHM_RIGHT_SYMMETRIC_6:
5487 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
5488 break;
5489 case ALGORITHM_PARITY_0_6:
5490 new_layout = ALGORITHM_PARITY_0;
5491 break;
5492 case ALGORITHM_PARITY_N:
5493 new_layout = ALGORITHM_PARITY_N;
5494 break;
5495 default:
5496 return ERR_PTR(-EINVAL);
5497 }
5498 mddev->new_level = 5;
5499 mddev->new_layout = new_layout;
5500 mddev->delta_disks = -1;
5501 mddev->raid_disks -= 1;
5502 return setup_conf(mddev);
5503}
5504
NeilBrownd562b0c2009-03-31 14:39:39 +11005505
NeilBrownfd01b882011-10-11 16:47:53 +11005506static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11005507{
NeilBrown88ce4932009-03-31 15:24:23 +11005508 /* For a 2-drive array, the layout and chunk size can be changed
5509 * immediately as not restriping is needed.
5510 * For larger arrays we record the new value - after validation
5511 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11005512 */
NeilBrownd1688a62011-10-11 16:49:52 +11005513 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10005514 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11005515
NeilBrown597a7112009-06-18 08:47:42 +10005516 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11005517 return -EINVAL;
5518 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005519 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11005520 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005521 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11005522 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005523 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11005524 /* not factor of array size */
5525 return -EINVAL;
5526 }
5527
5528 /* They look valid */
5529
NeilBrown88ce4932009-03-31 15:24:23 +11005530 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10005531 /* can make the change immediately */
5532 if (mddev->new_layout >= 0) {
5533 conf->algorithm = mddev->new_layout;
5534 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11005535 }
5536 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10005537 conf->chunk_sectors = new_chunk ;
5538 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11005539 }
5540 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5541 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11005542 }
NeilBrown50ac1682009-06-18 08:47:55 +10005543 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11005544}
5545
NeilBrownfd01b882011-10-11 16:47:53 +11005546static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11005547{
NeilBrown597a7112009-06-18 08:47:42 +10005548 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10005549
NeilBrown597a7112009-06-18 08:47:42 +10005550 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11005551 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005552 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005553 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11005554 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005555 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11005556 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005557 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11005558 /* not factor of array size */
5559 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005560 }
NeilBrown88ce4932009-03-31 15:24:23 +11005561
5562 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10005563 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11005564}
5565
NeilBrownfd01b882011-10-11 16:47:53 +11005566static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005567{
5568 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005569 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005570 * raid1 - if there are two drives. We need to know the chunk size
5571 * raid4 - trivial - just use a raid4 layout.
5572 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005573 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005574 if (mddev->level == 0)
5575 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11005576 if (mddev->level == 1)
5577 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11005578 if (mddev->level == 4) {
5579 mddev->new_layout = ALGORITHM_PARITY_N;
5580 mddev->new_level = 5;
5581 return setup_conf(mddev);
5582 }
NeilBrownfc9739c2009-03-31 14:57:20 +11005583 if (mddev->level == 6)
5584 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11005585
5586 return ERR_PTR(-EINVAL);
5587}
5588
NeilBrownfd01b882011-10-11 16:47:53 +11005589static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11005590{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005591 /* raid4 can take over:
5592 * raid0 - if there is only one strip zone
5593 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11005594 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005595 if (mddev->level == 0)
5596 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11005597 if (mddev->level == 5 &&
5598 mddev->layout == ALGORITHM_PARITY_N) {
5599 mddev->new_layout = 0;
5600 mddev->new_level = 4;
5601 return setup_conf(mddev);
5602 }
5603 return ERR_PTR(-EINVAL);
5604}
NeilBrownd562b0c2009-03-31 14:39:39 +11005605
NeilBrown84fc4b52011-10-11 16:49:58 +11005606static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11005607
NeilBrownfd01b882011-10-11 16:47:53 +11005608static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11005609{
5610 /* Currently can only take over a raid5. We map the
5611 * personality to an equivalent raid6 personality
5612 * with the Q block at the end.
5613 */
5614 int new_layout;
5615
5616 if (mddev->pers != &raid5_personality)
5617 return ERR_PTR(-EINVAL);
5618 if (mddev->degraded > 1)
5619 return ERR_PTR(-EINVAL);
5620 if (mddev->raid_disks > 253)
5621 return ERR_PTR(-EINVAL);
5622 if (mddev->raid_disks < 3)
5623 return ERR_PTR(-EINVAL);
5624
5625 switch (mddev->layout) {
5626 case ALGORITHM_LEFT_ASYMMETRIC:
5627 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
5628 break;
5629 case ALGORITHM_RIGHT_ASYMMETRIC:
5630 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
5631 break;
5632 case ALGORITHM_LEFT_SYMMETRIC:
5633 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
5634 break;
5635 case ALGORITHM_RIGHT_SYMMETRIC:
5636 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
5637 break;
5638 case ALGORITHM_PARITY_0:
5639 new_layout = ALGORITHM_PARITY_0_6;
5640 break;
5641 case ALGORITHM_PARITY_N:
5642 new_layout = ALGORITHM_PARITY_N;
5643 break;
5644 default:
5645 return ERR_PTR(-EINVAL);
5646 }
5647 mddev->new_level = 6;
5648 mddev->new_layout = new_layout;
5649 mddev->delta_disks = 1;
5650 mddev->raid_disks += 1;
5651 return setup_conf(mddev);
5652}
5653
5654
NeilBrown84fc4b52011-10-11 16:49:58 +11005655static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07005656{
5657 .name = "raid6",
5658 .level = 6,
5659 .owner = THIS_MODULE,
5660 .make_request = make_request,
5661 .run = run,
5662 .stop = stop,
5663 .status = status,
5664 .error_handler = error,
5665 .hot_add_disk = raid5_add_disk,
5666 .hot_remove_disk= raid5_remove_disk,
5667 .spare_active = raid5_spare_active,
5668 .sync_request = sync_request,
5669 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005670 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10005671 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08005672 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005673 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07005674 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11005675 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07005676};
NeilBrown84fc4b52011-10-11 16:49:58 +11005677static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678{
5679 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08005680 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681 .owner = THIS_MODULE,
5682 .make_request = make_request,
5683 .run = run,
5684 .stop = stop,
5685 .status = status,
5686 .error_handler = error,
5687 .hot_add_disk = raid5_add_disk,
5688 .hot_remove_disk= raid5_remove_disk,
5689 .spare_active = raid5_spare_active,
5690 .sync_request = sync_request,
5691 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005692 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08005693 .check_reshape = raid5_check_reshape,
5694 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005695 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07005696 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11005697 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698};
5699
NeilBrown84fc4b52011-10-11 16:49:58 +11005700static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701{
NeilBrown2604b702006-01-06 00:20:36 -08005702 .name = "raid4",
5703 .level = 4,
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,
NeilBrown3d378902007-03-26 21:32:13 -08005716 .check_reshape = raid5_check_reshape,
5717 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005718 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08005719 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11005720 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08005721};
5722
5723static int __init raid5_init(void)
5724{
NeilBrown16a53ec2006-06-26 00:27:38 -07005725 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08005726 register_md_personality(&raid5_personality);
5727 register_md_personality(&raid4_personality);
5728 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729}
5730
NeilBrown2604b702006-01-06 00:20:36 -08005731static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732{
NeilBrown16a53ec2006-06-26 00:27:38 -07005733 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08005734 unregister_md_personality(&raid5_personality);
5735 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736}
5737
5738module_init(raid5_init);
5739module_exit(raid5_exit);
5740MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11005741MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08005743MODULE_ALIAS("md-raid5");
5744MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08005745MODULE_ALIAS("md-level-5");
5746MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07005747MODULE_ALIAS("md-personality-8"); /* RAID6 */
5748MODULE_ALIAS("md-raid6");
5749MODULE_ALIAS("md-level-6");
5750
5751/* This used to be two separate modules, they were: */
5752MODULE_ALIAS("raid5");
5753MODULE_ALIAS("raid6");