blob: 6b9fc58e8f2de4cdea46b2441fc850d9ea76203e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * raid5.c : Multiple Devices driver for Linux
3 * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
4 * Copyright (C) 1999, 2000 Ingo Molnar
NeilBrown16a53ec2006-06-26 00:27:38 -07005 * Copyright (C) 2002, 2003 H. Peter Anvin
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
NeilBrown16a53ec2006-06-26 00:27:38 -07007 * RAID-4/5/6 management functions.
8 * Thanks to Penguin Computing for making the RAID-6 development possible
9 * by donating a test server!
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
NeilBrownae3c20c2006-07-10 04:44:17 -070021/*
22 * BITMAP UNPLUGGING:
23 *
24 * The sequencing for updating the bitmap reliably is a little
25 * subtle (and I got it wrong the first time) so it deserves some
26 * explanation.
27 *
28 * We group bitmap updates into batches. Each batch has a number.
29 * We may write out several batches at once, but that isn't very important.
NeilBrown7c13edc2011-04-18 18:25:43 +100030 * conf->seq_write is the number of the last batch successfully written.
31 * conf->seq_flush is the number of the last batch that was closed to
NeilBrownae3c20c2006-07-10 04:44:17 -070032 * new additions.
33 * When we discover that we will need to write to any block in a stripe
34 * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq
NeilBrown7c13edc2011-04-18 18:25:43 +100035 * the number of the batch it will be in. This is seq_flush+1.
NeilBrownae3c20c2006-07-10 04:44:17 -070036 * When we are ready to do a write, if that batch hasn't been written yet,
37 * we plug the array and queue the stripe for later.
38 * When an unplug happens, we increment bm_flush, thus closing the current
39 * batch.
40 * When we notice that bm_flush > bm_write, we write out all pending updates
41 * to the bitmap, and advance bm_write to where bm_flush was.
42 * This may occasionally write a bit out twice, but is sure never to
43 * miss any bits.
44 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
NeilBrownbff61972009-03-31 14:33:13 +110046#include <linux/blkdev.h>
NeilBrownf6705572006-03-27 01:18:11 -080047#include <linux/kthread.h>
Dan Williamsf701d582009-03-31 15:09:39 +110048#include <linux/raid/pq.h>
Dan Williams91c00922007-01-02 13:52:30 -070049#include <linux/async_tx.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040050#include <linux/module.h>
Dan Williams07a3b412009-08-29 19:13:13 -070051#include <linux/async.h>
NeilBrownbff61972009-03-31 14:33:13 +110052#include <linux/seq_file.h>
Dan Williams36d1c642009-07-14 11:48:22 -070053#include <linux/cpu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090054#include <linux/slab.h>
Christian Dietrich8bda4702011-07-27 11:00:36 +100055#include <linux/ratelimit.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110056#include "md.h"
NeilBrownbff61972009-03-31 14:33:13 +110057#include "raid5.h"
Trela Maciej54071b32010-03-08 16:02:42 +110058#include "raid0.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110059#include "bitmap.h"
NeilBrown72626682005-09-09 16:23:54 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/*
62 * Stripe cache
63 */
64
65#define NR_STRIPES 256
66#define STRIPE_SIZE PAGE_SIZE
67#define STRIPE_SHIFT (PAGE_SHIFT - 9)
68#define STRIPE_SECTORS (STRIPE_SIZE>>9)
69#define IO_THRESHOLD 1
Dan Williams8b3e6cd2008-04-28 02:15:53 -070070#define BYPASS_THRESHOLD 1
NeilBrownfccddba2006-01-06 00:20:33 -080071#define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define HASH_MASK (NR_HASH - 1)
73
NeilBrownd1688a62011-10-11 16:49:52 +110074static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
NeilBrowndb298e12011-10-07 14:23:00 +110075{
76 int hash = (sect >> STRIPE_SHIFT) & HASH_MASK;
77 return &conf->stripe_hashtbl[hash];
78}
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80/* bio's attached to a stripe+device for I/O are linked together in bi_sector
81 * order without overlap. There may be several bio's per stripe+device, and
82 * a bio could span several devices.
83 * When walking this list for a particular stripe+device, we must never proceed
84 * beyond a bio that extends past this device, as the next bio might no longer
85 * be valid.
NeilBrowndb298e12011-10-07 14:23:00 +110086 * This function is used to determine the 'next' bio in the list, given the sector
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * of the current stripe+device
88 */
NeilBrowndb298e12011-10-07 14:23:00 +110089static inline struct bio *r5_next_bio(struct bio *bio, sector_t sector)
90{
91 int sectors = bio->bi_size >> 9;
92 if (bio->bi_sector + sectors < sector + STRIPE_SECTORS)
93 return bio->bi_next;
94 else
95 return NULL;
96}
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Jens Axboe960e7392008-08-15 10:41:18 +020098/*
Jens Axboe5b99c2f2008-08-15 10:56:11 +020099 * We maintain a biased count of active stripes in the bottom 16 bits of
100 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
Jens Axboe960e7392008-08-15 10:41:18 +0200101 */
102static inline int raid5_bi_phys_segments(struct bio *bio)
103{
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200104 return bio->bi_phys_segments & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200105}
106
107static inline int raid5_bi_hw_segments(struct bio *bio)
108{
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200109 return (bio->bi_phys_segments >> 16) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200110}
111
112static inline int raid5_dec_bi_phys_segments(struct bio *bio)
113{
114 --bio->bi_phys_segments;
115 return raid5_bi_phys_segments(bio);
116}
117
118static inline int raid5_dec_bi_hw_segments(struct bio *bio)
119{
120 unsigned short val = raid5_bi_hw_segments(bio);
121
122 --val;
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200123 bio->bi_phys_segments = (val << 16) | raid5_bi_phys_segments(bio);
Jens Axboe960e7392008-08-15 10:41:18 +0200124 return val;
125}
126
127static inline void raid5_set_bi_hw_segments(struct bio *bio, unsigned int cnt)
128{
Namhyung Kim9b2dc8b2011-06-13 14:48:22 +0900129 bio->bi_phys_segments = raid5_bi_phys_segments(bio) | (cnt << 16);
Jens Axboe960e7392008-08-15 10:41:18 +0200130}
131
NeilBrownd0dabf72009-03-31 14:39:38 +1100132/* Find first data disk in a raid6 stripe */
133static inline int raid6_d0(struct stripe_head *sh)
134{
NeilBrown67cc2b82009-03-31 14:39:38 +1100135 if (sh->ddf_layout)
136 /* ddf always start from first device */
137 return 0;
138 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100139 if (sh->qd_idx == sh->disks - 1)
140 return 0;
141 else
142 return sh->qd_idx + 1;
143}
NeilBrown16a53ec2006-06-26 00:27:38 -0700144static inline int raid6_next_disk(int disk, int raid_disks)
145{
146 disk++;
147 return (disk < raid_disks) ? disk : 0;
148}
Dan Williamsa4456852007-07-09 11:56:43 -0700149
NeilBrownd0dabf72009-03-31 14:39:38 +1100150/* When walking through the disks in a raid5, starting at raid6_d0,
151 * We need to map each disk to a 'slot', where the data disks are slot
152 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
153 * is raid_disks-1. This help does that mapping.
154 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100155static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
156 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100157{
Dan Williams66295422009-10-19 18:09:32 -0700158 int slot = *count;
NeilBrown67cc2b82009-03-31 14:39:38 +1100159
NeilBrowne4424fe2009-10-16 16:27:34 +1100160 if (sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700161 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100162 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100163 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100164 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100165 return syndrome_disks + 1;
NeilBrowne4424fe2009-10-16 16:27:34 +1100166 if (!sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700167 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100168 return slot;
169}
170
Dan Williamsa4456852007-07-09 11:56:43 -0700171static void return_io(struct bio *return_bi)
172{
173 struct bio *bi = return_bi;
174 while (bi) {
Dan Williamsa4456852007-07-09 11:56:43 -0700175
176 return_bi = bi->bi_next;
177 bi->bi_next = NULL;
178 bi->bi_size = 0;
Neil Brown0e13fe232008-06-28 08:31:20 +1000179 bio_endio(bi, 0);
Dan Williamsa4456852007-07-09 11:56:43 -0700180 bi = return_bi;
181 }
182}
183
NeilBrownd1688a62011-10-11 16:49:52 +1100184static void print_raid5_conf (struct r5conf *conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Dan Williams600aa102008-06-28 08:32:05 +1000186static int stripe_operations_active(struct stripe_head *sh)
187{
188 return sh->check_state || sh->reconstruct_state ||
189 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
190 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
191}
192
NeilBrownd1688a62011-10-11 16:49:52 +1100193static void __release_stripe(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 if (atomic_dec_and_test(&sh->count)) {
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200196 BUG_ON(!list_empty(&sh->lru));
197 BUG_ON(atomic_read(&conf->active_stripes)==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (test_bit(STRIPE_HANDLE, &sh->state)) {
NeilBrown482c0832011-04-18 18:25:42 +1000199 if (test_bit(STRIPE_DELAYED, &sh->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 list_add_tail(&sh->lru, &conf->delayed_list);
NeilBrown482c0832011-04-18 18:25:42 +1000201 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
202 sh->bm_seq - conf->seq_write > 0)
NeilBrown72626682005-09-09 16:23:54 -0700203 list_add_tail(&sh->lru, &conf->bitmap_list);
NeilBrown482c0832011-04-18 18:25:42 +1000204 else {
NeilBrown72626682005-09-09 16:23:54 -0700205 clear_bit(STRIPE_BIT_DELAY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 list_add_tail(&sh->lru, &conf->handle_list);
NeilBrown72626682005-09-09 16:23:54 -0700207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 md_wakeup_thread(conf->mddev->thread);
209 } else {
Dan Williams600aa102008-06-28 08:32:05 +1000210 BUG_ON(stripe_operations_active(sh));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
212 atomic_dec(&conf->preread_active_stripes);
213 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
214 md_wakeup_thread(conf->mddev->thread);
215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 atomic_dec(&conf->active_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800217 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
218 list_add_tail(&sh->lru, &conf->inactive_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 wake_up(&conf->wait_for_stripe);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -0800220 if (conf->retry_read_aligned)
221 md_wakeup_thread(conf->mddev->thread);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
224 }
225}
NeilBrownd0dabf72009-03-31 14:39:38 +1100226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227static void release_stripe(struct stripe_head *sh)
228{
NeilBrownd1688a62011-10-11 16:49:52 +1100229 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 unsigned long flags;
NeilBrown16a53ec2006-06-26 00:27:38 -0700231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 spin_lock_irqsave(&conf->device_lock, flags);
233 __release_stripe(conf, sh);
234 spin_unlock_irqrestore(&conf->device_lock, flags);
235}
236
NeilBrownfccddba2006-01-06 00:20:33 -0800237static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Dan Williams45b42332007-07-09 11:56:43 -0700239 pr_debug("remove_hash(), stripe %llu\n",
240 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
NeilBrownfccddba2006-01-06 00:20:33 -0800242 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
NeilBrownd1688a62011-10-11 16:49:52 +1100245static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
NeilBrownfccddba2006-01-06 00:20:33 -0800247 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Dan Williams45b42332007-07-09 11:56:43 -0700249 pr_debug("insert_hash(), stripe %llu\n",
250 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
NeilBrownfccddba2006-01-06 00:20:33 -0800252 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
255
256/* find an idle stripe, make sure it is unhashed, and return it. */
NeilBrownd1688a62011-10-11 16:49:52 +1100257static struct stripe_head *get_free_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 struct stripe_head *sh = NULL;
260 struct list_head *first;
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (list_empty(&conf->inactive_list))
263 goto out;
264 first = conf->inactive_list.next;
265 sh = list_entry(first, struct stripe_head, lru);
266 list_del_init(first);
267 remove_hash(sh);
268 atomic_inc(&conf->active_stripes);
269out:
270 return sh;
271}
272
NeilBrowne4e11e32010-06-16 16:45:16 +1000273static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 struct page *p;
276 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000277 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
NeilBrowne4e11e32010-06-16 16:45:16 +1000279 for (i = 0; i < num ; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 p = sh->dev[i].page;
281 if (!p)
282 continue;
283 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800284 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286}
287
NeilBrowne4e11e32010-06-16 16:45:16 +1000288static int grow_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000291 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
NeilBrowne4e11e32010-06-16 16:45:16 +1000293 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 struct page *page;
295
296 if (!(page = alloc_page(GFP_KERNEL))) {
297 return 1;
298 }
299 sh->dev[i].page = page;
300 }
301 return 0;
302}
303
NeilBrown784052e2009-03-31 15:19:07 +1100304static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrownd1688a62011-10-11 16:49:52 +1100305static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100306 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
NeilBrownb5663ba2009-03-31 14:39:38 +1100308static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
NeilBrownd1688a62011-10-11 16:49:52 +1100310 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800311 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200313 BUG_ON(atomic_read(&sh->count) != 0);
314 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000315 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700316
Dan Williams45b42332007-07-09 11:56:43 -0700317 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 (unsigned long long)sh->sector);
319
320 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700321
NeilBrown86b42c72009-03-31 15:19:03 +1100322 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100323 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100325 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 sh->state = 0;
327
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800328
329 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 struct r5dev *dev = &sh->dev[i];
331
Dan Williamsd84e0f12007-01-02 13:52:30 -0700332 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700334 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700336 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000338 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100341 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343 insert_hash(conf, sh);
344}
345
NeilBrownd1688a62011-10-11 16:49:52 +1100346static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100347 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -0800350 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Dan Williams45b42332007-07-09 11:56:43 -0700352 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
NeilBrownfccddba2006-01-06 00:20:33 -0800353 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100354 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700356 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return NULL;
358}
359
NeilBrown674806d2010-06-16 17:17:53 +1000360/*
361 * Need to check if array has failed when deciding whether to:
362 * - start an array
363 * - remove non-faulty devices
364 * - add a spare
365 * - allow a reshape
366 * This determination is simple when no reshape is happening.
367 * However if there is a reshape, we need to carefully check
368 * both the before and after sections.
369 * This is because some failed devices may only affect one
370 * of the two sections, and some non-in_sync devices may
371 * be insync in the section most affected by failed devices.
372 */
NeilBrown908f4fb2011-12-23 10:17:50 +1100373static int calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000374{
NeilBrown908f4fb2011-12-23 10:17:50 +1100375 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000376 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000377
378 rcu_read_lock();
379 degraded = 0;
380 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100381 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown674806d2010-06-16 17:17:53 +1000382 if (!rdev || test_bit(Faulty, &rdev->flags))
383 degraded++;
384 else if (test_bit(In_sync, &rdev->flags))
385 ;
386 else
387 /* not in-sync or faulty.
388 * If the reshape increases the number of devices,
389 * this is being recovered by the reshape, so
390 * this 'previous' section is not in_sync.
391 * If the number of devices is being reduced however,
392 * the device can only be part of the array if
393 * we are reverting a reshape, so this section will
394 * be in-sync.
395 */
396 if (conf->raid_disks >= conf->previous_raid_disks)
397 degraded++;
398 }
399 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100400 if (conf->raid_disks == conf->previous_raid_disks)
401 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000402 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100403 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000404 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100405 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown674806d2010-06-16 17:17:53 +1000406 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100407 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000408 else if (test_bit(In_sync, &rdev->flags))
409 ;
410 else
411 /* not in-sync or faulty.
412 * If reshape increases the number of devices, this
413 * section has already been recovered, else it
414 * almost certainly hasn't.
415 */
416 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100417 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000418 }
419 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100420 if (degraded2 > degraded)
421 return degraded2;
422 return degraded;
423}
424
425static int has_failed(struct r5conf *conf)
426{
427 int degraded;
428
429 if (conf->mddev->reshape_position == MaxSector)
430 return conf->mddev->degraded > conf->max_degraded;
431
432 degraded = calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000433 if (degraded > conf->max_degraded)
434 return 1;
435 return 0;
436}
437
NeilBrownb5663ba2009-03-31 14:39:38 +1100438static struct stripe_head *
NeilBrownd1688a62011-10-11 16:49:52 +1100439get_active_stripe(struct r5conf *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000440 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
442 struct stripe_head *sh;
443
Dan Williams45b42332007-07-09 11:56:43 -0700444 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 spin_lock_irq(&conf->device_lock);
447
448 do {
NeilBrown72626682005-09-09 16:23:54 -0700449 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000450 conf->quiesce == 0 || noquiesce,
NeilBrown72626682005-09-09 16:23:54 -0700451 conf->device_lock, /* nothing */);
NeilBrown86b42c72009-03-31 15:19:03 +1100452 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (!sh) {
454 if (!conf->inactive_blocked)
455 sh = get_free_stripe(conf);
456 if (noblock && sh == NULL)
457 break;
458 if (!sh) {
459 conf->inactive_blocked = 1;
460 wait_event_lock_irq(conf->wait_for_stripe,
461 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800462 (atomic_read(&conf->active_stripes)
463 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 || !conf->inactive_blocked),
465 conf->device_lock,
NeilBrown7c13edc2011-04-18 18:25:43 +1000466 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 conf->inactive_blocked = 0;
468 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100469 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 } else {
471 if (atomic_read(&sh->count)) {
NeilBrownab69ae12009-03-31 15:26:47 +1100472 BUG_ON(!list_empty(&sh->lru)
473 && !test_bit(STRIPE_EXPANDING, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 } else {
475 if (!test_bit(STRIPE_HANDLE, &sh->state))
476 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700477 if (list_empty(&sh->lru) &&
478 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700479 BUG();
480 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
482 }
483 } while (sh == NULL);
484
485 if (sh)
486 atomic_inc(&sh->count);
487
488 spin_unlock_irq(&conf->device_lock);
489 return sh;
490}
491
NeilBrown6712ecf2007-09-27 12:47:43 +0200492static void
493raid5_end_read_request(struct bio *bi, int error);
494static void
495raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700496
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000497static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700498{
NeilBrownd1688a62011-10-11 16:49:52 +1100499 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700500 int i, disks = sh->disks;
501
502 might_sleep();
503
504 for (i = disks; i--; ) {
505 int rw;
506 struct bio *bi;
NeilBrown3cb03002011-10-11 16:45:26 +1100507 struct md_rdev *rdev;
Tejun Heoe9c74692010-09-03 11:56:18 +0200508 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
509 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
510 rw = WRITE_FUA;
511 else
512 rw = WRITE;
513 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700514 rw = READ;
515 else
516 continue;
517
518 bi = &sh->dev[i].req;
519
520 bi->bi_rw = rw;
Namhyung Kimb0629622011-06-14 14:20:19 +1000521 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700522 bi->bi_end_io = raid5_end_write_request;
523 else
524 bi->bi_end_io = raid5_end_read_request;
525
526 rcu_read_lock();
527 rdev = rcu_dereference(conf->disks[i].rdev);
528 if (rdev && test_bit(Faulty, &rdev->flags))
529 rdev = NULL;
530 if (rdev)
531 atomic_inc(&rdev->nr_pending);
532 rcu_read_unlock();
533
NeilBrown73e92e52011-07-28 11:39:22 +1000534 /* We have already checked bad blocks for reads. Now
535 * need to check for writes.
536 */
537 while ((rw & WRITE) && rdev &&
538 test_bit(WriteErrorSeen, &rdev->flags)) {
539 sector_t first_bad;
540 int bad_sectors;
541 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
542 &first_bad, &bad_sectors);
543 if (!bad)
544 break;
545
546 if (bad < 0) {
547 set_bit(BlockedBadBlocks, &rdev->flags);
548 if (!conf->mddev->external &&
549 conf->mddev->flags) {
550 /* It is very unlikely, but we might
551 * still need to write out the
552 * bad block log - better give it
553 * a chance*/
554 md_check_recovery(conf->mddev);
555 }
556 md_wait_for_blocked_rdev(rdev, conf->mddev);
557 } else {
558 /* Acknowledged bad block - skip the write */
559 rdev_dec_pending(rdev, conf->mddev);
560 rdev = NULL;
561 }
562 }
563
Dan Williams91c00922007-01-02 13:52:30 -0700564 if (rdev) {
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000565 if (s->syncing || s->expanding || s->expanded)
Dan Williams91c00922007-01-02 13:52:30 -0700566 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
567
Dan Williams2b7497f2008-06-28 08:31:52 +1000568 set_bit(STRIPE_IO_STARTED, &sh->state);
569
Dan Williams91c00922007-01-02 13:52:30 -0700570 bi->bi_bdev = rdev->bdev;
571 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700572 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700573 bi->bi_rw, i);
574 atomic_inc(&sh->count);
575 bi->bi_sector = sh->sector + rdev->data_offset;
576 bi->bi_flags = 1 << BIO_UPTODATE;
577 bi->bi_vcnt = 1;
578 bi->bi_max_vecs = 1;
579 bi->bi_idx = 0;
580 bi->bi_io_vec = &sh->dev[i].vec;
581 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
582 bi->bi_io_vec[0].bv_offset = 0;
583 bi->bi_size = STRIPE_SIZE;
584 bi->bi_next = NULL;
Dan Williams91c00922007-01-02 13:52:30 -0700585 generic_make_request(bi);
586 } else {
Namhyung Kimb0629622011-06-14 14:20:19 +1000587 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700588 set_bit(STRIPE_DEGRADED, &sh->state);
589 pr_debug("skip op %ld on disc %d for sector %llu\n",
590 bi->bi_rw, i, (unsigned long long)sh->sector);
591 clear_bit(R5_LOCKED, &sh->dev[i].flags);
592 set_bit(STRIPE_HANDLE, &sh->state);
593 }
594 }
595}
596
597static struct dma_async_tx_descriptor *
598async_copy_data(int frombio, struct bio *bio, struct page *page,
599 sector_t sector, struct dma_async_tx_descriptor *tx)
600{
601 struct bio_vec *bvl;
602 struct page *bio_page;
603 int i;
604 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700605 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700606 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700607
608 if (bio->bi_sector >= sector)
609 page_offset = (signed)(bio->bi_sector - sector) * 512;
610 else
611 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700612
Dan Williams0403e382009-09-08 17:42:50 -0700613 if (frombio)
614 flags |= ASYNC_TX_FENCE;
615 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
616
Dan Williams91c00922007-01-02 13:52:30 -0700617 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000618 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700619 int clen;
620 int b_offset = 0;
621
622 if (page_offset < 0) {
623 b_offset = -page_offset;
624 page_offset += b_offset;
625 len -= b_offset;
626 }
627
628 if (len > 0 && page_offset + len > STRIPE_SIZE)
629 clen = STRIPE_SIZE - page_offset;
630 else
631 clen = len;
632
633 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000634 b_offset += bvl->bv_offset;
635 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700636 if (frombio)
637 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700638 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700639 else
640 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700641 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700642 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700643 /* chain the operations */
644 submit.depend_tx = tx;
645
Dan Williams91c00922007-01-02 13:52:30 -0700646 if (clen < len) /* hit end of page */
647 break;
648 page_offset += len;
649 }
650
651 return tx;
652}
653
654static void ops_complete_biofill(void *stripe_head_ref)
655{
656 struct stripe_head *sh = stripe_head_ref;
657 struct bio *return_bi = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +1100658 struct r5conf *conf = sh->raid_conf;
Dan Williamse4d84902007-09-24 10:06:13 -0700659 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700660
Harvey Harrisone46b2722008-04-28 02:15:50 -0700661 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700662 (unsigned long long)sh->sector);
663
664 /* clear completed biofills */
Dan Williams83de75c2008-06-28 08:31:58 +1000665 spin_lock_irq(&conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700666 for (i = sh->disks; i--; ) {
667 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700668
669 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700670 /* and check if we need to reply to a read request,
671 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000672 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700673 */
674 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700675 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700676
Dan Williams91c00922007-01-02 13:52:30 -0700677 BUG_ON(!dev->read);
678 rbi = dev->read;
679 dev->read = NULL;
680 while (rbi && rbi->bi_sector <
681 dev->sector + STRIPE_SECTORS) {
682 rbi2 = r5_next_bio(rbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +0200683 if (!raid5_dec_bi_phys_segments(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700684 rbi->bi_next = return_bi;
685 return_bi = rbi;
686 }
Dan Williams91c00922007-01-02 13:52:30 -0700687 rbi = rbi2;
688 }
689 }
690 }
Dan Williams83de75c2008-06-28 08:31:58 +1000691 spin_unlock_irq(&conf->device_lock);
692 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700693
694 return_io(return_bi);
695
Dan Williamse4d84902007-09-24 10:06:13 -0700696 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700697 release_stripe(sh);
698}
699
700static void ops_run_biofill(struct stripe_head *sh)
701{
702 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +1100703 struct r5conf *conf = sh->raid_conf;
Dan Williamsa08abd82009-06-03 11:43:59 -0700704 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700705 int i;
706
Harvey Harrisone46b2722008-04-28 02:15:50 -0700707 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700708 (unsigned long long)sh->sector);
709
710 for (i = sh->disks; i--; ) {
711 struct r5dev *dev = &sh->dev[i];
712 if (test_bit(R5_Wantfill, &dev->flags)) {
713 struct bio *rbi;
714 spin_lock_irq(&conf->device_lock);
715 dev->read = rbi = dev->toread;
716 dev->toread = NULL;
717 spin_unlock_irq(&conf->device_lock);
718 while (rbi && rbi->bi_sector <
719 dev->sector + STRIPE_SECTORS) {
720 tx = async_copy_data(0, rbi, dev->page,
721 dev->sector, tx);
722 rbi = r5_next_bio(rbi, dev->sector);
723 }
724 }
725 }
726
727 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700728 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
729 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700730}
731
Dan Williams4e7d2c02009-08-29 19:13:11 -0700732static void mark_target_uptodate(struct stripe_head *sh, int target)
733{
734 struct r5dev *tgt;
735
736 if (target < 0)
737 return;
738
739 tgt = &sh->dev[target];
740 set_bit(R5_UPTODATE, &tgt->flags);
741 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
742 clear_bit(R5_Wantcompute, &tgt->flags);
743}
744
Dan Williamsac6b53b2009-07-14 13:40:19 -0700745static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700746{
747 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700748
Harvey Harrisone46b2722008-04-28 02:15:50 -0700749 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700750 (unsigned long long)sh->sector);
751
Dan Williamsac6b53b2009-07-14 13:40:19 -0700752 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700753 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700754 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700755
Dan Williamsecc65c92008-06-28 08:31:57 +1000756 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
757 if (sh->check_state == check_state_compute_run)
758 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700759 set_bit(STRIPE_HANDLE, &sh->state);
760 release_stripe(sh);
761}
762
Dan Williamsd6f38f32009-07-14 11:50:52 -0700763/* return a pointer to the address conversion region of the scribble buffer */
764static addr_conv_t *to_addr_conv(struct stripe_head *sh,
765 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700766{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700767 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
768}
769
770static struct dma_async_tx_descriptor *
771ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
772{
Dan Williams91c00922007-01-02 13:52:30 -0700773 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700774 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700775 int target = sh->ops.target;
776 struct r5dev *tgt = &sh->dev[target];
777 struct page *xor_dest = tgt->page;
778 int count = 0;
779 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700780 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700781 int i;
782
783 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700784 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700785 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
786
787 for (i = disks; i--; )
788 if (i != target)
789 xor_srcs[count++] = sh->dev[i].page;
790
791 atomic_inc(&sh->count);
792
Dan Williams0403e382009-09-08 17:42:50 -0700793 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700794 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700795 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700796 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700797 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700798 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700799
Dan Williams91c00922007-01-02 13:52:30 -0700800 return tx;
801}
802
Dan Williamsac6b53b2009-07-14 13:40:19 -0700803/* set_syndrome_sources - populate source buffers for gen_syndrome
804 * @srcs - (struct page *) array of size sh->disks
805 * @sh - stripe_head to parse
806 *
807 * Populates srcs in proper layout order for the stripe and returns the
808 * 'count' of sources to be used in a call to async_gen_syndrome. The P
809 * destination buffer is recorded in srcs[count] and the Q destination
810 * is recorded in srcs[count+1]].
811 */
812static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
813{
814 int disks = sh->disks;
815 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
816 int d0_idx = raid6_d0(sh);
817 int count;
818 int i;
819
820 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100821 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700822
823 count = 0;
824 i = d0_idx;
825 do {
826 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
827
828 srcs[slot] = sh->dev[i].page;
829 i = raid6_next_disk(i, disks);
830 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700831
NeilBrowne4424fe2009-10-16 16:27:34 +1100832 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700833}
834
835static struct dma_async_tx_descriptor *
836ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
837{
838 int disks = sh->disks;
839 struct page **blocks = percpu->scribble;
840 int target;
841 int qd_idx = sh->qd_idx;
842 struct dma_async_tx_descriptor *tx;
843 struct async_submit_ctl submit;
844 struct r5dev *tgt;
845 struct page *dest;
846 int i;
847 int count;
848
849 if (sh->ops.target < 0)
850 target = sh->ops.target2;
851 else if (sh->ops.target2 < 0)
852 target = sh->ops.target;
853 else
854 /* we should only have one valid target */
855 BUG();
856 BUG_ON(target < 0);
857 pr_debug("%s: stripe %llu block: %d\n",
858 __func__, (unsigned long long)sh->sector, target);
859
860 tgt = &sh->dev[target];
861 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
862 dest = tgt->page;
863
864 atomic_inc(&sh->count);
865
866 if (target == qd_idx) {
867 count = set_syndrome_sources(blocks, sh);
868 blocks[count] = NULL; /* regenerating p is not necessary */
869 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700870 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
871 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700872 to_addr_conv(sh, percpu));
873 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
874 } else {
875 /* Compute any data- or p-drive using XOR */
876 count = 0;
877 for (i = disks; i-- ; ) {
878 if (i == target || i == qd_idx)
879 continue;
880 blocks[count++] = sh->dev[i].page;
881 }
882
Dan Williams0403e382009-09-08 17:42:50 -0700883 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
884 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700885 to_addr_conv(sh, percpu));
886 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
887 }
888
889 return tx;
890}
891
892static struct dma_async_tx_descriptor *
893ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
894{
895 int i, count, disks = sh->disks;
896 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
897 int d0_idx = raid6_d0(sh);
898 int faila = -1, failb = -1;
899 int target = sh->ops.target;
900 int target2 = sh->ops.target2;
901 struct r5dev *tgt = &sh->dev[target];
902 struct r5dev *tgt2 = &sh->dev[target2];
903 struct dma_async_tx_descriptor *tx;
904 struct page **blocks = percpu->scribble;
905 struct async_submit_ctl submit;
906
907 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
908 __func__, (unsigned long long)sh->sector, target, target2);
909 BUG_ON(target < 0 || target2 < 0);
910 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
911 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
912
Dan Williams6c910a72009-09-16 12:24:54 -0700913 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -0700914 * slot number conversion for 'faila' and 'failb'
915 */
916 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100917 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700918 count = 0;
919 i = d0_idx;
920 do {
921 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
922
923 blocks[slot] = sh->dev[i].page;
924
925 if (i == target)
926 faila = slot;
927 if (i == target2)
928 failb = slot;
929 i = raid6_next_disk(i, disks);
930 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700931
932 BUG_ON(faila == failb);
933 if (failb < faila)
934 swap(faila, failb);
935 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
936 __func__, (unsigned long long)sh->sector, faila, failb);
937
938 atomic_inc(&sh->count);
939
940 if (failb == syndrome_disks+1) {
941 /* Q disk is one of the missing disks */
942 if (faila == syndrome_disks) {
943 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -0700944 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
945 ops_complete_compute, sh,
946 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +1100947 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700948 STRIPE_SIZE, &submit);
949 } else {
950 struct page *dest;
951 int data_target;
952 int qd_idx = sh->qd_idx;
953
954 /* Missing D+Q: recompute D from P, then recompute Q */
955 if (target == qd_idx)
956 data_target = target2;
957 else
958 data_target = target;
959
960 count = 0;
961 for (i = disks; i-- ; ) {
962 if (i == data_target || i == qd_idx)
963 continue;
964 blocks[count++] = sh->dev[i].page;
965 }
966 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -0700967 init_async_submit(&submit,
968 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
969 NULL, NULL, NULL,
970 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -0700971 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
972 &submit);
973
974 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -0700975 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
976 ops_complete_compute, sh,
977 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -0700978 return async_gen_syndrome(blocks, 0, count+2,
979 STRIPE_SIZE, &submit);
980 }
Dan Williamsac6b53b2009-07-14 13:40:19 -0700981 } else {
Dan Williams6c910a72009-09-16 12:24:54 -0700982 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
983 ops_complete_compute, sh,
984 to_addr_conv(sh, percpu));
985 if (failb == syndrome_disks) {
986 /* We're missing D+P. */
987 return async_raid6_datap_recov(syndrome_disks+2,
988 STRIPE_SIZE, faila,
989 blocks, &submit);
990 } else {
991 /* We're missing D+D. */
992 return async_raid6_2data_recov(syndrome_disks+2,
993 STRIPE_SIZE, faila, failb,
994 blocks, &submit);
995 }
Dan Williamsac6b53b2009-07-14 13:40:19 -0700996 }
997}
998
999
Dan Williams91c00922007-01-02 13:52:30 -07001000static void ops_complete_prexor(void *stripe_head_ref)
1001{
1002 struct stripe_head *sh = stripe_head_ref;
1003
Harvey Harrisone46b2722008-04-28 02:15:50 -07001004 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001005 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001006}
1007
1008static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001009ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1010 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001011{
Dan Williams91c00922007-01-02 13:52:30 -07001012 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001013 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001014 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001015 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001016
1017 /* existing parity data subtracted */
1018 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1019
Harvey Harrisone46b2722008-04-28 02:15:50 -07001020 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001021 (unsigned long long)sh->sector);
1022
1023 for (i = disks; i--; ) {
1024 struct r5dev *dev = &sh->dev[i];
1025 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001026 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001027 xor_srcs[count++] = dev->page;
1028 }
1029
Dan Williams0403e382009-09-08 17:42:50 -07001030 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001031 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001032 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001033
1034 return tx;
1035}
1036
1037static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001038ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001039{
1040 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001041 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001042
Harvey Harrisone46b2722008-04-28 02:15:50 -07001043 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001044 (unsigned long long)sh->sector);
1045
1046 for (i = disks; i--; ) {
1047 struct r5dev *dev = &sh->dev[i];
1048 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001049
Dan Williamsd8ee0722008-06-28 08:32:06 +10001050 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001051 struct bio *wbi;
1052
NeilBrowncbe47ec2011-07-26 11:20:35 +10001053 spin_lock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001054 chosen = dev->towrite;
1055 dev->towrite = NULL;
1056 BUG_ON(dev->written);
1057 wbi = dev->written = chosen;
NeilBrowncbe47ec2011-07-26 11:20:35 +10001058 spin_unlock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001059
1060 while (wbi && wbi->bi_sector <
1061 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001062 if (wbi->bi_rw & REQ_FUA)
1063 set_bit(R5_WantFUA, &dev->flags);
Dan Williams91c00922007-01-02 13:52:30 -07001064 tx = async_copy_data(1, wbi, dev->page,
1065 dev->sector, tx);
1066 wbi = r5_next_bio(wbi, dev->sector);
1067 }
1068 }
1069 }
1070
1071 return tx;
1072}
1073
Dan Williamsac6b53b2009-07-14 13:40:19 -07001074static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001075{
1076 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001077 int disks = sh->disks;
1078 int pd_idx = sh->pd_idx;
1079 int qd_idx = sh->qd_idx;
1080 int i;
Tejun Heoe9c74692010-09-03 11:56:18 +02001081 bool fua = false;
Dan Williams91c00922007-01-02 13:52:30 -07001082
Harvey Harrisone46b2722008-04-28 02:15:50 -07001083 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001084 (unsigned long long)sh->sector);
1085
Tejun Heoe9c74692010-09-03 11:56:18 +02001086 for (i = disks; i--; )
1087 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
1088
Dan Williams91c00922007-01-02 13:52:30 -07001089 for (i = disks; i--; ) {
1090 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001091
Tejun Heoe9c74692010-09-03 11:56:18 +02001092 if (dev->written || i == pd_idx || i == qd_idx) {
Dan Williams91c00922007-01-02 13:52:30 -07001093 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001094 if (fua)
1095 set_bit(R5_WantFUA, &dev->flags);
1096 }
Dan Williams91c00922007-01-02 13:52:30 -07001097 }
1098
Dan Williamsd8ee0722008-06-28 08:32:06 +10001099 if (sh->reconstruct_state == reconstruct_state_drain_run)
1100 sh->reconstruct_state = reconstruct_state_drain_result;
1101 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1102 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1103 else {
1104 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1105 sh->reconstruct_state = reconstruct_state_result;
1106 }
Dan Williams91c00922007-01-02 13:52:30 -07001107
1108 set_bit(STRIPE_HANDLE, &sh->state);
1109 release_stripe(sh);
1110}
1111
1112static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001113ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1114 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001115{
Dan Williams91c00922007-01-02 13:52:30 -07001116 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001117 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001118 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001119 int count = 0, pd_idx = sh->pd_idx, i;
1120 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001121 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001122 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001123
Harvey Harrisone46b2722008-04-28 02:15:50 -07001124 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001125 (unsigned long long)sh->sector);
1126
1127 /* check if prexor is active which means only process blocks
1128 * that are part of a read-modify-write (written)
1129 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001130 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1131 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001132 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1133 for (i = disks; i--; ) {
1134 struct r5dev *dev = &sh->dev[i];
1135 if (dev->written)
1136 xor_srcs[count++] = dev->page;
1137 }
1138 } else {
1139 xor_dest = sh->dev[pd_idx].page;
1140 for (i = disks; i--; ) {
1141 struct r5dev *dev = &sh->dev[i];
1142 if (i != pd_idx)
1143 xor_srcs[count++] = dev->page;
1144 }
1145 }
1146
Dan Williams91c00922007-01-02 13:52:30 -07001147 /* 1/ if we prexor'd then the dest is reused as a source
1148 * 2/ if we did not prexor then we are redoing the parity
1149 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1150 * for the synchronous xor case
1151 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001152 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001153 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1154
1155 atomic_inc(&sh->count);
1156
Dan Williamsac6b53b2009-07-14 13:40:19 -07001157 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001158 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001159 if (unlikely(count == 1))
1160 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1161 else
1162 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001163}
1164
Dan Williamsac6b53b2009-07-14 13:40:19 -07001165static void
1166ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1167 struct dma_async_tx_descriptor *tx)
1168{
1169 struct async_submit_ctl submit;
1170 struct page **blocks = percpu->scribble;
1171 int count;
1172
1173 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1174
1175 count = set_syndrome_sources(blocks, sh);
1176
1177 atomic_inc(&sh->count);
1178
1179 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1180 sh, to_addr_conv(sh, percpu));
1181 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001182}
1183
1184static void ops_complete_check(void *stripe_head_ref)
1185{
1186 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001187
Harvey Harrisone46b2722008-04-28 02:15:50 -07001188 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001189 (unsigned long long)sh->sector);
1190
Dan Williamsecc65c92008-06-28 08:31:57 +10001191 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001192 set_bit(STRIPE_HANDLE, &sh->state);
1193 release_stripe(sh);
1194}
1195
Dan Williamsac6b53b2009-07-14 13:40:19 -07001196static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001197{
Dan Williams91c00922007-01-02 13:52:30 -07001198 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001199 int pd_idx = sh->pd_idx;
1200 int qd_idx = sh->qd_idx;
1201 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001202 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001203 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001204 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001205 int count;
1206 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001207
Harvey Harrisone46b2722008-04-28 02:15:50 -07001208 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001209 (unsigned long long)sh->sector);
1210
Dan Williamsac6b53b2009-07-14 13:40:19 -07001211 count = 0;
1212 xor_dest = sh->dev[pd_idx].page;
1213 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001214 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001215 if (i == pd_idx || i == qd_idx)
1216 continue;
1217 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001218 }
1219
Dan Williamsd6f38f32009-07-14 11:50:52 -07001220 init_async_submit(&submit, 0, NULL, NULL, NULL,
1221 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001222 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001223 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001224
Dan Williams91c00922007-01-02 13:52:30 -07001225 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001226 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1227 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001228}
1229
Dan Williamsac6b53b2009-07-14 13:40:19 -07001230static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1231{
1232 struct page **srcs = percpu->scribble;
1233 struct async_submit_ctl submit;
1234 int count;
1235
1236 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1237 (unsigned long long)sh->sector, checkp);
1238
1239 count = set_syndrome_sources(srcs, sh);
1240 if (!checkp)
1241 srcs[count] = NULL;
1242
1243 atomic_inc(&sh->count);
1244 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1245 sh, to_addr_conv(sh, percpu));
1246 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1247 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1248}
1249
Dan Williams417b8d42009-10-16 16:25:22 +11001250static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001251{
1252 int overlap_clear = 0, i, disks = sh->disks;
1253 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001254 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001255 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001256 struct raid5_percpu *percpu;
1257 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001258
Dan Williamsd6f38f32009-07-14 11:50:52 -07001259 cpu = get_cpu();
1260 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001261 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001262 ops_run_biofill(sh);
1263 overlap_clear++;
1264 }
1265
Dan Williams7b3a8712008-06-28 08:32:09 +10001266 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001267 if (level < 6)
1268 tx = ops_run_compute5(sh, percpu);
1269 else {
1270 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1271 tx = ops_run_compute6_1(sh, percpu);
1272 else
1273 tx = ops_run_compute6_2(sh, percpu);
1274 }
1275 /* terminate the chain if reconstruct is not set to be run */
1276 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001277 async_tx_ack(tx);
1278 }
Dan Williams91c00922007-01-02 13:52:30 -07001279
Dan Williams600aa102008-06-28 08:32:05 +10001280 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001281 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001282
Dan Williams600aa102008-06-28 08:32:05 +10001283 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001284 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001285 overlap_clear++;
1286 }
1287
Dan Williamsac6b53b2009-07-14 13:40:19 -07001288 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1289 if (level < 6)
1290 ops_run_reconstruct5(sh, percpu, tx);
1291 else
1292 ops_run_reconstruct6(sh, percpu, tx);
1293 }
Dan Williams91c00922007-01-02 13:52:30 -07001294
Dan Williamsac6b53b2009-07-14 13:40:19 -07001295 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1296 if (sh->check_state == check_state_run)
1297 ops_run_check_p(sh, percpu);
1298 else if (sh->check_state == check_state_run_q)
1299 ops_run_check_pq(sh, percpu, 0);
1300 else if (sh->check_state == check_state_run_pq)
1301 ops_run_check_pq(sh, percpu, 1);
1302 else
1303 BUG();
1304 }
Dan Williams91c00922007-01-02 13:52:30 -07001305
Dan Williams91c00922007-01-02 13:52:30 -07001306 if (overlap_clear)
1307 for (i = disks; i--; ) {
1308 struct r5dev *dev = &sh->dev[i];
1309 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1310 wake_up(&sh->raid_conf->wait_for_overlap);
1311 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001312 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001313}
1314
Dan Williams417b8d42009-10-16 16:25:22 +11001315#ifdef CONFIG_MULTICORE_RAID456
1316static void async_run_ops(void *param, async_cookie_t cookie)
1317{
1318 struct stripe_head *sh = param;
1319 unsigned long ops_request = sh->ops.request;
1320
1321 clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
1322 wake_up(&sh->ops.wait_for_ops);
1323
1324 __raid_run_ops(sh, ops_request);
1325 release_stripe(sh);
1326}
1327
1328static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1329{
1330 /* since handle_stripe can be called outside of raid5d context
1331 * we need to ensure sh->ops.request is de-staged before another
1332 * request arrives
1333 */
1334 wait_event(sh->ops.wait_for_ops,
1335 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
1336 sh->ops.request = ops_request;
1337
1338 atomic_inc(&sh->count);
1339 async_schedule(async_run_ops, sh);
1340}
1341#else
1342#define raid_run_ops __raid_run_ops
1343#endif
1344
NeilBrownd1688a62011-10-11 16:49:52 +11001345static int grow_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
1347 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001348 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001349 if (!sh)
1350 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001351
NeilBrown3f294f42005-11-08 21:39:25 -08001352 sh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001353 #ifdef CONFIG_MULTICORE_RAID456
1354 init_waitqueue_head(&sh->ops.wait_for_ops);
1355 #endif
NeilBrown3f294f42005-11-08 21:39:25 -08001356
NeilBrowne4e11e32010-06-16 16:45:16 +10001357 if (grow_buffers(sh)) {
1358 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001359 kmem_cache_free(conf->slab_cache, sh);
1360 return 0;
1361 }
1362 /* we just created an active stripe so... */
1363 atomic_set(&sh->count, 1);
1364 atomic_inc(&conf->active_stripes);
1365 INIT_LIST_HEAD(&sh->lru);
1366 release_stripe(sh);
1367 return 1;
1368}
1369
NeilBrownd1688a62011-10-11 16:49:52 +11001370static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001371{
Christoph Lametere18b8902006-12-06 20:33:20 -08001372 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001373 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
NeilBrownf4be6b42010-06-01 19:37:25 +10001375 if (conf->mddev->gendisk)
1376 sprintf(conf->cache_name[0],
1377 "raid%d-%s", conf->level, mdname(conf->mddev));
1378 else
1379 sprintf(conf->cache_name[0],
1380 "raid%d-%p", conf->level, conf->mddev);
1381 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1382
NeilBrownad01c9e2006-03-27 01:18:07 -08001383 conf->active_name = 0;
1384 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001386 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 if (!sc)
1388 return 1;
1389 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001390 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001391 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001392 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 return 0;
1395}
NeilBrown29269552006-03-27 01:18:10 -08001396
Dan Williamsd6f38f32009-07-14 11:50:52 -07001397/**
1398 * scribble_len - return the required size of the scribble region
1399 * @num - total number of disks in the array
1400 *
1401 * The size must be enough to contain:
1402 * 1/ a struct page pointer for each device in the array +2
1403 * 2/ room to convert each entry in (1) to its corresponding dma
1404 * (dma_map_page()) or page (page_address()) address.
1405 *
1406 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1407 * calculate over all devices (not just the data blocks), using zeros in place
1408 * of the P and Q blocks.
1409 */
1410static size_t scribble_len(int num)
1411{
1412 size_t len;
1413
1414 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1415
1416 return len;
1417}
1418
NeilBrownd1688a62011-10-11 16:49:52 +11001419static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001420{
1421 /* Make all the stripes able to hold 'newsize' devices.
1422 * New slots in each stripe get 'page' set to a new page.
1423 *
1424 * This happens in stages:
1425 * 1/ create a new kmem_cache and allocate the required number of
1426 * stripe_heads.
1427 * 2/ gather all the old stripe_heads and tranfer the pages across
1428 * to the new stripe_heads. This will have the side effect of
1429 * freezing the array as once all stripe_heads have been collected,
1430 * no IO will be possible. Old stripe heads are freed once their
1431 * pages have been transferred over, and the old kmem_cache is
1432 * freed when all stripes are done.
1433 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1434 * we simple return a failre status - no need to clean anything up.
1435 * 4/ allocate new pages for the new slots in the new stripe_heads.
1436 * If this fails, we don't bother trying the shrink the
1437 * stripe_heads down again, we just leave them as they are.
1438 * As each stripe_head is processed the new one is released into
1439 * active service.
1440 *
1441 * Once step2 is started, we cannot afford to wait for a write,
1442 * so we use GFP_NOIO allocations.
1443 */
1444 struct stripe_head *osh, *nsh;
1445 LIST_HEAD(newstripes);
1446 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001447 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001448 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001449 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001450 int i;
1451
1452 if (newsize <= conf->pool_size)
1453 return 0; /* never bother to shrink */
1454
Dan Williamsb5470dc2008-06-27 21:44:04 -07001455 err = md_allow_write(conf->mddev);
1456 if (err)
1457 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001458
NeilBrownad01c9e2006-03-27 01:18:07 -08001459 /* Step 1 */
1460 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1461 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001462 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001463 if (!sc)
1464 return -ENOMEM;
1465
1466 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001467 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001468 if (!nsh)
1469 break;
1470
NeilBrownad01c9e2006-03-27 01:18:07 -08001471 nsh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001472 #ifdef CONFIG_MULTICORE_RAID456
1473 init_waitqueue_head(&nsh->ops.wait_for_ops);
1474 #endif
NeilBrownad01c9e2006-03-27 01:18:07 -08001475
1476 list_add(&nsh->lru, &newstripes);
1477 }
1478 if (i) {
1479 /* didn't get enough, give up */
1480 while (!list_empty(&newstripes)) {
1481 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1482 list_del(&nsh->lru);
1483 kmem_cache_free(sc, nsh);
1484 }
1485 kmem_cache_destroy(sc);
1486 return -ENOMEM;
1487 }
1488 /* Step 2 - Must use GFP_NOIO now.
1489 * OK, we have enough stripes, start collecting inactive
1490 * stripes and copying them over
1491 */
1492 list_for_each_entry(nsh, &newstripes, lru) {
1493 spin_lock_irq(&conf->device_lock);
1494 wait_event_lock_irq(conf->wait_for_stripe,
1495 !list_empty(&conf->inactive_list),
1496 conf->device_lock,
NeilBrown482c0832011-04-18 18:25:42 +10001497 );
NeilBrownad01c9e2006-03-27 01:18:07 -08001498 osh = get_free_stripe(conf);
1499 spin_unlock_irq(&conf->device_lock);
1500 atomic_set(&nsh->count, 1);
1501 for(i=0; i<conf->pool_size; i++)
1502 nsh->dev[i].page = osh->dev[i].page;
1503 for( ; i<newsize; i++)
1504 nsh->dev[i].page = NULL;
1505 kmem_cache_free(conf->slab_cache, osh);
1506 }
1507 kmem_cache_destroy(conf->slab_cache);
1508
1509 /* Step 3.
1510 * At this point, we are holding all the stripes so the array
1511 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001512 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001513 */
1514 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1515 if (ndisks) {
1516 for (i=0; i<conf->raid_disks; i++)
1517 ndisks[i] = conf->disks[i];
1518 kfree(conf->disks);
1519 conf->disks = ndisks;
1520 } else
1521 err = -ENOMEM;
1522
Dan Williamsd6f38f32009-07-14 11:50:52 -07001523 get_online_cpus();
1524 conf->scribble_len = scribble_len(newsize);
1525 for_each_present_cpu(cpu) {
1526 struct raid5_percpu *percpu;
1527 void *scribble;
1528
1529 percpu = per_cpu_ptr(conf->percpu, cpu);
1530 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1531
1532 if (scribble) {
1533 kfree(percpu->scribble);
1534 percpu->scribble = scribble;
1535 } else {
1536 err = -ENOMEM;
1537 break;
1538 }
1539 }
1540 put_online_cpus();
1541
NeilBrownad01c9e2006-03-27 01:18:07 -08001542 /* Step 4, return new stripes to service */
1543 while(!list_empty(&newstripes)) {
1544 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1545 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001546
NeilBrownad01c9e2006-03-27 01:18:07 -08001547 for (i=conf->raid_disks; i < newsize; i++)
1548 if (nsh->dev[i].page == NULL) {
1549 struct page *p = alloc_page(GFP_NOIO);
1550 nsh->dev[i].page = p;
1551 if (!p)
1552 err = -ENOMEM;
1553 }
1554 release_stripe(nsh);
1555 }
1556 /* critical section pass, GFP_NOIO no longer needed */
1557
1558 conf->slab_cache = sc;
1559 conf->active_name = 1-conf->active_name;
1560 conf->pool_size = newsize;
1561 return err;
1562}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
NeilBrownd1688a62011-10-11 16:49:52 +11001564static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565{
1566 struct stripe_head *sh;
1567
NeilBrown3f294f42005-11-08 21:39:25 -08001568 spin_lock_irq(&conf->device_lock);
1569 sh = get_free_stripe(conf);
1570 spin_unlock_irq(&conf->device_lock);
1571 if (!sh)
1572 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001573 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001574 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001575 kmem_cache_free(conf->slab_cache, sh);
1576 atomic_dec(&conf->active_stripes);
1577 return 1;
1578}
1579
NeilBrownd1688a62011-10-11 16:49:52 +11001580static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001581{
1582 while (drop_one_stripe(conf))
1583 ;
1584
NeilBrown29fc7e32006-02-03 03:03:41 -08001585 if (conf->slab_cache)
1586 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 conf->slab_cache = NULL;
1588}
1589
NeilBrown6712ecf2007-09-27 12:47:43 +02001590static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
NeilBrown99c0fb52009-03-31 14:39:38 +11001592 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001593 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001594 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001596 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11001597 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
1600 for (i=0 ; i<disks; i++)
1601 if (bi == &sh->dev[i].req)
1602 break;
1603
Dan Williams45b42332007-07-09 11:56:43 -07001604 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1605 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 uptodate);
1607 if (i == disks) {
1608 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001609 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 }
1611
1612 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001614 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrownd6950432006-07-10 04:44:20 -07001615 rdev = conf->disks[i].rdev;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001616 printk_ratelimited(
1617 KERN_INFO
1618 "md/raid:%s: read error corrected"
1619 " (%lu sectors at %llu on %s)\n",
1620 mdname(conf->mddev), STRIPE_SECTORS,
1621 (unsigned long long)(sh->sector
1622 + rdev->data_offset),
1623 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001624 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001625 clear_bit(R5_ReadError, &sh->dev[i].flags);
1626 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1627 }
NeilBrownba22dcb2005-11-08 21:39:31 -08001628 if (atomic_read(&conf->disks[i].rdev->read_errors))
1629 atomic_set(&conf->disks[i].rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 } else {
NeilBrownd6950432006-07-10 04:44:20 -07001631 const char *bdn = bdevname(conf->disks[i].rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001632 int retry = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001633 rdev = conf->disks[i].rdev;
1634
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001636 atomic_inc(&rdev->read_errors);
Gabriele A. Trombetti7b0bb532010-04-28 11:51:17 +10001637 if (conf->mddev->degraded >= conf->max_degraded)
Christian Dietrich8bda4702011-07-27 11:00:36 +10001638 printk_ratelimited(
1639 KERN_WARNING
1640 "md/raid:%s: read error not correctable "
1641 "(sector %llu on %s).\n",
1642 mdname(conf->mddev),
1643 (unsigned long long)(sh->sector
1644 + rdev->data_offset),
1645 bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001646 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
NeilBrown4e5314b2005-11-08 21:39:22 -08001647 /* Oh, no!!! */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001648 printk_ratelimited(
1649 KERN_WARNING
1650 "md/raid:%s: read error NOT corrected!! "
1651 "(sector %llu on %s).\n",
1652 mdname(conf->mddev),
1653 (unsigned long long)(sh->sector
1654 + rdev->data_offset),
1655 bdn);
NeilBrownd6950432006-07-10 04:44:20 -07001656 else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001657 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001658 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001659 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001660 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001661 else
1662 retry = 1;
1663 if (retry)
1664 set_bit(R5_ReadError, &sh->dev[i].flags);
1665 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001666 clear_bit(R5_ReadError, &sh->dev[i].flags);
1667 clear_bit(R5_ReWrite, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001668 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 }
1671 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1673 set_bit(STRIPE_HANDLE, &sh->state);
1674 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675}
1676
NeilBrownd710e132008-10-13 11:55:12 +11001677static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
NeilBrown99c0fb52009-03-31 14:39:38 +11001679 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001680 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001681 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001683 sector_t first_bad;
1684 int bad_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 for (i=0 ; i<disks; i++)
1687 if (bi == &sh->dev[i].req)
1688 break;
1689
Dan Williams45b42332007-07-09 11:56:43 -07001690 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1692 uptodate);
1693 if (i == disks) {
1694 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001695 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 }
1697
NeilBrownbc2607f2011-07-28 11:39:22 +10001698 if (!uptodate) {
1699 set_bit(WriteErrorSeen, &conf->disks[i].rdev->flags);
1700 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrownb84db562011-07-28 11:39:23 +10001701 } else if (is_badblock(conf->disks[i].rdev, sh->sector, STRIPE_SECTORS,
1702 &first_bad, &bad_sectors))
1703 set_bit(R5_MadeGood, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
1705 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
1706
1707 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1708 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001709 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710}
1711
1712
NeilBrown784052e2009-03-31 15:19:07 +11001713static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
NeilBrown784052e2009-03-31 15:19:07 +11001715static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716{
1717 struct r5dev *dev = &sh->dev[i];
1718
1719 bio_init(&dev->req);
1720 dev->req.bi_io_vec = &dev->vec;
1721 dev->req.bi_vcnt++;
1722 dev->req.bi_max_vecs++;
1723 dev->vec.bv_page = dev->page;
1724 dev->vec.bv_len = STRIPE_SIZE;
1725 dev->vec.bv_offset = 0;
1726
1727 dev->req.bi_sector = sh->sector;
1728 dev->req.bi_private = sh;
1729
1730 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001731 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732}
1733
NeilBrownfd01b882011-10-11 16:47:53 +11001734static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
1736 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11001737 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11001738 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10001739 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
NeilBrown908f4fb2011-12-23 10:17:50 +11001741 spin_lock_irqsave(&conf->device_lock, flags);
1742 clear_bit(In_sync, &rdev->flags);
1743 mddev->degraded = calc_degraded(conf);
1744 spin_unlock_irqrestore(&conf->device_lock, flags);
1745 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1746
NeilBrownde393cd2011-07-28 11:31:48 +10001747 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001748 set_bit(Faulty, &rdev->flags);
1749 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1750 printk(KERN_ALERT
1751 "md/raid:%s: Disk failure on %s, disabling device.\n"
1752 "md/raid:%s: Operation continuing on %d devices.\n",
1753 mdname(mddev),
1754 bdevname(rdev->bdev, b),
1755 mdname(mddev),
1756 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001757}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
1759/*
1760 * Input: a 'big' sector number,
1761 * Output: index of the data and parity disk, and the sector # in them.
1762 */
NeilBrownd1688a62011-10-11 16:49:52 +11001763static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001764 int previous, int *dd_idx,
1765 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001767 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001768 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001770 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001771 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001773 int algorithm = previous ? conf->prev_algo
1774 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001775 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1776 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001777 int raid_disks = previous ? conf->previous_raid_disks
1778 : conf->raid_disks;
1779 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
1781 /* First compute the information on this sector */
1782
1783 /*
1784 * Compute the chunk number and the sector offset inside the chunk
1785 */
1786 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1787 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
1789 /*
1790 * Compute the stripe number
1791 */
NeilBrown35f2a592010-04-20 14:13:34 +10001792 stripe = chunk_number;
1793 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10001794 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 /*
1796 * Select the parity disk based on the user selected algorithm.
1797 */
NeilBrown84789552011-07-27 11:00:36 +10001798 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07001799 switch(conf->level) {
1800 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001801 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001802 break;
1803 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001804 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001806 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001807 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 (*dd_idx)++;
1809 break;
1810 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001811 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001812 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 (*dd_idx)++;
1814 break;
1815 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001816 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001817 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 break;
1819 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001820 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001821 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001823 case ALGORITHM_PARITY_0:
1824 pd_idx = 0;
1825 (*dd_idx)++;
1826 break;
1827 case ALGORITHM_PARITY_N:
1828 pd_idx = data_disks;
1829 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11001831 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001832 }
1833 break;
1834 case 6:
1835
NeilBrowne183eae2009-03-31 15:20:22 +11001836 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001837 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001838 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001839 qd_idx = pd_idx + 1;
1840 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001841 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001842 qd_idx = 0;
1843 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001844 (*dd_idx) += 2; /* D D P Q D */
1845 break;
1846 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001847 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001848 qd_idx = pd_idx + 1;
1849 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001850 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001851 qd_idx = 0;
1852 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001853 (*dd_idx) += 2; /* D D P Q D */
1854 break;
1855 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001856 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001857 qd_idx = (pd_idx + 1) % raid_disks;
1858 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001859 break;
1860 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001861 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001862 qd_idx = (pd_idx + 1) % raid_disks;
1863 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001864 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001865
1866 case ALGORITHM_PARITY_0:
1867 pd_idx = 0;
1868 qd_idx = 1;
1869 (*dd_idx) += 2;
1870 break;
1871 case ALGORITHM_PARITY_N:
1872 pd_idx = data_disks;
1873 qd_idx = data_disks + 1;
1874 break;
1875
1876 case ALGORITHM_ROTATING_ZERO_RESTART:
1877 /* Exactly the same as RIGHT_ASYMMETRIC, but or
1878 * of blocks for computing Q is different.
1879 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001880 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11001881 qd_idx = pd_idx + 1;
1882 if (pd_idx == raid_disks-1) {
1883 (*dd_idx)++; /* Q D D D P */
1884 qd_idx = 0;
1885 } else if (*dd_idx >= pd_idx)
1886 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001887 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001888 break;
1889
1890 case ALGORITHM_ROTATING_N_RESTART:
1891 /* Same a left_asymmetric, by first stripe is
1892 * D D D P Q rather than
1893 * Q D D D P
1894 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001895 stripe2 += 1;
1896 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11001897 qd_idx = pd_idx + 1;
1898 if (pd_idx == raid_disks-1) {
1899 (*dd_idx)++; /* Q D D D P */
1900 qd_idx = 0;
1901 } else if (*dd_idx >= pd_idx)
1902 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001903 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001904 break;
1905
1906 case ALGORITHM_ROTATING_N_CONTINUE:
1907 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001908 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11001909 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
1910 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11001911 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001912 break;
1913
1914 case ALGORITHM_LEFT_ASYMMETRIC_6:
1915 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001916 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11001917 if (*dd_idx >= pd_idx)
1918 (*dd_idx)++;
1919 qd_idx = raid_disks - 1;
1920 break;
1921
1922 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001923 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11001924 if (*dd_idx >= pd_idx)
1925 (*dd_idx)++;
1926 qd_idx = raid_disks - 1;
1927 break;
1928
1929 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001930 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11001931 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1932 qd_idx = raid_disks - 1;
1933 break;
1934
1935 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001936 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11001937 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1938 qd_idx = raid_disks - 1;
1939 break;
1940
1941 case ALGORITHM_PARITY_0_6:
1942 pd_idx = 0;
1943 (*dd_idx)++;
1944 qd_idx = raid_disks - 1;
1945 break;
1946
NeilBrown16a53ec2006-06-26 00:27:38 -07001947 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11001948 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001949 }
1950 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 }
1952
NeilBrown911d4ee2009-03-31 14:39:38 +11001953 if (sh) {
1954 sh->pd_idx = pd_idx;
1955 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001956 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11001957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 /*
1959 * Finally, compute the new sector number
1960 */
1961 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
1962 return new_sector;
1963}
1964
1965
NeilBrown784052e2009-03-31 15:19:07 +11001966static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967{
NeilBrownd1688a62011-10-11 16:49:52 +11001968 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08001969 int raid_disks = sh->disks;
1970 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001972 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1973 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11001974 int algorithm = previous ? conf->prev_algo
1975 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 sector_t stripe;
1977 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10001978 sector_t chunk_number;
1979 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11001981 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
NeilBrown16a53ec2006-06-26 00:27:38 -07001983
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 chunk_offset = sector_div(new_sector, sectors_per_chunk);
1985 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
NeilBrown16a53ec2006-06-26 00:27:38 -07001987 if (i == sh->pd_idx)
1988 return 0;
1989 switch(conf->level) {
1990 case 4: break;
1991 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001992 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 case ALGORITHM_LEFT_ASYMMETRIC:
1994 case ALGORITHM_RIGHT_ASYMMETRIC:
1995 if (i > sh->pd_idx)
1996 i--;
1997 break;
1998 case ALGORITHM_LEFT_SYMMETRIC:
1999 case ALGORITHM_RIGHT_SYMMETRIC:
2000 if (i < sh->pd_idx)
2001 i += raid_disks;
2002 i -= (sh->pd_idx + 1);
2003 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002004 case ALGORITHM_PARITY_0:
2005 i -= 1;
2006 break;
2007 case ALGORITHM_PARITY_N:
2008 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002010 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002011 }
2012 break;
2013 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002014 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002015 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002016 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002017 case ALGORITHM_LEFT_ASYMMETRIC:
2018 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002019 case ALGORITHM_ROTATING_ZERO_RESTART:
2020 case ALGORITHM_ROTATING_N_RESTART:
2021 if (sh->pd_idx == raid_disks-1)
2022 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002023 else if (i > sh->pd_idx)
2024 i -= 2; /* D D P Q D */
2025 break;
2026 case ALGORITHM_LEFT_SYMMETRIC:
2027 case ALGORITHM_RIGHT_SYMMETRIC:
2028 if (sh->pd_idx == raid_disks-1)
2029 i--; /* Q D D D P */
2030 else {
2031 /* D D P Q D */
2032 if (i < sh->pd_idx)
2033 i += raid_disks;
2034 i -= (sh->pd_idx + 2);
2035 }
2036 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002037 case ALGORITHM_PARITY_0:
2038 i -= 2;
2039 break;
2040 case ALGORITHM_PARITY_N:
2041 break;
2042 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002043 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002044 if (sh->pd_idx == 0)
2045 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002046 else {
2047 /* D D Q P D */
2048 if (i < sh->pd_idx)
2049 i += raid_disks;
2050 i -= (sh->pd_idx + 1);
2051 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002052 break;
2053 case ALGORITHM_LEFT_ASYMMETRIC_6:
2054 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2055 if (i > sh->pd_idx)
2056 i--;
2057 break;
2058 case ALGORITHM_LEFT_SYMMETRIC_6:
2059 case ALGORITHM_RIGHT_SYMMETRIC_6:
2060 if (i < sh->pd_idx)
2061 i += data_disks + 1;
2062 i -= (sh->pd_idx + 1);
2063 break;
2064 case ALGORITHM_PARITY_0_6:
2065 i -= 1;
2066 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002067 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002068 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002069 }
2070 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 }
2072
2073 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002074 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
NeilBrown112bf892009-03-31 14:39:38 +11002076 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002077 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002078 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2079 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002080 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2081 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 return 0;
2083 }
2084 return r_sector;
2085}
2086
2087
Dan Williams600aa102008-06-28 08:32:05 +10002088static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002089schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002090 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002091{
2092 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002093 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002094 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002095
Dan Williamse33129d2007-01-02 13:52:30 -07002096 if (rcw) {
2097 /* if we are not expanding this is a proper write request, and
2098 * there will be bios with new data to be drained into the
2099 * stripe cache
2100 */
2101 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10002102 sh->reconstruct_state = reconstruct_state_drain_run;
2103 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2104 } else
2105 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07002106
Dan Williamsac6b53b2009-07-14 13:40:19 -07002107 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002108
2109 for (i = disks; i--; ) {
2110 struct r5dev *dev = &sh->dev[i];
2111
2112 if (dev->towrite) {
2113 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002114 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002115 if (!expand)
2116 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002117 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002118 }
2119 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002120 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002121 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002122 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002123 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002124 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002125 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2126 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2127
Dan Williamsd8ee0722008-06-28 08:32:06 +10002128 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002129 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2130 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002131 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002132
2133 for (i = disks; i--; ) {
2134 struct r5dev *dev = &sh->dev[i];
2135 if (i == pd_idx)
2136 continue;
2137
Dan Williamse33129d2007-01-02 13:52:30 -07002138 if (dev->towrite &&
2139 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002140 test_bit(R5_Wantcompute, &dev->flags))) {
2141 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002142 set_bit(R5_LOCKED, &dev->flags);
2143 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002144 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002145 }
2146 }
2147 }
2148
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002149 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002150 * are in flight
2151 */
2152 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2153 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002154 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002155
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002156 if (level == 6) {
2157 int qd_idx = sh->qd_idx;
2158 struct r5dev *dev = &sh->dev[qd_idx];
2159
2160 set_bit(R5_LOCKED, &dev->flags);
2161 clear_bit(R5_UPTODATE, &dev->flags);
2162 s->locked++;
2163 }
2164
Dan Williams600aa102008-06-28 08:32:05 +10002165 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002166 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002167 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002168}
NeilBrown16a53ec2006-06-26 00:27:38 -07002169
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170/*
2171 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002172 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 * The bi_next chain must be in order.
2174 */
2175static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2176{
2177 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002178 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002179 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
NeilBrowncbe47ec2011-07-26 11:20:35 +10002181 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 (unsigned long long)bi->bi_sector,
2183 (unsigned long long)sh->sector);
2184
2185
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07002187 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -07002189 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
2190 firstwrite = 1;
2191 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 bip = &sh->dev[dd_idx].toread;
2193 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2194 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2195 goto overlap;
2196 bip = & (*bip)->bi_next;
2197 }
2198 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2199 goto overlap;
2200
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002201 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 if (*bip)
2203 bi->bi_next = *bip;
2204 *bip = bi;
Jens Axboe960e7392008-08-15 10:41:18 +02002205 bi->bi_phys_segments++;
NeilBrown72626682005-09-09 16:23:54 -07002206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 if (forwrite) {
2208 /* check if page is covered */
2209 sector_t sector = sh->dev[dd_idx].sector;
2210 for (bi=sh->dev[dd_idx].towrite;
2211 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2212 bi && bi->bi_sector <= sector;
2213 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2214 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2215 sector = bi->bi_sector + (bi->bi_size>>9);
2216 }
2217 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2218 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2219 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002220 spin_unlock_irq(&conf->device_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002221
2222 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2223 (unsigned long long)(*bip)->bi_sector,
2224 (unsigned long long)sh->sector, dd_idx);
2225
2226 if (conf->mddev->bitmap && firstwrite) {
2227 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2228 STRIPE_SECTORS, 0);
2229 sh->bm_seq = conf->seq_flush+1;
2230 set_bit(STRIPE_BIT_DELAY, &sh->state);
2231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 return 1;
2233
2234 overlap:
2235 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
2236 spin_unlock_irq(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 return 0;
2238}
2239
NeilBrownd1688a62011-10-11 16:49:52 +11002240static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002241
NeilBrownd1688a62011-10-11 16:49:52 +11002242static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002243 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002244{
NeilBrown784052e2009-03-31 15:19:07 +11002245 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002246 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002247 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002248 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002249 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002250
NeilBrown112bf892009-03-31 14:39:38 +11002251 raid5_compute_sector(conf,
2252 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002253 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002254 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002255 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002256}
2257
Dan Williamsa4456852007-07-09 11:56:43 -07002258static void
NeilBrownd1688a62011-10-11 16:49:52 +11002259handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002260 struct stripe_head_state *s, int disks,
2261 struct bio **return_bi)
2262{
2263 int i;
2264 for (i = disks; i--; ) {
2265 struct bio *bi;
2266 int bitmap_end = 0;
2267
2268 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002269 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002270 rcu_read_lock();
2271 rdev = rcu_dereference(conf->disks[i].rdev);
2272 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002273 atomic_inc(&rdev->nr_pending);
2274 else
2275 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002276 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002277 if (rdev) {
2278 if (!rdev_set_badblocks(
2279 rdev,
2280 sh->sector,
2281 STRIPE_SECTORS, 0))
2282 md_error(conf->mddev, rdev);
2283 rdev_dec_pending(rdev, conf->mddev);
2284 }
Dan Williamsa4456852007-07-09 11:56:43 -07002285 }
2286 spin_lock_irq(&conf->device_lock);
2287 /* fail all writes first */
2288 bi = sh->dev[i].towrite;
2289 sh->dev[i].towrite = NULL;
2290 if (bi) {
2291 s->to_write--;
2292 bitmap_end = 1;
2293 }
2294
2295 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2296 wake_up(&conf->wait_for_overlap);
2297
2298 while (bi && bi->bi_sector <
2299 sh->dev[i].sector + STRIPE_SECTORS) {
2300 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2301 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002302 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002303 md_write_end(conf->mddev);
2304 bi->bi_next = *return_bi;
2305 *return_bi = bi;
2306 }
2307 bi = nextbi;
2308 }
2309 /* and fail all 'written' */
2310 bi = sh->dev[i].written;
2311 sh->dev[i].written = NULL;
2312 if (bi) bitmap_end = 1;
2313 while (bi && bi->bi_sector <
2314 sh->dev[i].sector + STRIPE_SECTORS) {
2315 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2316 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002317 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002318 md_write_end(conf->mddev);
2319 bi->bi_next = *return_bi;
2320 *return_bi = bi;
2321 }
2322 bi = bi2;
2323 }
2324
Dan Williamsb5e98d62007-01-02 13:52:31 -07002325 /* fail any reads if this device is non-operational and
2326 * the data has not reached the cache yet.
2327 */
2328 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2329 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2330 test_bit(R5_ReadError, &sh->dev[i].flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002331 bi = sh->dev[i].toread;
2332 sh->dev[i].toread = NULL;
2333 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2334 wake_up(&conf->wait_for_overlap);
2335 if (bi) s->to_read--;
2336 while (bi && bi->bi_sector <
2337 sh->dev[i].sector + STRIPE_SECTORS) {
2338 struct bio *nextbi =
2339 r5_next_bio(bi, sh->dev[i].sector);
2340 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002341 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002342 bi->bi_next = *return_bi;
2343 *return_bi = bi;
2344 }
2345 bi = nextbi;
2346 }
2347 }
2348 spin_unlock_irq(&conf->device_lock);
2349 if (bitmap_end)
2350 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2351 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002352 /* If we were in the middle of a write the parity block might
2353 * still be locked - so just clear all R5_LOCKED flags
2354 */
2355 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002356 }
2357
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002358 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2359 if (atomic_dec_and_test(&conf->pending_full_writes))
2360 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002361}
2362
NeilBrown7f0da592011-07-28 11:39:22 +10002363static void
NeilBrownd1688a62011-10-11 16:49:52 +11002364handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002365 struct stripe_head_state *s)
2366{
2367 int abort = 0;
2368 int i;
2369
2370 md_done_sync(conf->mddev, STRIPE_SECTORS, 0);
2371 clear_bit(STRIPE_SYNCING, &sh->state);
2372 s->syncing = 0;
2373 /* There is nothing more to do for sync/check/repair.
2374 * For recover we need to record a bad block on all
2375 * non-sync devices, or abort the recovery
2376 */
2377 if (!test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery))
2378 return;
2379 /* During recovery devices cannot be removed, so locking and
2380 * refcounting of rdevs is not needed
2381 */
2382 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +11002383 struct md_rdev *rdev = conf->disks[i].rdev;
NeilBrown7f0da592011-07-28 11:39:22 +10002384 if (!rdev
2385 || test_bit(Faulty, &rdev->flags)
2386 || test_bit(In_sync, &rdev->flags))
2387 continue;
2388 if (!rdev_set_badblocks(rdev, sh->sector,
2389 STRIPE_SECTORS, 0))
2390 abort = 1;
2391 }
2392 if (abort) {
2393 conf->recovery_disabled = conf->mddev->recovery_disabled;
2394 set_bit(MD_RECOVERY_INTR, &conf->mddev->recovery);
2395 }
2396}
2397
NeilBrown93b3dbc2011-07-27 11:00:36 +10002398/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002399 * to be read or computed to satisfy a request.
2400 *
2401 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002402 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002403 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002404static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2405 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002406{
2407 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002408 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2409 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002410
Dan Williamsf38e1212007-01-02 13:52:30 -07002411 /* is the data in this block needed, and can we get it? */
2412 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002413 !test_bit(R5_UPTODATE, &dev->flags) &&
2414 (dev->toread ||
2415 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2416 s->syncing || s->expanding ||
NeilBrown5d35e092011-07-27 11:00:36 +10002417 (s->failed >= 1 && fdev[0]->toread) ||
2418 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002419 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2420 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2421 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002422 /* we would like to get this block, possibly by computing it,
2423 * otherwise read it if the backing disk is insync
2424 */
2425 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2426 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2427 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002428 (s->failed && (disk_idx == s->failed_num[0] ||
2429 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002430 /* have disk failed, and we're requested to fetch it;
2431 * do compute it
2432 */
2433 pr_debug("Computing stripe %llu block %d\n",
2434 (unsigned long long)sh->sector, disk_idx);
2435 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2436 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2437 set_bit(R5_Wantcompute, &dev->flags);
2438 sh->ops.target = disk_idx;
2439 sh->ops.target2 = -1; /* no 2nd target */
2440 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002441 /* Careful: from this point on 'uptodate' is in the eye
2442 * of raid_run_ops which services 'compute' operations
2443 * before writes. R5_Wantcompute flags a block that will
2444 * be R5_UPTODATE by the time it is needed for a
2445 * subsequent operation.
2446 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002447 s->uptodate++;
2448 return 1;
2449 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2450 /* Computing 2-failure is *very* expensive; only
2451 * do it if failed >= 2
2452 */
2453 int other;
2454 for (other = disks; other--; ) {
2455 if (other == disk_idx)
2456 continue;
2457 if (!test_bit(R5_UPTODATE,
2458 &sh->dev[other].flags))
2459 break;
2460 }
2461 BUG_ON(other < 0);
2462 pr_debug("Computing stripe %llu blocks %d,%d\n",
2463 (unsigned long long)sh->sector,
2464 disk_idx, other);
2465 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2466 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2467 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2468 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2469 sh->ops.target = disk_idx;
2470 sh->ops.target2 = other;
2471 s->uptodate += 2;
2472 s->req_compute = 1;
2473 return 1;
2474 } else if (test_bit(R5_Insync, &dev->flags)) {
2475 set_bit(R5_LOCKED, &dev->flags);
2476 set_bit(R5_Wantread, &dev->flags);
2477 s->locked++;
2478 pr_debug("Reading block %d (sync=%d)\n",
2479 disk_idx, s->syncing);
2480 }
2481 }
2482
2483 return 0;
2484}
2485
2486/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002487 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002488 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002489static void handle_stripe_fill(struct stripe_head *sh,
2490 struct stripe_head_state *s,
2491 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002492{
2493 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002494
2495 /* look for blocks to read/compute, skip this if a compute
2496 * is already in flight, or if the stripe contents are in the
2497 * midst of changing due to a write
2498 */
2499 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2500 !sh->reconstruct_state)
2501 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002502 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002503 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002504 set_bit(STRIPE_HANDLE, &sh->state);
2505}
2506
2507
Dan Williams1fe797e2008-06-28 09:16:30 +10002508/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002509 * any written block on an uptodate or failed drive can be returned.
2510 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2511 * never LOCKED, so we don't need to test 'failed' directly.
2512 */
NeilBrownd1688a62011-10-11 16:49:52 +11002513static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002514 struct stripe_head *sh, int disks, struct bio **return_bi)
2515{
2516 int i;
2517 struct r5dev *dev;
2518
2519 for (i = disks; i--; )
2520 if (sh->dev[i].written) {
2521 dev = &sh->dev[i];
2522 if (!test_bit(R5_LOCKED, &dev->flags) &&
2523 test_bit(R5_UPTODATE, &dev->flags)) {
2524 /* We can return any write requests */
2525 struct bio *wbi, *wbi2;
2526 int bitmap_end = 0;
Dan Williams45b42332007-07-09 11:56:43 -07002527 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002528 spin_lock_irq(&conf->device_lock);
2529 wbi = dev->written;
2530 dev->written = NULL;
2531 while (wbi && wbi->bi_sector <
2532 dev->sector + STRIPE_SECTORS) {
2533 wbi2 = r5_next_bio(wbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +02002534 if (!raid5_dec_bi_phys_segments(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002535 md_write_end(conf->mddev);
2536 wbi->bi_next = *return_bi;
2537 *return_bi = wbi;
2538 }
2539 wbi = wbi2;
2540 }
2541 if (dev->towrite == NULL)
2542 bitmap_end = 1;
2543 spin_unlock_irq(&conf->device_lock);
2544 if (bitmap_end)
2545 bitmap_endwrite(conf->mddev->bitmap,
2546 sh->sector,
2547 STRIPE_SECTORS,
2548 !test_bit(STRIPE_DEGRADED, &sh->state),
2549 0);
2550 }
2551 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002552
2553 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2554 if (atomic_dec_and_test(&conf->pending_full_writes))
2555 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002556}
2557
NeilBrownd1688a62011-10-11 16:49:52 +11002558static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002559 struct stripe_head *sh,
2560 struct stripe_head_state *s,
2561 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002562{
2563 int rmw = 0, rcw = 0, i;
NeilBrownc8ac1802011-07-27 11:00:36 +10002564 if (conf->max_degraded == 2) {
2565 /* RAID6 requires 'rcw' in current implementation
2566 * Calculate the real rcw later - for now fake it
2567 * look like rcw is cheaper
2568 */
2569 rcw = 1; rmw = 2;
2570 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002571 /* would I have to read this buffer for read_modify_write */
2572 struct r5dev *dev = &sh->dev[i];
2573 if ((dev->towrite || i == sh->pd_idx) &&
2574 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002575 !(test_bit(R5_UPTODATE, &dev->flags) ||
2576 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002577 if (test_bit(R5_Insync, &dev->flags))
2578 rmw++;
2579 else
2580 rmw += 2*disks; /* cannot read it */
2581 }
2582 /* Would I have to read this buffer for reconstruct_write */
2583 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2584 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002585 !(test_bit(R5_UPTODATE, &dev->flags) ||
2586 test_bit(R5_Wantcompute, &dev->flags))) {
2587 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002588 else
2589 rcw += 2*disks;
2590 }
2591 }
Dan Williams45b42332007-07-09 11:56:43 -07002592 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002593 (unsigned long long)sh->sector, rmw, rcw);
2594 set_bit(STRIPE_HANDLE, &sh->state);
2595 if (rmw < rcw && rmw > 0)
2596 /* prefer read-modify-write, but need to get some data */
2597 for (i = disks; i--; ) {
2598 struct r5dev *dev = &sh->dev[i];
2599 if ((dev->towrite || i == sh->pd_idx) &&
2600 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002601 !(test_bit(R5_UPTODATE, &dev->flags) ||
2602 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002603 test_bit(R5_Insync, &dev->flags)) {
2604 if (
2605 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002606 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002607 "%d for r-m-w\n", i);
2608 set_bit(R5_LOCKED, &dev->flags);
2609 set_bit(R5_Wantread, &dev->flags);
2610 s->locked++;
2611 } else {
2612 set_bit(STRIPE_DELAYED, &sh->state);
2613 set_bit(STRIPE_HANDLE, &sh->state);
2614 }
2615 }
2616 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002617 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002618 /* want reconstruct write, but need to get some data */
NeilBrownc8ac1802011-07-27 11:00:36 +10002619 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002620 for (i = disks; i--; ) {
2621 struct r5dev *dev = &sh->dev[i];
2622 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002623 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002624 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002625 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002626 test_bit(R5_Wantcompute, &dev->flags))) {
2627 rcw++;
2628 if (!test_bit(R5_Insync, &dev->flags))
2629 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002630 if (
2631 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002632 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002633 "%d for Reconstruct\n", i);
2634 set_bit(R5_LOCKED, &dev->flags);
2635 set_bit(R5_Wantread, &dev->flags);
2636 s->locked++;
2637 } else {
2638 set_bit(STRIPE_DELAYED, &sh->state);
2639 set_bit(STRIPE_HANDLE, &sh->state);
2640 }
2641 }
2642 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002643 }
Dan Williamsa4456852007-07-09 11:56:43 -07002644 /* now if nothing is locked, and if we have enough data,
2645 * we can start a write request
2646 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002647 /* since handle_stripe can be called at any time we need to handle the
2648 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002649 * subsequent call wants to start a write request. raid_run_ops only
2650 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002651 * simultaneously. If this is not the case then new writes need to be
2652 * held off until the compute completes.
2653 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002654 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2655 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2656 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002657 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002658}
2659
NeilBrownd1688a62011-10-11 16:49:52 +11002660static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002661 struct stripe_head_state *s, int disks)
2662{
Dan Williamsecc65c92008-06-28 08:31:57 +10002663 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002664
Dan Williamsbd2ab672008-04-10 21:29:27 -07002665 set_bit(STRIPE_HANDLE, &sh->state);
2666
Dan Williamsecc65c92008-06-28 08:31:57 +10002667 switch (sh->check_state) {
2668 case check_state_idle:
2669 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002670 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002671 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002672 sh->check_state = check_state_run;
2673 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002674 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002675 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002676 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002677 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002678 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002679 /* fall through */
2680 case check_state_compute_result:
2681 sh->check_state = check_state_idle;
2682 if (!dev)
2683 dev = &sh->dev[sh->pd_idx];
2684
2685 /* check that a write has not made the stripe insync */
2686 if (test_bit(STRIPE_INSYNC, &sh->state))
2687 break;
2688
2689 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002690 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2691 BUG_ON(s->uptodate != disks);
2692
2693 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002694 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002695 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002696
Dan Williamsa4456852007-07-09 11:56:43 -07002697 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002698 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002699 break;
2700 case check_state_run:
2701 break; /* we will be called again upon completion */
2702 case check_state_check_result:
2703 sh->check_state = check_state_idle;
2704
2705 /* if a failure occurred during the check operation, leave
2706 * STRIPE_INSYNC not set and let the stripe be handled again
2707 */
2708 if (s->failed)
2709 break;
2710
2711 /* handle a successful check operation, if parity is correct
2712 * we are done. Otherwise update the mismatch count and repair
2713 * parity if !MD_RECOVERY_CHECK
2714 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002715 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002716 /* parity is correct (on disc,
2717 * not in buffer any more)
2718 */
2719 set_bit(STRIPE_INSYNC, &sh->state);
2720 else {
2721 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2722 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2723 /* don't try to repair!! */
2724 set_bit(STRIPE_INSYNC, &sh->state);
2725 else {
2726 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002727 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002728 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2729 set_bit(R5_Wantcompute,
2730 &sh->dev[sh->pd_idx].flags);
2731 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002732 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10002733 s->uptodate++;
2734 }
2735 }
2736 break;
2737 case check_state_compute_run:
2738 break;
2739 default:
2740 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2741 __func__, sh->check_state,
2742 (unsigned long long) sh->sector);
2743 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002744 }
2745}
2746
2747
NeilBrownd1688a62011-10-11 16:49:52 +11002748static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07002749 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10002750 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002751{
Dan Williamsa4456852007-07-09 11:56:43 -07002752 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11002753 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07002754 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07002755
2756 set_bit(STRIPE_HANDLE, &sh->state);
2757
2758 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002759
Dan Williamsa4456852007-07-09 11:56:43 -07002760 /* Want to check and possibly repair P and Q.
2761 * However there could be one 'failed' device, in which
2762 * case we can only check one of them, possibly using the
2763 * other to generate missing data
2764 */
2765
Dan Williamsd82dfee2009-07-14 13:40:57 -07002766 switch (sh->check_state) {
2767 case check_state_idle:
2768 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10002769 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002770 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07002771 * makes sense to check P (If anything else were failed,
2772 * we would have used P to recreate it).
2773 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002774 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07002775 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002776 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002777 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07002778 * anything, so it makes sense to check it
2779 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002780 if (sh->check_state == check_state_run)
2781 sh->check_state = check_state_run_pq;
2782 else
2783 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07002784 }
Dan Williams36d1c642009-07-14 11:48:22 -07002785
Dan Williamsd82dfee2009-07-14 13:40:57 -07002786 /* discard potentially stale zero_sum_result */
2787 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07002788
Dan Williamsd82dfee2009-07-14 13:40:57 -07002789 if (sh->check_state == check_state_run) {
2790 /* async_xor_zero_sum destroys the contents of P */
2791 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
2792 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07002793 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002794 if (sh->check_state >= check_state_run &&
2795 sh->check_state <= check_state_run_pq) {
2796 /* async_syndrome_zero_sum preserves P and Q, so
2797 * no need to mark them !uptodate here
2798 */
2799 set_bit(STRIPE_OP_CHECK, &s->ops_request);
2800 break;
2801 }
Dan Williams36d1c642009-07-14 11:48:22 -07002802
Dan Williamsd82dfee2009-07-14 13:40:57 -07002803 /* we have 2-disk failure */
2804 BUG_ON(s->failed != 2);
2805 /* fall through */
2806 case check_state_compute_result:
2807 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07002808
Dan Williamsd82dfee2009-07-14 13:40:57 -07002809 /* check that a write has not made the stripe insync */
2810 if (test_bit(STRIPE_INSYNC, &sh->state))
2811 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002812
2813 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07002814 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07002815 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002816 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07002817 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10002818 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07002819 s->locked++;
2820 set_bit(R5_LOCKED, &dev->flags);
2821 set_bit(R5_Wantwrite, &dev->flags);
2822 }
2823 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10002824 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07002825 s->locked++;
2826 set_bit(R5_LOCKED, &dev->flags);
2827 set_bit(R5_Wantwrite, &dev->flags);
2828 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002829 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002830 dev = &sh->dev[pd_idx];
2831 s->locked++;
2832 set_bit(R5_LOCKED, &dev->flags);
2833 set_bit(R5_Wantwrite, &dev->flags);
2834 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002835 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002836 dev = &sh->dev[qd_idx];
2837 s->locked++;
2838 set_bit(R5_LOCKED, &dev->flags);
2839 set_bit(R5_Wantwrite, &dev->flags);
2840 }
2841 clear_bit(STRIPE_DEGRADED, &sh->state);
2842
2843 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002844 break;
2845 case check_state_run:
2846 case check_state_run_q:
2847 case check_state_run_pq:
2848 break; /* we will be called again upon completion */
2849 case check_state_check_result:
2850 sh->check_state = check_state_idle;
2851
2852 /* handle a successful check operation, if parity is correct
2853 * we are done. Otherwise update the mismatch count and repair
2854 * parity if !MD_RECOVERY_CHECK
2855 */
2856 if (sh->ops.zero_sum_result == 0) {
2857 /* both parities are correct */
2858 if (!s->failed)
2859 set_bit(STRIPE_INSYNC, &sh->state);
2860 else {
2861 /* in contrast to the raid5 case we can validate
2862 * parity, but still have a failure to write
2863 * back
2864 */
2865 sh->check_state = check_state_compute_result;
2866 /* Returning at this point means that we may go
2867 * off and bring p and/or q uptodate again so
2868 * we make sure to check zero_sum_result again
2869 * to verify if p or q need writeback
2870 */
2871 }
2872 } else {
2873 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2874 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2875 /* don't try to repair!! */
2876 set_bit(STRIPE_INSYNC, &sh->state);
2877 else {
2878 int *target = &sh->ops.target;
2879
2880 sh->ops.target = -1;
2881 sh->ops.target2 = -1;
2882 sh->check_state = check_state_compute_run;
2883 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2884 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2885 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
2886 set_bit(R5_Wantcompute,
2887 &sh->dev[pd_idx].flags);
2888 *target = pd_idx;
2889 target = &sh->ops.target2;
2890 s->uptodate++;
2891 }
2892 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
2893 set_bit(R5_Wantcompute,
2894 &sh->dev[qd_idx].flags);
2895 *target = qd_idx;
2896 s->uptodate++;
2897 }
2898 }
2899 }
2900 break;
2901 case check_state_compute_run:
2902 break;
2903 default:
2904 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2905 __func__, sh->check_state,
2906 (unsigned long long) sh->sector);
2907 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002908 }
2909}
2910
NeilBrownd1688a62011-10-11 16:49:52 +11002911static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07002912{
2913 int i;
2914
2915 /* We have read all the blocks in this stripe and now we need to
2916 * copy some of them into a target stripe for expand.
2917 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07002918 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002919 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2920 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11002921 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11002922 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07002923 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07002924 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07002925
NeilBrown784052e2009-03-31 15:19:07 +11002926 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11002927 sector_t s = raid5_compute_sector(conf, bn, 0,
2928 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10002929 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07002930 if (sh2 == NULL)
2931 /* so far only the early blocks of this stripe
2932 * have been requested. When later blocks
2933 * get requested, we will try again
2934 */
2935 continue;
2936 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
2937 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
2938 /* must have already done this block */
2939 release_stripe(sh2);
2940 continue;
2941 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07002942
2943 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07002944 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002945 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07002946 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07002947 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002948
Dan Williamsa4456852007-07-09 11:56:43 -07002949 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
2950 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
2951 for (j = 0; j < conf->raid_disks; j++)
2952 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10002953 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002954 !test_bit(R5_Expanded, &sh2->dev[j].flags))
2955 break;
2956 if (j == conf->raid_disks) {
2957 set_bit(STRIPE_EXPAND_READY, &sh2->state);
2958 set_bit(STRIPE_HANDLE, &sh2->state);
2959 }
2960 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002961
Dan Williamsa4456852007-07-09 11:56:43 -07002962 }
NeilBrowna2e08552007-09-11 15:23:36 -07002963 /* done submitting copies, wait for them to complete */
2964 if (tx) {
2965 async_tx_ack(tx);
2966 dma_wait_for_async_tx(tx);
2967 }
Dan Williamsa4456852007-07-09 11:56:43 -07002968}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
Dan Williams6bfe0b42008-04-30 00:52:32 -07002970
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971/*
2972 * handle_stripe - do things to a stripe.
2973 *
2974 * We lock the stripe and then examine the state of various bits
2975 * to see what needs to be done.
2976 * Possible results:
2977 * return some read request which now have data
2978 * return some write requests which are safely on disc
2979 * schedule a read on some buffers
2980 * schedule a write of some buffers
2981 * return confirmation of parity correctness
2982 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 * buffers are taken off read_list or write_list, and bh_cache buffers
2984 * get BH_Lock set before the stripe lock is released.
2985 *
2986 */
Dan Williamsa4456852007-07-09 11:56:43 -07002987
NeilBrownacfe7262011-07-27 11:00:36 +10002988static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07002989{
NeilBrownd1688a62011-10-11 16:49:52 +11002990 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08002991 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10002992 struct r5dev *dev;
2993 int i;
NeilBrown16a53ec2006-06-26 00:27:38 -07002994
NeilBrownacfe7262011-07-27 11:00:36 +10002995 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07002996
NeilBrownacfe7262011-07-27 11:00:36 +10002997 s->syncing = test_bit(STRIPE_SYNCING, &sh->state);
2998 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2999 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3000 s->failed_num[0] = -1;
3001 s->failed_num[1] = -1;
3002
3003 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003004 rcu_read_lock();
NeilBrownc4c16632011-07-26 11:34:20 +10003005 spin_lock_irq(&conf->device_lock);
NeilBrown16a53ec2006-06-26 00:27:38 -07003006 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003007 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003008 sector_t first_bad;
3009 int bad_sectors;
3010 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003011
NeilBrown16a53ec2006-06-26 00:27:38 -07003012 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003013
Dan Williams45b42332007-07-09 11:56:43 -07003014 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown16a53ec2006-06-26 00:27:38 -07003015 i, dev->flags, dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003016 /* maybe we can reply to a read
3017 *
3018 * new wantfill requests are only permitted while
3019 * ops_complete_biofill is guaranteed to be inactive
3020 */
3021 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3022 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3023 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003024
3025 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003026 if (test_bit(R5_LOCKED, &dev->flags))
3027 s->locked++;
3028 if (test_bit(R5_UPTODATE, &dev->flags))
3029 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003030 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003031 s->compute++;
3032 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003033 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003034
NeilBrownacfe7262011-07-27 11:00:36 +10003035 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003036 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003037 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003038 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003039 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003040 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003041 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003042 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003043 }
Dan Williamsa4456852007-07-09 11:56:43 -07003044 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003045 s->written++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003046 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown9283d8c2011-12-08 16:27:57 +11003047 if (rdev && test_bit(Faulty, &rdev->flags))
3048 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003049 if (rdev) {
3050 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3051 &first_bad, &bad_sectors);
3052 if (s->blocked_rdev == NULL
3053 && (test_bit(Blocked, &rdev->flags)
3054 || is_bad < 0)) {
3055 if (is_bad < 0)
3056 set_bit(BlockedBadBlocks,
3057 &rdev->flags);
3058 s->blocked_rdev = rdev;
3059 atomic_inc(&rdev->nr_pending);
3060 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003061 }
NeilBrown415e72d2010-06-17 17:25:21 +10003062 clear_bit(R5_Insync, &dev->flags);
3063 if (!rdev)
3064 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003065 else if (is_bad) {
3066 /* also not in-sync */
3067 if (!test_bit(WriteErrorSeen, &rdev->flags)) {
3068 /* treat as in-sync, but with a read error
3069 * which we can now try to correct
3070 */
3071 set_bit(R5_Insync, &dev->flags);
3072 set_bit(R5_ReadError, &dev->flags);
3073 }
3074 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003075 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003076 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003077 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003078 set_bit(R5_Insync, &dev->flags);
3079 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3080 test_bit(R5_Expanded, &dev->flags))
3081 /* If we've reshaped into here, we assume it is Insync.
3082 * We will shortly update recovery_offset to make
3083 * it official.
3084 */
3085 set_bit(R5_Insync, &dev->flags);
3086
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003087 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003088 clear_bit(R5_Insync, &dev->flags);
3089 if (!test_bit(Faulty, &rdev->flags)) {
3090 s->handle_bad_blocks = 1;
3091 atomic_inc(&rdev->nr_pending);
3092 } else
3093 clear_bit(R5_WriteError, &dev->flags);
3094 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003095 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003096 if (!test_bit(Faulty, &rdev->flags)) {
3097 s->handle_bad_blocks = 1;
3098 atomic_inc(&rdev->nr_pending);
3099 } else
3100 clear_bit(R5_MadeGood, &dev->flags);
3101 }
NeilBrown415e72d2010-06-17 17:25:21 +10003102 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003103 /* The ReadError flag will just be confusing now */
3104 clear_bit(R5_ReadError, &dev->flags);
3105 clear_bit(R5_ReWrite, &dev->flags);
3106 }
NeilBrown415e72d2010-06-17 17:25:21 +10003107 if (test_bit(R5_ReadError, &dev->flags))
3108 clear_bit(R5_Insync, &dev->flags);
3109 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003110 if (s->failed < 2)
3111 s->failed_num[s->failed] = i;
3112 s->failed++;
NeilBrown415e72d2010-06-17 17:25:21 +10003113 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003114 }
NeilBrownc4c16632011-07-26 11:34:20 +10003115 spin_unlock_irq(&conf->device_lock);
NeilBrown16a53ec2006-06-26 00:27:38 -07003116 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003117}
NeilBrownf4168852007-02-28 20:11:53 -08003118
NeilBrowncc940152011-07-26 11:35:35 +10003119static void handle_stripe(struct stripe_head *sh)
3120{
3121 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003122 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003123 int i;
NeilBrown84789552011-07-27 11:00:36 +10003124 int prexor;
3125 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003126 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003127
3128 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003129 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003130 /* already being handled, ensure it gets handled
3131 * again when current action finishes */
3132 set_bit(STRIPE_HANDLE, &sh->state);
3133 return;
3134 }
3135
3136 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3137 set_bit(STRIPE_SYNCING, &sh->state);
3138 clear_bit(STRIPE_INSYNC, &sh->state);
3139 }
3140 clear_bit(STRIPE_DELAYED, &sh->state);
3141
3142 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3143 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3144 (unsigned long long)sh->sector, sh->state,
3145 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3146 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003147
NeilBrownacfe7262011-07-27 11:00:36 +10003148 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003149
NeilBrownbc2607f2011-07-28 11:39:22 +10003150 if (s.handle_bad_blocks) {
3151 set_bit(STRIPE_HANDLE, &sh->state);
3152 goto finish;
3153 }
3154
NeilBrown474af965fe2011-07-27 11:00:36 +10003155 if (unlikely(s.blocked_rdev)) {
3156 if (s.syncing || s.expanding || s.expanded ||
3157 s.to_write || s.written) {
3158 set_bit(STRIPE_HANDLE, &sh->state);
3159 goto finish;
3160 }
3161 /* There is nothing for the blocked_rdev to block */
3162 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3163 s.blocked_rdev = NULL;
3164 }
3165
3166 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3167 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3168 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3169 }
3170
3171 pr_debug("locked=%d uptodate=%d to_read=%d"
3172 " to_write=%d failed=%d failed_num=%d,%d\n",
3173 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3174 s.failed_num[0], s.failed_num[1]);
3175 /* check if the array has lost more than max_degraded devices and,
3176 * if so, some requests might need to be failed.
3177 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003178 if (s.failed > conf->max_degraded) {
3179 sh->check_state = 0;
3180 sh->reconstruct_state = 0;
3181 if (s.to_read+s.to_write+s.written)
3182 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
3183 if (s.syncing)
3184 handle_failed_sync(conf, sh, &s);
3185 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003186
3187 /*
3188 * might be able to return some write requests if the parity blocks
3189 * are safe, or on a failed drive
3190 */
3191 pdev = &sh->dev[sh->pd_idx];
3192 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3193 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3194 qdev = &sh->dev[sh->qd_idx];
3195 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3196 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3197 || conf->level < 6;
3198
3199 if (s.written &&
3200 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3201 && !test_bit(R5_LOCKED, &pdev->flags)
3202 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3203 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3204 && !test_bit(R5_LOCKED, &qdev->flags)
3205 && test_bit(R5_UPTODATE, &qdev->flags)))))
3206 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3207
3208 /* Now we might consider reading some blocks, either to check/generate
3209 * parity, or to satisfy requests
3210 * or to load a block that is being partially written.
3211 */
3212 if (s.to_read || s.non_overwrite
3213 || (conf->level == 6 && s.to_write && s.failed)
3214 || (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
3215 handle_stripe_fill(sh, &s, disks);
3216
NeilBrown84789552011-07-27 11:00:36 +10003217 /* Now we check to see if any write operations have recently
3218 * completed
3219 */
3220 prexor = 0;
3221 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3222 prexor = 1;
3223 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3224 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3225 sh->reconstruct_state = reconstruct_state_idle;
3226
3227 /* All the 'written' buffers and the parity block are ready to
3228 * be written back to disk
3229 */
3230 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3231 BUG_ON(sh->qd_idx >= 0 &&
3232 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags));
3233 for (i = disks; i--; ) {
3234 struct r5dev *dev = &sh->dev[i];
3235 if (test_bit(R5_LOCKED, &dev->flags) &&
3236 (i == sh->pd_idx || i == sh->qd_idx ||
3237 dev->written)) {
3238 pr_debug("Writing block %d\n", i);
3239 set_bit(R5_Wantwrite, &dev->flags);
3240 if (prexor)
3241 continue;
3242 if (!test_bit(R5_Insync, &dev->flags) ||
3243 ((i == sh->pd_idx || i == sh->qd_idx) &&
3244 s.failed == 0))
3245 set_bit(STRIPE_INSYNC, &sh->state);
3246 }
3247 }
3248 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3249 s.dec_preread_active = 1;
3250 }
3251
3252 /* Now to consider new write requests and what else, if anything
3253 * should be read. We do not handle new writes when:
3254 * 1/ A 'write' operation (copy+xor) is already in flight.
3255 * 2/ A 'check' operation is in flight, as it may clobber the parity
3256 * block.
3257 */
3258 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3259 handle_stripe_dirtying(conf, sh, &s, disks);
3260
3261 /* maybe we need to check and possibly fix the parity for this stripe
3262 * Any reads will already have been scheduled, so we just see if enough
3263 * data is available. The parity check is held off while parity
3264 * dependent operations are in flight.
3265 */
3266 if (sh->check_state ||
3267 (s.syncing && s.locked == 0 &&
3268 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3269 !test_bit(STRIPE_INSYNC, &sh->state))) {
3270 if (conf->level == 6)
3271 handle_parity_checks6(conf, sh, &s, disks);
3272 else
3273 handle_parity_checks5(conf, sh, &s, disks);
3274 }
NeilBrownc5a31002011-07-27 11:00:36 +10003275
3276 if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
3277 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3278 clear_bit(STRIPE_SYNCING, &sh->state);
3279 }
3280
3281 /* If the failed drives are just a ReadError, then we might need
3282 * to progress the repair/check process
3283 */
3284 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3285 for (i = 0; i < s.failed; i++) {
3286 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3287 if (test_bit(R5_ReadError, &dev->flags)
3288 && !test_bit(R5_LOCKED, &dev->flags)
3289 && test_bit(R5_UPTODATE, &dev->flags)
3290 ) {
3291 if (!test_bit(R5_ReWrite, &dev->flags)) {
3292 set_bit(R5_Wantwrite, &dev->flags);
3293 set_bit(R5_ReWrite, &dev->flags);
3294 set_bit(R5_LOCKED, &dev->flags);
3295 s.locked++;
3296 } else {
3297 /* let's read it back */
3298 set_bit(R5_Wantread, &dev->flags);
3299 set_bit(R5_LOCKED, &dev->flags);
3300 s.locked++;
3301 }
3302 }
3303 }
3304
3305
NeilBrown3687c062011-07-27 11:00:36 +10003306 /* Finish reconstruct operations initiated by the expansion process */
3307 if (sh->reconstruct_state == reconstruct_state_result) {
3308 struct stripe_head *sh_src
3309 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3310 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3311 /* sh cannot be written until sh_src has been read.
3312 * so arrange for sh to be delayed a little
3313 */
3314 set_bit(STRIPE_DELAYED, &sh->state);
3315 set_bit(STRIPE_HANDLE, &sh->state);
3316 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3317 &sh_src->state))
3318 atomic_inc(&conf->preread_active_stripes);
3319 release_stripe(sh_src);
3320 goto finish;
3321 }
3322 if (sh_src)
3323 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003324
NeilBrown3687c062011-07-27 11:00:36 +10003325 sh->reconstruct_state = reconstruct_state_idle;
3326 clear_bit(STRIPE_EXPANDING, &sh->state);
3327 for (i = conf->raid_disks; i--; ) {
3328 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3329 set_bit(R5_LOCKED, &sh->dev[i].flags);
3330 s.locked++;
3331 }
3332 }
3333
3334 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3335 !sh->reconstruct_state) {
3336 /* Need to write out all blocks after computing parity */
3337 sh->disks = conf->raid_disks;
3338 stripe_set_idx(sh->sector, conf, 0, sh);
3339 schedule_reconstruction(sh, &s, 1, 1);
3340 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3341 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3342 atomic_dec(&conf->reshape_stripes);
3343 wake_up(&conf->wait_for_overlap);
3344 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3345 }
3346
3347 if (s.expanding && s.locked == 0 &&
3348 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3349 handle_stripe_expansion(conf, sh);
3350
3351finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003352 /* wait for this device to become unblocked */
NeilBrown43220aa2011-08-31 12:49:14 +10003353 if (conf->mddev->external && unlikely(s.blocked_rdev))
NeilBrownc5709ef2011-07-26 11:35:20 +10003354 md_wait_for_blocked_rdev(s.blocked_rdev, conf->mddev);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003355
NeilBrownbc2607f2011-07-28 11:39:22 +10003356 if (s.handle_bad_blocks)
3357 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003358 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003359 struct r5dev *dev = &sh->dev[i];
3360 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3361 /* We own a safe reference to the rdev */
3362 rdev = conf->disks[i].rdev;
3363 if (!rdev_set_badblocks(rdev, sh->sector,
3364 STRIPE_SECTORS, 0))
3365 md_error(conf->mddev, rdev);
3366 rdev_dec_pending(rdev, conf->mddev);
3367 }
NeilBrownb84db562011-07-28 11:39:23 +10003368 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3369 rdev = conf->disks[i].rdev;
3370 rdev_clear_badblocks(rdev, sh->sector,
3371 STRIPE_SECTORS);
3372 rdev_dec_pending(rdev, conf->mddev);
3373 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003374 }
3375
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003376 if (s.ops_request)
3377 raid_run_ops(sh, s.ops_request);
3378
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003379 ops_run_io(sh, &s);
3380
NeilBrownc5709ef2011-07-26 11:35:20 +10003381 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003382 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003383 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003384 * have actually been submitted.
3385 */
3386 atomic_dec(&conf->preread_active_stripes);
3387 if (atomic_read(&conf->preread_active_stripes) <
3388 IO_THRESHOLD)
3389 md_wakeup_thread(conf->mddev->thread);
3390 }
3391
NeilBrownc5709ef2011-07-26 11:35:20 +10003392 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003393
Dan Williams257a4b42011-11-08 16:22:06 +11003394 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003395}
3396
NeilBrownd1688a62011-10-11 16:49:52 +11003397static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398{
3399 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3400 while (!list_empty(&conf->delayed_list)) {
3401 struct list_head *l = conf->delayed_list.next;
3402 struct stripe_head *sh;
3403 sh = list_entry(l, struct stripe_head, lru);
3404 list_del_init(l);
3405 clear_bit(STRIPE_DELAYED, &sh->state);
3406 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3407 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003408 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 }
NeilBrown482c0832011-04-18 18:25:42 +10003410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411}
3412
NeilBrownd1688a62011-10-11 16:49:52 +11003413static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003414{
3415 /* device_lock is held */
3416 struct list_head head;
3417 list_add(&head, &conf->bitmap_list);
3418 list_del_init(&conf->bitmap_list);
3419 while (!list_empty(&head)) {
3420 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3421 list_del_init(&sh->lru);
3422 atomic_inc(&sh->count);
3423 __release_stripe(conf, sh);
3424 }
3425}
3426
NeilBrownfd01b882011-10-11 16:47:53 +11003427int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003428{
NeilBrownd1688a62011-10-11 16:49:52 +11003429 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003430
3431 /* No difference between reads and writes. Just check
3432 * how busy the stripe_cache is
3433 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003434
NeilBrownf022b2f2006-10-03 01:15:56 -07003435 if (conf->inactive_blocked)
3436 return 1;
3437 if (conf->quiesce)
3438 return 1;
3439 if (list_empty_careful(&conf->inactive_list))
3440 return 1;
3441
3442 return 0;
3443}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003444EXPORT_SYMBOL_GPL(md_raid5_congested);
3445
3446static int raid5_congested(void *data, int bits)
3447{
NeilBrownfd01b882011-10-11 16:47:53 +11003448 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003449
3450 return mddev_congested(mddev, bits) ||
3451 md_raid5_congested(mddev, bits);
3452}
NeilBrownf022b2f2006-10-03 01:15:56 -07003453
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003454/* We want read requests to align with chunks where possible,
3455 * but write requests don't need to.
3456 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003457static int raid5_mergeable_bvec(struct request_queue *q,
3458 struct bvec_merge_data *bvm,
3459 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003460{
NeilBrownfd01b882011-10-11 16:47:53 +11003461 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003462 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003463 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003464 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003465 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003466
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003467 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003468 return biovec->bv_len; /* always allow writes to be mergeable */
3469
Andre Noll664e7c42009-06-18 08:45:27 +10003470 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3471 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003472 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3473 if (max < 0) max = 0;
3474 if (max <= biovec->bv_len && bio_sectors == 0)
3475 return biovec->bv_len;
3476 else
3477 return max;
3478}
3479
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003480
NeilBrownfd01b882011-10-11 16:47:53 +11003481static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003482{
3483 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003484 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003485 unsigned int bio_sectors = bio->bi_size >> 9;
3486
Andre Noll664e7c42009-06-18 08:45:27 +10003487 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3488 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003489 return chunk_sectors >=
3490 ((sector & (chunk_sectors - 1)) + bio_sectors);
3491}
3492
3493/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003494 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3495 * later sampled by raid5d.
3496 */
NeilBrownd1688a62011-10-11 16:49:52 +11003497static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003498{
3499 unsigned long flags;
3500
3501 spin_lock_irqsave(&conf->device_lock, flags);
3502
3503 bi->bi_next = conf->retry_read_aligned_list;
3504 conf->retry_read_aligned_list = bi;
3505
3506 spin_unlock_irqrestore(&conf->device_lock, flags);
3507 md_wakeup_thread(conf->mddev->thread);
3508}
3509
3510
NeilBrownd1688a62011-10-11 16:49:52 +11003511static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003512{
3513 struct bio *bi;
3514
3515 bi = conf->retry_read_aligned;
3516 if (bi) {
3517 conf->retry_read_aligned = NULL;
3518 return bi;
3519 }
3520 bi = conf->retry_read_aligned_list;
3521 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003522 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003523 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003524 /*
3525 * this sets the active strip count to 1 and the processed
3526 * strip count to zero (upper 8 bits)
3527 */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003528 bi->bi_phys_segments = 1; /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003529 }
3530
3531 return bi;
3532}
3533
3534
3535/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003536 * The "raid5_align_endio" should check if the read succeeded and if it
3537 * did, call bio_endio on the original bio (having bio_put the new bio
3538 * first).
3539 * If the read failed..
3540 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003541static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003542{
3543 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003544 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003545 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003546 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003547 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003548
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003549 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003550
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003551 rdev = (void*)raid_bi->bi_next;
3552 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003553 mddev = rdev->mddev;
3554 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003555
3556 rdev_dec_pending(rdev, conf->mddev);
3557
3558 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003559 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003560 if (atomic_dec_and_test(&conf->active_aligned_reads))
3561 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003562 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003563 }
3564
3565
Dan Williams45b42332007-07-09 11:56:43 -07003566 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003567
3568 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003569}
3570
Neil Brown387bb172007-02-08 14:20:29 -08003571static int bio_fits_rdev(struct bio *bi)
3572{
Jens Axboe165125e2007-07-24 09:28:11 +02003573 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003574
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003575 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003576 return 0;
3577 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003578 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003579 return 0;
3580
3581 if (q->merge_bvec_fn)
3582 /* it's too hard to apply the merge_bvec_fn at this stage,
3583 * just just give up
3584 */
3585 return 0;
3586
3587 return 1;
3588}
3589
3590
NeilBrownfd01b882011-10-11 16:47:53 +11003591static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003592{
NeilBrownd1688a62011-10-11 16:49:52 +11003593 struct r5conf *conf = mddev->private;
NeilBrown8553fe72009-12-14 12:49:47 +11003594 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003595 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11003596 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003597
3598 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003599 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003600 return 0;
3601 }
3602 /*
NeilBrowna167f662010-10-26 18:31:13 +11003603 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003604 */
NeilBrowna167f662010-10-26 18:31:13 +11003605 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003606 if (!align_bi)
3607 return 0;
3608 /*
3609 * set bi_end_io to a new function, and set bi_private to the
3610 * original bio.
3611 */
3612 align_bi->bi_end_io = raid5_align_endio;
3613 align_bi->bi_private = raid_bio;
3614 /*
3615 * compute position
3616 */
NeilBrown112bf892009-03-31 14:39:38 +11003617 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3618 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003619 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003620
3621 rcu_read_lock();
3622 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3623 if (rdev && test_bit(In_sync, &rdev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003624 sector_t first_bad;
3625 int bad_sectors;
3626
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003627 atomic_inc(&rdev->nr_pending);
3628 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003629 raid_bio->bi_next = (void*)rdev;
3630 align_bi->bi_bdev = rdev->bdev;
3631 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
3632 align_bi->bi_sector += rdev->data_offset;
3633
NeilBrown31c176e2011-07-28 11:39:22 +10003634 if (!bio_fits_rdev(align_bi) ||
3635 is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
3636 &first_bad, &bad_sectors)) {
3637 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08003638 bio_put(align_bi);
3639 rdev_dec_pending(rdev, mddev);
3640 return 0;
3641 }
3642
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003643 spin_lock_irq(&conf->device_lock);
3644 wait_event_lock_irq(conf->wait_for_stripe,
3645 conf->quiesce == 0,
3646 conf->device_lock, /* nothing */);
3647 atomic_inc(&conf->active_aligned_reads);
3648 spin_unlock_irq(&conf->device_lock);
3649
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003650 generic_make_request(align_bi);
3651 return 1;
3652 } else {
3653 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003654 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003655 return 0;
3656 }
3657}
3658
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003659/* __get_priority_stripe - get the next stripe to process
3660 *
3661 * Full stripe writes are allowed to pass preread active stripes up until
3662 * the bypass_threshold is exceeded. In general the bypass_count
3663 * increments when the handle_list is handled before the hold_list; however, it
3664 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3665 * stripe with in flight i/o. The bypass_count will be reset when the
3666 * head of the hold_list has changed, i.e. the head was promoted to the
3667 * handle_list.
3668 */
NeilBrownd1688a62011-10-11 16:49:52 +11003669static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003670{
3671 struct stripe_head *sh;
3672
3673 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3674 __func__,
3675 list_empty(&conf->handle_list) ? "empty" : "busy",
3676 list_empty(&conf->hold_list) ? "empty" : "busy",
3677 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3678
3679 if (!list_empty(&conf->handle_list)) {
3680 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3681
3682 if (list_empty(&conf->hold_list))
3683 conf->bypass_count = 0;
3684 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3685 if (conf->hold_list.next == conf->last_hold)
3686 conf->bypass_count++;
3687 else {
3688 conf->last_hold = conf->hold_list.next;
3689 conf->bypass_count -= conf->bypass_threshold;
3690 if (conf->bypass_count < 0)
3691 conf->bypass_count = 0;
3692 }
3693 }
3694 } else if (!list_empty(&conf->hold_list) &&
3695 ((conf->bypass_threshold &&
3696 conf->bypass_count > conf->bypass_threshold) ||
3697 atomic_read(&conf->pending_full_writes) == 0)) {
3698 sh = list_entry(conf->hold_list.next,
3699 typeof(*sh), lru);
3700 conf->bypass_count -= conf->bypass_threshold;
3701 if (conf->bypass_count < 0)
3702 conf->bypass_count = 0;
3703 } else
3704 return NULL;
3705
3706 list_del_init(&sh->lru);
3707 atomic_inc(&sh->count);
3708 BUG_ON(atomic_read(&sh->count) != 1);
3709 return sh;
3710}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003711
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07003712static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713{
NeilBrownd1688a62011-10-11 16:49:52 +11003714 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11003715 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 sector_t new_sector;
3717 sector_t logical_sector, last_sector;
3718 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01003719 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11003720 int remaining;
NeilBrown7c13edc2011-04-18 18:25:43 +10003721 int plugged;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722
Tejun Heoe9c74692010-09-03 11:56:18 +02003723 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
3724 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02003725 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07003726 }
3727
NeilBrown3d310eb2005-06-21 17:17:26 -07003728 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07003729
NeilBrown802ba062006-12-13 00:34:13 -08003730 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003731 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11003732 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02003733 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003734
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
3736 last_sector = bi->bi_sector + (bi->bi_size>>9);
3737 bi->bi_next = NULL;
3738 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07003739
NeilBrown7c13edc2011-04-18 18:25:43 +10003740 plugged = mddev_check_plugged(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
3742 DEFINE_WAIT(w);
NeilBrown16a53ec2006-06-26 00:27:38 -07003743 int disks, data_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003744 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08003745
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003746 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11003747 previous = 0;
NeilBrownb0f9ec02009-03-31 15:27:18 +11003748 disks = conf->raid_disks;
NeilBrownb578d552006-03-27 01:18:12 -08003749 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11003750 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11003751 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08003752 * 64bit on a 32bit platform, and so it might be
3753 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02003754 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08003755 * the lock is dropped, so once we get a reference
3756 * to the stripe that we think it is, we will have
3757 * to check again.
3758 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003759 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11003760 if (mddev->delta_disks < 0
3761 ? logical_sector < conf->reshape_progress
3762 : logical_sector >= conf->reshape_progress) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003763 disks = conf->previous_raid_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003764 previous = 1;
3765 } else {
NeilBrownfef9c612009-03-31 15:16:46 +11003766 if (mddev->delta_disks < 0
3767 ? logical_sector < conf->reshape_safe
3768 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08003769 spin_unlock_irq(&conf->device_lock);
3770 schedule();
3771 goto retry;
3772 }
3773 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003774 spin_unlock_irq(&conf->device_lock);
3775 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003776 data_disks = disks - conf->max_degraded;
3777
NeilBrown112bf892009-03-31 14:39:38 +11003778 new_sector = raid5_compute_sector(conf, logical_sector,
3779 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003780 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10003781 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 (unsigned long long)new_sector,
3783 (unsigned long long)logical_sector);
3784
NeilBrownb5663ba2009-03-31 14:39:38 +11003785 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10003786 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11003788 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003789 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08003790 * stripe, so we must do the range check again.
3791 * Expansion could still move past after this
3792 * test, but as we are holding a reference to
3793 * 'sh', we know that if that happens,
3794 * STRIPE_EXPANDING will get set and the expansion
3795 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003796 */
3797 int must_retry = 0;
3798 spin_lock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11003799 if (mddev->delta_disks < 0
3800 ? logical_sector >= conf->reshape_progress
3801 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003802 /* mismatch, need to try again */
3803 must_retry = 1;
3804 spin_unlock_irq(&conf->device_lock);
3805 if (must_retry) {
3806 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07003807 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003808 goto retry;
3809 }
3810 }
NeilBrowne62e58a2009-07-01 13:15:35 +10003811
Namhyung Kimffd96e32011-07-18 17:38:51 +10003812 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10003813 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08003814 logical_sector < mddev->suspend_hi) {
3815 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10003816 /* As the suspend_* range is controlled by
3817 * userspace, we want an interruptible
3818 * wait.
3819 */
3820 flush_signals(current);
3821 prepare_to_wait(&conf->wait_for_overlap,
3822 &w, TASK_INTERRUPTIBLE);
3823 if (logical_sector >= mddev->suspend_lo &&
3824 logical_sector < mddev->suspend_hi)
3825 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08003826 goto retry;
3827 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003828
3829 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10003830 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003831 /* Stripe is busy expanding or
3832 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 * and wait a while
3834 */
NeilBrown482c0832011-04-18 18:25:42 +10003835 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 release_stripe(sh);
3837 schedule();
3838 goto retry;
3839 }
3840 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08003841 set_bit(STRIPE_HANDLE, &sh->state);
3842 clear_bit(STRIPE_DELAYED, &sh->state);
Tejun Heoe9c74692010-09-03 11:56:18 +02003843 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11003844 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3845 atomic_inc(&conf->preread_active_stripes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 } else {
3848 /* cannot get stripe for read-ahead, just give-up */
3849 clear_bit(BIO_UPTODATE, &bi->bi_flags);
3850 finish_wait(&conf->wait_for_overlap, &w);
3851 break;
3852 }
3853
3854 }
NeilBrown7c13edc2011-04-18 18:25:43 +10003855 if (!plugged)
3856 md_wakeup_thread(mddev->thread);
3857
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02003859 remaining = raid5_dec_bi_phys_segments(bi);
NeilBrownf6344752006-03-27 01:18:17 -08003860 spin_unlock_irq(&conf->device_lock);
3861 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862
NeilBrown16a53ec2006-06-26 00:27:38 -07003863 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02003865
Neil Brown0e13fe232008-06-28 08:31:20 +10003866 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868}
3869
NeilBrownfd01b882011-10-11 16:47:53 +11003870static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11003871
NeilBrownfd01b882011-10-11 16:47:53 +11003872static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873{
NeilBrown52c03292006-06-26 00:27:43 -07003874 /* reshaping is quite different to recovery/resync so it is
3875 * handled quite separately ... here.
3876 *
3877 * On each call to sync_request, we gather one chunk worth of
3878 * destination stripes and flag them as expanding.
3879 * Then we find all the source stripes and request reads.
3880 * As the reads complete, handle_stripe will copy the data
3881 * into the destination stripe and release that stripe.
3882 */
NeilBrownd1688a62011-10-11 16:49:52 +11003883 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08003885 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08003886 int raid_disks = conf->previous_raid_disks;
3887 int data_disks = raid_disks - conf->max_degraded;
3888 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07003889 int i;
3890 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11003891 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11003892 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11003893 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11003894 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07003895
NeilBrownfef9c612009-03-31 15:16:46 +11003896 if (sector_nr == 0) {
3897 /* If restarting in the middle, skip the initial sectors */
3898 if (mddev->delta_disks < 0 &&
3899 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
3900 sector_nr = raid5_size(mddev, 0, 0)
3901 - conf->reshape_progress;
NeilBrowna6397552009-08-13 10:13:00 +10003902 } else if (mddev->delta_disks >= 0 &&
NeilBrownfef9c612009-03-31 15:16:46 +11003903 conf->reshape_progress > 0)
3904 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08003905 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11003906 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11003907 mddev->curr_resync_completed = sector_nr;
3908 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11003909 *skipped = 1;
3910 return sector_nr;
3911 }
NeilBrown52c03292006-06-26 00:27:43 -07003912 }
3913
NeilBrown7a661382009-03-31 15:21:40 +11003914 /* We need to process a full chunk at a time.
3915 * If old and new chunk sizes differ, we need to process the
3916 * largest of these
3917 */
Andre Noll664e7c42009-06-18 08:45:27 +10003918 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
3919 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11003920 else
Andre Noll9d8f0362009-06-18 08:45:01 +10003921 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11003922
NeilBrown52c03292006-06-26 00:27:43 -07003923 /* we update the metadata when there is more than 3Meg
3924 * in the block range (that is rather arbitrary, should
3925 * probably be time based) or when the data about to be
3926 * copied would over-write the source of the data at
3927 * the front of the range.
NeilBrownfef9c612009-03-31 15:16:46 +11003928 * i.e. one new_stripe along from reshape_progress new_maps
3929 * to after where reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07003930 */
NeilBrownfef9c612009-03-31 15:16:46 +11003931 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08003932 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11003933 readpos = conf->reshape_progress;
3934 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11003935 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08003936 sector_div(safepos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11003937 if (mddev->delta_disks < 0) {
NeilBrowned37d832009-05-27 21:39:05 +10003938 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11003939 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11003940 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11003941 } else {
NeilBrown7a661382009-03-31 15:21:40 +11003942 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10003943 readpos -= min_t(sector_t, reshape_sectors, readpos);
3944 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11003945 }
NeilBrown52c03292006-06-26 00:27:43 -07003946
NeilBrownc8f517c2009-03-31 15:28:40 +11003947 /* 'writepos' is the most advanced device address we might write.
3948 * 'readpos' is the least advanced device address we might read.
3949 * 'safepos' is the least address recorded in the metadata as having
3950 * been reshaped.
3951 * If 'readpos' is behind 'writepos', then there is no way that we can
3952 * ensure safety in the face of a crash - that must be done by userspace
3953 * making a backup of the data. So in that case there is no particular
3954 * rush to update metadata.
3955 * Otherwise if 'safepos' is behind 'writepos', then we really need to
3956 * update the metadata to advance 'safepos' to match 'readpos' so that
3957 * we can be safe in the event of a crash.
3958 * So we insist on updating metadata if safepos is behind writepos and
3959 * readpos is beyond writepos.
3960 * In any case, update the metadata every 10 seconds.
3961 * Maybe that number should be configurable, but I'm not sure it is
3962 * worth it.... maybe it could be a multiple of safemode_delay???
3963 */
NeilBrownfef9c612009-03-31 15:16:46 +11003964 if ((mddev->delta_disks < 0
NeilBrownc8f517c2009-03-31 15:28:40 +11003965 ? (safepos > writepos && readpos < writepos)
3966 : (safepos < writepos && readpos > writepos)) ||
3967 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07003968 /* Cannot proceed until we've updated the superblock... */
3969 wait_event(conf->wait_for_overlap,
3970 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11003971 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11003972 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11003973 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07003974 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07003975 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07003976 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07003977 kthread_should_stop());
3978 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11003979 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07003980 spin_unlock_irq(&conf->device_lock);
3981 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10003982 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07003983 }
3984
NeilBrownec32a2b2009-03-31 15:17:38 +11003985 if (mddev->delta_disks < 0) {
3986 BUG_ON(conf->reshape_progress == 0);
3987 stripe_addr = writepos;
3988 BUG_ON((mddev->dev_sectors &
NeilBrown7a661382009-03-31 15:21:40 +11003989 ~((sector_t)reshape_sectors - 1))
3990 - reshape_sectors - stripe_addr
NeilBrownec32a2b2009-03-31 15:17:38 +11003991 != sector_nr);
3992 } else {
NeilBrown7a661382009-03-31 15:21:40 +11003993 BUG_ON(writepos != sector_nr + reshape_sectors);
NeilBrownec32a2b2009-03-31 15:17:38 +11003994 stripe_addr = sector_nr;
3995 }
NeilBrownab69ae12009-03-31 15:26:47 +11003996 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11003997 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07003998 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10003999 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004000 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004001 set_bit(STRIPE_EXPANDING, &sh->state);
4002 atomic_inc(&conf->reshape_stripes);
4003 /* If any of this stripe is beyond the end of the old
4004 * array, then we need to zero those blocks
4005 */
4006 for (j=sh->disks; j--;) {
4007 sector_t s;
4008 if (j == sh->pd_idx)
4009 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004010 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004011 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004012 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004013 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004014 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004015 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004016 continue;
4017 }
4018 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4019 set_bit(R5_Expanded, &sh->dev[j].flags);
4020 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4021 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004022 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004023 set_bit(STRIPE_EXPAND_READY, &sh->state);
4024 set_bit(STRIPE_HANDLE, &sh->state);
4025 }
NeilBrownab69ae12009-03-31 15:26:47 +11004026 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004027 }
4028 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004029 if (mddev->delta_disks < 0)
NeilBrown7a661382009-03-31 15:21:40 +11004030 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004031 else
NeilBrown7a661382009-03-31 15:21:40 +11004032 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004033 spin_unlock_irq(&conf->device_lock);
4034 /* Ok, those stripe are ready. We can start scheduling
4035 * reads on the source stripes.
4036 * The source stripes are determined by mapping the first and last
4037 * block on the destination stripes.
4038 */
NeilBrown52c03292006-06-26 00:27:43 -07004039 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004040 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004041 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004042 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004043 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004044 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004045 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004046 if (last_sector >= mddev->dev_sectors)
4047 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004048 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004049 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004050 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4051 set_bit(STRIPE_HANDLE, &sh->state);
4052 release_stripe(sh);
4053 first_sector += STRIPE_SECTORS;
4054 }
NeilBrownab69ae12009-03-31 15:26:47 +11004055 /* Now that the sources are clearly marked, we can release
4056 * the destination stripes
4057 */
4058 while (!list_empty(&stripes)) {
4059 sh = list_entry(stripes.next, struct stripe_head, lru);
4060 list_del_init(&sh->lru);
4061 release_stripe(sh);
4062 }
NeilBrownc6207272008-02-06 01:39:52 -08004063 /* If this takes us to the resync_max point where we have to pause,
4064 * then we need to write out the superblock.
4065 */
NeilBrown7a661382009-03-31 15:21:40 +11004066 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004067 if ((sector_nr - mddev->curr_resync_completed) * 2
4068 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004069 /* Cannot proceed until we've updated the superblock... */
4070 wait_event(conf->wait_for_overlap,
4071 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004072 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004073 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004074 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004075 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4076 md_wakeup_thread(mddev->thread);
4077 wait_event(mddev->sb_wait,
4078 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4079 || kthread_should_stop());
4080 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004081 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004082 spin_unlock_irq(&conf->device_lock);
4083 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004084 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004085 }
NeilBrown7a661382009-03-31 15:21:40 +11004086 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004087}
4088
4089/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004090static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004091{
NeilBrownd1688a62011-10-11 16:49:52 +11004092 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004093 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004094 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004095 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004096 int still_degraded = 0;
4097 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098
NeilBrown72626682005-09-09 16:23:54 -07004099 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004101
NeilBrown29269552006-03-27 01:18:10 -08004102 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4103 end_reshape(conf);
4104 return 0;
4105 }
NeilBrown72626682005-09-09 16:23:54 -07004106
4107 if (mddev->curr_resync < max_sector) /* aborted */
4108 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4109 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004110 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004111 conf->fullsync = 0;
4112 bitmap_close_sync(mddev->bitmap);
4113
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 return 0;
4115 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004116
NeilBrown64bd6602009-08-03 10:59:58 +10004117 /* Allow raid5_quiesce to complete */
4118 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4119
NeilBrown52c03292006-06-26 00:27:43 -07004120 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4121 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004122
NeilBrownc6207272008-02-06 01:39:52 -08004123 /* No need to check resync_max as we never do more than one
4124 * stripe, and as resync_max will always be on a chunk boundary,
4125 * if the check in md_do_sync didn't fire, there is no chance
4126 * of overstepping resync_max here
4127 */
4128
NeilBrown16a53ec2006-06-26 00:27:38 -07004129 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 * to resync, then assert that we are finished, because there is
4131 * nothing we can do.
4132 */
NeilBrown3285edf2006-06-26 00:27:55 -07004133 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004134 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004135 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004136 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 return rv;
4138 }
NeilBrown72626682005-09-09 16:23:54 -07004139 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004140 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004141 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4142 /* we can skip this block, and probably more */
4143 sync_blocks /= STRIPE_SECTORS;
4144 *skipped = 1;
4145 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147
NeilBrownb47490c2008-02-06 01:39:50 -08004148
4149 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4150
NeilBrowna8c906c2009-06-09 14:39:59 +10004151 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004153 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004155 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004157 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004159 /* Need to check if array will still be degraded after recovery/resync
4160 * We don't need to check the 'failed' flag as when that gets set,
4161 * recovery aborts.
4162 */
NeilBrownf001a702009-06-09 14:30:31 +10004163 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004164 if (conf->disks[i].rdev == NULL)
4165 still_degraded = 1;
4166
4167 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4168
NeilBrown83206d62011-07-26 11:19:49 +10004169 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170
NeilBrown14425772009-10-16 15:55:25 +11004171 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 release_stripe(sh);
4173
4174 return STRIPE_SECTORS;
4175}
4176
NeilBrownd1688a62011-10-11 16:49:52 +11004177static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004178{
4179 /* We may not be able to submit a whole bio at once as there
4180 * may not be enough stripe_heads available.
4181 * We cannot pre-allocate enough stripe_heads as we may need
4182 * more than exist in the cache (if we allow ever large chunks).
4183 * So we do one stripe head at a time and record in
4184 * ->bi_hw_segments how many have been done.
4185 *
4186 * We *know* that this entire raid_bio is in one chunk, so
4187 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4188 */
4189 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004190 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004191 sector_t sector, logical_sector, last_sector;
4192 int scnt = 0;
4193 int remaining;
4194 int handled = 0;
4195
4196 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004197 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004198 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004199 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4200
4201 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004202 logical_sector += STRIPE_SECTORS,
4203 sector += STRIPE_SECTORS,
4204 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004205
Jens Axboe960e7392008-08-15 10:41:18 +02004206 if (scnt < raid5_bi_hw_segments(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004207 /* already done this stripe */
4208 continue;
4209
NeilBrowna8c906c2009-06-09 14:39:59 +10004210 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004211
4212 if (!sh) {
4213 /* failed to get a stripe - must wait */
Jens Axboe960e7392008-08-15 10:41:18 +02004214 raid5_set_bi_hw_segments(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004215 conf->retry_read_aligned = raid_bio;
4216 return handled;
4217 }
4218
4219 set_bit(R5_ReadError, &sh->dev[dd_idx].flags);
Neil Brown387bb172007-02-08 14:20:29 -08004220 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4221 release_stripe(sh);
Jens Axboe960e7392008-08-15 10:41:18 +02004222 raid5_set_bi_hw_segments(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004223 conf->retry_read_aligned = raid_bio;
4224 return handled;
4225 }
4226
Dan Williams36d1c642009-07-14 11:48:22 -07004227 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004228 release_stripe(sh);
4229 handled++;
4230 }
4231 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004232 remaining = raid5_dec_bi_phys_segments(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004233 spin_unlock_irq(&conf->device_lock);
Neil Brown0e13fe232008-06-28 08:31:20 +10004234 if (remaining == 0)
4235 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004236 if (atomic_dec_and_test(&conf->active_aligned_reads))
4237 wake_up(&conf->wait_for_stripe);
4238 return handled;
4239}
4240
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004241
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242/*
4243 * This is our raid5 kernel thread.
4244 *
4245 * We scan the hash table for stripes which can be handled now.
4246 * During the scan, completed stripes are saved for us by the interrupt
4247 * handler, so that they will not have to wait for our next wakeup.
4248 */
NeilBrownfd01b882011-10-11 16:47:53 +11004249static void raid5d(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250{
4251 struct stripe_head *sh;
NeilBrownd1688a62011-10-11 16:49:52 +11004252 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004254 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255
Dan Williams45b42332007-07-09 11:56:43 -07004256 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257
4258 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004260 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 handled = 0;
4262 spin_lock_irq(&conf->device_lock);
4263 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004264 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265
NeilBrown7c13edc2011-04-18 18:25:43 +10004266 if (atomic_read(&mddev->plug_cnt) == 0 &&
4267 !list_empty(&conf->bitmap_list)) {
4268 /* Now is a good time to flush some bitmap updates */
4269 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004270 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004271 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004272 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004273 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004274 activate_bit_delay(conf);
4275 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004276 if (atomic_read(&mddev->plug_cnt) == 0)
4277 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004278
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004279 while ((bio = remove_bio_from_retry(conf))) {
4280 int ok;
4281 spin_unlock_irq(&conf->device_lock);
4282 ok = retry_aligned_read(conf, bio);
4283 spin_lock_irq(&conf->device_lock);
4284 if (!ok)
4285 break;
4286 handled++;
4287 }
4288
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004289 sh = __get_priority_stripe(conf);
4290
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004291 if (!sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 spin_unlock_irq(&conf->device_lock);
4294
4295 handled++;
Dan Williams417b8d42009-10-16 16:25:22 +11004296 handle_stripe(sh);
4297 release_stripe(sh);
4298 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299
NeilBrownde393cd2011-07-28 11:31:48 +10004300 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
4301 md_check_recovery(mddev);
4302
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 spin_lock_irq(&conf->device_lock);
4304 }
Dan Williams45b42332007-07-09 11:56:43 -07004305 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306
4307 spin_unlock_irq(&conf->device_lock);
4308
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004309 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004310 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311
Dan Williams45b42332007-07-09 11:56:43 -07004312 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313}
4314
NeilBrown3f294f42005-11-08 21:39:25 -08004315static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004316raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004317{
NeilBrownd1688a62011-10-11 16:49:52 +11004318 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004319 if (conf)
4320 return sprintf(page, "%d\n", conf->max_nr_stripes);
4321 else
4322 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004323}
4324
NeilBrownc41d4ac2010-06-01 19:37:24 +10004325int
NeilBrownfd01b882011-10-11 16:47:53 +11004326raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004327{
NeilBrownd1688a62011-10-11 16:49:52 +11004328 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004329 int err;
4330
4331 if (size <= 16 || size > 32768)
4332 return -EINVAL;
4333 while (size < conf->max_nr_stripes) {
4334 if (drop_one_stripe(conf))
4335 conf->max_nr_stripes--;
4336 else
4337 break;
4338 }
4339 err = md_allow_write(mddev);
4340 if (err)
4341 return err;
4342 while (size > conf->max_nr_stripes) {
4343 if (grow_one_stripe(conf))
4344 conf->max_nr_stripes++;
4345 else break;
4346 }
4347 return 0;
4348}
4349EXPORT_SYMBOL(raid5_set_cache_size);
4350
NeilBrown3f294f42005-11-08 21:39:25 -08004351static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004352raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004353{
NeilBrownd1688a62011-10-11 16:49:52 +11004354 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004355 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004356 int err;
4357
NeilBrown3f294f42005-11-08 21:39:25 -08004358 if (len >= PAGE_SIZE)
4359 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004360 if (!conf)
4361 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004362
Dan Williams4ef197d82008-04-28 02:15:54 -07004363 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004364 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004365 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004366 if (err)
4367 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004368 return len;
4369}
NeilBrown007583c2005-11-08 21:39:30 -08004370
NeilBrown96de1e62005-11-08 21:39:39 -08004371static struct md_sysfs_entry
4372raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4373 raid5_show_stripe_cache_size,
4374 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004375
4376static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004377raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004378{
NeilBrownd1688a62011-10-11 16:49:52 +11004379 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004380 if (conf)
4381 return sprintf(page, "%d\n", conf->bypass_threshold);
4382 else
4383 return 0;
4384}
4385
4386static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004387raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004388{
NeilBrownd1688a62011-10-11 16:49:52 +11004389 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004390 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004391 if (len >= PAGE_SIZE)
4392 return -EINVAL;
4393 if (!conf)
4394 return -ENODEV;
4395
Dan Williams4ef197d82008-04-28 02:15:54 -07004396 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004397 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004398 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004399 return -EINVAL;
4400 conf->bypass_threshold = new;
4401 return len;
4402}
4403
4404static struct md_sysfs_entry
4405raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4406 S_IRUGO | S_IWUSR,
4407 raid5_show_preread_threshold,
4408 raid5_store_preread_threshold);
4409
4410static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004411stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004412{
NeilBrownd1688a62011-10-11 16:49:52 +11004413 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004414 if (conf)
4415 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4416 else
4417 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004418}
4419
NeilBrown96de1e62005-11-08 21:39:39 -08004420static struct md_sysfs_entry
4421raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004422
NeilBrown007583c2005-11-08 21:39:30 -08004423static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004424 &raid5_stripecache_size.attr,
4425 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004426 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004427 NULL,
4428};
NeilBrown007583c2005-11-08 21:39:30 -08004429static struct attribute_group raid5_attrs_group = {
4430 .name = NULL,
4431 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004432};
4433
Dan Williams80c3a6c2009-03-17 18:10:40 -07004434static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11004435raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07004436{
NeilBrownd1688a62011-10-11 16:49:52 +11004437 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004438
4439 if (!sectors)
4440 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004441 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11004442 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11004443 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004444
Andre Noll9d8f0362009-06-18 08:45:01 +10004445 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004446 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004447 return sectors * (raid_disks - conf->max_degraded);
4448}
4449
NeilBrownd1688a62011-10-11 16:49:52 +11004450static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004451{
4452 struct raid5_percpu *percpu;
4453 unsigned long cpu;
4454
4455 if (!conf->percpu)
4456 return;
4457
4458 get_online_cpus();
4459 for_each_possible_cpu(cpu) {
4460 percpu = per_cpu_ptr(conf->percpu, cpu);
4461 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004462 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004463 }
4464#ifdef CONFIG_HOTPLUG_CPU
4465 unregister_cpu_notifier(&conf->cpu_notify);
4466#endif
4467 put_online_cpus();
4468
4469 free_percpu(conf->percpu);
4470}
4471
NeilBrownd1688a62011-10-11 16:49:52 +11004472static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10004473{
4474 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004475 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10004476 kfree(conf->disks);
4477 kfree(conf->stripe_hashtbl);
4478 kfree(conf);
4479}
4480
Dan Williams36d1c642009-07-14 11:48:22 -07004481#ifdef CONFIG_HOTPLUG_CPU
4482static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4483 void *hcpu)
4484{
NeilBrownd1688a62011-10-11 16:49:52 +11004485 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07004486 long cpu = (long)hcpu;
4487 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4488
4489 switch (action) {
4490 case CPU_UP_PREPARE:
4491 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07004492 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07004493 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004494 if (!percpu->scribble)
4495 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4496
4497 if (!percpu->scribble ||
4498 (conf->level == 6 && !percpu->spare_page)) {
4499 safe_put_page(percpu->spare_page);
4500 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004501 pr_err("%s: failed memory allocation for cpu%ld\n",
4502 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07004503 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07004504 }
4505 break;
4506 case CPU_DEAD:
4507 case CPU_DEAD_FROZEN:
4508 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004509 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004510 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004511 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07004512 break;
4513 default:
4514 break;
4515 }
4516 return NOTIFY_OK;
4517}
4518#endif
4519
NeilBrownd1688a62011-10-11 16:49:52 +11004520static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004521{
4522 unsigned long cpu;
4523 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09004524 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004525 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004526 int err;
4527
Dan Williams36d1c642009-07-14 11:48:22 -07004528 allcpus = alloc_percpu(struct raid5_percpu);
4529 if (!allcpus)
4530 return -ENOMEM;
4531 conf->percpu = allcpus;
4532
4533 get_online_cpus();
4534 err = 0;
4535 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07004536 if (conf->level == 6) {
4537 spare_page = alloc_page(GFP_KERNEL);
4538 if (!spare_page) {
4539 err = -ENOMEM;
4540 break;
4541 }
4542 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
4543 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11004544 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004545 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07004546 err = -ENOMEM;
4547 break;
4548 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07004549 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004550 }
4551#ifdef CONFIG_HOTPLUG_CPU
4552 conf->cpu_notify.notifier_call = raid456_cpu_notify;
4553 conf->cpu_notify.priority = 0;
4554 if (err == 0)
4555 err = register_cpu_notifier(&conf->cpu_notify);
4556#endif
4557 put_online_cpus();
4558
4559 return err;
4560}
4561
NeilBrownd1688a62011-10-11 16:49:52 +11004562static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563{
NeilBrownd1688a62011-10-11 16:49:52 +11004564 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004565 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11004566 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 struct disk_info *disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568
NeilBrown91adb562009-03-31 14:39:39 +11004569 if (mddev->new_level != 5
4570 && mddev->new_level != 4
4571 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10004572 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004573 mdname(mddev), mddev->new_level);
4574 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 }
NeilBrown91adb562009-03-31 14:39:39 +11004576 if ((mddev->new_level == 5
4577 && !algorithm_valid_raid5(mddev->new_layout)) ||
4578 (mddev->new_level == 6
4579 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004580 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11004581 mdname(mddev), mddev->new_layout);
4582 return ERR_PTR(-EIO);
4583 }
4584 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10004585 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004586 mdname(mddev), mddev->raid_disks);
4587 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11004588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589
Andre Noll664e7c42009-06-18 08:45:27 +10004590 if (!mddev->new_chunk_sectors ||
4591 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
4592 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10004593 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
4594 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11004595 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11004596 }
4597
NeilBrownd1688a62011-10-11 16:49:52 +11004598 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11004599 if (conf == NULL)
4600 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11004601 spin_lock_init(&conf->device_lock);
4602 init_waitqueue_head(&conf->wait_for_stripe);
4603 init_waitqueue_head(&conf->wait_for_overlap);
4604 INIT_LIST_HEAD(&conf->handle_list);
4605 INIT_LIST_HEAD(&conf->hold_list);
4606 INIT_LIST_HEAD(&conf->delayed_list);
4607 INIT_LIST_HEAD(&conf->bitmap_list);
4608 INIT_LIST_HEAD(&conf->inactive_list);
4609 atomic_set(&conf->active_stripes, 0);
4610 atomic_set(&conf->preread_active_stripes, 0);
4611 atomic_set(&conf->active_aligned_reads, 0);
4612 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11004613 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11004614
4615 conf->raid_disks = mddev->raid_disks;
4616 if (mddev->reshape_position == MaxSector)
4617 conf->previous_raid_disks = mddev->raid_disks;
4618 else
4619 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004620 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
4621 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11004622
NeilBrown5e5e3e72009-10-16 16:35:30 +11004623 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11004624 GFP_KERNEL);
4625 if (!conf->disks)
4626 goto abort;
4627
4628 conf->mddev = mddev;
4629
4630 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
4631 goto abort;
4632
Dan Williams36d1c642009-07-14 11:48:22 -07004633 conf->level = mddev->new_level;
4634 if (raid5_alloc_percpu(conf) != 0)
4635 goto abort;
4636
NeilBrown0c55e022010-05-03 14:09:02 +10004637 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11004638
4639 list_for_each_entry(rdev, &mddev->disks, same_set) {
4640 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004641 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11004642 || raid_disk < 0)
4643 continue;
4644 disk = conf->disks + raid_disk;
4645
4646 disk->rdev = rdev;
4647
4648 if (test_bit(In_sync, &rdev->flags)) {
4649 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10004650 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
4651 " disk %d\n",
4652 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05004653 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11004654 /* Cannot rely on bitmap to complete recovery */
4655 conf->fullsync = 1;
4656 }
4657
Andre Noll09c9e5f2009-06-18 08:45:55 +10004658 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11004659 conf->level = mddev->new_level;
4660 if (conf->level == 6)
4661 conf->max_degraded = 2;
4662 else
4663 conf->max_degraded = 1;
4664 conf->algorithm = mddev->new_layout;
4665 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11004666 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11004667 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10004668 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11004669 conf->prev_algo = mddev->layout;
4670 }
NeilBrown91adb562009-03-31 14:39:39 +11004671
4672 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11004673 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11004674 if (grow_stripes(conf, conf->max_nr_stripes)) {
4675 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004676 "md/raid:%s: couldn't allocate %dkB for buffers\n",
4677 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004678 goto abort;
4679 } else
NeilBrown0c55e022010-05-03 14:09:02 +10004680 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
4681 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004682
NeilBrown0da3c612009-09-23 18:09:45 +10004683 conf->thread = md_register_thread(raid5d, mddev, NULL);
NeilBrown91adb562009-03-31 14:39:39 +11004684 if (!conf->thread) {
4685 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004686 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11004687 mdname(mddev));
4688 goto abort;
4689 }
4690
4691 return conf;
4692
4693 abort:
4694 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10004695 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11004696 return ERR_PTR(-EIO);
4697 } else
4698 return ERR_PTR(-ENOMEM);
4699}
4700
NeilBrownc148ffd2009-11-13 17:47:00 +11004701
4702static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
4703{
4704 switch (algo) {
4705 case ALGORITHM_PARITY_0:
4706 if (raid_disk < max_degraded)
4707 return 1;
4708 break;
4709 case ALGORITHM_PARITY_N:
4710 if (raid_disk >= raid_disks - max_degraded)
4711 return 1;
4712 break;
4713 case ALGORITHM_PARITY_0_6:
4714 if (raid_disk == 0 ||
4715 raid_disk == raid_disks - 1)
4716 return 1;
4717 break;
4718 case ALGORITHM_LEFT_ASYMMETRIC_6:
4719 case ALGORITHM_RIGHT_ASYMMETRIC_6:
4720 case ALGORITHM_LEFT_SYMMETRIC_6:
4721 case ALGORITHM_RIGHT_SYMMETRIC_6:
4722 if (raid_disk == raid_disks - 1)
4723 return 1;
4724 }
4725 return 0;
4726}
4727
NeilBrownfd01b882011-10-11 16:47:53 +11004728static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11004729{
NeilBrownd1688a62011-10-11 16:49:52 +11004730 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10004731 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11004732 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11004733 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11004734 sector_t reshape_offset = 0;
NeilBrown91adb562009-03-31 14:39:39 +11004735
Andre Noll8c6ac862009-06-18 08:48:06 +10004736 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10004737 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10004738 " -- starting background reconstruction\n",
4739 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004740 if (mddev->reshape_position != MaxSector) {
4741 /* Check that we can continue the reshape.
4742 * Currently only disks can change, it must
4743 * increase, and we must be past the point where
4744 * a stripe over-writes itself
4745 */
4746 sector_t here_new, here_old;
4747 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11004748 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08004749
NeilBrown88ce4932009-03-31 15:24:23 +11004750 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10004751 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08004752 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08004753 mdname(mddev));
4754 return -EINVAL;
4755 }
NeilBrownf6705572006-03-27 01:18:11 -08004756 old_disks = mddev->raid_disks - mddev->delta_disks;
4757 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08004758 * further up in new geometry must map after here in old
4759 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08004760 */
4761 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10004762 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08004763 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004764 printk(KERN_ERR "md/raid:%s: reshape_position not "
4765 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004766 return -EINVAL;
4767 }
NeilBrownc148ffd2009-11-13 17:47:00 +11004768 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08004769 /* here_new is the stripe we will write to */
4770 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10004771 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08004772 (old_disks-max_degraded));
4773 /* here_old is the first stripe that we might need to read
4774 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10004775 if (mddev->delta_disks == 0) {
4776 /* We cannot be sure it is safe to start an in-place
4777 * reshape. It is only safe if user-space if monitoring
4778 * and taking constant backups.
4779 * mdadm always starts a situation like this in
4780 * readonly mode so it can take control before
4781 * allowing any writes. So just check for that.
4782 */
4783 if ((here_new * mddev->new_chunk_sectors !=
4784 here_old * mddev->chunk_sectors) ||
4785 mddev->ro == 0) {
NeilBrown0c55e022010-05-03 14:09:02 +10004786 printk(KERN_ERR "md/raid:%s: in-place reshape must be started"
4787 " in read-only mode - aborting\n",
4788 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10004789 return -EINVAL;
4790 }
4791 } else if (mddev->delta_disks < 0
4792 ? (here_new * mddev->new_chunk_sectors <=
4793 here_old * mddev->chunk_sectors)
4794 : (here_new * mddev->new_chunk_sectors >=
4795 here_old * mddev->chunk_sectors)) {
NeilBrownf6705572006-03-27 01:18:11 -08004796 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10004797 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
4798 "auto-recovery - aborting.\n",
4799 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004800 return -EINVAL;
4801 }
NeilBrown0c55e022010-05-03 14:09:02 +10004802 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
4803 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004804 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08004805 } else {
NeilBrown91adb562009-03-31 14:39:39 +11004806 BUG_ON(mddev->level != mddev->new_level);
4807 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10004808 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11004809 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08004810 }
4811
NeilBrown245f46c2009-03-31 14:39:39 +11004812 if (mddev->private == NULL)
4813 conf = setup_conf(mddev);
4814 else
4815 conf = mddev->private;
4816
NeilBrown91adb562009-03-31 14:39:39 +11004817 if (IS_ERR(conf))
4818 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08004819
NeilBrown91adb562009-03-31 14:39:39 +11004820 mddev->thread = conf->thread;
4821 conf->thread = NULL;
4822 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07004825 * 0 for a fully functional array, 1 or 2 for a degraded array.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 */
NeilBrownc148ffd2009-11-13 17:47:00 +11004827 list_for_each_entry(rdev, &mddev->disks, same_set) {
4828 if (rdev->raid_disk < 0)
4829 continue;
NeilBrown2f115882010-06-17 17:41:03 +10004830 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11004831 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10004832 continue;
4833 }
NeilBrownc148ffd2009-11-13 17:47:00 +11004834 /* This disc is not fully in-sync. However if it
4835 * just stored parity (beyond the recovery_offset),
4836 * when we don't need to be concerned about the
4837 * array being dirty.
4838 * When reshape goes 'backwards', we never have
4839 * partially completed devices, so we only need
4840 * to worry about reshape going forwards.
4841 */
4842 /* Hack because v0.91 doesn't store recovery_offset properly. */
4843 if (mddev->major_version == 0 &&
4844 mddev->minor_version > 90)
4845 rdev->recovery_offset = reshape_offset;
4846
NeilBrownc148ffd2009-11-13 17:47:00 +11004847 if (rdev->recovery_offset < reshape_offset) {
4848 /* We need to check old and new layout */
4849 if (!only_parity(rdev->raid_disk,
4850 conf->algorithm,
4851 conf->raid_disks,
4852 conf->max_degraded))
4853 continue;
4854 }
4855 if (!only_parity(rdev->raid_disk,
4856 conf->prev_algo,
4857 conf->previous_raid_disks,
4858 conf->max_degraded))
4859 continue;
4860 dirty_parity_disks++;
4861 }
NeilBrown91adb562009-03-31 14:39:39 +11004862
NeilBrown908f4fb2011-12-23 10:17:50 +11004863 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864
NeilBrown674806d2010-06-16 17:17:53 +10004865 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10004866 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07004868 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 goto abort;
4870 }
4871
NeilBrown91adb562009-03-31 14:39:39 +11004872 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004873 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11004874 mddev->resync_max_sectors = mddev->dev_sectors;
4875
NeilBrownc148ffd2009-11-13 17:47:00 +11004876 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004878 if (mddev->ok_start_degraded)
4879 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10004880 "md/raid:%s: starting dirty degraded array"
4881 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004882 mdname(mddev));
4883 else {
4884 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004885 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004886 mdname(mddev));
4887 goto abort;
4888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 }
4890
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10004892 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
4893 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11004894 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
4895 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 else
NeilBrown0c55e022010-05-03 14:09:02 +10004897 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
4898 " out of %d devices, algorithm %d\n",
4899 mdname(mddev), conf->level,
4900 mddev->raid_disks - mddev->degraded,
4901 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902
4903 print_raid5_conf(conf);
4904
NeilBrownfef9c612009-03-31 15:16:46 +11004905 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11004906 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08004907 atomic_set(&conf->reshape_stripes, 0);
4908 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4909 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4910 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4911 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4912 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10004913 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08004914 }
4915
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916
4917 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10004918 if (mddev->to_remove == &raid5_attrs_group)
4919 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10004920 else if (mddev->kobj.sd &&
4921 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08004922 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10004923 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08004924 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10004925 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
4926
4927 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10004928 int chunk_size;
NeilBrown4a5add42010-06-01 19:37:28 +10004929 /* read-ahead size must cover two whole stripes, which
4930 * is 2 * (datadisks) * chunksize where 'n' is the
4931 * number of raid devices
4932 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 int data_disks = conf->previous_raid_disks - conf->max_degraded;
4934 int stripe = data_disks *
4935 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
4936 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
4937 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10004938
4939 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10004940
4941 mddev->queue->backing_dev_info.congested_data = mddev;
4942 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10004943
4944 chunk_size = mddev->chunk_sectors << 9;
4945 blk_queue_io_min(mddev->queue, chunk_size);
4946 blk_queue_io_opt(mddev->queue, chunk_size *
4947 (conf->raid_disks - conf->max_degraded));
4948
4949 list_for_each_entry(rdev, &mddev->disks, same_set)
4950 disk_stack_limits(mddev->gendisk, rdev->bdev,
4951 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 }
4953
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 return 0;
4955abort:
NeilBrown01f96c02011-09-21 15:30:20 +10004956 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11004957 print_raid5_conf(conf);
4958 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10004960 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 return -EIO;
4962}
4963
NeilBrownfd01b882011-10-11 16:47:53 +11004964static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965{
NeilBrownd1688a62011-10-11 16:49:52 +11004966 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967
NeilBrown01f96c02011-09-21 15:30:20 +10004968 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10004969 if (mddev->queue)
4970 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10004971 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10004972 mddev->private = NULL;
4973 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 return 0;
4975}
4976
NeilBrownfd01b882011-10-11 16:47:53 +11004977static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978{
NeilBrownd1688a62011-10-11 16:49:52 +11004979 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 int i;
4981
Andre Noll9d8f0362009-06-18 08:45:01 +10004982 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
4983 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07004984 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 for (i = 0; i < conf->raid_disks; i++)
4986 seq_printf (seq, "%s",
4987 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08004988 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990}
4991
NeilBrownd1688a62011-10-11 16:49:52 +11004992static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993{
4994 int i;
4995 struct disk_info *tmp;
4996
NeilBrown0c55e022010-05-03 14:09:02 +10004997 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 if (!conf) {
4999 printk("(conf==NULL)\n");
5000 return;
5001 }
NeilBrown0c55e022010-05-03 14:09:02 +10005002 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5003 conf->raid_disks,
5004 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005
5006 for (i = 0; i < conf->raid_disks; i++) {
5007 char b[BDEVNAME_SIZE];
5008 tmp = conf->disks + i;
5009 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005010 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5011 i, !test_bit(Faulty, &tmp->rdev->flags),
5012 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 }
5014}
5015
NeilBrownfd01b882011-10-11 16:47:53 +11005016static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017{
5018 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005019 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005021 int count = 0;
5022 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023
5024 for (i = 0; i < conf->raid_disks; i++) {
5025 tmp = conf->disks + i;
5026 if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005027 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005028 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005029 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005030 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005031 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 }
5033 }
NeilBrown6b965622010-08-18 11:56:59 +10005034 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005035 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005036 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005038 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039}
5040
NeilBrownb8321b62011-12-23 10:17:51 +11005041static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042{
NeilBrownd1688a62011-10-11 16:49:52 +11005043 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11005045 int number = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 struct disk_info *p = conf->disks + number;
5047
5048 print_raid5_conf(conf);
NeilBrownb8321b62011-12-23 10:17:51 +11005049 if (rdev == p->rdev) {
NeilBrownec32a2b2009-03-31 15:17:38 +11005050 if (number >= conf->raid_disks &&
5051 conf->reshape_progress == MaxSector)
5052 clear_bit(In_sync, &rdev->flags);
5053
NeilBrownb2d444d2005-11-08 21:39:31 -08005054 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055 atomic_read(&rdev->nr_pending)) {
5056 err = -EBUSY;
5057 goto abort;
5058 }
NeilBrowndfc70642008-05-23 13:04:39 -07005059 /* Only remove non-faulty devices if recovery
5060 * isn't possible.
5061 */
5062 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7f0da592011-07-28 11:39:22 +10005063 mddev->recovery_disabled != conf->recovery_disabled &&
NeilBrown674806d2010-06-16 17:17:53 +10005064 !has_failed(conf) &&
NeilBrownec32a2b2009-03-31 15:17:38 +11005065 number < conf->raid_disks) {
NeilBrowndfc70642008-05-23 13:04:39 -07005066 err = -EBUSY;
5067 goto abort;
5068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07005070 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 if (atomic_read(&rdev->nr_pending)) {
5072 /* lost the race, try later */
5073 err = -EBUSY;
5074 p->rdev = rdev;
5075 }
5076 }
5077abort:
5078
5079 print_raid5_conf(conf);
5080 return err;
5081}
5082
NeilBrownfd01b882011-10-11 16:47:53 +11005083static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084{
NeilBrownd1688a62011-10-11 16:49:52 +11005085 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005086 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 int disk;
5088 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005089 int first = 0;
5090 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091
NeilBrown7f0da592011-07-28 11:39:22 +10005092 if (mddev->recovery_disabled == conf->recovery_disabled)
5093 return -EBUSY;
5094
NeilBrown674806d2010-06-16 17:17:53 +10005095 if (has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005097 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098
Neil Brown6c2fce22008-06-28 08:31:31 +10005099 if (rdev->raid_disk >= 0)
5100 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101
5102 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005103 * find the disk ... but prefer rdev->saved_raid_disk
5104 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005106 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005107 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005108 conf->disks[rdev->saved_raid_disk].rdev == NULL)
5109 disk = rdev->saved_raid_disk;
5110 else
Neil Brown6c2fce22008-06-28 08:31:31 +10005111 disk = first;
5112 for ( ; disk <= last ; disk++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 if ((p=conf->disks + disk)->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005114 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005116 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005117 if (rdev->saved_raid_disk != disk)
5118 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005119 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 break;
5121 }
5122 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005123 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124}
5125
NeilBrownfd01b882011-10-11 16:47:53 +11005126static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127{
5128 /* no resync is happening, and there is enough space
5129 * on all devices, so we can resize.
5130 * We need to make sure resync covers any new space.
5131 * If the array is shrinking we should possibly wait until
5132 * any io in the removed space completes, but it hardly seems
5133 * worth it.
5134 */
Andre Noll9d8f0362009-06-18 08:45:01 +10005135 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Dan Williams1f403622009-03-31 14:59:03 +11005136 md_set_array_sectors(mddev, raid5_size(mddev, sectors,
5137 mddev->raid_disks));
Dan Williamsb522adc2009-03-31 15:00:31 +11005138 if (mddev->array_sectors >
5139 raid5_size(mddev, sectors, mddev->raid_disks))
5140 return -EINVAL;
Andre Nollf233ea52008-07-21 17:05:22 +10005141 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005142 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005143 if (sectors > mddev->dev_sectors &&
5144 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005145 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5147 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005148 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005149 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 return 0;
5151}
5152
NeilBrownfd01b882011-10-11 16:47:53 +11005153static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005154{
5155 /* Can only proceed if there are plenty of stripe_heads.
5156 * We need a minimum of one full stripe,, and for sensible progress
5157 * it is best to have about 4 times that.
5158 * If we require 4 times, then the default 256 4K stripe_heads will
5159 * allow for chunk sizes up to 256K, which is probably OK.
5160 * If the chunk size is greater, user-space should request more
5161 * stripe_heads first.
5162 */
NeilBrownd1688a62011-10-11 16:49:52 +11005163 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005164 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5165 > conf->max_nr_stripes ||
5166 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5167 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005168 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5169 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005170 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5171 / STRIPE_SIZE)*4);
5172 return 0;
5173 }
5174 return 1;
5175}
5176
NeilBrownfd01b882011-10-11 16:47:53 +11005177static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005178{
NeilBrownd1688a62011-10-11 16:49:52 +11005179 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005180
NeilBrown88ce4932009-03-31 15:24:23 +11005181 if (mddev->delta_disks == 0 &&
5182 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005183 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005184 return 0; /* nothing to do */
NeilBrowndba034e2008-08-05 15:54:13 +10005185 if (mddev->bitmap)
5186 /* Cannot grow a bitmap yet */
5187 return -EBUSY;
NeilBrown674806d2010-06-16 17:17:53 +10005188 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005189 return -EINVAL;
5190 if (mddev->delta_disks < 0) {
5191 /* We might be able to shrink, but the devices must
5192 * be made bigger first.
5193 * For raid6, 4 is the minimum size.
5194 * Otherwise 2 is the minimum
5195 */
5196 int min = 2;
5197 if (mddev->level == 6)
5198 min = 4;
5199 if (mddev->raid_disks + mddev->delta_disks < min)
5200 return -EINVAL;
5201 }
NeilBrown29269552006-03-27 01:18:10 -08005202
NeilBrown01ee22b2009-06-18 08:47:20 +10005203 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005204 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005205
NeilBrownec32a2b2009-03-31 15:17:38 +11005206 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
NeilBrown63c70c42006-03-27 01:18:13 -08005207}
5208
NeilBrownfd01b882011-10-11 16:47:53 +11005209static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005210{
NeilBrownd1688a62011-10-11 16:49:52 +11005211 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005212 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005213 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005214 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005215
NeilBrownf4168852007-02-28 20:11:53 -08005216 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005217 return -EBUSY;
5218
NeilBrown01ee22b2009-06-18 08:47:20 +10005219 if (!check_stripe_cache(mddev))
5220 return -ENOSPC;
5221
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005222 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown469518a2011-01-31 11:57:43 +11005223 if (!test_bit(In_sync, &rdev->flags)
5224 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005225 spares++;
NeilBrown63c70c42006-03-27 01:18:13 -08005226
NeilBrownf4168852007-02-28 20:11:53 -08005227 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005228 /* Not enough devices even to make a degraded array
5229 * of that size
5230 */
5231 return -EINVAL;
5232
NeilBrownec32a2b2009-03-31 15:17:38 +11005233 /* Refuse to reduce size of the array. Any reductions in
5234 * array size must be through explicit setting of array_size
5235 * attribute.
5236 */
5237 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5238 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10005239 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11005240 "before number of disks\n", mdname(mddev));
5241 return -EINVAL;
5242 }
5243
NeilBrownf6705572006-03-27 01:18:11 -08005244 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005245 spin_lock_irq(&conf->device_lock);
5246 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005247 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005248 conf->prev_chunk_sectors = conf->chunk_sectors;
5249 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005250 conf->prev_algo = conf->algorithm;
5251 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11005252 if (mddev->delta_disks < 0)
5253 conf->reshape_progress = raid5_size(mddev, 0, 0);
5254 else
5255 conf->reshape_progress = 0;
5256 conf->reshape_safe = conf->reshape_progress;
NeilBrown86b42c72009-03-31 15:19:03 +11005257 conf->generation++;
NeilBrown29269552006-03-27 01:18:10 -08005258 spin_unlock_irq(&conf->device_lock);
5259
5260 /* Add some new drives, as many as will fit.
5261 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10005262 * Don't add devices if we are reducing the number of
5263 * devices in the array. This is because it is not possible
5264 * to correctly record the "partially reconstructed" state of
5265 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08005266 */
NeilBrown87a8dec2011-01-31 11:57:43 +11005267 if (mddev->delta_disks >= 0) {
5268 int added_devices = 0;
5269 list_for_each_entry(rdev, &mddev->disks, same_set)
5270 if (rdev->raid_disk < 0 &&
5271 !test_bit(Faulty, &rdev->flags)) {
5272 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11005273 if (rdev->raid_disk
5274 >= conf->previous_raid_disks) {
5275 set_bit(In_sync, &rdev->flags);
5276 added_devices++;
5277 } else
5278 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10005279
5280 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11005281 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11005282 }
NeilBrown87a8dec2011-01-31 11:57:43 +11005283 } else if (rdev->raid_disk >= conf->previous_raid_disks
5284 && !test_bit(Faulty, &rdev->flags)) {
5285 /* This is a spare that was manually added */
5286 set_bit(In_sync, &rdev->flags);
5287 added_devices++;
5288 }
NeilBrown29269552006-03-27 01:18:10 -08005289
NeilBrown87a8dec2011-01-31 11:57:43 +11005290 /* When a reshape changes the number of devices,
5291 * ->degraded is measured against the larger of the
5292 * pre and post number of devices.
5293 */
NeilBrownec32a2b2009-03-31 15:17:38 +11005294 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005295 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11005296 spin_unlock_irqrestore(&conf->device_lock, flags);
5297 }
NeilBrown63c70c42006-03-27 01:18:13 -08005298 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10005299 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07005300 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005301
NeilBrown29269552006-03-27 01:18:10 -08005302 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5303 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5304 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5305 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5306 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005307 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08005308 if (!mddev->sync_thread) {
5309 mddev->recovery = 0;
5310 spin_lock_irq(&conf->device_lock);
5311 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005312 conf->reshape_progress = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005313 spin_unlock_irq(&conf->device_lock);
5314 return -EAGAIN;
5315 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005316 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005317 md_wakeup_thread(mddev->sync_thread);
5318 md_new_event(mddev);
5319 return 0;
5320}
NeilBrown29269552006-03-27 01:18:10 -08005321
NeilBrownec32a2b2009-03-31 15:17:38 +11005322/* This is called from the reshape thread and should make any
5323 * changes needed in 'conf'
5324 */
NeilBrownd1688a62011-10-11 16:49:52 +11005325static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08005326{
NeilBrown29269552006-03-27 01:18:10 -08005327
NeilBrownf6705572006-03-27 01:18:11 -08005328 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
Dan Williams80c3a6c2009-03-17 18:10:40 -07005329
NeilBrownf6705572006-03-27 01:18:11 -08005330 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11005331 conf->previous_raid_disks = conf->raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005332 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08005333 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005334 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07005335
5336 /* read-ahead size must cover two whole stripes, which is
5337 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5338 */
NeilBrown4a5add42010-06-01 19:37:28 +10005339 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11005340 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005341 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11005342 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07005343 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5344 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5345 }
NeilBrown29269552006-03-27 01:18:10 -08005346 }
NeilBrown29269552006-03-27 01:18:10 -08005347}
5348
NeilBrownec32a2b2009-03-31 15:17:38 +11005349/* This is called from the raid5d thread with mddev_lock held.
5350 * It makes config changes to the device.
5351 */
NeilBrownfd01b882011-10-11 16:47:53 +11005352static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11005353{
NeilBrownd1688a62011-10-11 16:49:52 +11005354 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11005355
5356 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5357
NeilBrownec32a2b2009-03-31 15:17:38 +11005358 if (mddev->delta_disks > 0) {
5359 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5360 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005361 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11005362 } else {
5363 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11005364 spin_lock_irq(&conf->device_lock);
5365 mddev->degraded = calc_degraded(conf);
5366 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11005367 for (d = conf->raid_disks ;
5368 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10005369 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11005370 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownb8321b62011-12-23 10:17:51 +11005371 if (rdev &&
5372 raid5_remove_disk(mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10005373 sysfs_unlink_rdev(mddev, rdev);
NeilBrown1a67dde2009-08-13 10:41:49 +10005374 rdev->raid_disk = -1;
5375 }
5376 }
NeilBrowncea9c222009-03-31 15:15:05 +11005377 }
NeilBrown88ce4932009-03-31 15:24:23 +11005378 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005379 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11005380 mddev->reshape_position = MaxSector;
5381 mddev->delta_disks = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11005382 }
5383}
5384
NeilBrownfd01b882011-10-11 16:47:53 +11005385static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07005386{
NeilBrownd1688a62011-10-11 16:49:52 +11005387 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07005388
5389 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08005390 case 2: /* resume for a suspend */
5391 wake_up(&conf->wait_for_overlap);
5392 break;
5393
NeilBrown72626682005-09-09 16:23:54 -07005394 case 1: /* stop all writes */
5395 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005396 /* '2' tells resync/reshape to pause so that all
5397 * active stripes can drain
5398 */
5399 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07005400 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005401 atomic_read(&conf->active_stripes) == 0 &&
5402 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07005403 conf->device_lock, /* nothing */);
NeilBrown64bd6602009-08-03 10:59:58 +10005404 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07005405 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005406 /* allow reshape to continue */
5407 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005408 break;
5409
5410 case 0: /* re-enable writes */
5411 spin_lock_irq(&conf->device_lock);
5412 conf->quiesce = 0;
5413 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08005414 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005415 spin_unlock_irq(&conf->device_lock);
5416 break;
5417 }
NeilBrown72626682005-09-09 16:23:54 -07005418}
NeilBrownb15c2e52006-01-06 00:20:16 -08005419
NeilBrownd562b0c2009-03-31 14:39:39 +11005420
NeilBrownfd01b882011-10-11 16:47:53 +11005421static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11005422{
NeilBrowne373ab12011-10-11 16:48:59 +11005423 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07005424 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11005425
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005426 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11005427 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10005428 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
5429 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005430 return ERR_PTR(-EINVAL);
5431 }
5432
NeilBrowne373ab12011-10-11 16:48:59 +11005433 sectors = raid0_conf->strip_zone[0].zone_end;
5434 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10005435 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005436 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11005437 mddev->new_layout = ALGORITHM_PARITY_N;
5438 mddev->new_chunk_sectors = mddev->chunk_sectors;
5439 mddev->raid_disks += 1;
5440 mddev->delta_disks = 1;
5441 /* make sure it will be not marked as dirty */
5442 mddev->recovery_cp = MaxSector;
5443
5444 return setup_conf(mddev);
5445}
5446
5447
NeilBrownfd01b882011-10-11 16:47:53 +11005448static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005449{
5450 int chunksect;
5451
5452 if (mddev->raid_disks != 2 ||
5453 mddev->degraded > 1)
5454 return ERR_PTR(-EINVAL);
5455
5456 /* Should check if there are write-behind devices? */
5457
5458 chunksect = 64*2; /* 64K by default */
5459
5460 /* The array must be an exact multiple of chunksize */
5461 while (chunksect && (mddev->array_sectors & (chunksect-1)))
5462 chunksect >>= 1;
5463
5464 if ((chunksect<<9) < STRIPE_SIZE)
5465 /* array size does not allow a suitable chunk size */
5466 return ERR_PTR(-EINVAL);
5467
5468 mddev->new_level = 5;
5469 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10005470 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11005471
5472 return setup_conf(mddev);
5473}
5474
NeilBrownfd01b882011-10-11 16:47:53 +11005475static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11005476{
5477 int new_layout;
5478
5479 switch (mddev->layout) {
5480 case ALGORITHM_LEFT_ASYMMETRIC_6:
5481 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
5482 break;
5483 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5484 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
5485 break;
5486 case ALGORITHM_LEFT_SYMMETRIC_6:
5487 new_layout = ALGORITHM_LEFT_SYMMETRIC;
5488 break;
5489 case ALGORITHM_RIGHT_SYMMETRIC_6:
5490 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
5491 break;
5492 case ALGORITHM_PARITY_0_6:
5493 new_layout = ALGORITHM_PARITY_0;
5494 break;
5495 case ALGORITHM_PARITY_N:
5496 new_layout = ALGORITHM_PARITY_N;
5497 break;
5498 default:
5499 return ERR_PTR(-EINVAL);
5500 }
5501 mddev->new_level = 5;
5502 mddev->new_layout = new_layout;
5503 mddev->delta_disks = -1;
5504 mddev->raid_disks -= 1;
5505 return setup_conf(mddev);
5506}
5507
NeilBrownd562b0c2009-03-31 14:39:39 +11005508
NeilBrownfd01b882011-10-11 16:47:53 +11005509static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11005510{
NeilBrown88ce4932009-03-31 15:24:23 +11005511 /* For a 2-drive array, the layout and chunk size can be changed
5512 * immediately as not restriping is needed.
5513 * For larger arrays we record the new value - after validation
5514 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11005515 */
NeilBrownd1688a62011-10-11 16:49:52 +11005516 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10005517 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11005518
NeilBrown597a7112009-06-18 08:47:42 +10005519 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11005520 return -EINVAL;
5521 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005522 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11005523 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005524 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11005525 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005526 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11005527 /* not factor of array size */
5528 return -EINVAL;
5529 }
5530
5531 /* They look valid */
5532
NeilBrown88ce4932009-03-31 15:24:23 +11005533 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10005534 /* can make the change immediately */
5535 if (mddev->new_layout >= 0) {
5536 conf->algorithm = mddev->new_layout;
5537 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11005538 }
5539 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10005540 conf->chunk_sectors = new_chunk ;
5541 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11005542 }
5543 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5544 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11005545 }
NeilBrown50ac1682009-06-18 08:47:55 +10005546 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11005547}
5548
NeilBrownfd01b882011-10-11 16:47:53 +11005549static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11005550{
NeilBrown597a7112009-06-18 08:47:42 +10005551 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10005552
NeilBrown597a7112009-06-18 08:47:42 +10005553 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11005554 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005555 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005556 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11005557 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005558 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11005559 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005560 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11005561 /* not factor of array size */
5562 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005563 }
NeilBrown88ce4932009-03-31 15:24:23 +11005564
5565 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10005566 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11005567}
5568
NeilBrownfd01b882011-10-11 16:47:53 +11005569static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005570{
5571 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005572 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005573 * raid1 - if there are two drives. We need to know the chunk size
5574 * raid4 - trivial - just use a raid4 layout.
5575 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005576 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005577 if (mddev->level == 0)
5578 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11005579 if (mddev->level == 1)
5580 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11005581 if (mddev->level == 4) {
5582 mddev->new_layout = ALGORITHM_PARITY_N;
5583 mddev->new_level = 5;
5584 return setup_conf(mddev);
5585 }
NeilBrownfc9739c2009-03-31 14:57:20 +11005586 if (mddev->level == 6)
5587 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11005588
5589 return ERR_PTR(-EINVAL);
5590}
5591
NeilBrownfd01b882011-10-11 16:47:53 +11005592static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11005593{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005594 /* raid4 can take over:
5595 * raid0 - if there is only one strip zone
5596 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11005597 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005598 if (mddev->level == 0)
5599 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11005600 if (mddev->level == 5 &&
5601 mddev->layout == ALGORITHM_PARITY_N) {
5602 mddev->new_layout = 0;
5603 mddev->new_level = 4;
5604 return setup_conf(mddev);
5605 }
5606 return ERR_PTR(-EINVAL);
5607}
NeilBrownd562b0c2009-03-31 14:39:39 +11005608
NeilBrown84fc4b52011-10-11 16:49:58 +11005609static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11005610
NeilBrownfd01b882011-10-11 16:47:53 +11005611static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11005612{
5613 /* Currently can only take over a raid5. We map the
5614 * personality to an equivalent raid6 personality
5615 * with the Q block at the end.
5616 */
5617 int new_layout;
5618
5619 if (mddev->pers != &raid5_personality)
5620 return ERR_PTR(-EINVAL);
5621 if (mddev->degraded > 1)
5622 return ERR_PTR(-EINVAL);
5623 if (mddev->raid_disks > 253)
5624 return ERR_PTR(-EINVAL);
5625 if (mddev->raid_disks < 3)
5626 return ERR_PTR(-EINVAL);
5627
5628 switch (mddev->layout) {
5629 case ALGORITHM_LEFT_ASYMMETRIC:
5630 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
5631 break;
5632 case ALGORITHM_RIGHT_ASYMMETRIC:
5633 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
5634 break;
5635 case ALGORITHM_LEFT_SYMMETRIC:
5636 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
5637 break;
5638 case ALGORITHM_RIGHT_SYMMETRIC:
5639 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
5640 break;
5641 case ALGORITHM_PARITY_0:
5642 new_layout = ALGORITHM_PARITY_0_6;
5643 break;
5644 case ALGORITHM_PARITY_N:
5645 new_layout = ALGORITHM_PARITY_N;
5646 break;
5647 default:
5648 return ERR_PTR(-EINVAL);
5649 }
5650 mddev->new_level = 6;
5651 mddev->new_layout = new_layout;
5652 mddev->delta_disks = 1;
5653 mddev->raid_disks += 1;
5654 return setup_conf(mddev);
5655}
5656
5657
NeilBrown84fc4b52011-10-11 16:49:58 +11005658static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07005659{
5660 .name = "raid6",
5661 .level = 6,
5662 .owner = THIS_MODULE,
5663 .make_request = make_request,
5664 .run = run,
5665 .stop = stop,
5666 .status = status,
5667 .error_handler = error,
5668 .hot_add_disk = raid5_add_disk,
5669 .hot_remove_disk= raid5_remove_disk,
5670 .spare_active = raid5_spare_active,
5671 .sync_request = sync_request,
5672 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005673 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10005674 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08005675 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005676 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07005677 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11005678 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07005679};
NeilBrown84fc4b52011-10-11 16:49:58 +11005680static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681{
5682 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08005683 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684 .owner = THIS_MODULE,
5685 .make_request = make_request,
5686 .run = run,
5687 .stop = stop,
5688 .status = status,
5689 .error_handler = error,
5690 .hot_add_disk = raid5_add_disk,
5691 .hot_remove_disk= raid5_remove_disk,
5692 .spare_active = raid5_spare_active,
5693 .sync_request = sync_request,
5694 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005695 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08005696 .check_reshape = raid5_check_reshape,
5697 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005698 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07005699 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11005700 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701};
5702
NeilBrown84fc4b52011-10-11 16:49:58 +11005703static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704{
NeilBrown2604b702006-01-06 00:20:36 -08005705 .name = "raid4",
5706 .level = 4,
5707 .owner = THIS_MODULE,
5708 .make_request = make_request,
5709 .run = run,
5710 .stop = stop,
5711 .status = status,
5712 .error_handler = error,
5713 .hot_add_disk = raid5_add_disk,
5714 .hot_remove_disk= raid5_remove_disk,
5715 .spare_active = raid5_spare_active,
5716 .sync_request = sync_request,
5717 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005718 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08005719 .check_reshape = raid5_check_reshape,
5720 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005721 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08005722 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11005723 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08005724};
5725
5726static int __init raid5_init(void)
5727{
NeilBrown16a53ec2006-06-26 00:27:38 -07005728 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08005729 register_md_personality(&raid5_personality);
5730 register_md_personality(&raid4_personality);
5731 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732}
5733
NeilBrown2604b702006-01-06 00:20:36 -08005734static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735{
NeilBrown16a53ec2006-06-26 00:27:38 -07005736 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08005737 unregister_md_personality(&raid5_personality);
5738 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739}
5740
5741module_init(raid5_init);
5742module_exit(raid5_exit);
5743MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11005744MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08005746MODULE_ALIAS("md-raid5");
5747MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08005748MODULE_ALIAS("md-level-5");
5749MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07005750MODULE_ALIAS("md-personality-8"); /* RAID6 */
5751MODULE_ALIAS("md-raid6");
5752MODULE_ALIAS("md-level-6");
5753
5754/* This used to be two separate modules, they were: */
5755MODULE_ALIAS("raid5");
5756MODULE_ALIAS("raid6");