blob: 14878a9ae09d3be52ebe91fd21b32c16584c0c95 [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;
NeilBrown977df362011-12-23 10:17:53 +1100506 struct bio *bi, *rbi;
507 struct md_rdev *rdev, *rrdev = NULL;
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;
NeilBrown977df362011-12-23 10:17:53 +1100519 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700520
521 bi->bi_rw = rw;
NeilBrown977df362011-12-23 10:17:53 +1100522 rbi->bi_rw = rw;
523 if (rw & WRITE) {
Dan Williams91c00922007-01-02 13:52:30 -0700524 bi->bi_end_io = raid5_end_write_request;
NeilBrown977df362011-12-23 10:17:53 +1100525 rbi->bi_end_io = raid5_end_write_request;
526 } else
Dan Williams91c00922007-01-02 13:52:30 -0700527 bi->bi_end_io = raid5_end_read_request;
528
529 rcu_read_lock();
NeilBrown977df362011-12-23 10:17:53 +1100530 rdev = rcu_dereference(conf->disks[i].rdev);
531 if (rw & WRITE)
532 rrdev = rcu_dereference(conf->disks[i].replacement);
533 else if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrown14a75d32011-12-23 10:17:52 +1100534 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown977df362011-12-23 10:17:53 +1100535
Dan Williams91c00922007-01-02 13:52:30 -0700536 if (rdev && test_bit(Faulty, &rdev->flags))
537 rdev = NULL;
538 if (rdev)
539 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100540 if (rrdev && test_bit(Faulty, &rrdev->flags))
541 rrdev = NULL;
542 if (rrdev)
543 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700544 rcu_read_unlock();
545
NeilBrown73e92e52011-07-28 11:39:22 +1000546 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100547 * need to check for writes. We never accept write errors
548 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000549 */
550 while ((rw & WRITE) && rdev &&
551 test_bit(WriteErrorSeen, &rdev->flags)) {
552 sector_t first_bad;
553 int bad_sectors;
554 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
555 &first_bad, &bad_sectors);
556 if (!bad)
557 break;
558
559 if (bad < 0) {
560 set_bit(BlockedBadBlocks, &rdev->flags);
561 if (!conf->mddev->external &&
562 conf->mddev->flags) {
563 /* It is very unlikely, but we might
564 * still need to write out the
565 * bad block log - better give it
566 * a chance*/
567 md_check_recovery(conf->mddev);
568 }
569 md_wait_for_blocked_rdev(rdev, conf->mddev);
570 } else {
571 /* Acknowledged bad block - skip the write */
572 rdev_dec_pending(rdev, conf->mddev);
573 rdev = NULL;
574 }
575 }
576
Dan Williams91c00922007-01-02 13:52:30 -0700577 if (rdev) {
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000578 if (s->syncing || s->expanding || s->expanded)
Dan Williams91c00922007-01-02 13:52:30 -0700579 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
580
Dan Williams2b7497f2008-06-28 08:31:52 +1000581 set_bit(STRIPE_IO_STARTED, &sh->state);
582
Dan Williams91c00922007-01-02 13:52:30 -0700583 bi->bi_bdev = rdev->bdev;
584 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700585 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700586 bi->bi_rw, i);
587 atomic_inc(&sh->count);
588 bi->bi_sector = sh->sector + rdev->data_offset;
589 bi->bi_flags = 1 << BIO_UPTODATE;
Dan Williams91c00922007-01-02 13:52:30 -0700590 bi->bi_idx = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700591 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
592 bi->bi_io_vec[0].bv_offset = 0;
593 bi->bi_size = STRIPE_SIZE;
594 bi->bi_next = NULL;
NeilBrown977df362011-12-23 10:17:53 +1100595 if (rrdev)
596 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Dan Williams91c00922007-01-02 13:52:30 -0700597 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100598 }
599 if (rrdev) {
600 if (s->syncing || s->expanding || s->expanded)
601 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
602
603 set_bit(STRIPE_IO_STARTED, &sh->state);
604
605 rbi->bi_bdev = rrdev->bdev;
606 pr_debug("%s: for %llu schedule op %ld on "
607 "replacement disc %d\n",
608 __func__, (unsigned long long)sh->sector,
609 rbi->bi_rw, i);
610 atomic_inc(&sh->count);
611 rbi->bi_sector = sh->sector + rrdev->data_offset;
612 rbi->bi_flags = 1 << BIO_UPTODATE;
613 rbi->bi_idx = 0;
614 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
615 rbi->bi_io_vec[0].bv_offset = 0;
616 rbi->bi_size = STRIPE_SIZE;
617 rbi->bi_next = NULL;
618 generic_make_request(rbi);
619 }
620 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000621 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700622 set_bit(STRIPE_DEGRADED, &sh->state);
623 pr_debug("skip op %ld on disc %d for sector %llu\n",
624 bi->bi_rw, i, (unsigned long long)sh->sector);
625 clear_bit(R5_LOCKED, &sh->dev[i].flags);
626 set_bit(STRIPE_HANDLE, &sh->state);
627 }
628 }
629}
630
631static struct dma_async_tx_descriptor *
632async_copy_data(int frombio, struct bio *bio, struct page *page,
633 sector_t sector, struct dma_async_tx_descriptor *tx)
634{
635 struct bio_vec *bvl;
636 struct page *bio_page;
637 int i;
638 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700639 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700640 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700641
642 if (bio->bi_sector >= sector)
643 page_offset = (signed)(bio->bi_sector - sector) * 512;
644 else
645 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700646
Dan Williams0403e382009-09-08 17:42:50 -0700647 if (frombio)
648 flags |= ASYNC_TX_FENCE;
649 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
650
Dan Williams91c00922007-01-02 13:52:30 -0700651 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000652 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700653 int clen;
654 int b_offset = 0;
655
656 if (page_offset < 0) {
657 b_offset = -page_offset;
658 page_offset += b_offset;
659 len -= b_offset;
660 }
661
662 if (len > 0 && page_offset + len > STRIPE_SIZE)
663 clen = STRIPE_SIZE - page_offset;
664 else
665 clen = len;
666
667 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000668 b_offset += bvl->bv_offset;
669 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700670 if (frombio)
671 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700672 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700673 else
674 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700675 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700676 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700677 /* chain the operations */
678 submit.depend_tx = tx;
679
Dan Williams91c00922007-01-02 13:52:30 -0700680 if (clen < len) /* hit end of page */
681 break;
682 page_offset += len;
683 }
684
685 return tx;
686}
687
688static void ops_complete_biofill(void *stripe_head_ref)
689{
690 struct stripe_head *sh = stripe_head_ref;
691 struct bio *return_bi = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +1100692 struct r5conf *conf = sh->raid_conf;
Dan Williamse4d84902007-09-24 10:06:13 -0700693 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700694
Harvey Harrisone46b2722008-04-28 02:15:50 -0700695 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700696 (unsigned long long)sh->sector);
697
698 /* clear completed biofills */
Dan Williams83de75c2008-06-28 08:31:58 +1000699 spin_lock_irq(&conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700700 for (i = sh->disks; i--; ) {
701 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700702
703 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700704 /* and check if we need to reply to a read request,
705 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000706 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700707 */
708 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700709 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700710
Dan Williams91c00922007-01-02 13:52:30 -0700711 BUG_ON(!dev->read);
712 rbi = dev->read;
713 dev->read = NULL;
714 while (rbi && rbi->bi_sector <
715 dev->sector + STRIPE_SECTORS) {
716 rbi2 = r5_next_bio(rbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +0200717 if (!raid5_dec_bi_phys_segments(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700718 rbi->bi_next = return_bi;
719 return_bi = rbi;
720 }
Dan Williams91c00922007-01-02 13:52:30 -0700721 rbi = rbi2;
722 }
723 }
724 }
Dan Williams83de75c2008-06-28 08:31:58 +1000725 spin_unlock_irq(&conf->device_lock);
726 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700727
728 return_io(return_bi);
729
Dan Williamse4d84902007-09-24 10:06:13 -0700730 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700731 release_stripe(sh);
732}
733
734static void ops_run_biofill(struct stripe_head *sh)
735{
736 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +1100737 struct r5conf *conf = sh->raid_conf;
Dan Williamsa08abd82009-06-03 11:43:59 -0700738 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700739 int i;
740
Harvey Harrisone46b2722008-04-28 02:15:50 -0700741 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700742 (unsigned long long)sh->sector);
743
744 for (i = sh->disks; i--; ) {
745 struct r5dev *dev = &sh->dev[i];
746 if (test_bit(R5_Wantfill, &dev->flags)) {
747 struct bio *rbi;
748 spin_lock_irq(&conf->device_lock);
749 dev->read = rbi = dev->toread;
750 dev->toread = NULL;
751 spin_unlock_irq(&conf->device_lock);
752 while (rbi && rbi->bi_sector <
753 dev->sector + STRIPE_SECTORS) {
754 tx = async_copy_data(0, rbi, dev->page,
755 dev->sector, tx);
756 rbi = r5_next_bio(rbi, dev->sector);
757 }
758 }
759 }
760
761 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700762 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
763 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700764}
765
Dan Williams4e7d2c02009-08-29 19:13:11 -0700766static void mark_target_uptodate(struct stripe_head *sh, int target)
767{
768 struct r5dev *tgt;
769
770 if (target < 0)
771 return;
772
773 tgt = &sh->dev[target];
774 set_bit(R5_UPTODATE, &tgt->flags);
775 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
776 clear_bit(R5_Wantcompute, &tgt->flags);
777}
778
Dan Williamsac6b53b2009-07-14 13:40:19 -0700779static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700780{
781 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700782
Harvey Harrisone46b2722008-04-28 02:15:50 -0700783 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700784 (unsigned long long)sh->sector);
785
Dan Williamsac6b53b2009-07-14 13:40:19 -0700786 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700787 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700788 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700789
Dan Williamsecc65c92008-06-28 08:31:57 +1000790 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
791 if (sh->check_state == check_state_compute_run)
792 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700793 set_bit(STRIPE_HANDLE, &sh->state);
794 release_stripe(sh);
795}
796
Dan Williamsd6f38f32009-07-14 11:50:52 -0700797/* return a pointer to the address conversion region of the scribble buffer */
798static addr_conv_t *to_addr_conv(struct stripe_head *sh,
799 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700800{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700801 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
802}
803
804static struct dma_async_tx_descriptor *
805ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
806{
Dan Williams91c00922007-01-02 13:52:30 -0700807 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700808 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700809 int target = sh->ops.target;
810 struct r5dev *tgt = &sh->dev[target];
811 struct page *xor_dest = tgt->page;
812 int count = 0;
813 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700814 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700815 int i;
816
817 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700818 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700819 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
820
821 for (i = disks; i--; )
822 if (i != target)
823 xor_srcs[count++] = sh->dev[i].page;
824
825 atomic_inc(&sh->count);
826
Dan Williams0403e382009-09-08 17:42:50 -0700827 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700828 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700829 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700830 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700831 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700832 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700833
Dan Williams91c00922007-01-02 13:52:30 -0700834 return tx;
835}
836
Dan Williamsac6b53b2009-07-14 13:40:19 -0700837/* set_syndrome_sources - populate source buffers for gen_syndrome
838 * @srcs - (struct page *) array of size sh->disks
839 * @sh - stripe_head to parse
840 *
841 * Populates srcs in proper layout order for the stripe and returns the
842 * 'count' of sources to be used in a call to async_gen_syndrome. The P
843 * destination buffer is recorded in srcs[count] and the Q destination
844 * is recorded in srcs[count+1]].
845 */
846static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
847{
848 int disks = sh->disks;
849 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
850 int d0_idx = raid6_d0(sh);
851 int count;
852 int i;
853
854 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100855 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700856
857 count = 0;
858 i = d0_idx;
859 do {
860 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
861
862 srcs[slot] = sh->dev[i].page;
863 i = raid6_next_disk(i, disks);
864 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700865
NeilBrowne4424fe2009-10-16 16:27:34 +1100866 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700867}
868
869static struct dma_async_tx_descriptor *
870ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
871{
872 int disks = sh->disks;
873 struct page **blocks = percpu->scribble;
874 int target;
875 int qd_idx = sh->qd_idx;
876 struct dma_async_tx_descriptor *tx;
877 struct async_submit_ctl submit;
878 struct r5dev *tgt;
879 struct page *dest;
880 int i;
881 int count;
882
883 if (sh->ops.target < 0)
884 target = sh->ops.target2;
885 else if (sh->ops.target2 < 0)
886 target = sh->ops.target;
887 else
888 /* we should only have one valid target */
889 BUG();
890 BUG_ON(target < 0);
891 pr_debug("%s: stripe %llu block: %d\n",
892 __func__, (unsigned long long)sh->sector, target);
893
894 tgt = &sh->dev[target];
895 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
896 dest = tgt->page;
897
898 atomic_inc(&sh->count);
899
900 if (target == qd_idx) {
901 count = set_syndrome_sources(blocks, sh);
902 blocks[count] = NULL; /* regenerating p is not necessary */
903 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700904 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
905 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700906 to_addr_conv(sh, percpu));
907 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
908 } else {
909 /* Compute any data- or p-drive using XOR */
910 count = 0;
911 for (i = disks; i-- ; ) {
912 if (i == target || i == qd_idx)
913 continue;
914 blocks[count++] = sh->dev[i].page;
915 }
916
Dan Williams0403e382009-09-08 17:42:50 -0700917 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
918 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700919 to_addr_conv(sh, percpu));
920 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
921 }
922
923 return tx;
924}
925
926static struct dma_async_tx_descriptor *
927ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
928{
929 int i, count, disks = sh->disks;
930 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
931 int d0_idx = raid6_d0(sh);
932 int faila = -1, failb = -1;
933 int target = sh->ops.target;
934 int target2 = sh->ops.target2;
935 struct r5dev *tgt = &sh->dev[target];
936 struct r5dev *tgt2 = &sh->dev[target2];
937 struct dma_async_tx_descriptor *tx;
938 struct page **blocks = percpu->scribble;
939 struct async_submit_ctl submit;
940
941 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
942 __func__, (unsigned long long)sh->sector, target, target2);
943 BUG_ON(target < 0 || target2 < 0);
944 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
945 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
946
Dan Williams6c910a72009-09-16 12:24:54 -0700947 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -0700948 * slot number conversion for 'faila' and 'failb'
949 */
950 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100951 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700952 count = 0;
953 i = d0_idx;
954 do {
955 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
956
957 blocks[slot] = sh->dev[i].page;
958
959 if (i == target)
960 faila = slot;
961 if (i == target2)
962 failb = slot;
963 i = raid6_next_disk(i, disks);
964 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700965
966 BUG_ON(faila == failb);
967 if (failb < faila)
968 swap(faila, failb);
969 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
970 __func__, (unsigned long long)sh->sector, faila, failb);
971
972 atomic_inc(&sh->count);
973
974 if (failb == syndrome_disks+1) {
975 /* Q disk is one of the missing disks */
976 if (faila == syndrome_disks) {
977 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -0700978 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
979 ops_complete_compute, sh,
980 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +1100981 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700982 STRIPE_SIZE, &submit);
983 } else {
984 struct page *dest;
985 int data_target;
986 int qd_idx = sh->qd_idx;
987
988 /* Missing D+Q: recompute D from P, then recompute Q */
989 if (target == qd_idx)
990 data_target = target2;
991 else
992 data_target = target;
993
994 count = 0;
995 for (i = disks; i-- ; ) {
996 if (i == data_target || i == qd_idx)
997 continue;
998 blocks[count++] = sh->dev[i].page;
999 }
1000 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001001 init_async_submit(&submit,
1002 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1003 NULL, NULL, NULL,
1004 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001005 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1006 &submit);
1007
1008 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001009 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1010 ops_complete_compute, sh,
1011 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001012 return async_gen_syndrome(blocks, 0, count+2,
1013 STRIPE_SIZE, &submit);
1014 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001015 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001016 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1017 ops_complete_compute, sh,
1018 to_addr_conv(sh, percpu));
1019 if (failb == syndrome_disks) {
1020 /* We're missing D+P. */
1021 return async_raid6_datap_recov(syndrome_disks+2,
1022 STRIPE_SIZE, faila,
1023 blocks, &submit);
1024 } else {
1025 /* We're missing D+D. */
1026 return async_raid6_2data_recov(syndrome_disks+2,
1027 STRIPE_SIZE, faila, failb,
1028 blocks, &submit);
1029 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001030 }
1031}
1032
1033
Dan Williams91c00922007-01-02 13:52:30 -07001034static void ops_complete_prexor(void *stripe_head_ref)
1035{
1036 struct stripe_head *sh = stripe_head_ref;
1037
Harvey Harrisone46b2722008-04-28 02:15:50 -07001038 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001039 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001040}
1041
1042static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001043ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1044 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001045{
Dan Williams91c00922007-01-02 13:52:30 -07001046 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001047 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001048 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001049 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001050
1051 /* existing parity data subtracted */
1052 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1053
Harvey Harrisone46b2722008-04-28 02:15:50 -07001054 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001055 (unsigned long long)sh->sector);
1056
1057 for (i = disks; i--; ) {
1058 struct r5dev *dev = &sh->dev[i];
1059 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001060 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001061 xor_srcs[count++] = dev->page;
1062 }
1063
Dan Williams0403e382009-09-08 17:42:50 -07001064 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001065 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001066 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001067
1068 return tx;
1069}
1070
1071static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001072ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001073{
1074 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001075 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001076
Harvey Harrisone46b2722008-04-28 02:15:50 -07001077 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001078 (unsigned long long)sh->sector);
1079
1080 for (i = disks; i--; ) {
1081 struct r5dev *dev = &sh->dev[i];
1082 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001083
Dan Williamsd8ee0722008-06-28 08:32:06 +10001084 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001085 struct bio *wbi;
1086
NeilBrowncbe47ec2011-07-26 11:20:35 +10001087 spin_lock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001088 chosen = dev->towrite;
1089 dev->towrite = NULL;
1090 BUG_ON(dev->written);
1091 wbi = dev->written = chosen;
NeilBrowncbe47ec2011-07-26 11:20:35 +10001092 spin_unlock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001093
1094 while (wbi && wbi->bi_sector <
1095 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001096 if (wbi->bi_rw & REQ_FUA)
1097 set_bit(R5_WantFUA, &dev->flags);
Dan Williams91c00922007-01-02 13:52:30 -07001098 tx = async_copy_data(1, wbi, dev->page,
1099 dev->sector, tx);
1100 wbi = r5_next_bio(wbi, dev->sector);
1101 }
1102 }
1103 }
1104
1105 return tx;
1106}
1107
Dan Williamsac6b53b2009-07-14 13:40:19 -07001108static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001109{
1110 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001111 int disks = sh->disks;
1112 int pd_idx = sh->pd_idx;
1113 int qd_idx = sh->qd_idx;
1114 int i;
Tejun Heoe9c74692010-09-03 11:56:18 +02001115 bool fua = false;
Dan Williams91c00922007-01-02 13:52:30 -07001116
Harvey Harrisone46b2722008-04-28 02:15:50 -07001117 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001118 (unsigned long long)sh->sector);
1119
Tejun Heoe9c74692010-09-03 11:56:18 +02001120 for (i = disks; i--; )
1121 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
1122
Dan Williams91c00922007-01-02 13:52:30 -07001123 for (i = disks; i--; ) {
1124 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001125
Tejun Heoe9c74692010-09-03 11:56:18 +02001126 if (dev->written || i == pd_idx || i == qd_idx) {
Dan Williams91c00922007-01-02 13:52:30 -07001127 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001128 if (fua)
1129 set_bit(R5_WantFUA, &dev->flags);
1130 }
Dan Williams91c00922007-01-02 13:52:30 -07001131 }
1132
Dan Williamsd8ee0722008-06-28 08:32:06 +10001133 if (sh->reconstruct_state == reconstruct_state_drain_run)
1134 sh->reconstruct_state = reconstruct_state_drain_result;
1135 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1136 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1137 else {
1138 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1139 sh->reconstruct_state = reconstruct_state_result;
1140 }
Dan Williams91c00922007-01-02 13:52:30 -07001141
1142 set_bit(STRIPE_HANDLE, &sh->state);
1143 release_stripe(sh);
1144}
1145
1146static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001147ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1148 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001149{
Dan Williams91c00922007-01-02 13:52:30 -07001150 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001151 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001152 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001153 int count = 0, pd_idx = sh->pd_idx, i;
1154 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001155 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001156 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001157
Harvey Harrisone46b2722008-04-28 02:15:50 -07001158 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001159 (unsigned long long)sh->sector);
1160
1161 /* check if prexor is active which means only process blocks
1162 * that are part of a read-modify-write (written)
1163 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001164 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1165 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001166 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1167 for (i = disks; i--; ) {
1168 struct r5dev *dev = &sh->dev[i];
1169 if (dev->written)
1170 xor_srcs[count++] = dev->page;
1171 }
1172 } else {
1173 xor_dest = sh->dev[pd_idx].page;
1174 for (i = disks; i--; ) {
1175 struct r5dev *dev = &sh->dev[i];
1176 if (i != pd_idx)
1177 xor_srcs[count++] = dev->page;
1178 }
1179 }
1180
Dan Williams91c00922007-01-02 13:52:30 -07001181 /* 1/ if we prexor'd then the dest is reused as a source
1182 * 2/ if we did not prexor then we are redoing the parity
1183 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1184 * for the synchronous xor case
1185 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001186 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001187 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1188
1189 atomic_inc(&sh->count);
1190
Dan Williamsac6b53b2009-07-14 13:40:19 -07001191 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001192 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001193 if (unlikely(count == 1))
1194 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1195 else
1196 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001197}
1198
Dan Williamsac6b53b2009-07-14 13:40:19 -07001199static void
1200ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1201 struct dma_async_tx_descriptor *tx)
1202{
1203 struct async_submit_ctl submit;
1204 struct page **blocks = percpu->scribble;
1205 int count;
1206
1207 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1208
1209 count = set_syndrome_sources(blocks, sh);
1210
1211 atomic_inc(&sh->count);
1212
1213 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1214 sh, to_addr_conv(sh, percpu));
1215 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001216}
1217
1218static void ops_complete_check(void *stripe_head_ref)
1219{
1220 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001221
Harvey Harrisone46b2722008-04-28 02:15:50 -07001222 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001223 (unsigned long long)sh->sector);
1224
Dan Williamsecc65c92008-06-28 08:31:57 +10001225 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001226 set_bit(STRIPE_HANDLE, &sh->state);
1227 release_stripe(sh);
1228}
1229
Dan Williamsac6b53b2009-07-14 13:40:19 -07001230static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001231{
Dan Williams91c00922007-01-02 13:52:30 -07001232 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001233 int pd_idx = sh->pd_idx;
1234 int qd_idx = sh->qd_idx;
1235 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001236 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001237 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001238 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001239 int count;
1240 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001241
Harvey Harrisone46b2722008-04-28 02:15:50 -07001242 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001243 (unsigned long long)sh->sector);
1244
Dan Williamsac6b53b2009-07-14 13:40:19 -07001245 count = 0;
1246 xor_dest = sh->dev[pd_idx].page;
1247 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001248 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001249 if (i == pd_idx || i == qd_idx)
1250 continue;
1251 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001252 }
1253
Dan Williamsd6f38f32009-07-14 11:50:52 -07001254 init_async_submit(&submit, 0, NULL, NULL, NULL,
1255 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001256 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001257 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001258
Dan Williams91c00922007-01-02 13:52:30 -07001259 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001260 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1261 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001262}
1263
Dan Williamsac6b53b2009-07-14 13:40:19 -07001264static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1265{
1266 struct page **srcs = percpu->scribble;
1267 struct async_submit_ctl submit;
1268 int count;
1269
1270 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1271 (unsigned long long)sh->sector, checkp);
1272
1273 count = set_syndrome_sources(srcs, sh);
1274 if (!checkp)
1275 srcs[count] = NULL;
1276
1277 atomic_inc(&sh->count);
1278 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1279 sh, to_addr_conv(sh, percpu));
1280 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1281 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1282}
1283
Dan Williams417b8d42009-10-16 16:25:22 +11001284static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001285{
1286 int overlap_clear = 0, i, disks = sh->disks;
1287 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001288 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001289 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001290 struct raid5_percpu *percpu;
1291 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001292
Dan Williamsd6f38f32009-07-14 11:50:52 -07001293 cpu = get_cpu();
1294 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001295 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001296 ops_run_biofill(sh);
1297 overlap_clear++;
1298 }
1299
Dan Williams7b3a8712008-06-28 08:32:09 +10001300 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001301 if (level < 6)
1302 tx = ops_run_compute5(sh, percpu);
1303 else {
1304 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1305 tx = ops_run_compute6_1(sh, percpu);
1306 else
1307 tx = ops_run_compute6_2(sh, percpu);
1308 }
1309 /* terminate the chain if reconstruct is not set to be run */
1310 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001311 async_tx_ack(tx);
1312 }
Dan Williams91c00922007-01-02 13:52:30 -07001313
Dan Williams600aa102008-06-28 08:32:05 +10001314 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001315 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001316
Dan Williams600aa102008-06-28 08:32:05 +10001317 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001318 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001319 overlap_clear++;
1320 }
1321
Dan Williamsac6b53b2009-07-14 13:40:19 -07001322 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1323 if (level < 6)
1324 ops_run_reconstruct5(sh, percpu, tx);
1325 else
1326 ops_run_reconstruct6(sh, percpu, tx);
1327 }
Dan Williams91c00922007-01-02 13:52:30 -07001328
Dan Williamsac6b53b2009-07-14 13:40:19 -07001329 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1330 if (sh->check_state == check_state_run)
1331 ops_run_check_p(sh, percpu);
1332 else if (sh->check_state == check_state_run_q)
1333 ops_run_check_pq(sh, percpu, 0);
1334 else if (sh->check_state == check_state_run_pq)
1335 ops_run_check_pq(sh, percpu, 1);
1336 else
1337 BUG();
1338 }
Dan Williams91c00922007-01-02 13:52:30 -07001339
Dan Williams91c00922007-01-02 13:52:30 -07001340 if (overlap_clear)
1341 for (i = disks; i--; ) {
1342 struct r5dev *dev = &sh->dev[i];
1343 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1344 wake_up(&sh->raid_conf->wait_for_overlap);
1345 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001346 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001347}
1348
Dan Williams417b8d42009-10-16 16:25:22 +11001349#ifdef CONFIG_MULTICORE_RAID456
1350static void async_run_ops(void *param, async_cookie_t cookie)
1351{
1352 struct stripe_head *sh = param;
1353 unsigned long ops_request = sh->ops.request;
1354
1355 clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
1356 wake_up(&sh->ops.wait_for_ops);
1357
1358 __raid_run_ops(sh, ops_request);
1359 release_stripe(sh);
1360}
1361
1362static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1363{
1364 /* since handle_stripe can be called outside of raid5d context
1365 * we need to ensure sh->ops.request is de-staged before another
1366 * request arrives
1367 */
1368 wait_event(sh->ops.wait_for_ops,
1369 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
1370 sh->ops.request = ops_request;
1371
1372 atomic_inc(&sh->count);
1373 async_schedule(async_run_ops, sh);
1374}
1375#else
1376#define raid_run_ops __raid_run_ops
1377#endif
1378
NeilBrownd1688a62011-10-11 16:49:52 +11001379static int grow_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
1381 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001382 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001383 if (!sh)
1384 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001385
NeilBrown3f294f42005-11-08 21:39:25 -08001386 sh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001387 #ifdef CONFIG_MULTICORE_RAID456
1388 init_waitqueue_head(&sh->ops.wait_for_ops);
1389 #endif
NeilBrown3f294f42005-11-08 21:39:25 -08001390
NeilBrowne4e11e32010-06-16 16:45:16 +10001391 if (grow_buffers(sh)) {
1392 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001393 kmem_cache_free(conf->slab_cache, sh);
1394 return 0;
1395 }
1396 /* we just created an active stripe so... */
1397 atomic_set(&sh->count, 1);
1398 atomic_inc(&conf->active_stripes);
1399 INIT_LIST_HEAD(&sh->lru);
1400 release_stripe(sh);
1401 return 1;
1402}
1403
NeilBrownd1688a62011-10-11 16:49:52 +11001404static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001405{
Christoph Lametere18b8902006-12-06 20:33:20 -08001406 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001407 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
NeilBrownf4be6b42010-06-01 19:37:25 +10001409 if (conf->mddev->gendisk)
1410 sprintf(conf->cache_name[0],
1411 "raid%d-%s", conf->level, mdname(conf->mddev));
1412 else
1413 sprintf(conf->cache_name[0],
1414 "raid%d-%p", conf->level, conf->mddev);
1415 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1416
NeilBrownad01c9e2006-03-27 01:18:07 -08001417 conf->active_name = 0;
1418 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001420 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 if (!sc)
1422 return 1;
1423 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001424 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001425 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001426 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 return 0;
1429}
NeilBrown29269552006-03-27 01:18:10 -08001430
Dan Williamsd6f38f32009-07-14 11:50:52 -07001431/**
1432 * scribble_len - return the required size of the scribble region
1433 * @num - total number of disks in the array
1434 *
1435 * The size must be enough to contain:
1436 * 1/ a struct page pointer for each device in the array +2
1437 * 2/ room to convert each entry in (1) to its corresponding dma
1438 * (dma_map_page()) or page (page_address()) address.
1439 *
1440 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1441 * calculate over all devices (not just the data blocks), using zeros in place
1442 * of the P and Q blocks.
1443 */
1444static size_t scribble_len(int num)
1445{
1446 size_t len;
1447
1448 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1449
1450 return len;
1451}
1452
NeilBrownd1688a62011-10-11 16:49:52 +11001453static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001454{
1455 /* Make all the stripes able to hold 'newsize' devices.
1456 * New slots in each stripe get 'page' set to a new page.
1457 *
1458 * This happens in stages:
1459 * 1/ create a new kmem_cache and allocate the required number of
1460 * stripe_heads.
1461 * 2/ gather all the old stripe_heads and tranfer the pages across
1462 * to the new stripe_heads. This will have the side effect of
1463 * freezing the array as once all stripe_heads have been collected,
1464 * no IO will be possible. Old stripe heads are freed once their
1465 * pages have been transferred over, and the old kmem_cache is
1466 * freed when all stripes are done.
1467 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1468 * we simple return a failre status - no need to clean anything up.
1469 * 4/ allocate new pages for the new slots in the new stripe_heads.
1470 * If this fails, we don't bother trying the shrink the
1471 * stripe_heads down again, we just leave them as they are.
1472 * As each stripe_head is processed the new one is released into
1473 * active service.
1474 *
1475 * Once step2 is started, we cannot afford to wait for a write,
1476 * so we use GFP_NOIO allocations.
1477 */
1478 struct stripe_head *osh, *nsh;
1479 LIST_HEAD(newstripes);
1480 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001481 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001482 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001483 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001484 int i;
1485
1486 if (newsize <= conf->pool_size)
1487 return 0; /* never bother to shrink */
1488
Dan Williamsb5470dc2008-06-27 21:44:04 -07001489 err = md_allow_write(conf->mddev);
1490 if (err)
1491 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001492
NeilBrownad01c9e2006-03-27 01:18:07 -08001493 /* Step 1 */
1494 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1495 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001496 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001497 if (!sc)
1498 return -ENOMEM;
1499
1500 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001501 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001502 if (!nsh)
1503 break;
1504
NeilBrownad01c9e2006-03-27 01:18:07 -08001505 nsh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001506 #ifdef CONFIG_MULTICORE_RAID456
1507 init_waitqueue_head(&nsh->ops.wait_for_ops);
1508 #endif
NeilBrownad01c9e2006-03-27 01:18:07 -08001509
1510 list_add(&nsh->lru, &newstripes);
1511 }
1512 if (i) {
1513 /* didn't get enough, give up */
1514 while (!list_empty(&newstripes)) {
1515 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1516 list_del(&nsh->lru);
1517 kmem_cache_free(sc, nsh);
1518 }
1519 kmem_cache_destroy(sc);
1520 return -ENOMEM;
1521 }
1522 /* Step 2 - Must use GFP_NOIO now.
1523 * OK, we have enough stripes, start collecting inactive
1524 * stripes and copying them over
1525 */
1526 list_for_each_entry(nsh, &newstripes, lru) {
1527 spin_lock_irq(&conf->device_lock);
1528 wait_event_lock_irq(conf->wait_for_stripe,
1529 !list_empty(&conf->inactive_list),
1530 conf->device_lock,
NeilBrown482c0832011-04-18 18:25:42 +10001531 );
NeilBrownad01c9e2006-03-27 01:18:07 -08001532 osh = get_free_stripe(conf);
1533 spin_unlock_irq(&conf->device_lock);
1534 atomic_set(&nsh->count, 1);
1535 for(i=0; i<conf->pool_size; i++)
1536 nsh->dev[i].page = osh->dev[i].page;
1537 for( ; i<newsize; i++)
1538 nsh->dev[i].page = NULL;
1539 kmem_cache_free(conf->slab_cache, osh);
1540 }
1541 kmem_cache_destroy(conf->slab_cache);
1542
1543 /* Step 3.
1544 * At this point, we are holding all the stripes so the array
1545 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001546 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001547 */
1548 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1549 if (ndisks) {
1550 for (i=0; i<conf->raid_disks; i++)
1551 ndisks[i] = conf->disks[i];
1552 kfree(conf->disks);
1553 conf->disks = ndisks;
1554 } else
1555 err = -ENOMEM;
1556
Dan Williamsd6f38f32009-07-14 11:50:52 -07001557 get_online_cpus();
1558 conf->scribble_len = scribble_len(newsize);
1559 for_each_present_cpu(cpu) {
1560 struct raid5_percpu *percpu;
1561 void *scribble;
1562
1563 percpu = per_cpu_ptr(conf->percpu, cpu);
1564 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1565
1566 if (scribble) {
1567 kfree(percpu->scribble);
1568 percpu->scribble = scribble;
1569 } else {
1570 err = -ENOMEM;
1571 break;
1572 }
1573 }
1574 put_online_cpus();
1575
NeilBrownad01c9e2006-03-27 01:18:07 -08001576 /* Step 4, return new stripes to service */
1577 while(!list_empty(&newstripes)) {
1578 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1579 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001580
NeilBrownad01c9e2006-03-27 01:18:07 -08001581 for (i=conf->raid_disks; i < newsize; i++)
1582 if (nsh->dev[i].page == NULL) {
1583 struct page *p = alloc_page(GFP_NOIO);
1584 nsh->dev[i].page = p;
1585 if (!p)
1586 err = -ENOMEM;
1587 }
1588 release_stripe(nsh);
1589 }
1590 /* critical section pass, GFP_NOIO no longer needed */
1591
1592 conf->slab_cache = sc;
1593 conf->active_name = 1-conf->active_name;
1594 conf->pool_size = newsize;
1595 return err;
1596}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
NeilBrownd1688a62011-10-11 16:49:52 +11001598static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
1600 struct stripe_head *sh;
1601
NeilBrown3f294f42005-11-08 21:39:25 -08001602 spin_lock_irq(&conf->device_lock);
1603 sh = get_free_stripe(conf);
1604 spin_unlock_irq(&conf->device_lock);
1605 if (!sh)
1606 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001607 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001608 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001609 kmem_cache_free(conf->slab_cache, sh);
1610 atomic_dec(&conf->active_stripes);
1611 return 1;
1612}
1613
NeilBrownd1688a62011-10-11 16:49:52 +11001614static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001615{
1616 while (drop_one_stripe(conf))
1617 ;
1618
NeilBrown29fc7e32006-02-03 03:03:41 -08001619 if (conf->slab_cache)
1620 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 conf->slab_cache = NULL;
1622}
1623
NeilBrown6712ecf2007-09-27 12:47:43 +02001624static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625{
NeilBrown99c0fb52009-03-31 14:39:38 +11001626 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001627 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001628 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001630 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11001631 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
1634 for (i=0 ; i<disks; i++)
1635 if (bi == &sh->dev[i].req)
1636 break;
1637
Dan Williams45b42332007-07-09 11:56:43 -07001638 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1639 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 uptodate);
1641 if (i == disks) {
1642 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001643 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 }
NeilBrown14a75d32011-12-23 10:17:52 +11001645 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1646 rdev = conf->disks[i].replacement;
1647 else
1648 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
1650 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001652 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11001653 /* Note that this cannot happen on a
1654 * replacement device. We just fail those on
1655 * any error
1656 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001657 printk_ratelimited(
1658 KERN_INFO
1659 "md/raid:%s: read error corrected"
1660 " (%lu sectors at %llu on %s)\n",
1661 mdname(conf->mddev), STRIPE_SECTORS,
1662 (unsigned long long)(sh->sector
1663 + rdev->data_offset),
1664 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001665 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001666 clear_bit(R5_ReadError, &sh->dev[i].flags);
1667 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1668 }
NeilBrown14a75d32011-12-23 10:17:52 +11001669 if (atomic_read(&rdev->read_errors))
1670 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11001672 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001673 int retry = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001676 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11001677 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1678 printk_ratelimited(
1679 KERN_WARNING
1680 "md/raid:%s: read error on replacement device "
1681 "(sector %llu on %s).\n",
1682 mdname(conf->mddev),
1683 (unsigned long long)(sh->sector
1684 + rdev->data_offset),
1685 bdn);
1686 else if (conf->mddev->degraded >= conf->max_degraded)
Christian Dietrich8bda4702011-07-27 11:00:36 +10001687 printk_ratelimited(
1688 KERN_WARNING
1689 "md/raid:%s: read error not correctable "
1690 "(sector %llu on %s).\n",
1691 mdname(conf->mddev),
1692 (unsigned long long)(sh->sector
1693 + rdev->data_offset),
1694 bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001695 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
NeilBrown4e5314b2005-11-08 21:39:22 -08001696 /* Oh, no!!! */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001697 printk_ratelimited(
1698 KERN_WARNING
1699 "md/raid:%s: read error NOT corrected!! "
1700 "(sector %llu on %s).\n",
1701 mdname(conf->mddev),
1702 (unsigned long long)(sh->sector
1703 + rdev->data_offset),
1704 bdn);
NeilBrownd6950432006-07-10 04:44:20 -07001705 else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001706 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001707 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001708 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001709 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001710 else
1711 retry = 1;
1712 if (retry)
1713 set_bit(R5_ReadError, &sh->dev[i].flags);
1714 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001715 clear_bit(R5_ReadError, &sh->dev[i].flags);
1716 clear_bit(R5_ReWrite, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001717 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 }
NeilBrown14a75d32011-12-23 10:17:52 +11001720 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1722 set_bit(STRIPE_HANDLE, &sh->state);
1723 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724}
1725
NeilBrownd710e132008-10-13 11:55:12 +11001726static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727{
NeilBrown99c0fb52009-03-31 14:39:38 +11001728 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001729 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001730 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11001731 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001733 sector_t first_bad;
1734 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11001735 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
NeilBrown977df362011-12-23 10:17:53 +11001737 for (i = 0 ; i < disks; i++) {
1738 if (bi == &sh->dev[i].req) {
1739 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 break;
NeilBrown977df362011-12-23 10:17:53 +11001741 }
1742 if (bi == &sh->dev[i].rreq) {
1743 rdev = conf->disks[i].replacement;
1744 replacement = 1;
1745 break;
1746 }
1747 }
Dan Williams45b42332007-07-09 11:56:43 -07001748 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1750 uptodate);
1751 if (i == disks) {
1752 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001753 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 }
1755
NeilBrown977df362011-12-23 10:17:53 +11001756 if (replacement) {
1757 if (!uptodate)
1758 md_error(conf->mddev, rdev);
1759 else if (is_badblock(rdev, sh->sector,
1760 STRIPE_SECTORS,
1761 &first_bad, &bad_sectors))
1762 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1763 } else {
1764 if (!uptodate) {
1765 set_bit(WriteErrorSeen, &rdev->flags);
1766 set_bit(R5_WriteError, &sh->dev[i].flags);
1767 } else if (is_badblock(rdev, sh->sector,
1768 STRIPE_SECTORS,
1769 &first_bad, &bad_sectors))
1770 set_bit(R5_MadeGood, &sh->dev[i].flags);
1771 }
1772 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
NeilBrown977df362011-12-23 10:17:53 +11001774 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1775 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001777 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778}
1779
NeilBrown784052e2009-03-31 15:19:07 +11001780static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
NeilBrown784052e2009-03-31 15:19:07 +11001782static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
1784 struct r5dev *dev = &sh->dev[i];
1785
1786 bio_init(&dev->req);
1787 dev->req.bi_io_vec = &dev->vec;
1788 dev->req.bi_vcnt++;
1789 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11001791 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
NeilBrown977df362011-12-23 10:17:53 +11001793 bio_init(&dev->rreq);
1794 dev->rreq.bi_io_vec = &dev->rvec;
1795 dev->rreq.bi_vcnt++;
1796 dev->rreq.bi_max_vecs++;
1797 dev->rreq.bi_private = sh;
1798 dev->rvec.bv_page = dev->page;
1799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001801 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802}
1803
NeilBrownfd01b882011-10-11 16:47:53 +11001804static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
1806 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11001807 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11001808 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10001809 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
NeilBrown908f4fb2011-12-23 10:17:50 +11001811 spin_lock_irqsave(&conf->device_lock, flags);
1812 clear_bit(In_sync, &rdev->flags);
1813 mddev->degraded = calc_degraded(conf);
1814 spin_unlock_irqrestore(&conf->device_lock, flags);
1815 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1816
NeilBrownde393cd2011-07-28 11:31:48 +10001817 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001818 set_bit(Faulty, &rdev->flags);
1819 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1820 printk(KERN_ALERT
1821 "md/raid:%s: Disk failure on %s, disabling device.\n"
1822 "md/raid:%s: Operation continuing on %d devices.\n",
1823 mdname(mddev),
1824 bdevname(rdev->bdev, b),
1825 mdname(mddev),
1826 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001827}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
1829/*
1830 * Input: a 'big' sector number,
1831 * Output: index of the data and parity disk, and the sector # in them.
1832 */
NeilBrownd1688a62011-10-11 16:49:52 +11001833static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001834 int previous, int *dd_idx,
1835 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001837 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001838 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001840 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001841 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001843 int algorithm = previous ? conf->prev_algo
1844 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001845 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1846 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001847 int raid_disks = previous ? conf->previous_raid_disks
1848 : conf->raid_disks;
1849 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
1851 /* First compute the information on this sector */
1852
1853 /*
1854 * Compute the chunk number and the sector offset inside the chunk
1855 */
1856 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1857 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
1859 /*
1860 * Compute the stripe number
1861 */
NeilBrown35f2a592010-04-20 14:13:34 +10001862 stripe = chunk_number;
1863 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10001864 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 /*
1866 * Select the parity disk based on the user selected algorithm.
1867 */
NeilBrown84789552011-07-27 11:00:36 +10001868 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07001869 switch(conf->level) {
1870 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001871 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001872 break;
1873 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001874 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001876 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001877 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 (*dd_idx)++;
1879 break;
1880 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001881 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001882 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 (*dd_idx)++;
1884 break;
1885 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001886 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001887 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 break;
1889 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001890 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001891 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001893 case ALGORITHM_PARITY_0:
1894 pd_idx = 0;
1895 (*dd_idx)++;
1896 break;
1897 case ALGORITHM_PARITY_N:
1898 pd_idx = data_disks;
1899 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11001901 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001902 }
1903 break;
1904 case 6:
1905
NeilBrowne183eae2009-03-31 15:20:22 +11001906 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001907 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001908 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001909 qd_idx = pd_idx + 1;
1910 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001911 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001912 qd_idx = 0;
1913 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001914 (*dd_idx) += 2; /* D D P Q D */
1915 break;
1916 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001917 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001918 qd_idx = pd_idx + 1;
1919 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001920 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001921 qd_idx = 0;
1922 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001923 (*dd_idx) += 2; /* D D P Q D */
1924 break;
1925 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001926 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001927 qd_idx = (pd_idx + 1) % raid_disks;
1928 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001929 break;
1930 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001931 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001932 qd_idx = (pd_idx + 1) % raid_disks;
1933 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001934 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001935
1936 case ALGORITHM_PARITY_0:
1937 pd_idx = 0;
1938 qd_idx = 1;
1939 (*dd_idx) += 2;
1940 break;
1941 case ALGORITHM_PARITY_N:
1942 pd_idx = data_disks;
1943 qd_idx = data_disks + 1;
1944 break;
1945
1946 case ALGORITHM_ROTATING_ZERO_RESTART:
1947 /* Exactly the same as RIGHT_ASYMMETRIC, but or
1948 * of blocks for computing Q is different.
1949 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001950 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11001951 qd_idx = pd_idx + 1;
1952 if (pd_idx == raid_disks-1) {
1953 (*dd_idx)++; /* Q D D D P */
1954 qd_idx = 0;
1955 } else if (*dd_idx >= pd_idx)
1956 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001957 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001958 break;
1959
1960 case ALGORITHM_ROTATING_N_RESTART:
1961 /* Same a left_asymmetric, by first stripe is
1962 * D D D P Q rather than
1963 * Q D D D P
1964 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001965 stripe2 += 1;
1966 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11001967 qd_idx = pd_idx + 1;
1968 if (pd_idx == raid_disks-1) {
1969 (*dd_idx)++; /* Q D D D P */
1970 qd_idx = 0;
1971 } else if (*dd_idx >= pd_idx)
1972 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001973 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001974 break;
1975
1976 case ALGORITHM_ROTATING_N_CONTINUE:
1977 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001978 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11001979 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
1980 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11001981 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001982 break;
1983
1984 case ALGORITHM_LEFT_ASYMMETRIC_6:
1985 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001986 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11001987 if (*dd_idx >= pd_idx)
1988 (*dd_idx)++;
1989 qd_idx = raid_disks - 1;
1990 break;
1991
1992 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001993 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11001994 if (*dd_idx >= pd_idx)
1995 (*dd_idx)++;
1996 qd_idx = raid_disks - 1;
1997 break;
1998
1999 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002000 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002001 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2002 qd_idx = raid_disks - 1;
2003 break;
2004
2005 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002006 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002007 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2008 qd_idx = raid_disks - 1;
2009 break;
2010
2011 case ALGORITHM_PARITY_0_6:
2012 pd_idx = 0;
2013 (*dd_idx)++;
2014 qd_idx = raid_disks - 1;
2015 break;
2016
NeilBrown16a53ec2006-06-26 00:27:38 -07002017 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002018 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002019 }
2020 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 }
2022
NeilBrown911d4ee2009-03-31 14:39:38 +11002023 if (sh) {
2024 sh->pd_idx = pd_idx;
2025 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002026 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 /*
2029 * Finally, compute the new sector number
2030 */
2031 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2032 return new_sector;
2033}
2034
2035
NeilBrown784052e2009-03-31 15:19:07 +11002036static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037{
NeilBrownd1688a62011-10-11 16:49:52 +11002038 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002039 int raid_disks = sh->disks;
2040 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002042 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2043 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002044 int algorithm = previous ? conf->prev_algo
2045 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 sector_t stripe;
2047 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002048 sector_t chunk_number;
2049 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002051 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
NeilBrown16a53ec2006-06-26 00:27:38 -07002053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2055 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
NeilBrown16a53ec2006-06-26 00:27:38 -07002057 if (i == sh->pd_idx)
2058 return 0;
2059 switch(conf->level) {
2060 case 4: break;
2061 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002062 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 case ALGORITHM_LEFT_ASYMMETRIC:
2064 case ALGORITHM_RIGHT_ASYMMETRIC:
2065 if (i > sh->pd_idx)
2066 i--;
2067 break;
2068 case ALGORITHM_LEFT_SYMMETRIC:
2069 case ALGORITHM_RIGHT_SYMMETRIC:
2070 if (i < sh->pd_idx)
2071 i += raid_disks;
2072 i -= (sh->pd_idx + 1);
2073 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002074 case ALGORITHM_PARITY_0:
2075 i -= 1;
2076 break;
2077 case ALGORITHM_PARITY_N:
2078 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002080 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002081 }
2082 break;
2083 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002084 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002085 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002086 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002087 case ALGORITHM_LEFT_ASYMMETRIC:
2088 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002089 case ALGORITHM_ROTATING_ZERO_RESTART:
2090 case ALGORITHM_ROTATING_N_RESTART:
2091 if (sh->pd_idx == raid_disks-1)
2092 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002093 else if (i > sh->pd_idx)
2094 i -= 2; /* D D P Q D */
2095 break;
2096 case ALGORITHM_LEFT_SYMMETRIC:
2097 case ALGORITHM_RIGHT_SYMMETRIC:
2098 if (sh->pd_idx == raid_disks-1)
2099 i--; /* Q D D D P */
2100 else {
2101 /* D D P Q D */
2102 if (i < sh->pd_idx)
2103 i += raid_disks;
2104 i -= (sh->pd_idx + 2);
2105 }
2106 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002107 case ALGORITHM_PARITY_0:
2108 i -= 2;
2109 break;
2110 case ALGORITHM_PARITY_N:
2111 break;
2112 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002113 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002114 if (sh->pd_idx == 0)
2115 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002116 else {
2117 /* D D Q P D */
2118 if (i < sh->pd_idx)
2119 i += raid_disks;
2120 i -= (sh->pd_idx + 1);
2121 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002122 break;
2123 case ALGORITHM_LEFT_ASYMMETRIC_6:
2124 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2125 if (i > sh->pd_idx)
2126 i--;
2127 break;
2128 case ALGORITHM_LEFT_SYMMETRIC_6:
2129 case ALGORITHM_RIGHT_SYMMETRIC_6:
2130 if (i < sh->pd_idx)
2131 i += data_disks + 1;
2132 i -= (sh->pd_idx + 1);
2133 break;
2134 case ALGORITHM_PARITY_0_6:
2135 i -= 1;
2136 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002137 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002138 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002139 }
2140 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 }
2142
2143 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002144 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
NeilBrown112bf892009-03-31 14:39:38 +11002146 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002147 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002148 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2149 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002150 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2151 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 return 0;
2153 }
2154 return r_sector;
2155}
2156
2157
Dan Williams600aa102008-06-28 08:32:05 +10002158static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002159schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002160 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002161{
2162 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002163 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002164 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002165
Dan Williamse33129d2007-01-02 13:52:30 -07002166 if (rcw) {
2167 /* if we are not expanding this is a proper write request, and
2168 * there will be bios with new data to be drained into the
2169 * stripe cache
2170 */
2171 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10002172 sh->reconstruct_state = reconstruct_state_drain_run;
2173 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2174 } else
2175 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07002176
Dan Williamsac6b53b2009-07-14 13:40:19 -07002177 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002178
2179 for (i = disks; i--; ) {
2180 struct r5dev *dev = &sh->dev[i];
2181
2182 if (dev->towrite) {
2183 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002184 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002185 if (!expand)
2186 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002187 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002188 }
2189 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002190 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002191 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002192 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002193 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002194 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002195 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2196 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2197
Dan Williamsd8ee0722008-06-28 08:32:06 +10002198 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002199 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2200 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002201 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002202
2203 for (i = disks; i--; ) {
2204 struct r5dev *dev = &sh->dev[i];
2205 if (i == pd_idx)
2206 continue;
2207
Dan Williamse33129d2007-01-02 13:52:30 -07002208 if (dev->towrite &&
2209 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002210 test_bit(R5_Wantcompute, &dev->flags))) {
2211 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002212 set_bit(R5_LOCKED, &dev->flags);
2213 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002214 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002215 }
2216 }
2217 }
2218
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002219 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002220 * are in flight
2221 */
2222 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2223 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002224 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002225
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002226 if (level == 6) {
2227 int qd_idx = sh->qd_idx;
2228 struct r5dev *dev = &sh->dev[qd_idx];
2229
2230 set_bit(R5_LOCKED, &dev->flags);
2231 clear_bit(R5_UPTODATE, &dev->flags);
2232 s->locked++;
2233 }
2234
Dan Williams600aa102008-06-28 08:32:05 +10002235 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002236 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002237 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002238}
NeilBrown16a53ec2006-06-26 00:27:38 -07002239
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240/*
2241 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002242 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 * The bi_next chain must be in order.
2244 */
2245static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2246{
2247 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002248 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002249 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
NeilBrowncbe47ec2011-07-26 11:20:35 +10002251 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 (unsigned long long)bi->bi_sector,
2253 (unsigned long long)sh->sector);
2254
2255
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07002257 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -07002259 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
2260 firstwrite = 1;
2261 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 bip = &sh->dev[dd_idx].toread;
2263 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2264 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2265 goto overlap;
2266 bip = & (*bip)->bi_next;
2267 }
2268 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2269 goto overlap;
2270
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002271 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 if (*bip)
2273 bi->bi_next = *bip;
2274 *bip = bi;
Jens Axboe960e7392008-08-15 10:41:18 +02002275 bi->bi_phys_segments++;
NeilBrown72626682005-09-09 16:23:54 -07002276
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 if (forwrite) {
2278 /* check if page is covered */
2279 sector_t sector = sh->dev[dd_idx].sector;
2280 for (bi=sh->dev[dd_idx].towrite;
2281 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2282 bi && bi->bi_sector <= sector;
2283 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2284 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2285 sector = bi->bi_sector + (bi->bi_size>>9);
2286 }
2287 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2288 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2289 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002290 spin_unlock_irq(&conf->device_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002291
2292 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2293 (unsigned long long)(*bip)->bi_sector,
2294 (unsigned long long)sh->sector, dd_idx);
2295
2296 if (conf->mddev->bitmap && firstwrite) {
2297 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2298 STRIPE_SECTORS, 0);
2299 sh->bm_seq = conf->seq_flush+1;
2300 set_bit(STRIPE_BIT_DELAY, &sh->state);
2301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 return 1;
2303
2304 overlap:
2305 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
2306 spin_unlock_irq(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 return 0;
2308}
2309
NeilBrownd1688a62011-10-11 16:49:52 +11002310static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002311
NeilBrownd1688a62011-10-11 16:49:52 +11002312static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002313 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002314{
NeilBrown784052e2009-03-31 15:19:07 +11002315 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002316 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002317 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002318 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002319 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002320
NeilBrown112bf892009-03-31 14:39:38 +11002321 raid5_compute_sector(conf,
2322 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002323 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002324 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002325 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002326}
2327
Dan Williamsa4456852007-07-09 11:56:43 -07002328static void
NeilBrownd1688a62011-10-11 16:49:52 +11002329handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002330 struct stripe_head_state *s, int disks,
2331 struct bio **return_bi)
2332{
2333 int i;
2334 for (i = disks; i--; ) {
2335 struct bio *bi;
2336 int bitmap_end = 0;
2337
2338 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002339 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002340 rcu_read_lock();
2341 rdev = rcu_dereference(conf->disks[i].rdev);
2342 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002343 atomic_inc(&rdev->nr_pending);
2344 else
2345 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002346 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002347 if (rdev) {
2348 if (!rdev_set_badblocks(
2349 rdev,
2350 sh->sector,
2351 STRIPE_SECTORS, 0))
2352 md_error(conf->mddev, rdev);
2353 rdev_dec_pending(rdev, conf->mddev);
2354 }
Dan Williamsa4456852007-07-09 11:56:43 -07002355 }
2356 spin_lock_irq(&conf->device_lock);
2357 /* fail all writes first */
2358 bi = sh->dev[i].towrite;
2359 sh->dev[i].towrite = NULL;
2360 if (bi) {
2361 s->to_write--;
2362 bitmap_end = 1;
2363 }
2364
2365 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2366 wake_up(&conf->wait_for_overlap);
2367
2368 while (bi && bi->bi_sector <
2369 sh->dev[i].sector + STRIPE_SECTORS) {
2370 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2371 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002372 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002373 md_write_end(conf->mddev);
2374 bi->bi_next = *return_bi;
2375 *return_bi = bi;
2376 }
2377 bi = nextbi;
2378 }
2379 /* and fail all 'written' */
2380 bi = sh->dev[i].written;
2381 sh->dev[i].written = NULL;
2382 if (bi) bitmap_end = 1;
2383 while (bi && bi->bi_sector <
2384 sh->dev[i].sector + STRIPE_SECTORS) {
2385 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2386 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002387 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002388 md_write_end(conf->mddev);
2389 bi->bi_next = *return_bi;
2390 *return_bi = bi;
2391 }
2392 bi = bi2;
2393 }
2394
Dan Williamsb5e98d62007-01-02 13:52:31 -07002395 /* fail any reads if this device is non-operational and
2396 * the data has not reached the cache yet.
2397 */
2398 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2399 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2400 test_bit(R5_ReadError, &sh->dev[i].flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002401 bi = sh->dev[i].toread;
2402 sh->dev[i].toread = NULL;
2403 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2404 wake_up(&conf->wait_for_overlap);
2405 if (bi) s->to_read--;
2406 while (bi && bi->bi_sector <
2407 sh->dev[i].sector + STRIPE_SECTORS) {
2408 struct bio *nextbi =
2409 r5_next_bio(bi, sh->dev[i].sector);
2410 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002411 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002412 bi->bi_next = *return_bi;
2413 *return_bi = bi;
2414 }
2415 bi = nextbi;
2416 }
2417 }
2418 spin_unlock_irq(&conf->device_lock);
2419 if (bitmap_end)
2420 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2421 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002422 /* If we were in the middle of a write the parity block might
2423 * still be locked - so just clear all R5_LOCKED flags
2424 */
2425 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002426 }
2427
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002428 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2429 if (atomic_dec_and_test(&conf->pending_full_writes))
2430 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002431}
2432
NeilBrown7f0da592011-07-28 11:39:22 +10002433static void
NeilBrownd1688a62011-10-11 16:49:52 +11002434handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002435 struct stripe_head_state *s)
2436{
2437 int abort = 0;
2438 int i;
2439
2440 md_done_sync(conf->mddev, STRIPE_SECTORS, 0);
2441 clear_bit(STRIPE_SYNCING, &sh->state);
2442 s->syncing = 0;
2443 /* There is nothing more to do for sync/check/repair.
2444 * For recover we need to record a bad block on all
2445 * non-sync devices, or abort the recovery
2446 */
2447 if (!test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery))
2448 return;
2449 /* During recovery devices cannot be removed, so locking and
2450 * refcounting of rdevs is not needed
2451 */
2452 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +11002453 struct md_rdev *rdev = conf->disks[i].rdev;
NeilBrown7f0da592011-07-28 11:39:22 +10002454 if (!rdev
2455 || test_bit(Faulty, &rdev->flags)
2456 || test_bit(In_sync, &rdev->flags))
2457 continue;
2458 if (!rdev_set_badblocks(rdev, sh->sector,
2459 STRIPE_SECTORS, 0))
2460 abort = 1;
2461 }
2462 if (abort) {
2463 conf->recovery_disabled = conf->mddev->recovery_disabled;
2464 set_bit(MD_RECOVERY_INTR, &conf->mddev->recovery);
2465 }
2466}
2467
NeilBrown93b3dbc2011-07-27 11:00:36 +10002468/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002469 * to be read or computed to satisfy a request.
2470 *
2471 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002472 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002473 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002474static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2475 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002476{
2477 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002478 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2479 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002480
Dan Williamsf38e1212007-01-02 13:52:30 -07002481 /* is the data in this block needed, and can we get it? */
2482 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002483 !test_bit(R5_UPTODATE, &dev->flags) &&
2484 (dev->toread ||
2485 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2486 s->syncing || s->expanding ||
NeilBrown5d35e092011-07-27 11:00:36 +10002487 (s->failed >= 1 && fdev[0]->toread) ||
2488 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002489 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2490 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2491 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002492 /* we would like to get this block, possibly by computing it,
2493 * otherwise read it if the backing disk is insync
2494 */
2495 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2496 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2497 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002498 (s->failed && (disk_idx == s->failed_num[0] ||
2499 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002500 /* have disk failed, and we're requested to fetch it;
2501 * do compute it
2502 */
2503 pr_debug("Computing stripe %llu block %d\n",
2504 (unsigned long long)sh->sector, disk_idx);
2505 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2506 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2507 set_bit(R5_Wantcompute, &dev->flags);
2508 sh->ops.target = disk_idx;
2509 sh->ops.target2 = -1; /* no 2nd target */
2510 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002511 /* Careful: from this point on 'uptodate' is in the eye
2512 * of raid_run_ops which services 'compute' operations
2513 * before writes. R5_Wantcompute flags a block that will
2514 * be R5_UPTODATE by the time it is needed for a
2515 * subsequent operation.
2516 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002517 s->uptodate++;
2518 return 1;
2519 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2520 /* Computing 2-failure is *very* expensive; only
2521 * do it if failed >= 2
2522 */
2523 int other;
2524 for (other = disks; other--; ) {
2525 if (other == disk_idx)
2526 continue;
2527 if (!test_bit(R5_UPTODATE,
2528 &sh->dev[other].flags))
2529 break;
2530 }
2531 BUG_ON(other < 0);
2532 pr_debug("Computing stripe %llu blocks %d,%d\n",
2533 (unsigned long long)sh->sector,
2534 disk_idx, other);
2535 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2536 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2537 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2538 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2539 sh->ops.target = disk_idx;
2540 sh->ops.target2 = other;
2541 s->uptodate += 2;
2542 s->req_compute = 1;
2543 return 1;
2544 } else if (test_bit(R5_Insync, &dev->flags)) {
2545 set_bit(R5_LOCKED, &dev->flags);
2546 set_bit(R5_Wantread, &dev->flags);
2547 s->locked++;
2548 pr_debug("Reading block %d (sync=%d)\n",
2549 disk_idx, s->syncing);
2550 }
2551 }
2552
2553 return 0;
2554}
2555
2556/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002557 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002558 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002559static void handle_stripe_fill(struct stripe_head *sh,
2560 struct stripe_head_state *s,
2561 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002562{
2563 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002564
2565 /* look for blocks to read/compute, skip this if a compute
2566 * is already in flight, or if the stripe contents are in the
2567 * midst of changing due to a write
2568 */
2569 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2570 !sh->reconstruct_state)
2571 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002572 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002573 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002574 set_bit(STRIPE_HANDLE, &sh->state);
2575}
2576
2577
Dan Williams1fe797e2008-06-28 09:16:30 +10002578/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002579 * any written block on an uptodate or failed drive can be returned.
2580 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2581 * never LOCKED, so we don't need to test 'failed' directly.
2582 */
NeilBrownd1688a62011-10-11 16:49:52 +11002583static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002584 struct stripe_head *sh, int disks, struct bio **return_bi)
2585{
2586 int i;
2587 struct r5dev *dev;
2588
2589 for (i = disks; i--; )
2590 if (sh->dev[i].written) {
2591 dev = &sh->dev[i];
2592 if (!test_bit(R5_LOCKED, &dev->flags) &&
2593 test_bit(R5_UPTODATE, &dev->flags)) {
2594 /* We can return any write requests */
2595 struct bio *wbi, *wbi2;
2596 int bitmap_end = 0;
Dan Williams45b42332007-07-09 11:56:43 -07002597 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002598 spin_lock_irq(&conf->device_lock);
2599 wbi = dev->written;
2600 dev->written = NULL;
2601 while (wbi && wbi->bi_sector <
2602 dev->sector + STRIPE_SECTORS) {
2603 wbi2 = r5_next_bio(wbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +02002604 if (!raid5_dec_bi_phys_segments(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002605 md_write_end(conf->mddev);
2606 wbi->bi_next = *return_bi;
2607 *return_bi = wbi;
2608 }
2609 wbi = wbi2;
2610 }
2611 if (dev->towrite == NULL)
2612 bitmap_end = 1;
2613 spin_unlock_irq(&conf->device_lock);
2614 if (bitmap_end)
2615 bitmap_endwrite(conf->mddev->bitmap,
2616 sh->sector,
2617 STRIPE_SECTORS,
2618 !test_bit(STRIPE_DEGRADED, &sh->state),
2619 0);
2620 }
2621 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002622
2623 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2624 if (atomic_dec_and_test(&conf->pending_full_writes))
2625 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002626}
2627
NeilBrownd1688a62011-10-11 16:49:52 +11002628static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002629 struct stripe_head *sh,
2630 struct stripe_head_state *s,
2631 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002632{
2633 int rmw = 0, rcw = 0, i;
NeilBrownc8ac1802011-07-27 11:00:36 +10002634 if (conf->max_degraded == 2) {
2635 /* RAID6 requires 'rcw' in current implementation
2636 * Calculate the real rcw later - for now fake it
2637 * look like rcw is cheaper
2638 */
2639 rcw = 1; rmw = 2;
2640 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002641 /* would I have to read this buffer for read_modify_write */
2642 struct r5dev *dev = &sh->dev[i];
2643 if ((dev->towrite || i == sh->pd_idx) &&
2644 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002645 !(test_bit(R5_UPTODATE, &dev->flags) ||
2646 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002647 if (test_bit(R5_Insync, &dev->flags))
2648 rmw++;
2649 else
2650 rmw += 2*disks; /* cannot read it */
2651 }
2652 /* Would I have to read this buffer for reconstruct_write */
2653 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2654 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002655 !(test_bit(R5_UPTODATE, &dev->flags) ||
2656 test_bit(R5_Wantcompute, &dev->flags))) {
2657 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002658 else
2659 rcw += 2*disks;
2660 }
2661 }
Dan Williams45b42332007-07-09 11:56:43 -07002662 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002663 (unsigned long long)sh->sector, rmw, rcw);
2664 set_bit(STRIPE_HANDLE, &sh->state);
2665 if (rmw < rcw && rmw > 0)
2666 /* prefer read-modify-write, but need to get some data */
2667 for (i = disks; i--; ) {
2668 struct r5dev *dev = &sh->dev[i];
2669 if ((dev->towrite || i == sh->pd_idx) &&
2670 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002671 !(test_bit(R5_UPTODATE, &dev->flags) ||
2672 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002673 test_bit(R5_Insync, &dev->flags)) {
2674 if (
2675 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002676 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002677 "%d for r-m-w\n", i);
2678 set_bit(R5_LOCKED, &dev->flags);
2679 set_bit(R5_Wantread, &dev->flags);
2680 s->locked++;
2681 } else {
2682 set_bit(STRIPE_DELAYED, &sh->state);
2683 set_bit(STRIPE_HANDLE, &sh->state);
2684 }
2685 }
2686 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002687 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002688 /* want reconstruct write, but need to get some data */
NeilBrownc8ac1802011-07-27 11:00:36 +10002689 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002690 for (i = disks; i--; ) {
2691 struct r5dev *dev = &sh->dev[i];
2692 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002693 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002694 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002695 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002696 test_bit(R5_Wantcompute, &dev->flags))) {
2697 rcw++;
2698 if (!test_bit(R5_Insync, &dev->flags))
2699 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002700 if (
2701 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002702 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002703 "%d for Reconstruct\n", i);
2704 set_bit(R5_LOCKED, &dev->flags);
2705 set_bit(R5_Wantread, &dev->flags);
2706 s->locked++;
2707 } else {
2708 set_bit(STRIPE_DELAYED, &sh->state);
2709 set_bit(STRIPE_HANDLE, &sh->state);
2710 }
2711 }
2712 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002713 }
Dan Williamsa4456852007-07-09 11:56:43 -07002714 /* now if nothing is locked, and if we have enough data,
2715 * we can start a write request
2716 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002717 /* since handle_stripe can be called at any time we need to handle the
2718 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002719 * subsequent call wants to start a write request. raid_run_ops only
2720 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002721 * simultaneously. If this is not the case then new writes need to be
2722 * held off until the compute completes.
2723 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002724 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2725 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2726 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002727 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002728}
2729
NeilBrownd1688a62011-10-11 16:49:52 +11002730static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002731 struct stripe_head_state *s, int disks)
2732{
Dan Williamsecc65c92008-06-28 08:31:57 +10002733 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002734
Dan Williamsbd2ab672008-04-10 21:29:27 -07002735 set_bit(STRIPE_HANDLE, &sh->state);
2736
Dan Williamsecc65c92008-06-28 08:31:57 +10002737 switch (sh->check_state) {
2738 case check_state_idle:
2739 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002740 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002741 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002742 sh->check_state = check_state_run;
2743 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002744 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002745 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002746 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002747 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002748 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002749 /* fall through */
2750 case check_state_compute_result:
2751 sh->check_state = check_state_idle;
2752 if (!dev)
2753 dev = &sh->dev[sh->pd_idx];
2754
2755 /* check that a write has not made the stripe insync */
2756 if (test_bit(STRIPE_INSYNC, &sh->state))
2757 break;
2758
2759 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002760 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2761 BUG_ON(s->uptodate != disks);
2762
2763 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002764 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002765 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002766
Dan Williamsa4456852007-07-09 11:56:43 -07002767 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002768 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002769 break;
2770 case check_state_run:
2771 break; /* we will be called again upon completion */
2772 case check_state_check_result:
2773 sh->check_state = check_state_idle;
2774
2775 /* if a failure occurred during the check operation, leave
2776 * STRIPE_INSYNC not set and let the stripe be handled again
2777 */
2778 if (s->failed)
2779 break;
2780
2781 /* handle a successful check operation, if parity is correct
2782 * we are done. Otherwise update the mismatch count and repair
2783 * parity if !MD_RECOVERY_CHECK
2784 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002785 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002786 /* parity is correct (on disc,
2787 * not in buffer any more)
2788 */
2789 set_bit(STRIPE_INSYNC, &sh->state);
2790 else {
2791 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2792 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2793 /* don't try to repair!! */
2794 set_bit(STRIPE_INSYNC, &sh->state);
2795 else {
2796 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002797 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002798 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2799 set_bit(R5_Wantcompute,
2800 &sh->dev[sh->pd_idx].flags);
2801 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002802 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10002803 s->uptodate++;
2804 }
2805 }
2806 break;
2807 case check_state_compute_run:
2808 break;
2809 default:
2810 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2811 __func__, sh->check_state,
2812 (unsigned long long) sh->sector);
2813 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002814 }
2815}
2816
2817
NeilBrownd1688a62011-10-11 16:49:52 +11002818static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07002819 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10002820 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002821{
Dan Williamsa4456852007-07-09 11:56:43 -07002822 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11002823 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07002824 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07002825
2826 set_bit(STRIPE_HANDLE, &sh->state);
2827
2828 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002829
Dan Williamsa4456852007-07-09 11:56:43 -07002830 /* Want to check and possibly repair P and Q.
2831 * However there could be one 'failed' device, in which
2832 * case we can only check one of them, possibly using the
2833 * other to generate missing data
2834 */
2835
Dan Williamsd82dfee2009-07-14 13:40:57 -07002836 switch (sh->check_state) {
2837 case check_state_idle:
2838 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10002839 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002840 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07002841 * makes sense to check P (If anything else were failed,
2842 * we would have used P to recreate it).
2843 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002844 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07002845 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002846 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002847 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07002848 * anything, so it makes sense to check it
2849 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002850 if (sh->check_state == check_state_run)
2851 sh->check_state = check_state_run_pq;
2852 else
2853 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07002854 }
Dan Williams36d1c642009-07-14 11:48:22 -07002855
Dan Williamsd82dfee2009-07-14 13:40:57 -07002856 /* discard potentially stale zero_sum_result */
2857 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07002858
Dan Williamsd82dfee2009-07-14 13:40:57 -07002859 if (sh->check_state == check_state_run) {
2860 /* async_xor_zero_sum destroys the contents of P */
2861 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
2862 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07002863 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002864 if (sh->check_state >= check_state_run &&
2865 sh->check_state <= check_state_run_pq) {
2866 /* async_syndrome_zero_sum preserves P and Q, so
2867 * no need to mark them !uptodate here
2868 */
2869 set_bit(STRIPE_OP_CHECK, &s->ops_request);
2870 break;
2871 }
Dan Williams36d1c642009-07-14 11:48:22 -07002872
Dan Williamsd82dfee2009-07-14 13:40:57 -07002873 /* we have 2-disk failure */
2874 BUG_ON(s->failed != 2);
2875 /* fall through */
2876 case check_state_compute_result:
2877 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07002878
Dan Williamsd82dfee2009-07-14 13:40:57 -07002879 /* check that a write has not made the stripe insync */
2880 if (test_bit(STRIPE_INSYNC, &sh->state))
2881 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002882
2883 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07002884 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07002885 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002886 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07002887 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10002888 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07002889 s->locked++;
2890 set_bit(R5_LOCKED, &dev->flags);
2891 set_bit(R5_Wantwrite, &dev->flags);
2892 }
2893 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10002894 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07002895 s->locked++;
2896 set_bit(R5_LOCKED, &dev->flags);
2897 set_bit(R5_Wantwrite, &dev->flags);
2898 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002899 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002900 dev = &sh->dev[pd_idx];
2901 s->locked++;
2902 set_bit(R5_LOCKED, &dev->flags);
2903 set_bit(R5_Wantwrite, &dev->flags);
2904 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002905 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002906 dev = &sh->dev[qd_idx];
2907 s->locked++;
2908 set_bit(R5_LOCKED, &dev->flags);
2909 set_bit(R5_Wantwrite, &dev->flags);
2910 }
2911 clear_bit(STRIPE_DEGRADED, &sh->state);
2912
2913 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002914 break;
2915 case check_state_run:
2916 case check_state_run_q:
2917 case check_state_run_pq:
2918 break; /* we will be called again upon completion */
2919 case check_state_check_result:
2920 sh->check_state = check_state_idle;
2921
2922 /* handle a successful check operation, if parity is correct
2923 * we are done. Otherwise update the mismatch count and repair
2924 * parity if !MD_RECOVERY_CHECK
2925 */
2926 if (sh->ops.zero_sum_result == 0) {
2927 /* both parities are correct */
2928 if (!s->failed)
2929 set_bit(STRIPE_INSYNC, &sh->state);
2930 else {
2931 /* in contrast to the raid5 case we can validate
2932 * parity, but still have a failure to write
2933 * back
2934 */
2935 sh->check_state = check_state_compute_result;
2936 /* Returning at this point means that we may go
2937 * off and bring p and/or q uptodate again so
2938 * we make sure to check zero_sum_result again
2939 * to verify if p or q need writeback
2940 */
2941 }
2942 } else {
2943 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2944 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2945 /* don't try to repair!! */
2946 set_bit(STRIPE_INSYNC, &sh->state);
2947 else {
2948 int *target = &sh->ops.target;
2949
2950 sh->ops.target = -1;
2951 sh->ops.target2 = -1;
2952 sh->check_state = check_state_compute_run;
2953 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2954 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2955 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
2956 set_bit(R5_Wantcompute,
2957 &sh->dev[pd_idx].flags);
2958 *target = pd_idx;
2959 target = &sh->ops.target2;
2960 s->uptodate++;
2961 }
2962 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
2963 set_bit(R5_Wantcompute,
2964 &sh->dev[qd_idx].flags);
2965 *target = qd_idx;
2966 s->uptodate++;
2967 }
2968 }
2969 }
2970 break;
2971 case check_state_compute_run:
2972 break;
2973 default:
2974 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2975 __func__, sh->check_state,
2976 (unsigned long long) sh->sector);
2977 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002978 }
2979}
2980
NeilBrownd1688a62011-10-11 16:49:52 +11002981static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07002982{
2983 int i;
2984
2985 /* We have read all the blocks in this stripe and now we need to
2986 * copy some of them into a target stripe for expand.
2987 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07002988 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002989 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2990 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11002991 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11002992 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07002993 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07002994 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07002995
NeilBrown784052e2009-03-31 15:19:07 +11002996 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11002997 sector_t s = raid5_compute_sector(conf, bn, 0,
2998 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10002999 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003000 if (sh2 == NULL)
3001 /* so far only the early blocks of this stripe
3002 * have been requested. When later blocks
3003 * get requested, we will try again
3004 */
3005 continue;
3006 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3007 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3008 /* must have already done this block */
3009 release_stripe(sh2);
3010 continue;
3011 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003012
3013 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003014 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003015 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003016 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003017 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003018
Dan Williamsa4456852007-07-09 11:56:43 -07003019 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3020 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3021 for (j = 0; j < conf->raid_disks; j++)
3022 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003023 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003024 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3025 break;
3026 if (j == conf->raid_disks) {
3027 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3028 set_bit(STRIPE_HANDLE, &sh2->state);
3029 }
3030 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003031
Dan Williamsa4456852007-07-09 11:56:43 -07003032 }
NeilBrowna2e08552007-09-11 15:23:36 -07003033 /* done submitting copies, wait for them to complete */
3034 if (tx) {
3035 async_tx_ack(tx);
3036 dma_wait_for_async_tx(tx);
3037 }
Dan Williamsa4456852007-07-09 11:56:43 -07003038}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
Dan Williams6bfe0b42008-04-30 00:52:32 -07003040
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041/*
3042 * handle_stripe - do things to a stripe.
3043 *
3044 * We lock the stripe and then examine the state of various bits
3045 * to see what needs to be done.
3046 * Possible results:
3047 * return some read request which now have data
3048 * return some write requests which are safely on disc
3049 * schedule a read on some buffers
3050 * schedule a write of some buffers
3051 * return confirmation of parity correctness
3052 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 * buffers are taken off read_list or write_list, and bh_cache buffers
3054 * get BH_Lock set before the stripe lock is released.
3055 *
3056 */
Dan Williamsa4456852007-07-09 11:56:43 -07003057
NeilBrownacfe7262011-07-27 11:00:36 +10003058static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003059{
NeilBrownd1688a62011-10-11 16:49:52 +11003060 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003061 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003062 struct r5dev *dev;
3063 int i;
NeilBrown16a53ec2006-06-26 00:27:38 -07003064
NeilBrownacfe7262011-07-27 11:00:36 +10003065 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003066
NeilBrownacfe7262011-07-27 11:00:36 +10003067 s->syncing = test_bit(STRIPE_SYNCING, &sh->state);
3068 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3069 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3070 s->failed_num[0] = -1;
3071 s->failed_num[1] = -1;
3072
3073 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003074 rcu_read_lock();
NeilBrownc4c16632011-07-26 11:34:20 +10003075 spin_lock_irq(&conf->device_lock);
NeilBrown16a53ec2006-06-26 00:27:38 -07003076 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003077 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003078 sector_t first_bad;
3079 int bad_sectors;
3080 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003081
NeilBrown16a53ec2006-06-26 00:27:38 -07003082 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003083
Dan Williams45b42332007-07-09 11:56:43 -07003084 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown16a53ec2006-06-26 00:27:38 -07003085 i, dev->flags, dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003086 /* maybe we can reply to a read
3087 *
3088 * new wantfill requests are only permitted while
3089 * ops_complete_biofill is guaranteed to be inactive
3090 */
3091 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3092 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3093 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003094
3095 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003096 if (test_bit(R5_LOCKED, &dev->flags))
3097 s->locked++;
3098 if (test_bit(R5_UPTODATE, &dev->flags))
3099 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003100 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003101 s->compute++;
3102 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003103 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003104
NeilBrownacfe7262011-07-27 11:00:36 +10003105 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003106 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003107 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003108 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003109 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003110 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003111 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003112 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003113 }
Dan Williamsa4456852007-07-09 11:56:43 -07003114 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003115 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003116 /* Prefer to use the replacement for reads, but only
3117 * if it is recovered enough and has no bad blocks.
3118 */
3119 rdev = rcu_dereference(conf->disks[i].replacement);
3120 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3121 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3122 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3123 &first_bad, &bad_sectors))
3124 set_bit(R5_ReadRepl, &dev->flags);
3125 else {
3126 rdev = rcu_dereference(conf->disks[i].rdev);
3127 clear_bit(R5_ReadRepl, &dev->flags);
3128 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003129 if (rdev && test_bit(Faulty, &rdev->flags))
3130 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003131 if (rdev) {
3132 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3133 &first_bad, &bad_sectors);
3134 if (s->blocked_rdev == NULL
3135 && (test_bit(Blocked, &rdev->flags)
3136 || is_bad < 0)) {
3137 if (is_bad < 0)
3138 set_bit(BlockedBadBlocks,
3139 &rdev->flags);
3140 s->blocked_rdev = rdev;
3141 atomic_inc(&rdev->nr_pending);
3142 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003143 }
NeilBrown415e72d2010-06-17 17:25:21 +10003144 clear_bit(R5_Insync, &dev->flags);
3145 if (!rdev)
3146 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003147 else if (is_bad) {
3148 /* also not in-sync */
3149 if (!test_bit(WriteErrorSeen, &rdev->flags)) {
3150 /* treat as in-sync, but with a read error
3151 * which we can now try to correct
3152 */
3153 set_bit(R5_Insync, &dev->flags);
3154 set_bit(R5_ReadError, &dev->flags);
3155 }
3156 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003157 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003158 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003159 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003160 set_bit(R5_Insync, &dev->flags);
3161 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3162 test_bit(R5_Expanded, &dev->flags))
3163 /* If we've reshaped into here, we assume it is Insync.
3164 * We will shortly update recovery_offset to make
3165 * it official.
3166 */
3167 set_bit(R5_Insync, &dev->flags);
3168
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003169 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003170 /* This flag does not apply to '.replacement'
3171 * only to .rdev, so make sure to check that*/
3172 struct md_rdev *rdev2 = rcu_dereference(
3173 conf->disks[i].rdev);
3174 if (rdev2 == rdev)
3175 clear_bit(R5_Insync, &dev->flags);
3176 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003177 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003178 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003179 } else
3180 clear_bit(R5_WriteError, &dev->flags);
3181 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003182 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003183 /* This flag does not apply to '.replacement'
3184 * only to .rdev, so make sure to check that*/
3185 struct md_rdev *rdev2 = rcu_dereference(
3186 conf->disks[i].rdev);
3187 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003188 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003189 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003190 } else
3191 clear_bit(R5_MadeGood, &dev->flags);
3192 }
NeilBrown977df362011-12-23 10:17:53 +11003193 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3194 struct md_rdev *rdev2 = rcu_dereference(
3195 conf->disks[i].replacement);
3196 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3197 s->handle_bad_blocks = 1;
3198 atomic_inc(&rdev2->nr_pending);
3199 } else
3200 clear_bit(R5_MadeGoodRepl, &dev->flags);
3201 }
NeilBrown415e72d2010-06-17 17:25:21 +10003202 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003203 /* The ReadError flag will just be confusing now */
3204 clear_bit(R5_ReadError, &dev->flags);
3205 clear_bit(R5_ReWrite, &dev->flags);
3206 }
NeilBrown415e72d2010-06-17 17:25:21 +10003207 if (test_bit(R5_ReadError, &dev->flags))
3208 clear_bit(R5_Insync, &dev->flags);
3209 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003210 if (s->failed < 2)
3211 s->failed_num[s->failed] = i;
3212 s->failed++;
NeilBrown415e72d2010-06-17 17:25:21 +10003213 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003214 }
NeilBrownc4c16632011-07-26 11:34:20 +10003215 spin_unlock_irq(&conf->device_lock);
NeilBrown16a53ec2006-06-26 00:27:38 -07003216 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003217}
NeilBrownf4168852007-02-28 20:11:53 -08003218
NeilBrowncc940152011-07-26 11:35:35 +10003219static void handle_stripe(struct stripe_head *sh)
3220{
3221 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003222 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003223 int i;
NeilBrown84789552011-07-27 11:00:36 +10003224 int prexor;
3225 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003226 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003227
3228 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003229 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003230 /* already being handled, ensure it gets handled
3231 * again when current action finishes */
3232 set_bit(STRIPE_HANDLE, &sh->state);
3233 return;
3234 }
3235
3236 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3237 set_bit(STRIPE_SYNCING, &sh->state);
3238 clear_bit(STRIPE_INSYNC, &sh->state);
3239 }
3240 clear_bit(STRIPE_DELAYED, &sh->state);
3241
3242 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3243 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3244 (unsigned long long)sh->sector, sh->state,
3245 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3246 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003247
NeilBrownacfe7262011-07-27 11:00:36 +10003248 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003249
NeilBrownbc2607f2011-07-28 11:39:22 +10003250 if (s.handle_bad_blocks) {
3251 set_bit(STRIPE_HANDLE, &sh->state);
3252 goto finish;
3253 }
3254
NeilBrown474af965fe2011-07-27 11:00:36 +10003255 if (unlikely(s.blocked_rdev)) {
3256 if (s.syncing || s.expanding || s.expanded ||
3257 s.to_write || s.written) {
3258 set_bit(STRIPE_HANDLE, &sh->state);
3259 goto finish;
3260 }
3261 /* There is nothing for the blocked_rdev to block */
3262 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3263 s.blocked_rdev = NULL;
3264 }
3265
3266 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3267 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3268 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3269 }
3270
3271 pr_debug("locked=%d uptodate=%d to_read=%d"
3272 " to_write=%d failed=%d failed_num=%d,%d\n",
3273 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3274 s.failed_num[0], s.failed_num[1]);
3275 /* check if the array has lost more than max_degraded devices and,
3276 * if so, some requests might need to be failed.
3277 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003278 if (s.failed > conf->max_degraded) {
3279 sh->check_state = 0;
3280 sh->reconstruct_state = 0;
3281 if (s.to_read+s.to_write+s.written)
3282 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
3283 if (s.syncing)
3284 handle_failed_sync(conf, sh, &s);
3285 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003286
3287 /*
3288 * might be able to return some write requests if the parity blocks
3289 * are safe, or on a failed drive
3290 */
3291 pdev = &sh->dev[sh->pd_idx];
3292 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3293 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3294 qdev = &sh->dev[sh->qd_idx];
3295 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3296 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3297 || conf->level < 6;
3298
3299 if (s.written &&
3300 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3301 && !test_bit(R5_LOCKED, &pdev->flags)
3302 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3303 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3304 && !test_bit(R5_LOCKED, &qdev->flags)
3305 && test_bit(R5_UPTODATE, &qdev->flags)))))
3306 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3307
3308 /* Now we might consider reading some blocks, either to check/generate
3309 * parity, or to satisfy requests
3310 * or to load a block that is being partially written.
3311 */
3312 if (s.to_read || s.non_overwrite
3313 || (conf->level == 6 && s.to_write && s.failed)
3314 || (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
3315 handle_stripe_fill(sh, &s, disks);
3316
NeilBrown84789552011-07-27 11:00:36 +10003317 /* Now we check to see if any write operations have recently
3318 * completed
3319 */
3320 prexor = 0;
3321 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3322 prexor = 1;
3323 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3324 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3325 sh->reconstruct_state = reconstruct_state_idle;
3326
3327 /* All the 'written' buffers and the parity block are ready to
3328 * be written back to disk
3329 */
3330 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3331 BUG_ON(sh->qd_idx >= 0 &&
3332 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags));
3333 for (i = disks; i--; ) {
3334 struct r5dev *dev = &sh->dev[i];
3335 if (test_bit(R5_LOCKED, &dev->flags) &&
3336 (i == sh->pd_idx || i == sh->qd_idx ||
3337 dev->written)) {
3338 pr_debug("Writing block %d\n", i);
3339 set_bit(R5_Wantwrite, &dev->flags);
3340 if (prexor)
3341 continue;
3342 if (!test_bit(R5_Insync, &dev->flags) ||
3343 ((i == sh->pd_idx || i == sh->qd_idx) &&
3344 s.failed == 0))
3345 set_bit(STRIPE_INSYNC, &sh->state);
3346 }
3347 }
3348 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3349 s.dec_preread_active = 1;
3350 }
3351
3352 /* Now to consider new write requests and what else, if anything
3353 * should be read. We do not handle new writes when:
3354 * 1/ A 'write' operation (copy+xor) is already in flight.
3355 * 2/ A 'check' operation is in flight, as it may clobber the parity
3356 * block.
3357 */
3358 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3359 handle_stripe_dirtying(conf, sh, &s, disks);
3360
3361 /* maybe we need to check and possibly fix the parity for this stripe
3362 * Any reads will already have been scheduled, so we just see if enough
3363 * data is available. The parity check is held off while parity
3364 * dependent operations are in flight.
3365 */
3366 if (sh->check_state ||
3367 (s.syncing && s.locked == 0 &&
3368 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3369 !test_bit(STRIPE_INSYNC, &sh->state))) {
3370 if (conf->level == 6)
3371 handle_parity_checks6(conf, sh, &s, disks);
3372 else
3373 handle_parity_checks5(conf, sh, &s, disks);
3374 }
NeilBrownc5a31002011-07-27 11:00:36 +10003375
3376 if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
3377 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3378 clear_bit(STRIPE_SYNCING, &sh->state);
3379 }
3380
3381 /* If the failed drives are just a ReadError, then we might need
3382 * to progress the repair/check process
3383 */
3384 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3385 for (i = 0; i < s.failed; i++) {
3386 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3387 if (test_bit(R5_ReadError, &dev->flags)
3388 && !test_bit(R5_LOCKED, &dev->flags)
3389 && test_bit(R5_UPTODATE, &dev->flags)
3390 ) {
3391 if (!test_bit(R5_ReWrite, &dev->flags)) {
3392 set_bit(R5_Wantwrite, &dev->flags);
3393 set_bit(R5_ReWrite, &dev->flags);
3394 set_bit(R5_LOCKED, &dev->flags);
3395 s.locked++;
3396 } else {
3397 /* let's read it back */
3398 set_bit(R5_Wantread, &dev->flags);
3399 set_bit(R5_LOCKED, &dev->flags);
3400 s.locked++;
3401 }
3402 }
3403 }
3404
3405
NeilBrown3687c062011-07-27 11:00:36 +10003406 /* Finish reconstruct operations initiated by the expansion process */
3407 if (sh->reconstruct_state == reconstruct_state_result) {
3408 struct stripe_head *sh_src
3409 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3410 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3411 /* sh cannot be written until sh_src has been read.
3412 * so arrange for sh to be delayed a little
3413 */
3414 set_bit(STRIPE_DELAYED, &sh->state);
3415 set_bit(STRIPE_HANDLE, &sh->state);
3416 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3417 &sh_src->state))
3418 atomic_inc(&conf->preread_active_stripes);
3419 release_stripe(sh_src);
3420 goto finish;
3421 }
3422 if (sh_src)
3423 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003424
NeilBrown3687c062011-07-27 11:00:36 +10003425 sh->reconstruct_state = reconstruct_state_idle;
3426 clear_bit(STRIPE_EXPANDING, &sh->state);
3427 for (i = conf->raid_disks; i--; ) {
3428 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3429 set_bit(R5_LOCKED, &sh->dev[i].flags);
3430 s.locked++;
3431 }
3432 }
3433
3434 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3435 !sh->reconstruct_state) {
3436 /* Need to write out all blocks after computing parity */
3437 sh->disks = conf->raid_disks;
3438 stripe_set_idx(sh->sector, conf, 0, sh);
3439 schedule_reconstruction(sh, &s, 1, 1);
3440 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3441 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3442 atomic_dec(&conf->reshape_stripes);
3443 wake_up(&conf->wait_for_overlap);
3444 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3445 }
3446
3447 if (s.expanding && s.locked == 0 &&
3448 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3449 handle_stripe_expansion(conf, sh);
3450
3451finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003452 /* wait for this device to become unblocked */
NeilBrown43220aa2011-08-31 12:49:14 +10003453 if (conf->mddev->external && unlikely(s.blocked_rdev))
NeilBrownc5709ef2011-07-26 11:35:20 +10003454 md_wait_for_blocked_rdev(s.blocked_rdev, conf->mddev);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003455
NeilBrownbc2607f2011-07-28 11:39:22 +10003456 if (s.handle_bad_blocks)
3457 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003458 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003459 struct r5dev *dev = &sh->dev[i];
3460 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3461 /* We own a safe reference to the rdev */
3462 rdev = conf->disks[i].rdev;
3463 if (!rdev_set_badblocks(rdev, sh->sector,
3464 STRIPE_SECTORS, 0))
3465 md_error(conf->mddev, rdev);
3466 rdev_dec_pending(rdev, conf->mddev);
3467 }
NeilBrownb84db562011-07-28 11:39:23 +10003468 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3469 rdev = conf->disks[i].rdev;
3470 rdev_clear_badblocks(rdev, sh->sector,
3471 STRIPE_SECTORS);
3472 rdev_dec_pending(rdev, conf->mddev);
3473 }
NeilBrown977df362011-12-23 10:17:53 +11003474 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3475 rdev = conf->disks[i].replacement;
3476 rdev_clear_badblocks(rdev, sh->sector,
3477 STRIPE_SECTORS);
3478 rdev_dec_pending(rdev, conf->mddev);
3479 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003480 }
3481
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003482 if (s.ops_request)
3483 raid_run_ops(sh, s.ops_request);
3484
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003485 ops_run_io(sh, &s);
3486
NeilBrownc5709ef2011-07-26 11:35:20 +10003487 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003488 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003489 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003490 * have actually been submitted.
3491 */
3492 atomic_dec(&conf->preread_active_stripes);
3493 if (atomic_read(&conf->preread_active_stripes) <
3494 IO_THRESHOLD)
3495 md_wakeup_thread(conf->mddev->thread);
3496 }
3497
NeilBrownc5709ef2011-07-26 11:35:20 +10003498 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003499
Dan Williams257a4b42011-11-08 16:22:06 +11003500 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003501}
3502
NeilBrownd1688a62011-10-11 16:49:52 +11003503static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504{
3505 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3506 while (!list_empty(&conf->delayed_list)) {
3507 struct list_head *l = conf->delayed_list.next;
3508 struct stripe_head *sh;
3509 sh = list_entry(l, struct stripe_head, lru);
3510 list_del_init(l);
3511 clear_bit(STRIPE_DELAYED, &sh->state);
3512 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3513 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003514 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 }
NeilBrown482c0832011-04-18 18:25:42 +10003516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517}
3518
NeilBrownd1688a62011-10-11 16:49:52 +11003519static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003520{
3521 /* device_lock is held */
3522 struct list_head head;
3523 list_add(&head, &conf->bitmap_list);
3524 list_del_init(&conf->bitmap_list);
3525 while (!list_empty(&head)) {
3526 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3527 list_del_init(&sh->lru);
3528 atomic_inc(&sh->count);
3529 __release_stripe(conf, sh);
3530 }
3531}
3532
NeilBrownfd01b882011-10-11 16:47:53 +11003533int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003534{
NeilBrownd1688a62011-10-11 16:49:52 +11003535 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003536
3537 /* No difference between reads and writes. Just check
3538 * how busy the stripe_cache is
3539 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003540
NeilBrownf022b2f2006-10-03 01:15:56 -07003541 if (conf->inactive_blocked)
3542 return 1;
3543 if (conf->quiesce)
3544 return 1;
3545 if (list_empty_careful(&conf->inactive_list))
3546 return 1;
3547
3548 return 0;
3549}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003550EXPORT_SYMBOL_GPL(md_raid5_congested);
3551
3552static int raid5_congested(void *data, int bits)
3553{
NeilBrownfd01b882011-10-11 16:47:53 +11003554 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003555
3556 return mddev_congested(mddev, bits) ||
3557 md_raid5_congested(mddev, bits);
3558}
NeilBrownf022b2f2006-10-03 01:15:56 -07003559
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003560/* We want read requests to align with chunks where possible,
3561 * but write requests don't need to.
3562 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003563static int raid5_mergeable_bvec(struct request_queue *q,
3564 struct bvec_merge_data *bvm,
3565 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003566{
NeilBrownfd01b882011-10-11 16:47:53 +11003567 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003568 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003569 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003570 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003571 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003572
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003573 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003574 return biovec->bv_len; /* always allow writes to be mergeable */
3575
Andre Noll664e7c42009-06-18 08:45:27 +10003576 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3577 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003578 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3579 if (max < 0) max = 0;
3580 if (max <= biovec->bv_len && bio_sectors == 0)
3581 return biovec->bv_len;
3582 else
3583 return max;
3584}
3585
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003586
NeilBrownfd01b882011-10-11 16:47:53 +11003587static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003588{
3589 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003590 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003591 unsigned int bio_sectors = bio->bi_size >> 9;
3592
Andre Noll664e7c42009-06-18 08:45:27 +10003593 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3594 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003595 return chunk_sectors >=
3596 ((sector & (chunk_sectors - 1)) + bio_sectors);
3597}
3598
3599/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003600 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3601 * later sampled by raid5d.
3602 */
NeilBrownd1688a62011-10-11 16:49:52 +11003603static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003604{
3605 unsigned long flags;
3606
3607 spin_lock_irqsave(&conf->device_lock, flags);
3608
3609 bi->bi_next = conf->retry_read_aligned_list;
3610 conf->retry_read_aligned_list = bi;
3611
3612 spin_unlock_irqrestore(&conf->device_lock, flags);
3613 md_wakeup_thread(conf->mddev->thread);
3614}
3615
3616
NeilBrownd1688a62011-10-11 16:49:52 +11003617static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003618{
3619 struct bio *bi;
3620
3621 bi = conf->retry_read_aligned;
3622 if (bi) {
3623 conf->retry_read_aligned = NULL;
3624 return bi;
3625 }
3626 bi = conf->retry_read_aligned_list;
3627 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003628 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003629 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003630 /*
3631 * this sets the active strip count to 1 and the processed
3632 * strip count to zero (upper 8 bits)
3633 */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003634 bi->bi_phys_segments = 1; /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003635 }
3636
3637 return bi;
3638}
3639
3640
3641/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003642 * The "raid5_align_endio" should check if the read succeeded and if it
3643 * did, call bio_endio on the original bio (having bio_put the new bio
3644 * first).
3645 * If the read failed..
3646 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003647static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003648{
3649 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003650 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003651 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003652 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003653 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003654
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003655 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003656
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003657 rdev = (void*)raid_bi->bi_next;
3658 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003659 mddev = rdev->mddev;
3660 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003661
3662 rdev_dec_pending(rdev, conf->mddev);
3663
3664 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003665 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003666 if (atomic_dec_and_test(&conf->active_aligned_reads))
3667 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003668 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003669 }
3670
3671
Dan Williams45b42332007-07-09 11:56:43 -07003672 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003673
3674 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003675}
3676
Neil Brown387bb172007-02-08 14:20:29 -08003677static int bio_fits_rdev(struct bio *bi)
3678{
Jens Axboe165125e2007-07-24 09:28:11 +02003679 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003680
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003681 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003682 return 0;
3683 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003684 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003685 return 0;
3686
3687 if (q->merge_bvec_fn)
3688 /* it's too hard to apply the merge_bvec_fn at this stage,
3689 * just just give up
3690 */
3691 return 0;
3692
3693 return 1;
3694}
3695
3696
NeilBrownfd01b882011-10-11 16:47:53 +11003697static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003698{
NeilBrownd1688a62011-10-11 16:49:52 +11003699 struct r5conf *conf = mddev->private;
NeilBrown8553fe72009-12-14 12:49:47 +11003700 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003701 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11003702 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11003703 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003704
3705 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003706 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003707 return 0;
3708 }
3709 /*
NeilBrowna167f662010-10-26 18:31:13 +11003710 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003711 */
NeilBrowna167f662010-10-26 18:31:13 +11003712 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003713 if (!align_bi)
3714 return 0;
3715 /*
3716 * set bi_end_io to a new function, and set bi_private to the
3717 * original bio.
3718 */
3719 align_bi->bi_end_io = raid5_align_endio;
3720 align_bi->bi_private = raid_bio;
3721 /*
3722 * compute position
3723 */
NeilBrown112bf892009-03-31 14:39:38 +11003724 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3725 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003726 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003727
NeilBrown671488c2011-12-23 10:17:52 +11003728 end_sector = align_bi->bi_sector + (align_bi->bi_size >> 9);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003729 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11003730 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
3731 if (!rdev || test_bit(Faulty, &rdev->flags) ||
3732 rdev->recovery_offset < end_sector) {
3733 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3734 if (rdev &&
3735 (test_bit(Faulty, &rdev->flags) ||
3736 !(test_bit(In_sync, &rdev->flags) ||
3737 rdev->recovery_offset >= end_sector)))
3738 rdev = NULL;
3739 }
3740 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10003741 sector_t first_bad;
3742 int bad_sectors;
3743
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003744 atomic_inc(&rdev->nr_pending);
3745 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003746 raid_bio->bi_next = (void*)rdev;
3747 align_bi->bi_bdev = rdev->bdev;
3748 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
3749 align_bi->bi_sector += rdev->data_offset;
3750
NeilBrown31c176e2011-07-28 11:39:22 +10003751 if (!bio_fits_rdev(align_bi) ||
3752 is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
3753 &first_bad, &bad_sectors)) {
3754 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08003755 bio_put(align_bi);
3756 rdev_dec_pending(rdev, mddev);
3757 return 0;
3758 }
3759
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003760 spin_lock_irq(&conf->device_lock);
3761 wait_event_lock_irq(conf->wait_for_stripe,
3762 conf->quiesce == 0,
3763 conf->device_lock, /* nothing */);
3764 atomic_inc(&conf->active_aligned_reads);
3765 spin_unlock_irq(&conf->device_lock);
3766
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003767 generic_make_request(align_bi);
3768 return 1;
3769 } else {
3770 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003771 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003772 return 0;
3773 }
3774}
3775
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003776/* __get_priority_stripe - get the next stripe to process
3777 *
3778 * Full stripe writes are allowed to pass preread active stripes up until
3779 * the bypass_threshold is exceeded. In general the bypass_count
3780 * increments when the handle_list is handled before the hold_list; however, it
3781 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3782 * stripe with in flight i/o. The bypass_count will be reset when the
3783 * head of the hold_list has changed, i.e. the head was promoted to the
3784 * handle_list.
3785 */
NeilBrownd1688a62011-10-11 16:49:52 +11003786static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003787{
3788 struct stripe_head *sh;
3789
3790 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3791 __func__,
3792 list_empty(&conf->handle_list) ? "empty" : "busy",
3793 list_empty(&conf->hold_list) ? "empty" : "busy",
3794 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3795
3796 if (!list_empty(&conf->handle_list)) {
3797 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3798
3799 if (list_empty(&conf->hold_list))
3800 conf->bypass_count = 0;
3801 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3802 if (conf->hold_list.next == conf->last_hold)
3803 conf->bypass_count++;
3804 else {
3805 conf->last_hold = conf->hold_list.next;
3806 conf->bypass_count -= conf->bypass_threshold;
3807 if (conf->bypass_count < 0)
3808 conf->bypass_count = 0;
3809 }
3810 }
3811 } else if (!list_empty(&conf->hold_list) &&
3812 ((conf->bypass_threshold &&
3813 conf->bypass_count > conf->bypass_threshold) ||
3814 atomic_read(&conf->pending_full_writes) == 0)) {
3815 sh = list_entry(conf->hold_list.next,
3816 typeof(*sh), lru);
3817 conf->bypass_count -= conf->bypass_threshold;
3818 if (conf->bypass_count < 0)
3819 conf->bypass_count = 0;
3820 } else
3821 return NULL;
3822
3823 list_del_init(&sh->lru);
3824 atomic_inc(&sh->count);
3825 BUG_ON(atomic_read(&sh->count) != 1);
3826 return sh;
3827}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003828
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07003829static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830{
NeilBrownd1688a62011-10-11 16:49:52 +11003831 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11003832 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 sector_t new_sector;
3834 sector_t logical_sector, last_sector;
3835 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01003836 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11003837 int remaining;
NeilBrown7c13edc2011-04-18 18:25:43 +10003838 int plugged;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839
Tejun Heoe9c74692010-09-03 11:56:18 +02003840 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
3841 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02003842 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07003843 }
3844
NeilBrown3d310eb2005-06-21 17:17:26 -07003845 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07003846
NeilBrown802ba062006-12-13 00:34:13 -08003847 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003848 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11003849 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02003850 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003851
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
3853 last_sector = bi->bi_sector + (bi->bi_size>>9);
3854 bi->bi_next = NULL;
3855 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07003856
NeilBrown7c13edc2011-04-18 18:25:43 +10003857 plugged = mddev_check_plugged(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
3859 DEFINE_WAIT(w);
NeilBrown16a53ec2006-06-26 00:27:38 -07003860 int disks, data_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003861 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08003862
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003863 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11003864 previous = 0;
NeilBrownb0f9ec02009-03-31 15:27:18 +11003865 disks = conf->raid_disks;
NeilBrownb578d552006-03-27 01:18:12 -08003866 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11003867 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11003868 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08003869 * 64bit on a 32bit platform, and so it might be
3870 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02003871 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08003872 * the lock is dropped, so once we get a reference
3873 * to the stripe that we think it is, we will have
3874 * to check again.
3875 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003876 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11003877 if (mddev->delta_disks < 0
3878 ? logical_sector < conf->reshape_progress
3879 : logical_sector >= conf->reshape_progress) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003880 disks = conf->previous_raid_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003881 previous = 1;
3882 } else {
NeilBrownfef9c612009-03-31 15:16:46 +11003883 if (mddev->delta_disks < 0
3884 ? logical_sector < conf->reshape_safe
3885 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08003886 spin_unlock_irq(&conf->device_lock);
3887 schedule();
3888 goto retry;
3889 }
3890 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003891 spin_unlock_irq(&conf->device_lock);
3892 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003893 data_disks = disks - conf->max_degraded;
3894
NeilBrown112bf892009-03-31 14:39:38 +11003895 new_sector = raid5_compute_sector(conf, logical_sector,
3896 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003897 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10003898 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 (unsigned long long)new_sector,
3900 (unsigned long long)logical_sector);
3901
NeilBrownb5663ba2009-03-31 14:39:38 +11003902 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10003903 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11003905 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003906 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08003907 * stripe, so we must do the range check again.
3908 * Expansion could still move past after this
3909 * test, but as we are holding a reference to
3910 * 'sh', we know that if that happens,
3911 * STRIPE_EXPANDING will get set and the expansion
3912 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003913 */
3914 int must_retry = 0;
3915 spin_lock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11003916 if (mddev->delta_disks < 0
3917 ? logical_sector >= conf->reshape_progress
3918 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003919 /* mismatch, need to try again */
3920 must_retry = 1;
3921 spin_unlock_irq(&conf->device_lock);
3922 if (must_retry) {
3923 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07003924 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003925 goto retry;
3926 }
3927 }
NeilBrowne62e58a2009-07-01 13:15:35 +10003928
Namhyung Kimffd96e32011-07-18 17:38:51 +10003929 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10003930 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08003931 logical_sector < mddev->suspend_hi) {
3932 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10003933 /* As the suspend_* range is controlled by
3934 * userspace, we want an interruptible
3935 * wait.
3936 */
3937 flush_signals(current);
3938 prepare_to_wait(&conf->wait_for_overlap,
3939 &w, TASK_INTERRUPTIBLE);
3940 if (logical_sector >= mddev->suspend_lo &&
3941 logical_sector < mddev->suspend_hi)
3942 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08003943 goto retry;
3944 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003945
3946 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10003947 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003948 /* Stripe is busy expanding or
3949 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 * and wait a while
3951 */
NeilBrown482c0832011-04-18 18:25:42 +10003952 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 release_stripe(sh);
3954 schedule();
3955 goto retry;
3956 }
3957 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08003958 set_bit(STRIPE_HANDLE, &sh->state);
3959 clear_bit(STRIPE_DELAYED, &sh->state);
Tejun Heoe9c74692010-09-03 11:56:18 +02003960 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11003961 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3962 atomic_inc(&conf->preread_active_stripes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 } else {
3965 /* cannot get stripe for read-ahead, just give-up */
3966 clear_bit(BIO_UPTODATE, &bi->bi_flags);
3967 finish_wait(&conf->wait_for_overlap, &w);
3968 break;
3969 }
3970
3971 }
NeilBrown7c13edc2011-04-18 18:25:43 +10003972 if (!plugged)
3973 md_wakeup_thread(mddev->thread);
3974
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02003976 remaining = raid5_dec_bi_phys_segments(bi);
NeilBrownf6344752006-03-27 01:18:17 -08003977 spin_unlock_irq(&conf->device_lock);
3978 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979
NeilBrown16a53ec2006-06-26 00:27:38 -07003980 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02003982
Neil Brown0e13fe232008-06-28 08:31:20 +10003983 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985}
3986
NeilBrownfd01b882011-10-11 16:47:53 +11003987static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11003988
NeilBrownfd01b882011-10-11 16:47:53 +11003989static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990{
NeilBrown52c03292006-06-26 00:27:43 -07003991 /* reshaping is quite different to recovery/resync so it is
3992 * handled quite separately ... here.
3993 *
3994 * On each call to sync_request, we gather one chunk worth of
3995 * destination stripes and flag them as expanding.
3996 * Then we find all the source stripes and request reads.
3997 * As the reads complete, handle_stripe will copy the data
3998 * into the destination stripe and release that stripe.
3999 */
NeilBrownd1688a62011-10-11 16:49:52 +11004000 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004002 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004003 int raid_disks = conf->previous_raid_disks;
4004 int data_disks = raid_disks - conf->max_degraded;
4005 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004006 int i;
4007 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004008 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004009 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004010 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004011 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004012
NeilBrownfef9c612009-03-31 15:16:46 +11004013 if (sector_nr == 0) {
4014 /* If restarting in the middle, skip the initial sectors */
4015 if (mddev->delta_disks < 0 &&
4016 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4017 sector_nr = raid5_size(mddev, 0, 0)
4018 - conf->reshape_progress;
NeilBrowna6397552009-08-13 10:13:00 +10004019 } else if (mddev->delta_disks >= 0 &&
NeilBrownfef9c612009-03-31 15:16:46 +11004020 conf->reshape_progress > 0)
4021 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004022 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004023 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004024 mddev->curr_resync_completed = sector_nr;
4025 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004026 *skipped = 1;
4027 return sector_nr;
4028 }
NeilBrown52c03292006-06-26 00:27:43 -07004029 }
4030
NeilBrown7a661382009-03-31 15:21:40 +11004031 /* We need to process a full chunk at a time.
4032 * If old and new chunk sizes differ, we need to process the
4033 * largest of these
4034 */
Andre Noll664e7c42009-06-18 08:45:27 +10004035 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4036 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004037 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004038 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004039
NeilBrown52c03292006-06-26 00:27:43 -07004040 /* we update the metadata when there is more than 3Meg
4041 * in the block range (that is rather arbitrary, should
4042 * probably be time based) or when the data about to be
4043 * copied would over-write the source of the data at
4044 * the front of the range.
NeilBrownfef9c612009-03-31 15:16:46 +11004045 * i.e. one new_stripe along from reshape_progress new_maps
4046 * to after where reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004047 */
NeilBrownfef9c612009-03-31 15:16:46 +11004048 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004049 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004050 readpos = conf->reshape_progress;
4051 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004052 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004053 sector_div(safepos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004054 if (mddev->delta_disks < 0) {
NeilBrowned37d832009-05-27 21:39:05 +10004055 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004056 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004057 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004058 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004059 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004060 readpos -= min_t(sector_t, reshape_sectors, readpos);
4061 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004062 }
NeilBrown52c03292006-06-26 00:27:43 -07004063
NeilBrownc8f517c2009-03-31 15:28:40 +11004064 /* 'writepos' is the most advanced device address we might write.
4065 * 'readpos' is the least advanced device address we might read.
4066 * 'safepos' is the least address recorded in the metadata as having
4067 * been reshaped.
4068 * If 'readpos' is behind 'writepos', then there is no way that we can
4069 * ensure safety in the face of a crash - that must be done by userspace
4070 * making a backup of the data. So in that case there is no particular
4071 * rush to update metadata.
4072 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4073 * update the metadata to advance 'safepos' to match 'readpos' so that
4074 * we can be safe in the event of a crash.
4075 * So we insist on updating metadata if safepos is behind writepos and
4076 * readpos is beyond writepos.
4077 * In any case, update the metadata every 10 seconds.
4078 * Maybe that number should be configurable, but I'm not sure it is
4079 * worth it.... maybe it could be a multiple of safemode_delay???
4080 */
NeilBrownfef9c612009-03-31 15:16:46 +11004081 if ((mddev->delta_disks < 0
NeilBrownc8f517c2009-03-31 15:28:40 +11004082 ? (safepos > writepos && readpos < writepos)
4083 : (safepos < writepos && readpos > writepos)) ||
4084 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004085 /* Cannot proceed until we've updated the superblock... */
4086 wait_event(conf->wait_for_overlap,
4087 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004088 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004089 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004090 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07004091 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004092 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07004093 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004094 kthread_should_stop());
4095 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004096 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004097 spin_unlock_irq(&conf->device_lock);
4098 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004099 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004100 }
4101
NeilBrownec32a2b2009-03-31 15:17:38 +11004102 if (mddev->delta_disks < 0) {
4103 BUG_ON(conf->reshape_progress == 0);
4104 stripe_addr = writepos;
4105 BUG_ON((mddev->dev_sectors &
NeilBrown7a661382009-03-31 15:21:40 +11004106 ~((sector_t)reshape_sectors - 1))
4107 - reshape_sectors - stripe_addr
NeilBrownec32a2b2009-03-31 15:17:38 +11004108 != sector_nr);
4109 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004110 BUG_ON(writepos != sector_nr + reshape_sectors);
NeilBrownec32a2b2009-03-31 15:17:38 +11004111 stripe_addr = sector_nr;
4112 }
NeilBrownab69ae12009-03-31 15:26:47 +11004113 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004114 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004115 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004116 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004117 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004118 set_bit(STRIPE_EXPANDING, &sh->state);
4119 atomic_inc(&conf->reshape_stripes);
4120 /* If any of this stripe is beyond the end of the old
4121 * array, then we need to zero those blocks
4122 */
4123 for (j=sh->disks; j--;) {
4124 sector_t s;
4125 if (j == sh->pd_idx)
4126 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004127 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004128 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004129 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004130 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004131 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004132 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004133 continue;
4134 }
4135 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4136 set_bit(R5_Expanded, &sh->dev[j].flags);
4137 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4138 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004139 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004140 set_bit(STRIPE_EXPAND_READY, &sh->state);
4141 set_bit(STRIPE_HANDLE, &sh->state);
4142 }
NeilBrownab69ae12009-03-31 15:26:47 +11004143 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004144 }
4145 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004146 if (mddev->delta_disks < 0)
NeilBrown7a661382009-03-31 15:21:40 +11004147 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004148 else
NeilBrown7a661382009-03-31 15:21:40 +11004149 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004150 spin_unlock_irq(&conf->device_lock);
4151 /* Ok, those stripe are ready. We can start scheduling
4152 * reads on the source stripes.
4153 * The source stripes are determined by mapping the first and last
4154 * block on the destination stripes.
4155 */
NeilBrown52c03292006-06-26 00:27:43 -07004156 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004157 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004158 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004159 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004160 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004161 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004162 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004163 if (last_sector >= mddev->dev_sectors)
4164 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004165 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004166 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004167 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4168 set_bit(STRIPE_HANDLE, &sh->state);
4169 release_stripe(sh);
4170 first_sector += STRIPE_SECTORS;
4171 }
NeilBrownab69ae12009-03-31 15:26:47 +11004172 /* Now that the sources are clearly marked, we can release
4173 * the destination stripes
4174 */
4175 while (!list_empty(&stripes)) {
4176 sh = list_entry(stripes.next, struct stripe_head, lru);
4177 list_del_init(&sh->lru);
4178 release_stripe(sh);
4179 }
NeilBrownc6207272008-02-06 01:39:52 -08004180 /* If this takes us to the resync_max point where we have to pause,
4181 * then we need to write out the superblock.
4182 */
NeilBrown7a661382009-03-31 15:21:40 +11004183 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004184 if ((sector_nr - mddev->curr_resync_completed) * 2
4185 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004186 /* Cannot proceed until we've updated the superblock... */
4187 wait_event(conf->wait_for_overlap,
4188 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004189 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004190 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004191 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004192 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4193 md_wakeup_thread(mddev->thread);
4194 wait_event(mddev->sb_wait,
4195 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4196 || kthread_should_stop());
4197 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004198 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004199 spin_unlock_irq(&conf->device_lock);
4200 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004201 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004202 }
NeilBrown7a661382009-03-31 15:21:40 +11004203 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004204}
4205
4206/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004207static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004208{
NeilBrownd1688a62011-10-11 16:49:52 +11004209 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004210 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004211 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004212 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004213 int still_degraded = 0;
4214 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215
NeilBrown72626682005-09-09 16:23:54 -07004216 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004218
NeilBrown29269552006-03-27 01:18:10 -08004219 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4220 end_reshape(conf);
4221 return 0;
4222 }
NeilBrown72626682005-09-09 16:23:54 -07004223
4224 if (mddev->curr_resync < max_sector) /* aborted */
4225 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4226 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004227 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004228 conf->fullsync = 0;
4229 bitmap_close_sync(mddev->bitmap);
4230
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 return 0;
4232 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004233
NeilBrown64bd6602009-08-03 10:59:58 +10004234 /* Allow raid5_quiesce to complete */
4235 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4236
NeilBrown52c03292006-06-26 00:27:43 -07004237 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4238 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004239
NeilBrownc6207272008-02-06 01:39:52 -08004240 /* No need to check resync_max as we never do more than one
4241 * stripe, and as resync_max will always be on a chunk boundary,
4242 * if the check in md_do_sync didn't fire, there is no chance
4243 * of overstepping resync_max here
4244 */
4245
NeilBrown16a53ec2006-06-26 00:27:38 -07004246 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 * to resync, then assert that we are finished, because there is
4248 * nothing we can do.
4249 */
NeilBrown3285edf2006-06-26 00:27:55 -07004250 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004251 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004252 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004253 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 return rv;
4255 }
NeilBrown72626682005-09-09 16:23:54 -07004256 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004257 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004258 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4259 /* we can skip this block, and probably more */
4260 sync_blocks /= STRIPE_SECTORS;
4261 *skipped = 1;
4262 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264
NeilBrownb47490c2008-02-06 01:39:50 -08004265
4266 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4267
NeilBrowna8c906c2009-06-09 14:39:59 +10004268 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004270 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004272 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004274 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004276 /* Need to check if array will still be degraded after recovery/resync
4277 * We don't need to check the 'failed' flag as when that gets set,
4278 * recovery aborts.
4279 */
NeilBrownf001a702009-06-09 14:30:31 +10004280 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004281 if (conf->disks[i].rdev == NULL)
4282 still_degraded = 1;
4283
4284 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4285
NeilBrown83206d62011-07-26 11:19:49 +10004286 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287
NeilBrown14425772009-10-16 15:55:25 +11004288 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 release_stripe(sh);
4290
4291 return STRIPE_SECTORS;
4292}
4293
NeilBrownd1688a62011-10-11 16:49:52 +11004294static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004295{
4296 /* We may not be able to submit a whole bio at once as there
4297 * may not be enough stripe_heads available.
4298 * We cannot pre-allocate enough stripe_heads as we may need
4299 * more than exist in the cache (if we allow ever large chunks).
4300 * So we do one stripe head at a time and record in
4301 * ->bi_hw_segments how many have been done.
4302 *
4303 * We *know* that this entire raid_bio is in one chunk, so
4304 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4305 */
4306 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004307 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004308 sector_t sector, logical_sector, last_sector;
4309 int scnt = 0;
4310 int remaining;
4311 int handled = 0;
4312
4313 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004314 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004315 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004316 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4317
4318 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004319 logical_sector += STRIPE_SECTORS,
4320 sector += STRIPE_SECTORS,
4321 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004322
Jens Axboe960e7392008-08-15 10:41:18 +02004323 if (scnt < raid5_bi_hw_segments(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004324 /* already done this stripe */
4325 continue;
4326
NeilBrowna8c906c2009-06-09 14:39:59 +10004327 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004328
4329 if (!sh) {
4330 /* failed to get a stripe - must wait */
Jens Axboe960e7392008-08-15 10:41:18 +02004331 raid5_set_bi_hw_segments(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004332 conf->retry_read_aligned = raid_bio;
4333 return handled;
4334 }
4335
Neil Brown387bb172007-02-08 14:20:29 -08004336 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4337 release_stripe(sh);
Jens Axboe960e7392008-08-15 10:41:18 +02004338 raid5_set_bi_hw_segments(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004339 conf->retry_read_aligned = raid_bio;
4340 return handled;
4341 }
4342
Dan Williams36d1c642009-07-14 11:48:22 -07004343 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004344 release_stripe(sh);
4345 handled++;
4346 }
4347 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004348 remaining = raid5_dec_bi_phys_segments(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004349 spin_unlock_irq(&conf->device_lock);
Neil Brown0e13fe232008-06-28 08:31:20 +10004350 if (remaining == 0)
4351 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004352 if (atomic_dec_and_test(&conf->active_aligned_reads))
4353 wake_up(&conf->wait_for_stripe);
4354 return handled;
4355}
4356
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004357
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358/*
4359 * This is our raid5 kernel thread.
4360 *
4361 * We scan the hash table for stripes which can be handled now.
4362 * During the scan, completed stripes are saved for us by the interrupt
4363 * handler, so that they will not have to wait for our next wakeup.
4364 */
NeilBrownfd01b882011-10-11 16:47:53 +11004365static void raid5d(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366{
4367 struct stripe_head *sh;
NeilBrownd1688a62011-10-11 16:49:52 +11004368 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004370 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371
Dan Williams45b42332007-07-09 11:56:43 -07004372 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373
4374 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004376 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 handled = 0;
4378 spin_lock_irq(&conf->device_lock);
4379 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004380 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381
NeilBrown7c13edc2011-04-18 18:25:43 +10004382 if (atomic_read(&mddev->plug_cnt) == 0 &&
4383 !list_empty(&conf->bitmap_list)) {
4384 /* Now is a good time to flush some bitmap updates */
4385 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004386 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004387 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004388 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004389 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004390 activate_bit_delay(conf);
4391 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004392 if (atomic_read(&mddev->plug_cnt) == 0)
4393 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004394
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004395 while ((bio = remove_bio_from_retry(conf))) {
4396 int ok;
4397 spin_unlock_irq(&conf->device_lock);
4398 ok = retry_aligned_read(conf, bio);
4399 spin_lock_irq(&conf->device_lock);
4400 if (!ok)
4401 break;
4402 handled++;
4403 }
4404
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004405 sh = __get_priority_stripe(conf);
4406
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004407 if (!sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 spin_unlock_irq(&conf->device_lock);
4410
4411 handled++;
Dan Williams417b8d42009-10-16 16:25:22 +11004412 handle_stripe(sh);
4413 release_stripe(sh);
4414 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415
NeilBrownde393cd2011-07-28 11:31:48 +10004416 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
4417 md_check_recovery(mddev);
4418
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 spin_lock_irq(&conf->device_lock);
4420 }
Dan Williams45b42332007-07-09 11:56:43 -07004421 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422
4423 spin_unlock_irq(&conf->device_lock);
4424
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004425 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004426 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427
Dan Williams45b42332007-07-09 11:56:43 -07004428 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429}
4430
NeilBrown3f294f42005-11-08 21:39:25 -08004431static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004432raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004433{
NeilBrownd1688a62011-10-11 16:49:52 +11004434 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004435 if (conf)
4436 return sprintf(page, "%d\n", conf->max_nr_stripes);
4437 else
4438 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004439}
4440
NeilBrownc41d4ac2010-06-01 19:37:24 +10004441int
NeilBrownfd01b882011-10-11 16:47:53 +11004442raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004443{
NeilBrownd1688a62011-10-11 16:49:52 +11004444 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004445 int err;
4446
4447 if (size <= 16 || size > 32768)
4448 return -EINVAL;
4449 while (size < conf->max_nr_stripes) {
4450 if (drop_one_stripe(conf))
4451 conf->max_nr_stripes--;
4452 else
4453 break;
4454 }
4455 err = md_allow_write(mddev);
4456 if (err)
4457 return err;
4458 while (size > conf->max_nr_stripes) {
4459 if (grow_one_stripe(conf))
4460 conf->max_nr_stripes++;
4461 else break;
4462 }
4463 return 0;
4464}
4465EXPORT_SYMBOL(raid5_set_cache_size);
4466
NeilBrown3f294f42005-11-08 21:39:25 -08004467static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004468raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004469{
NeilBrownd1688a62011-10-11 16:49:52 +11004470 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004471 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004472 int err;
4473
NeilBrown3f294f42005-11-08 21:39:25 -08004474 if (len >= PAGE_SIZE)
4475 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004476 if (!conf)
4477 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004478
Dan Williams4ef197d82008-04-28 02:15:54 -07004479 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004480 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004481 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004482 if (err)
4483 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004484 return len;
4485}
NeilBrown007583c2005-11-08 21:39:30 -08004486
NeilBrown96de1e62005-11-08 21:39:39 -08004487static struct md_sysfs_entry
4488raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4489 raid5_show_stripe_cache_size,
4490 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004491
4492static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004493raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004494{
NeilBrownd1688a62011-10-11 16:49:52 +11004495 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004496 if (conf)
4497 return sprintf(page, "%d\n", conf->bypass_threshold);
4498 else
4499 return 0;
4500}
4501
4502static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004503raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004504{
NeilBrownd1688a62011-10-11 16:49:52 +11004505 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004506 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004507 if (len >= PAGE_SIZE)
4508 return -EINVAL;
4509 if (!conf)
4510 return -ENODEV;
4511
Dan Williams4ef197d82008-04-28 02:15:54 -07004512 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004513 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004514 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004515 return -EINVAL;
4516 conf->bypass_threshold = new;
4517 return len;
4518}
4519
4520static struct md_sysfs_entry
4521raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4522 S_IRUGO | S_IWUSR,
4523 raid5_show_preread_threshold,
4524 raid5_store_preread_threshold);
4525
4526static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004527stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004528{
NeilBrownd1688a62011-10-11 16:49:52 +11004529 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004530 if (conf)
4531 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4532 else
4533 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004534}
4535
NeilBrown96de1e62005-11-08 21:39:39 -08004536static struct md_sysfs_entry
4537raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004538
NeilBrown007583c2005-11-08 21:39:30 -08004539static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004540 &raid5_stripecache_size.attr,
4541 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004542 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004543 NULL,
4544};
NeilBrown007583c2005-11-08 21:39:30 -08004545static struct attribute_group raid5_attrs_group = {
4546 .name = NULL,
4547 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004548};
4549
Dan Williams80c3a6c2009-03-17 18:10:40 -07004550static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11004551raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07004552{
NeilBrownd1688a62011-10-11 16:49:52 +11004553 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004554
4555 if (!sectors)
4556 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004557 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11004558 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11004559 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004560
Andre Noll9d8f0362009-06-18 08:45:01 +10004561 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004562 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004563 return sectors * (raid_disks - conf->max_degraded);
4564}
4565
NeilBrownd1688a62011-10-11 16:49:52 +11004566static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004567{
4568 struct raid5_percpu *percpu;
4569 unsigned long cpu;
4570
4571 if (!conf->percpu)
4572 return;
4573
4574 get_online_cpus();
4575 for_each_possible_cpu(cpu) {
4576 percpu = per_cpu_ptr(conf->percpu, cpu);
4577 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004578 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004579 }
4580#ifdef CONFIG_HOTPLUG_CPU
4581 unregister_cpu_notifier(&conf->cpu_notify);
4582#endif
4583 put_online_cpus();
4584
4585 free_percpu(conf->percpu);
4586}
4587
NeilBrownd1688a62011-10-11 16:49:52 +11004588static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10004589{
4590 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004591 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10004592 kfree(conf->disks);
4593 kfree(conf->stripe_hashtbl);
4594 kfree(conf);
4595}
4596
Dan Williams36d1c642009-07-14 11:48:22 -07004597#ifdef CONFIG_HOTPLUG_CPU
4598static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4599 void *hcpu)
4600{
NeilBrownd1688a62011-10-11 16:49:52 +11004601 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07004602 long cpu = (long)hcpu;
4603 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4604
4605 switch (action) {
4606 case CPU_UP_PREPARE:
4607 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07004608 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07004609 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004610 if (!percpu->scribble)
4611 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4612
4613 if (!percpu->scribble ||
4614 (conf->level == 6 && !percpu->spare_page)) {
4615 safe_put_page(percpu->spare_page);
4616 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004617 pr_err("%s: failed memory allocation for cpu%ld\n",
4618 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07004619 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07004620 }
4621 break;
4622 case CPU_DEAD:
4623 case CPU_DEAD_FROZEN:
4624 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004625 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004626 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004627 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07004628 break;
4629 default:
4630 break;
4631 }
4632 return NOTIFY_OK;
4633}
4634#endif
4635
NeilBrownd1688a62011-10-11 16:49:52 +11004636static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004637{
4638 unsigned long cpu;
4639 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09004640 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004641 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004642 int err;
4643
Dan Williams36d1c642009-07-14 11:48:22 -07004644 allcpus = alloc_percpu(struct raid5_percpu);
4645 if (!allcpus)
4646 return -ENOMEM;
4647 conf->percpu = allcpus;
4648
4649 get_online_cpus();
4650 err = 0;
4651 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07004652 if (conf->level == 6) {
4653 spare_page = alloc_page(GFP_KERNEL);
4654 if (!spare_page) {
4655 err = -ENOMEM;
4656 break;
4657 }
4658 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
4659 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11004660 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004661 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07004662 err = -ENOMEM;
4663 break;
4664 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07004665 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004666 }
4667#ifdef CONFIG_HOTPLUG_CPU
4668 conf->cpu_notify.notifier_call = raid456_cpu_notify;
4669 conf->cpu_notify.priority = 0;
4670 if (err == 0)
4671 err = register_cpu_notifier(&conf->cpu_notify);
4672#endif
4673 put_online_cpus();
4674
4675 return err;
4676}
4677
NeilBrownd1688a62011-10-11 16:49:52 +11004678static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679{
NeilBrownd1688a62011-10-11 16:49:52 +11004680 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004681 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11004682 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 struct disk_info *disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684
NeilBrown91adb562009-03-31 14:39:39 +11004685 if (mddev->new_level != 5
4686 && mddev->new_level != 4
4687 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10004688 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004689 mdname(mddev), mddev->new_level);
4690 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 }
NeilBrown91adb562009-03-31 14:39:39 +11004692 if ((mddev->new_level == 5
4693 && !algorithm_valid_raid5(mddev->new_layout)) ||
4694 (mddev->new_level == 6
4695 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004696 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11004697 mdname(mddev), mddev->new_layout);
4698 return ERR_PTR(-EIO);
4699 }
4700 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10004701 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004702 mdname(mddev), mddev->raid_disks);
4703 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11004704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705
Andre Noll664e7c42009-06-18 08:45:27 +10004706 if (!mddev->new_chunk_sectors ||
4707 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
4708 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10004709 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
4710 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11004711 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11004712 }
4713
NeilBrownd1688a62011-10-11 16:49:52 +11004714 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11004715 if (conf == NULL)
4716 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11004717 spin_lock_init(&conf->device_lock);
4718 init_waitqueue_head(&conf->wait_for_stripe);
4719 init_waitqueue_head(&conf->wait_for_overlap);
4720 INIT_LIST_HEAD(&conf->handle_list);
4721 INIT_LIST_HEAD(&conf->hold_list);
4722 INIT_LIST_HEAD(&conf->delayed_list);
4723 INIT_LIST_HEAD(&conf->bitmap_list);
4724 INIT_LIST_HEAD(&conf->inactive_list);
4725 atomic_set(&conf->active_stripes, 0);
4726 atomic_set(&conf->preread_active_stripes, 0);
4727 atomic_set(&conf->active_aligned_reads, 0);
4728 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11004729 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11004730
4731 conf->raid_disks = mddev->raid_disks;
4732 if (mddev->reshape_position == MaxSector)
4733 conf->previous_raid_disks = mddev->raid_disks;
4734 else
4735 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004736 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
4737 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11004738
NeilBrown5e5e3e72009-10-16 16:35:30 +11004739 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11004740 GFP_KERNEL);
4741 if (!conf->disks)
4742 goto abort;
4743
4744 conf->mddev = mddev;
4745
4746 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
4747 goto abort;
4748
Dan Williams36d1c642009-07-14 11:48:22 -07004749 conf->level = mddev->new_level;
4750 if (raid5_alloc_percpu(conf) != 0)
4751 goto abort;
4752
NeilBrown0c55e022010-05-03 14:09:02 +10004753 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11004754
4755 list_for_each_entry(rdev, &mddev->disks, same_set) {
4756 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004757 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11004758 || raid_disk < 0)
4759 continue;
4760 disk = conf->disks + raid_disk;
4761
4762 disk->rdev = rdev;
4763
4764 if (test_bit(In_sync, &rdev->flags)) {
4765 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10004766 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
4767 " disk %d\n",
4768 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05004769 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11004770 /* Cannot rely on bitmap to complete recovery */
4771 conf->fullsync = 1;
4772 }
4773
Andre Noll09c9e5f2009-06-18 08:45:55 +10004774 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11004775 conf->level = mddev->new_level;
4776 if (conf->level == 6)
4777 conf->max_degraded = 2;
4778 else
4779 conf->max_degraded = 1;
4780 conf->algorithm = mddev->new_layout;
4781 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11004782 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11004783 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10004784 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11004785 conf->prev_algo = mddev->layout;
4786 }
NeilBrown91adb562009-03-31 14:39:39 +11004787
4788 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11004789 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11004790 if (grow_stripes(conf, conf->max_nr_stripes)) {
4791 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004792 "md/raid:%s: couldn't allocate %dkB for buffers\n",
4793 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004794 goto abort;
4795 } else
NeilBrown0c55e022010-05-03 14:09:02 +10004796 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
4797 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004798
NeilBrown0da3c612009-09-23 18:09:45 +10004799 conf->thread = md_register_thread(raid5d, mddev, NULL);
NeilBrown91adb562009-03-31 14:39:39 +11004800 if (!conf->thread) {
4801 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004802 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11004803 mdname(mddev));
4804 goto abort;
4805 }
4806
4807 return conf;
4808
4809 abort:
4810 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10004811 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11004812 return ERR_PTR(-EIO);
4813 } else
4814 return ERR_PTR(-ENOMEM);
4815}
4816
NeilBrownc148ffd2009-11-13 17:47:00 +11004817
4818static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
4819{
4820 switch (algo) {
4821 case ALGORITHM_PARITY_0:
4822 if (raid_disk < max_degraded)
4823 return 1;
4824 break;
4825 case ALGORITHM_PARITY_N:
4826 if (raid_disk >= raid_disks - max_degraded)
4827 return 1;
4828 break;
4829 case ALGORITHM_PARITY_0_6:
4830 if (raid_disk == 0 ||
4831 raid_disk == raid_disks - 1)
4832 return 1;
4833 break;
4834 case ALGORITHM_LEFT_ASYMMETRIC_6:
4835 case ALGORITHM_RIGHT_ASYMMETRIC_6:
4836 case ALGORITHM_LEFT_SYMMETRIC_6:
4837 case ALGORITHM_RIGHT_SYMMETRIC_6:
4838 if (raid_disk == raid_disks - 1)
4839 return 1;
4840 }
4841 return 0;
4842}
4843
NeilBrownfd01b882011-10-11 16:47:53 +11004844static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11004845{
NeilBrownd1688a62011-10-11 16:49:52 +11004846 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10004847 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11004848 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11004849 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11004850 sector_t reshape_offset = 0;
NeilBrown91adb562009-03-31 14:39:39 +11004851
Andre Noll8c6ac862009-06-18 08:48:06 +10004852 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10004853 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10004854 " -- starting background reconstruction\n",
4855 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004856 if (mddev->reshape_position != MaxSector) {
4857 /* Check that we can continue the reshape.
4858 * Currently only disks can change, it must
4859 * increase, and we must be past the point where
4860 * a stripe over-writes itself
4861 */
4862 sector_t here_new, here_old;
4863 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11004864 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08004865
NeilBrown88ce4932009-03-31 15:24:23 +11004866 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10004867 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08004868 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08004869 mdname(mddev));
4870 return -EINVAL;
4871 }
NeilBrownf6705572006-03-27 01:18:11 -08004872 old_disks = mddev->raid_disks - mddev->delta_disks;
4873 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08004874 * further up in new geometry must map after here in old
4875 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08004876 */
4877 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10004878 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08004879 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004880 printk(KERN_ERR "md/raid:%s: reshape_position not "
4881 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004882 return -EINVAL;
4883 }
NeilBrownc148ffd2009-11-13 17:47:00 +11004884 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08004885 /* here_new is the stripe we will write to */
4886 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10004887 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08004888 (old_disks-max_degraded));
4889 /* here_old is the first stripe that we might need to read
4890 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10004891 if (mddev->delta_disks == 0) {
4892 /* We cannot be sure it is safe to start an in-place
4893 * reshape. It is only safe if user-space if monitoring
4894 * and taking constant backups.
4895 * mdadm always starts a situation like this in
4896 * readonly mode so it can take control before
4897 * allowing any writes. So just check for that.
4898 */
4899 if ((here_new * mddev->new_chunk_sectors !=
4900 here_old * mddev->chunk_sectors) ||
4901 mddev->ro == 0) {
NeilBrown0c55e022010-05-03 14:09:02 +10004902 printk(KERN_ERR "md/raid:%s: in-place reshape must be started"
4903 " in read-only mode - aborting\n",
4904 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10004905 return -EINVAL;
4906 }
4907 } else if (mddev->delta_disks < 0
4908 ? (here_new * mddev->new_chunk_sectors <=
4909 here_old * mddev->chunk_sectors)
4910 : (here_new * mddev->new_chunk_sectors >=
4911 here_old * mddev->chunk_sectors)) {
NeilBrownf6705572006-03-27 01:18:11 -08004912 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10004913 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
4914 "auto-recovery - aborting.\n",
4915 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004916 return -EINVAL;
4917 }
NeilBrown0c55e022010-05-03 14:09:02 +10004918 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
4919 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004920 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08004921 } else {
NeilBrown91adb562009-03-31 14:39:39 +11004922 BUG_ON(mddev->level != mddev->new_level);
4923 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10004924 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11004925 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08004926 }
4927
NeilBrown245f46c2009-03-31 14:39:39 +11004928 if (mddev->private == NULL)
4929 conf = setup_conf(mddev);
4930 else
4931 conf = mddev->private;
4932
NeilBrown91adb562009-03-31 14:39:39 +11004933 if (IS_ERR(conf))
4934 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08004935
NeilBrown91adb562009-03-31 14:39:39 +11004936 mddev->thread = conf->thread;
4937 conf->thread = NULL;
4938 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07004941 * 0 for a fully functional array, 1 or 2 for a degraded array.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 */
NeilBrownc148ffd2009-11-13 17:47:00 +11004943 list_for_each_entry(rdev, &mddev->disks, same_set) {
4944 if (rdev->raid_disk < 0)
4945 continue;
NeilBrown2f115882010-06-17 17:41:03 +10004946 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11004947 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10004948 continue;
4949 }
NeilBrownc148ffd2009-11-13 17:47:00 +11004950 /* This disc is not fully in-sync. However if it
4951 * just stored parity (beyond the recovery_offset),
4952 * when we don't need to be concerned about the
4953 * array being dirty.
4954 * When reshape goes 'backwards', we never have
4955 * partially completed devices, so we only need
4956 * to worry about reshape going forwards.
4957 */
4958 /* Hack because v0.91 doesn't store recovery_offset properly. */
4959 if (mddev->major_version == 0 &&
4960 mddev->minor_version > 90)
4961 rdev->recovery_offset = reshape_offset;
4962
NeilBrownc148ffd2009-11-13 17:47:00 +11004963 if (rdev->recovery_offset < reshape_offset) {
4964 /* We need to check old and new layout */
4965 if (!only_parity(rdev->raid_disk,
4966 conf->algorithm,
4967 conf->raid_disks,
4968 conf->max_degraded))
4969 continue;
4970 }
4971 if (!only_parity(rdev->raid_disk,
4972 conf->prev_algo,
4973 conf->previous_raid_disks,
4974 conf->max_degraded))
4975 continue;
4976 dirty_parity_disks++;
4977 }
NeilBrown91adb562009-03-31 14:39:39 +11004978
NeilBrown908f4fb2011-12-23 10:17:50 +11004979 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980
NeilBrown674806d2010-06-16 17:17:53 +10004981 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10004982 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07004984 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 goto abort;
4986 }
4987
NeilBrown91adb562009-03-31 14:39:39 +11004988 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004989 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11004990 mddev->resync_max_sectors = mddev->dev_sectors;
4991
NeilBrownc148ffd2009-11-13 17:47:00 +11004992 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004994 if (mddev->ok_start_degraded)
4995 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10004996 "md/raid:%s: starting dirty degraded array"
4997 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004998 mdname(mddev));
4999 else {
5000 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005001 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005002 mdname(mddev));
5003 goto abort;
5004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 }
5006
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10005008 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5009 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11005010 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5011 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 else
NeilBrown0c55e022010-05-03 14:09:02 +10005013 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5014 " out of %d devices, algorithm %d\n",
5015 mdname(mddev), conf->level,
5016 mddev->raid_disks - mddev->degraded,
5017 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018
5019 print_raid5_conf(conf);
5020
NeilBrownfef9c612009-03-31 15:16:46 +11005021 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11005022 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005023 atomic_set(&conf->reshape_stripes, 0);
5024 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5025 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5026 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5027 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5028 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005029 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005030 }
5031
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032
5033 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005034 if (mddev->to_remove == &raid5_attrs_group)
5035 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005036 else if (mddev->kobj.sd &&
5037 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005038 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10005039 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08005040 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10005041 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5042
5043 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005044 int chunk_size;
NeilBrown4a5add42010-06-01 19:37:28 +10005045 /* read-ahead size must cover two whole stripes, which
5046 * is 2 * (datadisks) * chunksize where 'n' is the
5047 * number of raid devices
5048 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5050 int stripe = data_disks *
5051 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5052 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5053 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10005054
5055 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005056
5057 mddev->queue->backing_dev_info.congested_data = mddev;
5058 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10005059
5060 chunk_size = mddev->chunk_sectors << 9;
5061 blk_queue_io_min(mddev->queue, chunk_size);
5062 blk_queue_io_opt(mddev->queue, chunk_size *
5063 (conf->raid_disks - conf->max_degraded));
5064
5065 list_for_each_entry(rdev, &mddev->disks, same_set)
5066 disk_stack_limits(mddev->gendisk, rdev->bdev,
5067 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 }
5069
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 return 0;
5071abort:
NeilBrown01f96c02011-09-21 15:30:20 +10005072 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11005073 print_raid5_conf(conf);
5074 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005076 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 return -EIO;
5078}
5079
NeilBrownfd01b882011-10-11 16:47:53 +11005080static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081{
NeilBrownd1688a62011-10-11 16:49:52 +11005082 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083
NeilBrown01f96c02011-09-21 15:30:20 +10005084 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005085 if (mddev->queue)
5086 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10005087 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005088 mddev->private = NULL;
5089 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 return 0;
5091}
5092
NeilBrownfd01b882011-10-11 16:47:53 +11005093static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094{
NeilBrownd1688a62011-10-11 16:49:52 +11005095 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 int i;
5097
Andre Noll9d8f0362009-06-18 08:45:01 +10005098 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5099 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005100 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 for (i = 0; i < conf->raid_disks; i++)
5102 seq_printf (seq, "%s",
5103 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005104 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106}
5107
NeilBrownd1688a62011-10-11 16:49:52 +11005108static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109{
5110 int i;
5111 struct disk_info *tmp;
5112
NeilBrown0c55e022010-05-03 14:09:02 +10005113 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 if (!conf) {
5115 printk("(conf==NULL)\n");
5116 return;
5117 }
NeilBrown0c55e022010-05-03 14:09:02 +10005118 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5119 conf->raid_disks,
5120 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121
5122 for (i = 0; i < conf->raid_disks; i++) {
5123 char b[BDEVNAME_SIZE];
5124 tmp = conf->disks + i;
5125 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005126 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5127 i, !test_bit(Faulty, &tmp->rdev->flags),
5128 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 }
5130}
5131
NeilBrownfd01b882011-10-11 16:47:53 +11005132static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133{
5134 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005135 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005137 int count = 0;
5138 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139
5140 for (i = 0; i < conf->raid_disks; i++) {
5141 tmp = conf->disks + i;
5142 if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005143 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005144 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005145 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005146 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005147 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 }
5149 }
NeilBrown6b965622010-08-18 11:56:59 +10005150 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005151 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005152 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005154 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155}
5156
NeilBrownb8321b62011-12-23 10:17:51 +11005157static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158{
NeilBrownd1688a62011-10-11 16:49:52 +11005159 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11005161 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11005162 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 struct disk_info *p = conf->disks + number;
5164
5165 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11005166 if (rdev == p->rdev)
5167 rdevp = &p->rdev;
5168 else if (rdev == p->replacement)
5169 rdevp = &p->replacement;
5170 else
5171 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11005172
NeilBrown657e3e42011-12-23 10:17:52 +11005173 if (number >= conf->raid_disks &&
5174 conf->reshape_progress == MaxSector)
5175 clear_bit(In_sync, &rdev->flags);
5176
5177 if (test_bit(In_sync, &rdev->flags) ||
5178 atomic_read(&rdev->nr_pending)) {
5179 err = -EBUSY;
5180 goto abort;
5181 }
5182 /* Only remove non-faulty devices if recovery
5183 * isn't possible.
5184 */
5185 if (!test_bit(Faulty, &rdev->flags) &&
5186 mddev->recovery_disabled != conf->recovery_disabled &&
5187 !has_failed(conf) &&
5188 number < conf->raid_disks) {
5189 err = -EBUSY;
5190 goto abort;
5191 }
5192 *rdevp = NULL;
5193 synchronize_rcu();
5194 if (atomic_read(&rdev->nr_pending)) {
5195 /* lost the race, try later */
5196 err = -EBUSY;
5197 *rdevp = rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 }
5199abort:
5200
5201 print_raid5_conf(conf);
5202 return err;
5203}
5204
NeilBrownfd01b882011-10-11 16:47:53 +11005205static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206{
NeilBrownd1688a62011-10-11 16:49:52 +11005207 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005208 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 int disk;
5210 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005211 int first = 0;
5212 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213
NeilBrown7f0da592011-07-28 11:39:22 +10005214 if (mddev->recovery_disabled == conf->recovery_disabled)
5215 return -EBUSY;
5216
NeilBrown674806d2010-06-16 17:17:53 +10005217 if (has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005219 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220
Neil Brown6c2fce22008-06-28 08:31:31 +10005221 if (rdev->raid_disk >= 0)
5222 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223
5224 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005225 * find the disk ... but prefer rdev->saved_raid_disk
5226 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005228 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005229 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005230 conf->disks[rdev->saved_raid_disk].rdev == NULL)
5231 disk = rdev->saved_raid_disk;
5232 else
Neil Brown6c2fce22008-06-28 08:31:31 +10005233 disk = first;
5234 for ( ; disk <= last ; disk++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 if ((p=conf->disks + disk)->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005236 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005238 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005239 if (rdev->saved_raid_disk != disk)
5240 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005241 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 break;
5243 }
5244 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005245 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246}
5247
NeilBrownfd01b882011-10-11 16:47:53 +11005248static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249{
5250 /* no resync is happening, and there is enough space
5251 * on all devices, so we can resize.
5252 * We need to make sure resync covers any new space.
5253 * If the array is shrinking we should possibly wait until
5254 * any io in the removed space completes, but it hardly seems
5255 * worth it.
5256 */
Andre Noll9d8f0362009-06-18 08:45:01 +10005257 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Dan Williams1f403622009-03-31 14:59:03 +11005258 md_set_array_sectors(mddev, raid5_size(mddev, sectors,
5259 mddev->raid_disks));
Dan Williamsb522adc2009-03-31 15:00:31 +11005260 if (mddev->array_sectors >
5261 raid5_size(mddev, sectors, mddev->raid_disks))
5262 return -EINVAL;
Andre Nollf233ea52008-07-21 17:05:22 +10005263 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005264 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005265 if (sectors > mddev->dev_sectors &&
5266 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005267 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5269 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005270 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005271 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272 return 0;
5273}
5274
NeilBrownfd01b882011-10-11 16:47:53 +11005275static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005276{
5277 /* Can only proceed if there are plenty of stripe_heads.
5278 * We need a minimum of one full stripe,, and for sensible progress
5279 * it is best to have about 4 times that.
5280 * If we require 4 times, then the default 256 4K stripe_heads will
5281 * allow for chunk sizes up to 256K, which is probably OK.
5282 * If the chunk size is greater, user-space should request more
5283 * stripe_heads first.
5284 */
NeilBrownd1688a62011-10-11 16:49:52 +11005285 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005286 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5287 > conf->max_nr_stripes ||
5288 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5289 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005290 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5291 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005292 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5293 / STRIPE_SIZE)*4);
5294 return 0;
5295 }
5296 return 1;
5297}
5298
NeilBrownfd01b882011-10-11 16:47:53 +11005299static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005300{
NeilBrownd1688a62011-10-11 16:49:52 +11005301 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005302
NeilBrown88ce4932009-03-31 15:24:23 +11005303 if (mddev->delta_disks == 0 &&
5304 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005305 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005306 return 0; /* nothing to do */
NeilBrowndba034e2008-08-05 15:54:13 +10005307 if (mddev->bitmap)
5308 /* Cannot grow a bitmap yet */
5309 return -EBUSY;
NeilBrown674806d2010-06-16 17:17:53 +10005310 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005311 return -EINVAL;
5312 if (mddev->delta_disks < 0) {
5313 /* We might be able to shrink, but the devices must
5314 * be made bigger first.
5315 * For raid6, 4 is the minimum size.
5316 * Otherwise 2 is the minimum
5317 */
5318 int min = 2;
5319 if (mddev->level == 6)
5320 min = 4;
5321 if (mddev->raid_disks + mddev->delta_disks < min)
5322 return -EINVAL;
5323 }
NeilBrown29269552006-03-27 01:18:10 -08005324
NeilBrown01ee22b2009-06-18 08:47:20 +10005325 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005326 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005327
NeilBrownec32a2b2009-03-31 15:17:38 +11005328 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
NeilBrown63c70c42006-03-27 01:18:13 -08005329}
5330
NeilBrownfd01b882011-10-11 16:47:53 +11005331static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005332{
NeilBrownd1688a62011-10-11 16:49:52 +11005333 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005334 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005335 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005336 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005337
NeilBrownf4168852007-02-28 20:11:53 -08005338 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005339 return -EBUSY;
5340
NeilBrown01ee22b2009-06-18 08:47:20 +10005341 if (!check_stripe_cache(mddev))
5342 return -ENOSPC;
5343
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005344 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown469518a2011-01-31 11:57:43 +11005345 if (!test_bit(In_sync, &rdev->flags)
5346 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005347 spares++;
NeilBrown63c70c42006-03-27 01:18:13 -08005348
NeilBrownf4168852007-02-28 20:11:53 -08005349 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005350 /* Not enough devices even to make a degraded array
5351 * of that size
5352 */
5353 return -EINVAL;
5354
NeilBrownec32a2b2009-03-31 15:17:38 +11005355 /* Refuse to reduce size of the array. Any reductions in
5356 * array size must be through explicit setting of array_size
5357 * attribute.
5358 */
5359 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5360 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10005361 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11005362 "before number of disks\n", mdname(mddev));
5363 return -EINVAL;
5364 }
5365
NeilBrownf6705572006-03-27 01:18:11 -08005366 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005367 spin_lock_irq(&conf->device_lock);
5368 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005369 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005370 conf->prev_chunk_sectors = conf->chunk_sectors;
5371 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005372 conf->prev_algo = conf->algorithm;
5373 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11005374 if (mddev->delta_disks < 0)
5375 conf->reshape_progress = raid5_size(mddev, 0, 0);
5376 else
5377 conf->reshape_progress = 0;
5378 conf->reshape_safe = conf->reshape_progress;
NeilBrown86b42c72009-03-31 15:19:03 +11005379 conf->generation++;
NeilBrown29269552006-03-27 01:18:10 -08005380 spin_unlock_irq(&conf->device_lock);
5381
5382 /* Add some new drives, as many as will fit.
5383 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10005384 * Don't add devices if we are reducing the number of
5385 * devices in the array. This is because it is not possible
5386 * to correctly record the "partially reconstructed" state of
5387 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08005388 */
NeilBrown87a8dec2011-01-31 11:57:43 +11005389 if (mddev->delta_disks >= 0) {
5390 int added_devices = 0;
5391 list_for_each_entry(rdev, &mddev->disks, same_set)
5392 if (rdev->raid_disk < 0 &&
5393 !test_bit(Faulty, &rdev->flags)) {
5394 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11005395 if (rdev->raid_disk
5396 >= conf->previous_raid_disks) {
5397 set_bit(In_sync, &rdev->flags);
5398 added_devices++;
5399 } else
5400 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10005401
5402 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11005403 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11005404 }
NeilBrown87a8dec2011-01-31 11:57:43 +11005405 } else if (rdev->raid_disk >= conf->previous_raid_disks
5406 && !test_bit(Faulty, &rdev->flags)) {
5407 /* This is a spare that was manually added */
5408 set_bit(In_sync, &rdev->flags);
5409 added_devices++;
5410 }
NeilBrown29269552006-03-27 01:18:10 -08005411
NeilBrown87a8dec2011-01-31 11:57:43 +11005412 /* When a reshape changes the number of devices,
5413 * ->degraded is measured against the larger of the
5414 * pre and post number of devices.
5415 */
NeilBrownec32a2b2009-03-31 15:17:38 +11005416 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005417 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11005418 spin_unlock_irqrestore(&conf->device_lock, flags);
5419 }
NeilBrown63c70c42006-03-27 01:18:13 -08005420 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10005421 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07005422 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005423
NeilBrown29269552006-03-27 01:18:10 -08005424 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5425 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5426 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5427 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5428 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005429 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08005430 if (!mddev->sync_thread) {
5431 mddev->recovery = 0;
5432 spin_lock_irq(&conf->device_lock);
5433 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005434 conf->reshape_progress = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005435 spin_unlock_irq(&conf->device_lock);
5436 return -EAGAIN;
5437 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005438 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005439 md_wakeup_thread(mddev->sync_thread);
5440 md_new_event(mddev);
5441 return 0;
5442}
NeilBrown29269552006-03-27 01:18:10 -08005443
NeilBrownec32a2b2009-03-31 15:17:38 +11005444/* This is called from the reshape thread and should make any
5445 * changes needed in 'conf'
5446 */
NeilBrownd1688a62011-10-11 16:49:52 +11005447static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08005448{
NeilBrown29269552006-03-27 01:18:10 -08005449
NeilBrownf6705572006-03-27 01:18:11 -08005450 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
Dan Williams80c3a6c2009-03-17 18:10:40 -07005451
NeilBrownf6705572006-03-27 01:18:11 -08005452 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11005453 conf->previous_raid_disks = conf->raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005454 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08005455 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005456 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07005457
5458 /* read-ahead size must cover two whole stripes, which is
5459 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5460 */
NeilBrown4a5add42010-06-01 19:37:28 +10005461 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11005462 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005463 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11005464 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07005465 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5466 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5467 }
NeilBrown29269552006-03-27 01:18:10 -08005468 }
NeilBrown29269552006-03-27 01:18:10 -08005469}
5470
NeilBrownec32a2b2009-03-31 15:17:38 +11005471/* This is called from the raid5d thread with mddev_lock held.
5472 * It makes config changes to the device.
5473 */
NeilBrownfd01b882011-10-11 16:47:53 +11005474static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11005475{
NeilBrownd1688a62011-10-11 16:49:52 +11005476 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11005477
5478 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5479
NeilBrownec32a2b2009-03-31 15:17:38 +11005480 if (mddev->delta_disks > 0) {
5481 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5482 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005483 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11005484 } else {
5485 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11005486 spin_lock_irq(&conf->device_lock);
5487 mddev->degraded = calc_degraded(conf);
5488 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11005489 for (d = conf->raid_disks ;
5490 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10005491 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11005492 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownb8321b62011-12-23 10:17:51 +11005493 if (rdev &&
5494 raid5_remove_disk(mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10005495 sysfs_unlink_rdev(mddev, rdev);
NeilBrown1a67dde2009-08-13 10:41:49 +10005496 rdev->raid_disk = -1;
5497 }
5498 }
NeilBrowncea9c222009-03-31 15:15:05 +11005499 }
NeilBrown88ce4932009-03-31 15:24:23 +11005500 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005501 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11005502 mddev->reshape_position = MaxSector;
5503 mddev->delta_disks = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11005504 }
5505}
5506
NeilBrownfd01b882011-10-11 16:47:53 +11005507static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07005508{
NeilBrownd1688a62011-10-11 16:49:52 +11005509 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07005510
5511 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08005512 case 2: /* resume for a suspend */
5513 wake_up(&conf->wait_for_overlap);
5514 break;
5515
NeilBrown72626682005-09-09 16:23:54 -07005516 case 1: /* stop all writes */
5517 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005518 /* '2' tells resync/reshape to pause so that all
5519 * active stripes can drain
5520 */
5521 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07005522 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005523 atomic_read(&conf->active_stripes) == 0 &&
5524 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07005525 conf->device_lock, /* nothing */);
NeilBrown64bd6602009-08-03 10:59:58 +10005526 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07005527 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005528 /* allow reshape to continue */
5529 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005530 break;
5531
5532 case 0: /* re-enable writes */
5533 spin_lock_irq(&conf->device_lock);
5534 conf->quiesce = 0;
5535 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08005536 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005537 spin_unlock_irq(&conf->device_lock);
5538 break;
5539 }
NeilBrown72626682005-09-09 16:23:54 -07005540}
NeilBrownb15c2e52006-01-06 00:20:16 -08005541
NeilBrownd562b0c2009-03-31 14:39:39 +11005542
NeilBrownfd01b882011-10-11 16:47:53 +11005543static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11005544{
NeilBrowne373ab12011-10-11 16:48:59 +11005545 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07005546 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11005547
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005548 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11005549 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10005550 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
5551 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005552 return ERR_PTR(-EINVAL);
5553 }
5554
NeilBrowne373ab12011-10-11 16:48:59 +11005555 sectors = raid0_conf->strip_zone[0].zone_end;
5556 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10005557 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005558 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11005559 mddev->new_layout = ALGORITHM_PARITY_N;
5560 mddev->new_chunk_sectors = mddev->chunk_sectors;
5561 mddev->raid_disks += 1;
5562 mddev->delta_disks = 1;
5563 /* make sure it will be not marked as dirty */
5564 mddev->recovery_cp = MaxSector;
5565
5566 return setup_conf(mddev);
5567}
5568
5569
NeilBrownfd01b882011-10-11 16:47:53 +11005570static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005571{
5572 int chunksect;
5573
5574 if (mddev->raid_disks != 2 ||
5575 mddev->degraded > 1)
5576 return ERR_PTR(-EINVAL);
5577
5578 /* Should check if there are write-behind devices? */
5579
5580 chunksect = 64*2; /* 64K by default */
5581
5582 /* The array must be an exact multiple of chunksize */
5583 while (chunksect && (mddev->array_sectors & (chunksect-1)))
5584 chunksect >>= 1;
5585
5586 if ((chunksect<<9) < STRIPE_SIZE)
5587 /* array size does not allow a suitable chunk size */
5588 return ERR_PTR(-EINVAL);
5589
5590 mddev->new_level = 5;
5591 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10005592 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11005593
5594 return setup_conf(mddev);
5595}
5596
NeilBrownfd01b882011-10-11 16:47:53 +11005597static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11005598{
5599 int new_layout;
5600
5601 switch (mddev->layout) {
5602 case ALGORITHM_LEFT_ASYMMETRIC_6:
5603 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
5604 break;
5605 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5606 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
5607 break;
5608 case ALGORITHM_LEFT_SYMMETRIC_6:
5609 new_layout = ALGORITHM_LEFT_SYMMETRIC;
5610 break;
5611 case ALGORITHM_RIGHT_SYMMETRIC_6:
5612 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
5613 break;
5614 case ALGORITHM_PARITY_0_6:
5615 new_layout = ALGORITHM_PARITY_0;
5616 break;
5617 case ALGORITHM_PARITY_N:
5618 new_layout = ALGORITHM_PARITY_N;
5619 break;
5620 default:
5621 return ERR_PTR(-EINVAL);
5622 }
5623 mddev->new_level = 5;
5624 mddev->new_layout = new_layout;
5625 mddev->delta_disks = -1;
5626 mddev->raid_disks -= 1;
5627 return setup_conf(mddev);
5628}
5629
NeilBrownd562b0c2009-03-31 14:39:39 +11005630
NeilBrownfd01b882011-10-11 16:47:53 +11005631static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11005632{
NeilBrown88ce4932009-03-31 15:24:23 +11005633 /* For a 2-drive array, the layout and chunk size can be changed
5634 * immediately as not restriping is needed.
5635 * For larger arrays we record the new value - after validation
5636 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11005637 */
NeilBrownd1688a62011-10-11 16:49:52 +11005638 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10005639 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11005640
NeilBrown597a7112009-06-18 08:47:42 +10005641 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11005642 return -EINVAL;
5643 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005644 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11005645 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005646 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11005647 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005648 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11005649 /* not factor of array size */
5650 return -EINVAL;
5651 }
5652
5653 /* They look valid */
5654
NeilBrown88ce4932009-03-31 15:24:23 +11005655 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10005656 /* can make the change immediately */
5657 if (mddev->new_layout >= 0) {
5658 conf->algorithm = mddev->new_layout;
5659 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11005660 }
5661 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10005662 conf->chunk_sectors = new_chunk ;
5663 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11005664 }
5665 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5666 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11005667 }
NeilBrown50ac1682009-06-18 08:47:55 +10005668 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11005669}
5670
NeilBrownfd01b882011-10-11 16:47:53 +11005671static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11005672{
NeilBrown597a7112009-06-18 08:47:42 +10005673 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10005674
NeilBrown597a7112009-06-18 08:47:42 +10005675 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11005676 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005677 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005678 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11005679 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005680 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11005681 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005682 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11005683 /* not factor of array size */
5684 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005685 }
NeilBrown88ce4932009-03-31 15:24:23 +11005686
5687 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10005688 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11005689}
5690
NeilBrownfd01b882011-10-11 16:47:53 +11005691static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005692{
5693 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005694 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005695 * raid1 - if there are two drives. We need to know the chunk size
5696 * raid4 - trivial - just use a raid4 layout.
5697 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005698 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005699 if (mddev->level == 0)
5700 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11005701 if (mddev->level == 1)
5702 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11005703 if (mddev->level == 4) {
5704 mddev->new_layout = ALGORITHM_PARITY_N;
5705 mddev->new_level = 5;
5706 return setup_conf(mddev);
5707 }
NeilBrownfc9739c2009-03-31 14:57:20 +11005708 if (mddev->level == 6)
5709 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11005710
5711 return ERR_PTR(-EINVAL);
5712}
5713
NeilBrownfd01b882011-10-11 16:47:53 +11005714static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11005715{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005716 /* raid4 can take over:
5717 * raid0 - if there is only one strip zone
5718 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11005719 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005720 if (mddev->level == 0)
5721 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11005722 if (mddev->level == 5 &&
5723 mddev->layout == ALGORITHM_PARITY_N) {
5724 mddev->new_layout = 0;
5725 mddev->new_level = 4;
5726 return setup_conf(mddev);
5727 }
5728 return ERR_PTR(-EINVAL);
5729}
NeilBrownd562b0c2009-03-31 14:39:39 +11005730
NeilBrown84fc4b52011-10-11 16:49:58 +11005731static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11005732
NeilBrownfd01b882011-10-11 16:47:53 +11005733static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11005734{
5735 /* Currently can only take over a raid5. We map the
5736 * personality to an equivalent raid6 personality
5737 * with the Q block at the end.
5738 */
5739 int new_layout;
5740
5741 if (mddev->pers != &raid5_personality)
5742 return ERR_PTR(-EINVAL);
5743 if (mddev->degraded > 1)
5744 return ERR_PTR(-EINVAL);
5745 if (mddev->raid_disks > 253)
5746 return ERR_PTR(-EINVAL);
5747 if (mddev->raid_disks < 3)
5748 return ERR_PTR(-EINVAL);
5749
5750 switch (mddev->layout) {
5751 case ALGORITHM_LEFT_ASYMMETRIC:
5752 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
5753 break;
5754 case ALGORITHM_RIGHT_ASYMMETRIC:
5755 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
5756 break;
5757 case ALGORITHM_LEFT_SYMMETRIC:
5758 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
5759 break;
5760 case ALGORITHM_RIGHT_SYMMETRIC:
5761 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
5762 break;
5763 case ALGORITHM_PARITY_0:
5764 new_layout = ALGORITHM_PARITY_0_6;
5765 break;
5766 case ALGORITHM_PARITY_N:
5767 new_layout = ALGORITHM_PARITY_N;
5768 break;
5769 default:
5770 return ERR_PTR(-EINVAL);
5771 }
5772 mddev->new_level = 6;
5773 mddev->new_layout = new_layout;
5774 mddev->delta_disks = 1;
5775 mddev->raid_disks += 1;
5776 return setup_conf(mddev);
5777}
5778
5779
NeilBrown84fc4b52011-10-11 16:49:58 +11005780static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07005781{
5782 .name = "raid6",
5783 .level = 6,
5784 .owner = THIS_MODULE,
5785 .make_request = make_request,
5786 .run = run,
5787 .stop = stop,
5788 .status = status,
5789 .error_handler = error,
5790 .hot_add_disk = raid5_add_disk,
5791 .hot_remove_disk= raid5_remove_disk,
5792 .spare_active = raid5_spare_active,
5793 .sync_request = sync_request,
5794 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005795 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10005796 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08005797 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005798 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07005799 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11005800 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07005801};
NeilBrown84fc4b52011-10-11 16:49:58 +11005802static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803{
5804 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08005805 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806 .owner = THIS_MODULE,
5807 .make_request = make_request,
5808 .run = run,
5809 .stop = stop,
5810 .status = status,
5811 .error_handler = error,
5812 .hot_add_disk = raid5_add_disk,
5813 .hot_remove_disk= raid5_remove_disk,
5814 .spare_active = raid5_spare_active,
5815 .sync_request = sync_request,
5816 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005817 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08005818 .check_reshape = raid5_check_reshape,
5819 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005820 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07005821 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11005822 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823};
5824
NeilBrown84fc4b52011-10-11 16:49:58 +11005825static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826{
NeilBrown2604b702006-01-06 00:20:36 -08005827 .name = "raid4",
5828 .level = 4,
5829 .owner = THIS_MODULE,
5830 .make_request = make_request,
5831 .run = run,
5832 .stop = stop,
5833 .status = status,
5834 .error_handler = error,
5835 .hot_add_disk = raid5_add_disk,
5836 .hot_remove_disk= raid5_remove_disk,
5837 .spare_active = raid5_spare_active,
5838 .sync_request = sync_request,
5839 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005840 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08005841 .check_reshape = raid5_check_reshape,
5842 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005843 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08005844 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11005845 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08005846};
5847
5848static int __init raid5_init(void)
5849{
NeilBrown16a53ec2006-06-26 00:27:38 -07005850 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08005851 register_md_personality(&raid5_personality);
5852 register_md_personality(&raid4_personality);
5853 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854}
5855
NeilBrown2604b702006-01-06 00:20:36 -08005856static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857{
NeilBrown16a53ec2006-06-26 00:27:38 -07005858 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08005859 unregister_md_personality(&raid5_personality);
5860 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861}
5862
5863module_init(raid5_init);
5864module_exit(raid5_exit);
5865MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11005866MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08005868MODULE_ALIAS("md-raid5");
5869MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08005870MODULE_ALIAS("md-level-5");
5871MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07005872MODULE_ALIAS("md-personality-8"); /* RAID6 */
5873MODULE_ALIAS("md-raid6");
5874MODULE_ALIAS("md-level-6");
5875
5876/* This used to be two separate modules, they were: */
5877MODULE_ALIAS("raid5");
5878MODULE_ALIAS("raid6");