blob: 2ae63c5b1c2591f7a174a9b11f3f7638ba97ff66 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * raid5.c : Multiple Devices driver for Linux
3 * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
4 * Copyright (C) 1999, 2000 Ingo Molnar
NeilBrown16a53ec2006-06-26 00:27:38 -07005 * Copyright (C) 2002, 2003 H. Peter Anvin
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
NeilBrown16a53ec2006-06-26 00:27:38 -07007 * RAID-4/5/6 management functions.
8 * Thanks to Penguin Computing for making the RAID-6 development possible
9 * by donating a test server!
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
NeilBrownae3c20c2006-07-10 04:44:17 -070021/*
22 * BITMAP UNPLUGGING:
23 *
24 * The sequencing for updating the bitmap reliably is a little
25 * subtle (and I got it wrong the first time) so it deserves some
26 * explanation.
27 *
28 * We group bitmap updates into batches. Each batch has a number.
29 * We may write out several batches at once, but that isn't very important.
NeilBrown7c13edc2011-04-18 18:25:43 +100030 * conf->seq_write is the number of the last batch successfully written.
31 * conf->seq_flush is the number of the last batch that was closed to
NeilBrownae3c20c2006-07-10 04:44:17 -070032 * new additions.
33 * When we discover that we will need to write to any block in a stripe
34 * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq
NeilBrown7c13edc2011-04-18 18:25:43 +100035 * the number of the batch it will be in. This is seq_flush+1.
NeilBrownae3c20c2006-07-10 04:44:17 -070036 * When we are ready to do a write, if that batch hasn't been written yet,
37 * we plug the array and queue the stripe for later.
38 * When an unplug happens, we increment bm_flush, thus closing the current
39 * batch.
40 * When we notice that bm_flush > bm_write, we write out all pending updates
41 * to the bitmap, and advance bm_write to where bm_flush was.
42 * This may occasionally write a bit out twice, but is sure never to
43 * miss any bits.
44 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
NeilBrownbff61972009-03-31 14:33:13 +110046#include <linux/blkdev.h>
NeilBrownf6705572006-03-27 01:18:11 -080047#include <linux/kthread.h>
Dan Williamsf701d582009-03-31 15:09:39 +110048#include <linux/raid/pq.h>
Dan Williams91c00922007-01-02 13:52:30 -070049#include <linux/async_tx.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040050#include <linux/module.h>
Dan Williams07a3b412009-08-29 19:13:13 -070051#include <linux/async.h>
NeilBrownbff61972009-03-31 14:33:13 +110052#include <linux/seq_file.h>
Dan Williams36d1c642009-07-14 11:48:22 -070053#include <linux/cpu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090054#include <linux/slab.h>
Christian Dietrich8bda4702011-07-27 11:00:36 +100055#include <linux/ratelimit.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110056#include "md.h"
NeilBrownbff61972009-03-31 14:33:13 +110057#include "raid5.h"
Trela Maciej54071b32010-03-08 16:02:42 +110058#include "raid0.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110059#include "bitmap.h"
NeilBrown72626682005-09-09 16:23:54 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/*
62 * Stripe cache
63 */
64
65#define NR_STRIPES 256
66#define STRIPE_SIZE PAGE_SIZE
67#define STRIPE_SHIFT (PAGE_SHIFT - 9)
68#define STRIPE_SECTORS (STRIPE_SIZE>>9)
69#define IO_THRESHOLD 1
Dan Williams8b3e6cd2008-04-28 02:15:53 -070070#define BYPASS_THRESHOLD 1
NeilBrownfccddba2006-01-06 00:20:33 -080071#define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define HASH_MASK (NR_HASH - 1)
73
NeilBrownd1688a62011-10-11 16:49:52 +110074static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
NeilBrowndb298e12011-10-07 14:23:00 +110075{
76 int hash = (sect >> STRIPE_SHIFT) & HASH_MASK;
77 return &conf->stripe_hashtbl[hash];
78}
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80/* bio's attached to a stripe+device for I/O are linked together in bi_sector
81 * order without overlap. There may be several bio's per stripe+device, and
82 * a bio could span several devices.
83 * When walking this list for a particular stripe+device, we must never proceed
84 * beyond a bio that extends past this device, as the next bio might no longer
85 * be valid.
NeilBrowndb298e12011-10-07 14:23:00 +110086 * This function is used to determine the 'next' bio in the list, given the sector
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * of the current stripe+device
88 */
NeilBrowndb298e12011-10-07 14:23:00 +110089static inline struct bio *r5_next_bio(struct bio *bio, sector_t sector)
90{
91 int sectors = bio->bi_size >> 9;
92 if (bio->bi_sector + sectors < sector + STRIPE_SECTORS)
93 return bio->bi_next;
94 else
95 return NULL;
96}
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Jens Axboe960e7392008-08-15 10:41:18 +020098/*
Jens Axboe5b99c2f2008-08-15 10:56:11 +020099 * We maintain a biased count of active stripes in the bottom 16 bits of
100 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
Jens Axboe960e7392008-08-15 10:41:18 +0200101 */
102static inline int raid5_bi_phys_segments(struct bio *bio)
103{
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200104 return bio->bi_phys_segments & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200105}
106
107static inline int raid5_bi_hw_segments(struct bio *bio)
108{
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200109 return (bio->bi_phys_segments >> 16) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200110}
111
112static inline int raid5_dec_bi_phys_segments(struct bio *bio)
113{
114 --bio->bi_phys_segments;
115 return raid5_bi_phys_segments(bio);
116}
117
118static inline int raid5_dec_bi_hw_segments(struct bio *bio)
119{
120 unsigned short val = raid5_bi_hw_segments(bio);
121
122 --val;
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200123 bio->bi_phys_segments = (val << 16) | raid5_bi_phys_segments(bio);
Jens Axboe960e7392008-08-15 10:41:18 +0200124 return val;
125}
126
127static inline void raid5_set_bi_hw_segments(struct bio *bio, unsigned int cnt)
128{
Namhyung Kim9b2dc8b2011-06-13 14:48:22 +0900129 bio->bi_phys_segments = raid5_bi_phys_segments(bio) | (cnt << 16);
Jens Axboe960e7392008-08-15 10:41:18 +0200130}
131
NeilBrownd0dabf72009-03-31 14:39:38 +1100132/* Find first data disk in a raid6 stripe */
133static inline int raid6_d0(struct stripe_head *sh)
134{
NeilBrown67cc2b82009-03-31 14:39:38 +1100135 if (sh->ddf_layout)
136 /* ddf always start from first device */
137 return 0;
138 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100139 if (sh->qd_idx == sh->disks - 1)
140 return 0;
141 else
142 return sh->qd_idx + 1;
143}
NeilBrown16a53ec2006-06-26 00:27:38 -0700144static inline int raid6_next_disk(int disk, int raid_disks)
145{
146 disk++;
147 return (disk < raid_disks) ? disk : 0;
148}
Dan Williamsa4456852007-07-09 11:56:43 -0700149
NeilBrownd0dabf72009-03-31 14:39:38 +1100150/* When walking through the disks in a raid5, starting at raid6_d0,
151 * We need to map each disk to a 'slot', where the data disks are slot
152 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
153 * is raid_disks-1. This help does that mapping.
154 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100155static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
156 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100157{
Dan Williams66295422009-10-19 18:09:32 -0700158 int slot = *count;
NeilBrown67cc2b82009-03-31 14:39:38 +1100159
NeilBrowne4424fe2009-10-16 16:27:34 +1100160 if (sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700161 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100162 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100163 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100164 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100165 return syndrome_disks + 1;
NeilBrowne4424fe2009-10-16 16:27:34 +1100166 if (!sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700167 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100168 return slot;
169}
170
Dan Williamsa4456852007-07-09 11:56:43 -0700171static void return_io(struct bio *return_bi)
172{
173 struct bio *bi = return_bi;
174 while (bi) {
Dan Williamsa4456852007-07-09 11:56:43 -0700175
176 return_bi = bi->bi_next;
177 bi->bi_next = NULL;
178 bi->bi_size = 0;
Neil Brown0e13fe232008-06-28 08:31:20 +1000179 bio_endio(bi, 0);
Dan Williamsa4456852007-07-09 11:56:43 -0700180 bi = return_bi;
181 }
182}
183
NeilBrownd1688a62011-10-11 16:49:52 +1100184static void print_raid5_conf (struct r5conf *conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Dan Williams600aa102008-06-28 08:32:05 +1000186static int stripe_operations_active(struct stripe_head *sh)
187{
188 return sh->check_state || sh->reconstruct_state ||
189 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
190 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
191}
192
NeilBrownd1688a62011-10-11 16:49:52 +1100193static void __release_stripe(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 if (atomic_dec_and_test(&sh->count)) {
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200196 BUG_ON(!list_empty(&sh->lru));
197 BUG_ON(atomic_read(&conf->active_stripes)==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (test_bit(STRIPE_HANDLE, &sh->state)) {
NeilBrown482c0832011-04-18 18:25:42 +1000199 if (test_bit(STRIPE_DELAYED, &sh->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 list_add_tail(&sh->lru, &conf->delayed_list);
NeilBrown482c0832011-04-18 18:25:42 +1000201 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
202 sh->bm_seq - conf->seq_write > 0)
NeilBrown72626682005-09-09 16:23:54 -0700203 list_add_tail(&sh->lru, &conf->bitmap_list);
NeilBrown482c0832011-04-18 18:25:42 +1000204 else {
NeilBrown72626682005-09-09 16:23:54 -0700205 clear_bit(STRIPE_BIT_DELAY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 list_add_tail(&sh->lru, &conf->handle_list);
NeilBrown72626682005-09-09 16:23:54 -0700207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 md_wakeup_thread(conf->mddev->thread);
209 } else {
Dan Williams600aa102008-06-28 08:32:05 +1000210 BUG_ON(stripe_operations_active(sh));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
212 atomic_dec(&conf->preread_active_stripes);
213 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
214 md_wakeup_thread(conf->mddev->thread);
215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 atomic_dec(&conf->active_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800217 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
218 list_add_tail(&sh->lru, &conf->inactive_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 wake_up(&conf->wait_for_stripe);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -0800220 if (conf->retry_read_aligned)
221 md_wakeup_thread(conf->mddev->thread);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
224 }
225}
NeilBrownd0dabf72009-03-31 14:39:38 +1100226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227static void release_stripe(struct stripe_head *sh)
228{
NeilBrownd1688a62011-10-11 16:49:52 +1100229 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 unsigned long flags;
NeilBrown16a53ec2006-06-26 00:27:38 -0700231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 spin_lock_irqsave(&conf->device_lock, flags);
233 __release_stripe(conf, sh);
234 spin_unlock_irqrestore(&conf->device_lock, flags);
235}
236
NeilBrownfccddba2006-01-06 00:20:33 -0800237static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Dan Williams45b42332007-07-09 11:56:43 -0700239 pr_debug("remove_hash(), stripe %llu\n",
240 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
NeilBrownfccddba2006-01-06 00:20:33 -0800242 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
NeilBrownd1688a62011-10-11 16:49:52 +1100245static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
NeilBrownfccddba2006-01-06 00:20:33 -0800247 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Dan Williams45b42332007-07-09 11:56:43 -0700249 pr_debug("insert_hash(), stripe %llu\n",
250 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
NeilBrownfccddba2006-01-06 00:20:33 -0800252 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
255
256/* find an idle stripe, make sure it is unhashed, and return it. */
NeilBrownd1688a62011-10-11 16:49:52 +1100257static struct stripe_head *get_free_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 struct stripe_head *sh = NULL;
260 struct list_head *first;
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (list_empty(&conf->inactive_list))
263 goto out;
264 first = conf->inactive_list.next;
265 sh = list_entry(first, struct stripe_head, lru);
266 list_del_init(first);
267 remove_hash(sh);
268 atomic_inc(&conf->active_stripes);
269out:
270 return sh;
271}
272
NeilBrowne4e11e32010-06-16 16:45:16 +1000273static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 struct page *p;
276 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000277 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
NeilBrowne4e11e32010-06-16 16:45:16 +1000279 for (i = 0; i < num ; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 p = sh->dev[i].page;
281 if (!p)
282 continue;
283 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800284 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286}
287
NeilBrowne4e11e32010-06-16 16:45:16 +1000288static int grow_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000291 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
NeilBrowne4e11e32010-06-16 16:45:16 +1000293 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 struct page *page;
295
296 if (!(page = alloc_page(GFP_KERNEL))) {
297 return 1;
298 }
299 sh->dev[i].page = page;
300 }
301 return 0;
302}
303
NeilBrown784052e2009-03-31 15:19:07 +1100304static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrownd1688a62011-10-11 16:49:52 +1100305static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100306 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
NeilBrownb5663ba2009-03-31 14:39:38 +1100308static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
NeilBrownd1688a62011-10-11 16:49:52 +1100310 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800311 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200313 BUG_ON(atomic_read(&sh->count) != 0);
314 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000315 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700316
Dan Williams45b42332007-07-09 11:56:43 -0700317 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 (unsigned long long)sh->sector);
319
320 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700321
NeilBrown86b42c72009-03-31 15:19:03 +1100322 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100323 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100325 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 sh->state = 0;
327
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800328
329 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 struct r5dev *dev = &sh->dev[i];
331
Dan Williamsd84e0f12007-01-02 13:52:30 -0700332 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700334 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700336 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000338 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100341 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343 insert_hash(conf, sh);
344}
345
NeilBrownd1688a62011-10-11 16:49:52 +1100346static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100347 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -0800350 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Dan Williams45b42332007-07-09 11:56:43 -0700352 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
NeilBrownfccddba2006-01-06 00:20:33 -0800353 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100354 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700356 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return NULL;
358}
359
NeilBrown674806d2010-06-16 17:17:53 +1000360/*
361 * Need to check if array has failed when deciding whether to:
362 * - start an array
363 * - remove non-faulty devices
364 * - add a spare
365 * - allow a reshape
366 * This determination is simple when no reshape is happening.
367 * However if there is a reshape, we need to carefully check
368 * both the before and after sections.
369 * This is because some failed devices may only affect one
370 * of the two sections, and some non-in_sync devices may
371 * be insync in the section most affected by failed devices.
372 */
NeilBrown908f4fb2011-12-23 10:17:50 +1100373static int calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000374{
NeilBrown908f4fb2011-12-23 10:17:50 +1100375 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000376 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000377
378 rcu_read_lock();
379 degraded = 0;
380 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100381 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown674806d2010-06-16 17:17:53 +1000382 if (!rdev || test_bit(Faulty, &rdev->flags))
383 degraded++;
384 else if (test_bit(In_sync, &rdev->flags))
385 ;
386 else
387 /* not in-sync or faulty.
388 * If the reshape increases the number of devices,
389 * this is being recovered by the reshape, so
390 * this 'previous' section is not in_sync.
391 * If the number of devices is being reduced however,
392 * the device can only be part of the array if
393 * we are reverting a reshape, so this section will
394 * be in-sync.
395 */
396 if (conf->raid_disks >= conf->previous_raid_disks)
397 degraded++;
398 }
399 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100400 if (conf->raid_disks == conf->previous_raid_disks)
401 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000402 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100403 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000404 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100405 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown674806d2010-06-16 17:17:53 +1000406 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100407 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000408 else if (test_bit(In_sync, &rdev->flags))
409 ;
410 else
411 /* not in-sync or faulty.
412 * If reshape increases the number of devices, this
413 * section has already been recovered, else it
414 * almost certainly hasn't.
415 */
416 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100417 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000418 }
419 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100420 if (degraded2 > degraded)
421 return degraded2;
422 return degraded;
423}
424
425static int has_failed(struct r5conf *conf)
426{
427 int degraded;
428
429 if (conf->mddev->reshape_position == MaxSector)
430 return conf->mddev->degraded > conf->max_degraded;
431
432 degraded = calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000433 if (degraded > conf->max_degraded)
434 return 1;
435 return 0;
436}
437
NeilBrownb5663ba2009-03-31 14:39:38 +1100438static struct stripe_head *
NeilBrownd1688a62011-10-11 16:49:52 +1100439get_active_stripe(struct r5conf *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000440 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
442 struct stripe_head *sh;
443
Dan Williams45b42332007-07-09 11:56:43 -0700444 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 spin_lock_irq(&conf->device_lock);
447
448 do {
NeilBrown72626682005-09-09 16:23:54 -0700449 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000450 conf->quiesce == 0 || noquiesce,
NeilBrown72626682005-09-09 16:23:54 -0700451 conf->device_lock, /* nothing */);
NeilBrown86b42c72009-03-31 15:19:03 +1100452 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (!sh) {
454 if (!conf->inactive_blocked)
455 sh = get_free_stripe(conf);
456 if (noblock && sh == NULL)
457 break;
458 if (!sh) {
459 conf->inactive_blocked = 1;
460 wait_event_lock_irq(conf->wait_for_stripe,
461 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800462 (atomic_read(&conf->active_stripes)
463 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 || !conf->inactive_blocked),
465 conf->device_lock,
NeilBrown7c13edc2011-04-18 18:25:43 +1000466 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 conf->inactive_blocked = 0;
468 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100469 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 } else {
471 if (atomic_read(&sh->count)) {
NeilBrownab69ae12009-03-31 15:26:47 +1100472 BUG_ON(!list_empty(&sh->lru)
473 && !test_bit(STRIPE_EXPANDING, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 } else {
475 if (!test_bit(STRIPE_HANDLE, &sh->state))
476 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700477 if (list_empty(&sh->lru) &&
478 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700479 BUG();
480 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
482 }
483 } while (sh == NULL);
484
485 if (sh)
486 atomic_inc(&sh->count);
487
488 spin_unlock_irq(&conf->device_lock);
489 return sh;
490}
491
NeilBrown6712ecf2007-09-27 12:47:43 +0200492static void
493raid5_end_read_request(struct bio *bi, int error);
494static void
495raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700496
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000497static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700498{
NeilBrownd1688a62011-10-11 16:49:52 +1100499 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700500 int i, disks = sh->disks;
501
502 might_sleep();
503
504 for (i = disks; i--; ) {
505 int rw;
506 struct bio *bi;
NeilBrown3cb03002011-10-11 16:45:26 +1100507 struct md_rdev *rdev;
Tejun Heoe9c74692010-09-03 11:56:18 +0200508 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
509 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
510 rw = WRITE_FUA;
511 else
512 rw = WRITE;
513 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700514 rw = READ;
515 else
516 continue;
517
518 bi = &sh->dev[i].req;
519
520 bi->bi_rw = rw;
Namhyung Kimb0629622011-06-14 14:20:19 +1000521 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700522 bi->bi_end_io = raid5_end_write_request;
523 else
524 bi->bi_end_io = raid5_end_read_request;
525
526 rcu_read_lock();
NeilBrown14a75d32011-12-23 10:17:52 +1100527 if (rw == READ &&
528 test_bit(R5_ReadRepl, &sh->dev[i].flags))
529 rdev = rcu_dereference(conf->disks[i].replacement);
530 else
531 rdev = rcu_dereference(conf->disks[i].rdev);
Dan Williams91c00922007-01-02 13:52:30 -0700532 if (rdev && test_bit(Faulty, &rdev->flags))
533 rdev = NULL;
534 if (rdev)
535 atomic_inc(&rdev->nr_pending);
536 rcu_read_unlock();
537
NeilBrown73e92e52011-07-28 11:39:22 +1000538 /* We have already checked bad blocks for reads. Now
539 * need to check for writes.
540 */
541 while ((rw & WRITE) && rdev &&
542 test_bit(WriteErrorSeen, &rdev->flags)) {
543 sector_t first_bad;
544 int bad_sectors;
545 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
546 &first_bad, &bad_sectors);
547 if (!bad)
548 break;
549
550 if (bad < 0) {
551 set_bit(BlockedBadBlocks, &rdev->flags);
552 if (!conf->mddev->external &&
553 conf->mddev->flags) {
554 /* It is very unlikely, but we might
555 * still need to write out the
556 * bad block log - better give it
557 * a chance*/
558 md_check_recovery(conf->mddev);
559 }
560 md_wait_for_blocked_rdev(rdev, conf->mddev);
561 } else {
562 /* Acknowledged bad block - skip the write */
563 rdev_dec_pending(rdev, conf->mddev);
564 rdev = NULL;
565 }
566 }
567
Dan Williams91c00922007-01-02 13:52:30 -0700568 if (rdev) {
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000569 if (s->syncing || s->expanding || s->expanded)
Dan Williams91c00922007-01-02 13:52:30 -0700570 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
571
Dan Williams2b7497f2008-06-28 08:31:52 +1000572 set_bit(STRIPE_IO_STARTED, &sh->state);
573
Dan Williams91c00922007-01-02 13:52:30 -0700574 bi->bi_bdev = rdev->bdev;
575 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700576 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700577 bi->bi_rw, i);
578 atomic_inc(&sh->count);
579 bi->bi_sector = sh->sector + rdev->data_offset;
580 bi->bi_flags = 1 << BIO_UPTODATE;
Dan Williams91c00922007-01-02 13:52:30 -0700581 bi->bi_idx = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700582 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
583 bi->bi_io_vec[0].bv_offset = 0;
584 bi->bi_size = STRIPE_SIZE;
585 bi->bi_next = NULL;
Dan Williams91c00922007-01-02 13:52:30 -0700586 generic_make_request(bi);
587 } else {
Namhyung Kimb0629622011-06-14 14:20:19 +1000588 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700589 set_bit(STRIPE_DEGRADED, &sh->state);
590 pr_debug("skip op %ld on disc %d for sector %llu\n",
591 bi->bi_rw, i, (unsigned long long)sh->sector);
592 clear_bit(R5_LOCKED, &sh->dev[i].flags);
593 set_bit(STRIPE_HANDLE, &sh->state);
594 }
595 }
596}
597
598static struct dma_async_tx_descriptor *
599async_copy_data(int frombio, struct bio *bio, struct page *page,
600 sector_t sector, struct dma_async_tx_descriptor *tx)
601{
602 struct bio_vec *bvl;
603 struct page *bio_page;
604 int i;
605 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700606 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700607 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700608
609 if (bio->bi_sector >= sector)
610 page_offset = (signed)(bio->bi_sector - sector) * 512;
611 else
612 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700613
Dan Williams0403e382009-09-08 17:42:50 -0700614 if (frombio)
615 flags |= ASYNC_TX_FENCE;
616 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
617
Dan Williams91c00922007-01-02 13:52:30 -0700618 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000619 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700620 int clen;
621 int b_offset = 0;
622
623 if (page_offset < 0) {
624 b_offset = -page_offset;
625 page_offset += b_offset;
626 len -= b_offset;
627 }
628
629 if (len > 0 && page_offset + len > STRIPE_SIZE)
630 clen = STRIPE_SIZE - page_offset;
631 else
632 clen = len;
633
634 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000635 b_offset += bvl->bv_offset;
636 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700637 if (frombio)
638 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700639 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700640 else
641 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700642 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700643 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700644 /* chain the operations */
645 submit.depend_tx = tx;
646
Dan Williams91c00922007-01-02 13:52:30 -0700647 if (clen < len) /* hit end of page */
648 break;
649 page_offset += len;
650 }
651
652 return tx;
653}
654
655static void ops_complete_biofill(void *stripe_head_ref)
656{
657 struct stripe_head *sh = stripe_head_ref;
658 struct bio *return_bi = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +1100659 struct r5conf *conf = sh->raid_conf;
Dan Williamse4d84902007-09-24 10:06:13 -0700660 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700661
Harvey Harrisone46b2722008-04-28 02:15:50 -0700662 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700663 (unsigned long long)sh->sector);
664
665 /* clear completed biofills */
Dan Williams83de75c2008-06-28 08:31:58 +1000666 spin_lock_irq(&conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700667 for (i = sh->disks; i--; ) {
668 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700669
670 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700671 /* and check if we need to reply to a read request,
672 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000673 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700674 */
675 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700676 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700677
Dan Williams91c00922007-01-02 13:52:30 -0700678 BUG_ON(!dev->read);
679 rbi = dev->read;
680 dev->read = NULL;
681 while (rbi && rbi->bi_sector <
682 dev->sector + STRIPE_SECTORS) {
683 rbi2 = r5_next_bio(rbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +0200684 if (!raid5_dec_bi_phys_segments(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700685 rbi->bi_next = return_bi;
686 return_bi = rbi;
687 }
Dan Williams91c00922007-01-02 13:52:30 -0700688 rbi = rbi2;
689 }
690 }
691 }
Dan Williams83de75c2008-06-28 08:31:58 +1000692 spin_unlock_irq(&conf->device_lock);
693 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700694
695 return_io(return_bi);
696
Dan Williamse4d84902007-09-24 10:06:13 -0700697 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700698 release_stripe(sh);
699}
700
701static void ops_run_biofill(struct stripe_head *sh)
702{
703 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +1100704 struct r5conf *conf = sh->raid_conf;
Dan Williamsa08abd82009-06-03 11:43:59 -0700705 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700706 int i;
707
Harvey Harrisone46b2722008-04-28 02:15:50 -0700708 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700709 (unsigned long long)sh->sector);
710
711 for (i = sh->disks; i--; ) {
712 struct r5dev *dev = &sh->dev[i];
713 if (test_bit(R5_Wantfill, &dev->flags)) {
714 struct bio *rbi;
715 spin_lock_irq(&conf->device_lock);
716 dev->read = rbi = dev->toread;
717 dev->toread = NULL;
718 spin_unlock_irq(&conf->device_lock);
719 while (rbi && rbi->bi_sector <
720 dev->sector + STRIPE_SECTORS) {
721 tx = async_copy_data(0, rbi, dev->page,
722 dev->sector, tx);
723 rbi = r5_next_bio(rbi, dev->sector);
724 }
725 }
726 }
727
728 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700729 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
730 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700731}
732
Dan Williams4e7d2c02009-08-29 19:13:11 -0700733static void mark_target_uptodate(struct stripe_head *sh, int target)
734{
735 struct r5dev *tgt;
736
737 if (target < 0)
738 return;
739
740 tgt = &sh->dev[target];
741 set_bit(R5_UPTODATE, &tgt->flags);
742 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
743 clear_bit(R5_Wantcompute, &tgt->flags);
744}
745
Dan Williamsac6b53b2009-07-14 13:40:19 -0700746static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700747{
748 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700749
Harvey Harrisone46b2722008-04-28 02:15:50 -0700750 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700751 (unsigned long long)sh->sector);
752
Dan Williamsac6b53b2009-07-14 13:40:19 -0700753 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700754 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700755 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700756
Dan Williamsecc65c92008-06-28 08:31:57 +1000757 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
758 if (sh->check_state == check_state_compute_run)
759 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700760 set_bit(STRIPE_HANDLE, &sh->state);
761 release_stripe(sh);
762}
763
Dan Williamsd6f38f32009-07-14 11:50:52 -0700764/* return a pointer to the address conversion region of the scribble buffer */
765static addr_conv_t *to_addr_conv(struct stripe_head *sh,
766 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700767{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700768 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
769}
770
771static struct dma_async_tx_descriptor *
772ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
773{
Dan Williams91c00922007-01-02 13:52:30 -0700774 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700775 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700776 int target = sh->ops.target;
777 struct r5dev *tgt = &sh->dev[target];
778 struct page *xor_dest = tgt->page;
779 int count = 0;
780 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700781 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700782 int i;
783
784 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700785 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700786 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
787
788 for (i = disks; i--; )
789 if (i != target)
790 xor_srcs[count++] = sh->dev[i].page;
791
792 atomic_inc(&sh->count);
793
Dan Williams0403e382009-09-08 17:42:50 -0700794 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700795 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700796 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700797 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700798 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700799 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700800
Dan Williams91c00922007-01-02 13:52:30 -0700801 return tx;
802}
803
Dan Williamsac6b53b2009-07-14 13:40:19 -0700804/* set_syndrome_sources - populate source buffers for gen_syndrome
805 * @srcs - (struct page *) array of size sh->disks
806 * @sh - stripe_head to parse
807 *
808 * Populates srcs in proper layout order for the stripe and returns the
809 * 'count' of sources to be used in a call to async_gen_syndrome. The P
810 * destination buffer is recorded in srcs[count] and the Q destination
811 * is recorded in srcs[count+1]].
812 */
813static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
814{
815 int disks = sh->disks;
816 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
817 int d0_idx = raid6_d0(sh);
818 int count;
819 int i;
820
821 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100822 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700823
824 count = 0;
825 i = d0_idx;
826 do {
827 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
828
829 srcs[slot] = sh->dev[i].page;
830 i = raid6_next_disk(i, disks);
831 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700832
NeilBrowne4424fe2009-10-16 16:27:34 +1100833 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700834}
835
836static struct dma_async_tx_descriptor *
837ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
838{
839 int disks = sh->disks;
840 struct page **blocks = percpu->scribble;
841 int target;
842 int qd_idx = sh->qd_idx;
843 struct dma_async_tx_descriptor *tx;
844 struct async_submit_ctl submit;
845 struct r5dev *tgt;
846 struct page *dest;
847 int i;
848 int count;
849
850 if (sh->ops.target < 0)
851 target = sh->ops.target2;
852 else if (sh->ops.target2 < 0)
853 target = sh->ops.target;
854 else
855 /* we should only have one valid target */
856 BUG();
857 BUG_ON(target < 0);
858 pr_debug("%s: stripe %llu block: %d\n",
859 __func__, (unsigned long long)sh->sector, target);
860
861 tgt = &sh->dev[target];
862 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
863 dest = tgt->page;
864
865 atomic_inc(&sh->count);
866
867 if (target == qd_idx) {
868 count = set_syndrome_sources(blocks, sh);
869 blocks[count] = NULL; /* regenerating p is not necessary */
870 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700871 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
872 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700873 to_addr_conv(sh, percpu));
874 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
875 } else {
876 /* Compute any data- or p-drive using XOR */
877 count = 0;
878 for (i = disks; i-- ; ) {
879 if (i == target || i == qd_idx)
880 continue;
881 blocks[count++] = sh->dev[i].page;
882 }
883
Dan Williams0403e382009-09-08 17:42:50 -0700884 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
885 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700886 to_addr_conv(sh, percpu));
887 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
888 }
889
890 return tx;
891}
892
893static struct dma_async_tx_descriptor *
894ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
895{
896 int i, count, disks = sh->disks;
897 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
898 int d0_idx = raid6_d0(sh);
899 int faila = -1, failb = -1;
900 int target = sh->ops.target;
901 int target2 = sh->ops.target2;
902 struct r5dev *tgt = &sh->dev[target];
903 struct r5dev *tgt2 = &sh->dev[target2];
904 struct dma_async_tx_descriptor *tx;
905 struct page **blocks = percpu->scribble;
906 struct async_submit_ctl submit;
907
908 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
909 __func__, (unsigned long long)sh->sector, target, target2);
910 BUG_ON(target < 0 || target2 < 0);
911 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
912 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
913
Dan Williams6c910a72009-09-16 12:24:54 -0700914 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -0700915 * slot number conversion for 'faila' and 'failb'
916 */
917 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100918 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700919 count = 0;
920 i = d0_idx;
921 do {
922 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
923
924 blocks[slot] = sh->dev[i].page;
925
926 if (i == target)
927 faila = slot;
928 if (i == target2)
929 failb = slot;
930 i = raid6_next_disk(i, disks);
931 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700932
933 BUG_ON(faila == failb);
934 if (failb < faila)
935 swap(faila, failb);
936 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
937 __func__, (unsigned long long)sh->sector, faila, failb);
938
939 atomic_inc(&sh->count);
940
941 if (failb == syndrome_disks+1) {
942 /* Q disk is one of the missing disks */
943 if (faila == syndrome_disks) {
944 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -0700945 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
946 ops_complete_compute, sh,
947 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +1100948 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700949 STRIPE_SIZE, &submit);
950 } else {
951 struct page *dest;
952 int data_target;
953 int qd_idx = sh->qd_idx;
954
955 /* Missing D+Q: recompute D from P, then recompute Q */
956 if (target == qd_idx)
957 data_target = target2;
958 else
959 data_target = target;
960
961 count = 0;
962 for (i = disks; i-- ; ) {
963 if (i == data_target || i == qd_idx)
964 continue;
965 blocks[count++] = sh->dev[i].page;
966 }
967 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -0700968 init_async_submit(&submit,
969 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
970 NULL, NULL, NULL,
971 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -0700972 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
973 &submit);
974
975 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -0700976 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
977 ops_complete_compute, sh,
978 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -0700979 return async_gen_syndrome(blocks, 0, count+2,
980 STRIPE_SIZE, &submit);
981 }
Dan Williamsac6b53b2009-07-14 13:40:19 -0700982 } else {
Dan Williams6c910a72009-09-16 12:24:54 -0700983 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
984 ops_complete_compute, sh,
985 to_addr_conv(sh, percpu));
986 if (failb == syndrome_disks) {
987 /* We're missing D+P. */
988 return async_raid6_datap_recov(syndrome_disks+2,
989 STRIPE_SIZE, faila,
990 blocks, &submit);
991 } else {
992 /* We're missing D+D. */
993 return async_raid6_2data_recov(syndrome_disks+2,
994 STRIPE_SIZE, faila, failb,
995 blocks, &submit);
996 }
Dan Williamsac6b53b2009-07-14 13:40:19 -0700997 }
998}
999
1000
Dan Williams91c00922007-01-02 13:52:30 -07001001static void ops_complete_prexor(void *stripe_head_ref)
1002{
1003 struct stripe_head *sh = stripe_head_ref;
1004
Harvey Harrisone46b2722008-04-28 02:15:50 -07001005 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001006 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001007}
1008
1009static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001010ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1011 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001012{
Dan Williams91c00922007-01-02 13:52:30 -07001013 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001014 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001015 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001016 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001017
1018 /* existing parity data subtracted */
1019 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1020
Harvey Harrisone46b2722008-04-28 02:15:50 -07001021 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001022 (unsigned long long)sh->sector);
1023
1024 for (i = disks; i--; ) {
1025 struct r5dev *dev = &sh->dev[i];
1026 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001027 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001028 xor_srcs[count++] = dev->page;
1029 }
1030
Dan Williams0403e382009-09-08 17:42:50 -07001031 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001032 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001033 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001034
1035 return tx;
1036}
1037
1038static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001039ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001040{
1041 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001042 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001043
Harvey Harrisone46b2722008-04-28 02:15:50 -07001044 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001045 (unsigned long long)sh->sector);
1046
1047 for (i = disks; i--; ) {
1048 struct r5dev *dev = &sh->dev[i];
1049 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001050
Dan Williamsd8ee0722008-06-28 08:32:06 +10001051 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001052 struct bio *wbi;
1053
NeilBrowncbe47ec2011-07-26 11:20:35 +10001054 spin_lock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001055 chosen = dev->towrite;
1056 dev->towrite = NULL;
1057 BUG_ON(dev->written);
1058 wbi = dev->written = chosen;
NeilBrowncbe47ec2011-07-26 11:20:35 +10001059 spin_unlock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001060
1061 while (wbi && wbi->bi_sector <
1062 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001063 if (wbi->bi_rw & REQ_FUA)
1064 set_bit(R5_WantFUA, &dev->flags);
Dan Williams91c00922007-01-02 13:52:30 -07001065 tx = async_copy_data(1, wbi, dev->page,
1066 dev->sector, tx);
1067 wbi = r5_next_bio(wbi, dev->sector);
1068 }
1069 }
1070 }
1071
1072 return tx;
1073}
1074
Dan Williamsac6b53b2009-07-14 13:40:19 -07001075static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001076{
1077 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001078 int disks = sh->disks;
1079 int pd_idx = sh->pd_idx;
1080 int qd_idx = sh->qd_idx;
1081 int i;
Tejun Heoe9c74692010-09-03 11:56:18 +02001082 bool fua = false;
Dan Williams91c00922007-01-02 13:52:30 -07001083
Harvey Harrisone46b2722008-04-28 02:15:50 -07001084 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001085 (unsigned long long)sh->sector);
1086
Tejun Heoe9c74692010-09-03 11:56:18 +02001087 for (i = disks; i--; )
1088 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
1089
Dan Williams91c00922007-01-02 13:52:30 -07001090 for (i = disks; i--; ) {
1091 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001092
Tejun Heoe9c74692010-09-03 11:56:18 +02001093 if (dev->written || i == pd_idx || i == qd_idx) {
Dan Williams91c00922007-01-02 13:52:30 -07001094 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001095 if (fua)
1096 set_bit(R5_WantFUA, &dev->flags);
1097 }
Dan Williams91c00922007-01-02 13:52:30 -07001098 }
1099
Dan Williamsd8ee0722008-06-28 08:32:06 +10001100 if (sh->reconstruct_state == reconstruct_state_drain_run)
1101 sh->reconstruct_state = reconstruct_state_drain_result;
1102 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1103 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1104 else {
1105 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1106 sh->reconstruct_state = reconstruct_state_result;
1107 }
Dan Williams91c00922007-01-02 13:52:30 -07001108
1109 set_bit(STRIPE_HANDLE, &sh->state);
1110 release_stripe(sh);
1111}
1112
1113static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001114ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1115 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001116{
Dan Williams91c00922007-01-02 13:52:30 -07001117 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001118 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001119 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001120 int count = 0, pd_idx = sh->pd_idx, i;
1121 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001122 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001123 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001124
Harvey Harrisone46b2722008-04-28 02:15:50 -07001125 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001126 (unsigned long long)sh->sector);
1127
1128 /* check if prexor is active which means only process blocks
1129 * that are part of a read-modify-write (written)
1130 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001131 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1132 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001133 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1134 for (i = disks; i--; ) {
1135 struct r5dev *dev = &sh->dev[i];
1136 if (dev->written)
1137 xor_srcs[count++] = dev->page;
1138 }
1139 } else {
1140 xor_dest = sh->dev[pd_idx].page;
1141 for (i = disks; i--; ) {
1142 struct r5dev *dev = &sh->dev[i];
1143 if (i != pd_idx)
1144 xor_srcs[count++] = dev->page;
1145 }
1146 }
1147
Dan Williams91c00922007-01-02 13:52:30 -07001148 /* 1/ if we prexor'd then the dest is reused as a source
1149 * 2/ if we did not prexor then we are redoing the parity
1150 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1151 * for the synchronous xor case
1152 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001153 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001154 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1155
1156 atomic_inc(&sh->count);
1157
Dan Williamsac6b53b2009-07-14 13:40:19 -07001158 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001159 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001160 if (unlikely(count == 1))
1161 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1162 else
1163 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001164}
1165
Dan Williamsac6b53b2009-07-14 13:40:19 -07001166static void
1167ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1168 struct dma_async_tx_descriptor *tx)
1169{
1170 struct async_submit_ctl submit;
1171 struct page **blocks = percpu->scribble;
1172 int count;
1173
1174 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1175
1176 count = set_syndrome_sources(blocks, sh);
1177
1178 atomic_inc(&sh->count);
1179
1180 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1181 sh, to_addr_conv(sh, percpu));
1182 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001183}
1184
1185static void ops_complete_check(void *stripe_head_ref)
1186{
1187 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001188
Harvey Harrisone46b2722008-04-28 02:15:50 -07001189 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001190 (unsigned long long)sh->sector);
1191
Dan Williamsecc65c92008-06-28 08:31:57 +10001192 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001193 set_bit(STRIPE_HANDLE, &sh->state);
1194 release_stripe(sh);
1195}
1196
Dan Williamsac6b53b2009-07-14 13:40:19 -07001197static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001198{
Dan Williams91c00922007-01-02 13:52:30 -07001199 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001200 int pd_idx = sh->pd_idx;
1201 int qd_idx = sh->qd_idx;
1202 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001203 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001204 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001205 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001206 int count;
1207 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001208
Harvey Harrisone46b2722008-04-28 02:15:50 -07001209 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001210 (unsigned long long)sh->sector);
1211
Dan Williamsac6b53b2009-07-14 13:40:19 -07001212 count = 0;
1213 xor_dest = sh->dev[pd_idx].page;
1214 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001215 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001216 if (i == pd_idx || i == qd_idx)
1217 continue;
1218 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001219 }
1220
Dan Williamsd6f38f32009-07-14 11:50:52 -07001221 init_async_submit(&submit, 0, NULL, NULL, NULL,
1222 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001223 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001224 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001225
Dan Williams91c00922007-01-02 13:52:30 -07001226 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001227 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1228 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001229}
1230
Dan Williamsac6b53b2009-07-14 13:40:19 -07001231static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1232{
1233 struct page **srcs = percpu->scribble;
1234 struct async_submit_ctl submit;
1235 int count;
1236
1237 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1238 (unsigned long long)sh->sector, checkp);
1239
1240 count = set_syndrome_sources(srcs, sh);
1241 if (!checkp)
1242 srcs[count] = NULL;
1243
1244 atomic_inc(&sh->count);
1245 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1246 sh, to_addr_conv(sh, percpu));
1247 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1248 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1249}
1250
Dan Williams417b8d42009-10-16 16:25:22 +11001251static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001252{
1253 int overlap_clear = 0, i, disks = sh->disks;
1254 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001255 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001256 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001257 struct raid5_percpu *percpu;
1258 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001259
Dan Williamsd6f38f32009-07-14 11:50:52 -07001260 cpu = get_cpu();
1261 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001262 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001263 ops_run_biofill(sh);
1264 overlap_clear++;
1265 }
1266
Dan Williams7b3a8712008-06-28 08:32:09 +10001267 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001268 if (level < 6)
1269 tx = ops_run_compute5(sh, percpu);
1270 else {
1271 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1272 tx = ops_run_compute6_1(sh, percpu);
1273 else
1274 tx = ops_run_compute6_2(sh, percpu);
1275 }
1276 /* terminate the chain if reconstruct is not set to be run */
1277 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001278 async_tx_ack(tx);
1279 }
Dan Williams91c00922007-01-02 13:52:30 -07001280
Dan Williams600aa102008-06-28 08:32:05 +10001281 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001282 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001283
Dan Williams600aa102008-06-28 08:32:05 +10001284 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001285 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001286 overlap_clear++;
1287 }
1288
Dan Williamsac6b53b2009-07-14 13:40:19 -07001289 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1290 if (level < 6)
1291 ops_run_reconstruct5(sh, percpu, tx);
1292 else
1293 ops_run_reconstruct6(sh, percpu, tx);
1294 }
Dan Williams91c00922007-01-02 13:52:30 -07001295
Dan Williamsac6b53b2009-07-14 13:40:19 -07001296 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1297 if (sh->check_state == check_state_run)
1298 ops_run_check_p(sh, percpu);
1299 else if (sh->check_state == check_state_run_q)
1300 ops_run_check_pq(sh, percpu, 0);
1301 else if (sh->check_state == check_state_run_pq)
1302 ops_run_check_pq(sh, percpu, 1);
1303 else
1304 BUG();
1305 }
Dan Williams91c00922007-01-02 13:52:30 -07001306
Dan Williams91c00922007-01-02 13:52:30 -07001307 if (overlap_clear)
1308 for (i = disks; i--; ) {
1309 struct r5dev *dev = &sh->dev[i];
1310 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1311 wake_up(&sh->raid_conf->wait_for_overlap);
1312 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001313 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001314}
1315
Dan Williams417b8d42009-10-16 16:25:22 +11001316#ifdef CONFIG_MULTICORE_RAID456
1317static void async_run_ops(void *param, async_cookie_t cookie)
1318{
1319 struct stripe_head *sh = param;
1320 unsigned long ops_request = sh->ops.request;
1321
1322 clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
1323 wake_up(&sh->ops.wait_for_ops);
1324
1325 __raid_run_ops(sh, ops_request);
1326 release_stripe(sh);
1327}
1328
1329static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1330{
1331 /* since handle_stripe can be called outside of raid5d context
1332 * we need to ensure sh->ops.request is de-staged before another
1333 * request arrives
1334 */
1335 wait_event(sh->ops.wait_for_ops,
1336 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
1337 sh->ops.request = ops_request;
1338
1339 atomic_inc(&sh->count);
1340 async_schedule(async_run_ops, sh);
1341}
1342#else
1343#define raid_run_ops __raid_run_ops
1344#endif
1345
NeilBrownd1688a62011-10-11 16:49:52 +11001346static int grow_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
1348 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001349 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001350 if (!sh)
1351 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001352
NeilBrown3f294f42005-11-08 21:39:25 -08001353 sh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001354 #ifdef CONFIG_MULTICORE_RAID456
1355 init_waitqueue_head(&sh->ops.wait_for_ops);
1356 #endif
NeilBrown3f294f42005-11-08 21:39:25 -08001357
NeilBrowne4e11e32010-06-16 16:45:16 +10001358 if (grow_buffers(sh)) {
1359 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001360 kmem_cache_free(conf->slab_cache, sh);
1361 return 0;
1362 }
1363 /* we just created an active stripe so... */
1364 atomic_set(&sh->count, 1);
1365 atomic_inc(&conf->active_stripes);
1366 INIT_LIST_HEAD(&sh->lru);
1367 release_stripe(sh);
1368 return 1;
1369}
1370
NeilBrownd1688a62011-10-11 16:49:52 +11001371static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001372{
Christoph Lametere18b8902006-12-06 20:33:20 -08001373 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001374 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
NeilBrownf4be6b42010-06-01 19:37:25 +10001376 if (conf->mddev->gendisk)
1377 sprintf(conf->cache_name[0],
1378 "raid%d-%s", conf->level, mdname(conf->mddev));
1379 else
1380 sprintf(conf->cache_name[0],
1381 "raid%d-%p", conf->level, conf->mddev);
1382 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1383
NeilBrownad01c9e2006-03-27 01:18:07 -08001384 conf->active_name = 0;
1385 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001387 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 if (!sc)
1389 return 1;
1390 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001391 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001392 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001393 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 return 0;
1396}
NeilBrown29269552006-03-27 01:18:10 -08001397
Dan Williamsd6f38f32009-07-14 11:50:52 -07001398/**
1399 * scribble_len - return the required size of the scribble region
1400 * @num - total number of disks in the array
1401 *
1402 * The size must be enough to contain:
1403 * 1/ a struct page pointer for each device in the array +2
1404 * 2/ room to convert each entry in (1) to its corresponding dma
1405 * (dma_map_page()) or page (page_address()) address.
1406 *
1407 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1408 * calculate over all devices (not just the data blocks), using zeros in place
1409 * of the P and Q blocks.
1410 */
1411static size_t scribble_len(int num)
1412{
1413 size_t len;
1414
1415 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1416
1417 return len;
1418}
1419
NeilBrownd1688a62011-10-11 16:49:52 +11001420static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001421{
1422 /* Make all the stripes able to hold 'newsize' devices.
1423 * New slots in each stripe get 'page' set to a new page.
1424 *
1425 * This happens in stages:
1426 * 1/ create a new kmem_cache and allocate the required number of
1427 * stripe_heads.
1428 * 2/ gather all the old stripe_heads and tranfer the pages across
1429 * to the new stripe_heads. This will have the side effect of
1430 * freezing the array as once all stripe_heads have been collected,
1431 * no IO will be possible. Old stripe heads are freed once their
1432 * pages have been transferred over, and the old kmem_cache is
1433 * freed when all stripes are done.
1434 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1435 * we simple return a failre status - no need to clean anything up.
1436 * 4/ allocate new pages for the new slots in the new stripe_heads.
1437 * If this fails, we don't bother trying the shrink the
1438 * stripe_heads down again, we just leave them as they are.
1439 * As each stripe_head is processed the new one is released into
1440 * active service.
1441 *
1442 * Once step2 is started, we cannot afford to wait for a write,
1443 * so we use GFP_NOIO allocations.
1444 */
1445 struct stripe_head *osh, *nsh;
1446 LIST_HEAD(newstripes);
1447 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001448 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001449 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001450 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001451 int i;
1452
1453 if (newsize <= conf->pool_size)
1454 return 0; /* never bother to shrink */
1455
Dan Williamsb5470dc2008-06-27 21:44:04 -07001456 err = md_allow_write(conf->mddev);
1457 if (err)
1458 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001459
NeilBrownad01c9e2006-03-27 01:18:07 -08001460 /* Step 1 */
1461 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1462 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001463 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001464 if (!sc)
1465 return -ENOMEM;
1466
1467 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001468 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001469 if (!nsh)
1470 break;
1471
NeilBrownad01c9e2006-03-27 01:18:07 -08001472 nsh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001473 #ifdef CONFIG_MULTICORE_RAID456
1474 init_waitqueue_head(&nsh->ops.wait_for_ops);
1475 #endif
NeilBrownad01c9e2006-03-27 01:18:07 -08001476
1477 list_add(&nsh->lru, &newstripes);
1478 }
1479 if (i) {
1480 /* didn't get enough, give up */
1481 while (!list_empty(&newstripes)) {
1482 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1483 list_del(&nsh->lru);
1484 kmem_cache_free(sc, nsh);
1485 }
1486 kmem_cache_destroy(sc);
1487 return -ENOMEM;
1488 }
1489 /* Step 2 - Must use GFP_NOIO now.
1490 * OK, we have enough stripes, start collecting inactive
1491 * stripes and copying them over
1492 */
1493 list_for_each_entry(nsh, &newstripes, lru) {
1494 spin_lock_irq(&conf->device_lock);
1495 wait_event_lock_irq(conf->wait_for_stripe,
1496 !list_empty(&conf->inactive_list),
1497 conf->device_lock,
NeilBrown482c0832011-04-18 18:25:42 +10001498 );
NeilBrownad01c9e2006-03-27 01:18:07 -08001499 osh = get_free_stripe(conf);
1500 spin_unlock_irq(&conf->device_lock);
1501 atomic_set(&nsh->count, 1);
1502 for(i=0; i<conf->pool_size; i++)
1503 nsh->dev[i].page = osh->dev[i].page;
1504 for( ; i<newsize; i++)
1505 nsh->dev[i].page = NULL;
1506 kmem_cache_free(conf->slab_cache, osh);
1507 }
1508 kmem_cache_destroy(conf->slab_cache);
1509
1510 /* Step 3.
1511 * At this point, we are holding all the stripes so the array
1512 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001513 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001514 */
1515 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1516 if (ndisks) {
1517 for (i=0; i<conf->raid_disks; i++)
1518 ndisks[i] = conf->disks[i];
1519 kfree(conf->disks);
1520 conf->disks = ndisks;
1521 } else
1522 err = -ENOMEM;
1523
Dan Williamsd6f38f32009-07-14 11:50:52 -07001524 get_online_cpus();
1525 conf->scribble_len = scribble_len(newsize);
1526 for_each_present_cpu(cpu) {
1527 struct raid5_percpu *percpu;
1528 void *scribble;
1529
1530 percpu = per_cpu_ptr(conf->percpu, cpu);
1531 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1532
1533 if (scribble) {
1534 kfree(percpu->scribble);
1535 percpu->scribble = scribble;
1536 } else {
1537 err = -ENOMEM;
1538 break;
1539 }
1540 }
1541 put_online_cpus();
1542
NeilBrownad01c9e2006-03-27 01:18:07 -08001543 /* Step 4, return new stripes to service */
1544 while(!list_empty(&newstripes)) {
1545 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1546 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001547
NeilBrownad01c9e2006-03-27 01:18:07 -08001548 for (i=conf->raid_disks; i < newsize; i++)
1549 if (nsh->dev[i].page == NULL) {
1550 struct page *p = alloc_page(GFP_NOIO);
1551 nsh->dev[i].page = p;
1552 if (!p)
1553 err = -ENOMEM;
1554 }
1555 release_stripe(nsh);
1556 }
1557 /* critical section pass, GFP_NOIO no longer needed */
1558
1559 conf->slab_cache = sc;
1560 conf->active_name = 1-conf->active_name;
1561 conf->pool_size = newsize;
1562 return err;
1563}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
NeilBrownd1688a62011-10-11 16:49:52 +11001565static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
1567 struct stripe_head *sh;
1568
NeilBrown3f294f42005-11-08 21:39:25 -08001569 spin_lock_irq(&conf->device_lock);
1570 sh = get_free_stripe(conf);
1571 spin_unlock_irq(&conf->device_lock);
1572 if (!sh)
1573 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001574 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001575 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001576 kmem_cache_free(conf->slab_cache, sh);
1577 atomic_dec(&conf->active_stripes);
1578 return 1;
1579}
1580
NeilBrownd1688a62011-10-11 16:49:52 +11001581static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001582{
1583 while (drop_one_stripe(conf))
1584 ;
1585
NeilBrown29fc7e32006-02-03 03:03:41 -08001586 if (conf->slab_cache)
1587 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 conf->slab_cache = NULL;
1589}
1590
NeilBrown6712ecf2007-09-27 12:47:43 +02001591static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
NeilBrown99c0fb52009-03-31 14:39:38 +11001593 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001594 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001595 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001597 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11001598 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601 for (i=0 ; i<disks; i++)
1602 if (bi == &sh->dev[i].req)
1603 break;
1604
Dan Williams45b42332007-07-09 11:56:43 -07001605 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1606 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 uptodate);
1608 if (i == disks) {
1609 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001610 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 }
NeilBrown14a75d32011-12-23 10:17:52 +11001612 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1613 rdev = conf->disks[i].replacement;
1614 else
1615 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
1617 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001619 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11001620 /* Note that this cannot happen on a
1621 * replacement device. We just fail those on
1622 * any error
1623 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001624 printk_ratelimited(
1625 KERN_INFO
1626 "md/raid:%s: read error corrected"
1627 " (%lu sectors at %llu on %s)\n",
1628 mdname(conf->mddev), STRIPE_SECTORS,
1629 (unsigned long long)(sh->sector
1630 + rdev->data_offset),
1631 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001632 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001633 clear_bit(R5_ReadError, &sh->dev[i].flags);
1634 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1635 }
NeilBrown14a75d32011-12-23 10:17:52 +11001636 if (atomic_read(&rdev->read_errors))
1637 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11001639 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001640 int retry = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001643 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11001644 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1645 printk_ratelimited(
1646 KERN_WARNING
1647 "md/raid:%s: read error on replacement device "
1648 "(sector %llu on %s).\n",
1649 mdname(conf->mddev),
1650 (unsigned long long)(sh->sector
1651 + rdev->data_offset),
1652 bdn);
1653 else if (conf->mddev->degraded >= conf->max_degraded)
Christian Dietrich8bda4702011-07-27 11:00:36 +10001654 printk_ratelimited(
1655 KERN_WARNING
1656 "md/raid:%s: read error not correctable "
1657 "(sector %llu on %s).\n",
1658 mdname(conf->mddev),
1659 (unsigned long long)(sh->sector
1660 + rdev->data_offset),
1661 bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001662 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
NeilBrown4e5314b2005-11-08 21:39:22 -08001663 /* Oh, no!!! */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001664 printk_ratelimited(
1665 KERN_WARNING
1666 "md/raid:%s: read error NOT corrected!! "
1667 "(sector %llu on %s).\n",
1668 mdname(conf->mddev),
1669 (unsigned long long)(sh->sector
1670 + rdev->data_offset),
1671 bdn);
NeilBrownd6950432006-07-10 04:44:20 -07001672 else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001673 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001674 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001675 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001676 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001677 else
1678 retry = 1;
1679 if (retry)
1680 set_bit(R5_ReadError, &sh->dev[i].flags);
1681 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001682 clear_bit(R5_ReadError, &sh->dev[i].flags);
1683 clear_bit(R5_ReWrite, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001684 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 }
NeilBrown14a75d32011-12-23 10:17:52 +11001687 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1689 set_bit(STRIPE_HANDLE, &sh->state);
1690 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691}
1692
NeilBrownd710e132008-10-13 11:55:12 +11001693static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
NeilBrown99c0fb52009-03-31 14:39:38 +11001695 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001696 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001697 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001699 sector_t first_bad;
1700 int bad_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 for (i=0 ; i<disks; i++)
1703 if (bi == &sh->dev[i].req)
1704 break;
1705
Dan Williams45b42332007-07-09 11:56:43 -07001706 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1708 uptodate);
1709 if (i == disks) {
1710 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001711 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 }
1713
NeilBrownbc2607f2011-07-28 11:39:22 +10001714 if (!uptodate) {
1715 set_bit(WriteErrorSeen, &conf->disks[i].rdev->flags);
1716 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrownb84db562011-07-28 11:39:23 +10001717 } else if (is_badblock(conf->disks[i].rdev, sh->sector, STRIPE_SECTORS,
1718 &first_bad, &bad_sectors))
1719 set_bit(R5_MadeGood, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
1721 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
1722
1723 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1724 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001725 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726}
1727
1728
NeilBrown784052e2009-03-31 15:19:07 +11001729static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
NeilBrown784052e2009-03-31 15:19:07 +11001731static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732{
1733 struct r5dev *dev = &sh->dev[i];
1734
1735 bio_init(&dev->req);
1736 dev->req.bi_io_vec = &dev->vec;
1737 dev->req.bi_vcnt++;
1738 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11001740 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
1742 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001743 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744}
1745
NeilBrownfd01b882011-10-11 16:47:53 +11001746static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747{
1748 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11001749 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11001750 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10001751 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
NeilBrown908f4fb2011-12-23 10:17:50 +11001753 spin_lock_irqsave(&conf->device_lock, flags);
1754 clear_bit(In_sync, &rdev->flags);
1755 mddev->degraded = calc_degraded(conf);
1756 spin_unlock_irqrestore(&conf->device_lock, flags);
1757 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1758
NeilBrownde393cd2011-07-28 11:31:48 +10001759 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001760 set_bit(Faulty, &rdev->flags);
1761 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1762 printk(KERN_ALERT
1763 "md/raid:%s: Disk failure on %s, disabling device.\n"
1764 "md/raid:%s: Operation continuing on %d devices.\n",
1765 mdname(mddev),
1766 bdevname(rdev->bdev, b),
1767 mdname(mddev),
1768 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001769}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
1771/*
1772 * Input: a 'big' sector number,
1773 * Output: index of the data and parity disk, and the sector # in them.
1774 */
NeilBrownd1688a62011-10-11 16:49:52 +11001775static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001776 int previous, int *dd_idx,
1777 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001779 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001780 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001782 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001783 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001785 int algorithm = previous ? conf->prev_algo
1786 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001787 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1788 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001789 int raid_disks = previous ? conf->previous_raid_disks
1790 : conf->raid_disks;
1791 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
1793 /* First compute the information on this sector */
1794
1795 /*
1796 * Compute the chunk number and the sector offset inside the chunk
1797 */
1798 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1799 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
1801 /*
1802 * Compute the stripe number
1803 */
NeilBrown35f2a592010-04-20 14:13:34 +10001804 stripe = chunk_number;
1805 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10001806 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 /*
1808 * Select the parity disk based on the user selected algorithm.
1809 */
NeilBrown84789552011-07-27 11:00:36 +10001810 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07001811 switch(conf->level) {
1812 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001813 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001814 break;
1815 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001816 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001818 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001819 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 (*dd_idx)++;
1821 break;
1822 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001823 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001824 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 (*dd_idx)++;
1826 break;
1827 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001828 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001829 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 break;
1831 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001832 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001833 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001835 case ALGORITHM_PARITY_0:
1836 pd_idx = 0;
1837 (*dd_idx)++;
1838 break;
1839 case ALGORITHM_PARITY_N:
1840 pd_idx = data_disks;
1841 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11001843 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001844 }
1845 break;
1846 case 6:
1847
NeilBrowne183eae2009-03-31 15:20:22 +11001848 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001849 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001850 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001851 qd_idx = pd_idx + 1;
1852 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001853 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001854 qd_idx = 0;
1855 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001856 (*dd_idx) += 2; /* D D P Q D */
1857 break;
1858 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001859 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001860 qd_idx = pd_idx + 1;
1861 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001862 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001863 qd_idx = 0;
1864 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001865 (*dd_idx) += 2; /* D D P Q D */
1866 break;
1867 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001868 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001869 qd_idx = (pd_idx + 1) % raid_disks;
1870 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001871 break;
1872 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001873 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001874 qd_idx = (pd_idx + 1) % raid_disks;
1875 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001876 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001877
1878 case ALGORITHM_PARITY_0:
1879 pd_idx = 0;
1880 qd_idx = 1;
1881 (*dd_idx) += 2;
1882 break;
1883 case ALGORITHM_PARITY_N:
1884 pd_idx = data_disks;
1885 qd_idx = data_disks + 1;
1886 break;
1887
1888 case ALGORITHM_ROTATING_ZERO_RESTART:
1889 /* Exactly the same as RIGHT_ASYMMETRIC, but or
1890 * of blocks for computing Q is different.
1891 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001892 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11001893 qd_idx = pd_idx + 1;
1894 if (pd_idx == raid_disks-1) {
1895 (*dd_idx)++; /* Q D D D P */
1896 qd_idx = 0;
1897 } else if (*dd_idx >= pd_idx)
1898 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001899 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001900 break;
1901
1902 case ALGORITHM_ROTATING_N_RESTART:
1903 /* Same a left_asymmetric, by first stripe is
1904 * D D D P Q rather than
1905 * Q D D D P
1906 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001907 stripe2 += 1;
1908 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11001909 qd_idx = pd_idx + 1;
1910 if (pd_idx == raid_disks-1) {
1911 (*dd_idx)++; /* Q D D D P */
1912 qd_idx = 0;
1913 } else if (*dd_idx >= pd_idx)
1914 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001915 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001916 break;
1917
1918 case ALGORITHM_ROTATING_N_CONTINUE:
1919 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001920 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11001921 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
1922 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11001923 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001924 break;
1925
1926 case ALGORITHM_LEFT_ASYMMETRIC_6:
1927 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001928 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11001929 if (*dd_idx >= pd_idx)
1930 (*dd_idx)++;
1931 qd_idx = raid_disks - 1;
1932 break;
1933
1934 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001935 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11001936 if (*dd_idx >= pd_idx)
1937 (*dd_idx)++;
1938 qd_idx = raid_disks - 1;
1939 break;
1940
1941 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001942 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11001943 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1944 qd_idx = raid_disks - 1;
1945 break;
1946
1947 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001948 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11001949 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1950 qd_idx = raid_disks - 1;
1951 break;
1952
1953 case ALGORITHM_PARITY_0_6:
1954 pd_idx = 0;
1955 (*dd_idx)++;
1956 qd_idx = raid_disks - 1;
1957 break;
1958
NeilBrown16a53ec2006-06-26 00:27:38 -07001959 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11001960 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001961 }
1962 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 }
1964
NeilBrown911d4ee2009-03-31 14:39:38 +11001965 if (sh) {
1966 sh->pd_idx = pd_idx;
1967 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001968 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11001969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 /*
1971 * Finally, compute the new sector number
1972 */
1973 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
1974 return new_sector;
1975}
1976
1977
NeilBrown784052e2009-03-31 15:19:07 +11001978static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
NeilBrownd1688a62011-10-11 16:49:52 +11001980 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08001981 int raid_disks = sh->disks;
1982 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001984 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1985 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11001986 int algorithm = previous ? conf->prev_algo
1987 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 sector_t stripe;
1989 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10001990 sector_t chunk_number;
1991 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11001993 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
NeilBrown16a53ec2006-06-26 00:27:38 -07001995
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 chunk_offset = sector_div(new_sector, sectors_per_chunk);
1997 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
NeilBrown16a53ec2006-06-26 00:27:38 -07001999 if (i == sh->pd_idx)
2000 return 0;
2001 switch(conf->level) {
2002 case 4: break;
2003 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002004 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 case ALGORITHM_LEFT_ASYMMETRIC:
2006 case ALGORITHM_RIGHT_ASYMMETRIC:
2007 if (i > sh->pd_idx)
2008 i--;
2009 break;
2010 case ALGORITHM_LEFT_SYMMETRIC:
2011 case ALGORITHM_RIGHT_SYMMETRIC:
2012 if (i < sh->pd_idx)
2013 i += raid_disks;
2014 i -= (sh->pd_idx + 1);
2015 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002016 case ALGORITHM_PARITY_0:
2017 i -= 1;
2018 break;
2019 case ALGORITHM_PARITY_N:
2020 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002022 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002023 }
2024 break;
2025 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002026 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002027 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002028 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002029 case ALGORITHM_LEFT_ASYMMETRIC:
2030 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002031 case ALGORITHM_ROTATING_ZERO_RESTART:
2032 case ALGORITHM_ROTATING_N_RESTART:
2033 if (sh->pd_idx == raid_disks-1)
2034 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002035 else if (i > sh->pd_idx)
2036 i -= 2; /* D D P Q D */
2037 break;
2038 case ALGORITHM_LEFT_SYMMETRIC:
2039 case ALGORITHM_RIGHT_SYMMETRIC:
2040 if (sh->pd_idx == raid_disks-1)
2041 i--; /* Q D D D P */
2042 else {
2043 /* D D P Q D */
2044 if (i < sh->pd_idx)
2045 i += raid_disks;
2046 i -= (sh->pd_idx + 2);
2047 }
2048 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002049 case ALGORITHM_PARITY_0:
2050 i -= 2;
2051 break;
2052 case ALGORITHM_PARITY_N:
2053 break;
2054 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002055 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002056 if (sh->pd_idx == 0)
2057 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002058 else {
2059 /* D D Q P D */
2060 if (i < sh->pd_idx)
2061 i += raid_disks;
2062 i -= (sh->pd_idx + 1);
2063 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002064 break;
2065 case ALGORITHM_LEFT_ASYMMETRIC_6:
2066 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2067 if (i > sh->pd_idx)
2068 i--;
2069 break;
2070 case ALGORITHM_LEFT_SYMMETRIC_6:
2071 case ALGORITHM_RIGHT_SYMMETRIC_6:
2072 if (i < sh->pd_idx)
2073 i += data_disks + 1;
2074 i -= (sh->pd_idx + 1);
2075 break;
2076 case ALGORITHM_PARITY_0_6:
2077 i -= 1;
2078 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002079 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002080 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002081 }
2082 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 }
2084
2085 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002086 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
NeilBrown112bf892009-03-31 14:39:38 +11002088 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002089 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002090 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2091 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002092 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2093 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 return 0;
2095 }
2096 return r_sector;
2097}
2098
2099
Dan Williams600aa102008-06-28 08:32:05 +10002100static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002101schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002102 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002103{
2104 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002105 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002106 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002107
Dan Williamse33129d2007-01-02 13:52:30 -07002108 if (rcw) {
2109 /* if we are not expanding this is a proper write request, and
2110 * there will be bios with new data to be drained into the
2111 * stripe cache
2112 */
2113 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10002114 sh->reconstruct_state = reconstruct_state_drain_run;
2115 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2116 } else
2117 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07002118
Dan Williamsac6b53b2009-07-14 13:40:19 -07002119 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002120
2121 for (i = disks; i--; ) {
2122 struct r5dev *dev = &sh->dev[i];
2123
2124 if (dev->towrite) {
2125 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002126 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002127 if (!expand)
2128 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002129 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002130 }
2131 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002132 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002133 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002134 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002135 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002136 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002137 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2138 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2139
Dan Williamsd8ee0722008-06-28 08:32:06 +10002140 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002141 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2142 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002143 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002144
2145 for (i = disks; i--; ) {
2146 struct r5dev *dev = &sh->dev[i];
2147 if (i == pd_idx)
2148 continue;
2149
Dan Williamse33129d2007-01-02 13:52:30 -07002150 if (dev->towrite &&
2151 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002152 test_bit(R5_Wantcompute, &dev->flags))) {
2153 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002154 set_bit(R5_LOCKED, &dev->flags);
2155 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002156 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002157 }
2158 }
2159 }
2160
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002161 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002162 * are in flight
2163 */
2164 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2165 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002166 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002167
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002168 if (level == 6) {
2169 int qd_idx = sh->qd_idx;
2170 struct r5dev *dev = &sh->dev[qd_idx];
2171
2172 set_bit(R5_LOCKED, &dev->flags);
2173 clear_bit(R5_UPTODATE, &dev->flags);
2174 s->locked++;
2175 }
2176
Dan Williams600aa102008-06-28 08:32:05 +10002177 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002178 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002179 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002180}
NeilBrown16a53ec2006-06-26 00:27:38 -07002181
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182/*
2183 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002184 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 * The bi_next chain must be in order.
2186 */
2187static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2188{
2189 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002190 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002191 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
NeilBrowncbe47ec2011-07-26 11:20:35 +10002193 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 (unsigned long long)bi->bi_sector,
2195 (unsigned long long)sh->sector);
2196
2197
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07002199 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -07002201 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
2202 firstwrite = 1;
2203 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 bip = &sh->dev[dd_idx].toread;
2205 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2206 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2207 goto overlap;
2208 bip = & (*bip)->bi_next;
2209 }
2210 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2211 goto overlap;
2212
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002213 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 if (*bip)
2215 bi->bi_next = *bip;
2216 *bip = bi;
Jens Axboe960e7392008-08-15 10:41:18 +02002217 bi->bi_phys_segments++;
NeilBrown72626682005-09-09 16:23:54 -07002218
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 if (forwrite) {
2220 /* check if page is covered */
2221 sector_t sector = sh->dev[dd_idx].sector;
2222 for (bi=sh->dev[dd_idx].towrite;
2223 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2224 bi && bi->bi_sector <= sector;
2225 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2226 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2227 sector = bi->bi_sector + (bi->bi_size>>9);
2228 }
2229 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2230 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2231 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002232 spin_unlock_irq(&conf->device_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002233
2234 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2235 (unsigned long long)(*bip)->bi_sector,
2236 (unsigned long long)sh->sector, dd_idx);
2237
2238 if (conf->mddev->bitmap && firstwrite) {
2239 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2240 STRIPE_SECTORS, 0);
2241 sh->bm_seq = conf->seq_flush+1;
2242 set_bit(STRIPE_BIT_DELAY, &sh->state);
2243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 return 1;
2245
2246 overlap:
2247 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
2248 spin_unlock_irq(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 return 0;
2250}
2251
NeilBrownd1688a62011-10-11 16:49:52 +11002252static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002253
NeilBrownd1688a62011-10-11 16:49:52 +11002254static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002255 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002256{
NeilBrown784052e2009-03-31 15:19:07 +11002257 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002258 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002259 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002260 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002261 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002262
NeilBrown112bf892009-03-31 14:39:38 +11002263 raid5_compute_sector(conf,
2264 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002265 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002266 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002267 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002268}
2269
Dan Williamsa4456852007-07-09 11:56:43 -07002270static void
NeilBrownd1688a62011-10-11 16:49:52 +11002271handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002272 struct stripe_head_state *s, int disks,
2273 struct bio **return_bi)
2274{
2275 int i;
2276 for (i = disks; i--; ) {
2277 struct bio *bi;
2278 int bitmap_end = 0;
2279
2280 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002281 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002282 rcu_read_lock();
2283 rdev = rcu_dereference(conf->disks[i].rdev);
2284 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002285 atomic_inc(&rdev->nr_pending);
2286 else
2287 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002288 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002289 if (rdev) {
2290 if (!rdev_set_badblocks(
2291 rdev,
2292 sh->sector,
2293 STRIPE_SECTORS, 0))
2294 md_error(conf->mddev, rdev);
2295 rdev_dec_pending(rdev, conf->mddev);
2296 }
Dan Williamsa4456852007-07-09 11:56:43 -07002297 }
2298 spin_lock_irq(&conf->device_lock);
2299 /* fail all writes first */
2300 bi = sh->dev[i].towrite;
2301 sh->dev[i].towrite = NULL;
2302 if (bi) {
2303 s->to_write--;
2304 bitmap_end = 1;
2305 }
2306
2307 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2308 wake_up(&conf->wait_for_overlap);
2309
2310 while (bi && bi->bi_sector <
2311 sh->dev[i].sector + STRIPE_SECTORS) {
2312 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2313 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002314 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002315 md_write_end(conf->mddev);
2316 bi->bi_next = *return_bi;
2317 *return_bi = bi;
2318 }
2319 bi = nextbi;
2320 }
2321 /* and fail all 'written' */
2322 bi = sh->dev[i].written;
2323 sh->dev[i].written = NULL;
2324 if (bi) bitmap_end = 1;
2325 while (bi && bi->bi_sector <
2326 sh->dev[i].sector + STRIPE_SECTORS) {
2327 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2328 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002329 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002330 md_write_end(conf->mddev);
2331 bi->bi_next = *return_bi;
2332 *return_bi = bi;
2333 }
2334 bi = bi2;
2335 }
2336
Dan Williamsb5e98d62007-01-02 13:52:31 -07002337 /* fail any reads if this device is non-operational and
2338 * the data has not reached the cache yet.
2339 */
2340 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2341 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2342 test_bit(R5_ReadError, &sh->dev[i].flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002343 bi = sh->dev[i].toread;
2344 sh->dev[i].toread = NULL;
2345 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2346 wake_up(&conf->wait_for_overlap);
2347 if (bi) s->to_read--;
2348 while (bi && bi->bi_sector <
2349 sh->dev[i].sector + STRIPE_SECTORS) {
2350 struct bio *nextbi =
2351 r5_next_bio(bi, sh->dev[i].sector);
2352 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002353 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002354 bi->bi_next = *return_bi;
2355 *return_bi = bi;
2356 }
2357 bi = nextbi;
2358 }
2359 }
2360 spin_unlock_irq(&conf->device_lock);
2361 if (bitmap_end)
2362 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2363 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002364 /* If we were in the middle of a write the parity block might
2365 * still be locked - so just clear all R5_LOCKED flags
2366 */
2367 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002368 }
2369
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002370 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2371 if (atomic_dec_and_test(&conf->pending_full_writes))
2372 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002373}
2374
NeilBrown7f0da592011-07-28 11:39:22 +10002375static void
NeilBrownd1688a62011-10-11 16:49:52 +11002376handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002377 struct stripe_head_state *s)
2378{
2379 int abort = 0;
2380 int i;
2381
2382 md_done_sync(conf->mddev, STRIPE_SECTORS, 0);
2383 clear_bit(STRIPE_SYNCING, &sh->state);
2384 s->syncing = 0;
2385 /* There is nothing more to do for sync/check/repair.
2386 * For recover we need to record a bad block on all
2387 * non-sync devices, or abort the recovery
2388 */
2389 if (!test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery))
2390 return;
2391 /* During recovery devices cannot be removed, so locking and
2392 * refcounting of rdevs is not needed
2393 */
2394 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +11002395 struct md_rdev *rdev = conf->disks[i].rdev;
NeilBrown7f0da592011-07-28 11:39:22 +10002396 if (!rdev
2397 || test_bit(Faulty, &rdev->flags)
2398 || test_bit(In_sync, &rdev->flags))
2399 continue;
2400 if (!rdev_set_badblocks(rdev, sh->sector,
2401 STRIPE_SECTORS, 0))
2402 abort = 1;
2403 }
2404 if (abort) {
2405 conf->recovery_disabled = conf->mddev->recovery_disabled;
2406 set_bit(MD_RECOVERY_INTR, &conf->mddev->recovery);
2407 }
2408}
2409
NeilBrown93b3dbc2011-07-27 11:00:36 +10002410/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002411 * to be read or computed to satisfy a request.
2412 *
2413 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002414 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002415 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002416static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2417 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002418{
2419 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002420 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2421 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002422
Dan Williamsf38e1212007-01-02 13:52:30 -07002423 /* is the data in this block needed, and can we get it? */
2424 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002425 !test_bit(R5_UPTODATE, &dev->flags) &&
2426 (dev->toread ||
2427 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2428 s->syncing || s->expanding ||
NeilBrown5d35e092011-07-27 11:00:36 +10002429 (s->failed >= 1 && fdev[0]->toread) ||
2430 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002431 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2432 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2433 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002434 /* we would like to get this block, possibly by computing it,
2435 * otherwise read it if the backing disk is insync
2436 */
2437 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2438 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2439 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002440 (s->failed && (disk_idx == s->failed_num[0] ||
2441 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002442 /* have disk failed, and we're requested to fetch it;
2443 * do compute it
2444 */
2445 pr_debug("Computing stripe %llu block %d\n",
2446 (unsigned long long)sh->sector, disk_idx);
2447 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2448 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2449 set_bit(R5_Wantcompute, &dev->flags);
2450 sh->ops.target = disk_idx;
2451 sh->ops.target2 = -1; /* no 2nd target */
2452 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002453 /* Careful: from this point on 'uptodate' is in the eye
2454 * of raid_run_ops which services 'compute' operations
2455 * before writes. R5_Wantcompute flags a block that will
2456 * be R5_UPTODATE by the time it is needed for a
2457 * subsequent operation.
2458 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002459 s->uptodate++;
2460 return 1;
2461 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2462 /* Computing 2-failure is *very* expensive; only
2463 * do it if failed >= 2
2464 */
2465 int other;
2466 for (other = disks; other--; ) {
2467 if (other == disk_idx)
2468 continue;
2469 if (!test_bit(R5_UPTODATE,
2470 &sh->dev[other].flags))
2471 break;
2472 }
2473 BUG_ON(other < 0);
2474 pr_debug("Computing stripe %llu blocks %d,%d\n",
2475 (unsigned long long)sh->sector,
2476 disk_idx, other);
2477 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2478 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2479 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2480 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2481 sh->ops.target = disk_idx;
2482 sh->ops.target2 = other;
2483 s->uptodate += 2;
2484 s->req_compute = 1;
2485 return 1;
2486 } else if (test_bit(R5_Insync, &dev->flags)) {
2487 set_bit(R5_LOCKED, &dev->flags);
2488 set_bit(R5_Wantread, &dev->flags);
2489 s->locked++;
2490 pr_debug("Reading block %d (sync=%d)\n",
2491 disk_idx, s->syncing);
2492 }
2493 }
2494
2495 return 0;
2496}
2497
2498/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002499 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002500 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002501static void handle_stripe_fill(struct stripe_head *sh,
2502 struct stripe_head_state *s,
2503 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002504{
2505 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002506
2507 /* look for blocks to read/compute, skip this if a compute
2508 * is already in flight, or if the stripe contents are in the
2509 * midst of changing due to a write
2510 */
2511 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2512 !sh->reconstruct_state)
2513 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002514 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002515 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002516 set_bit(STRIPE_HANDLE, &sh->state);
2517}
2518
2519
Dan Williams1fe797e2008-06-28 09:16:30 +10002520/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002521 * any written block on an uptodate or failed drive can be returned.
2522 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2523 * never LOCKED, so we don't need to test 'failed' directly.
2524 */
NeilBrownd1688a62011-10-11 16:49:52 +11002525static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002526 struct stripe_head *sh, int disks, struct bio **return_bi)
2527{
2528 int i;
2529 struct r5dev *dev;
2530
2531 for (i = disks; i--; )
2532 if (sh->dev[i].written) {
2533 dev = &sh->dev[i];
2534 if (!test_bit(R5_LOCKED, &dev->flags) &&
2535 test_bit(R5_UPTODATE, &dev->flags)) {
2536 /* We can return any write requests */
2537 struct bio *wbi, *wbi2;
2538 int bitmap_end = 0;
Dan Williams45b42332007-07-09 11:56:43 -07002539 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002540 spin_lock_irq(&conf->device_lock);
2541 wbi = dev->written;
2542 dev->written = NULL;
2543 while (wbi && wbi->bi_sector <
2544 dev->sector + STRIPE_SECTORS) {
2545 wbi2 = r5_next_bio(wbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +02002546 if (!raid5_dec_bi_phys_segments(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002547 md_write_end(conf->mddev);
2548 wbi->bi_next = *return_bi;
2549 *return_bi = wbi;
2550 }
2551 wbi = wbi2;
2552 }
2553 if (dev->towrite == NULL)
2554 bitmap_end = 1;
2555 spin_unlock_irq(&conf->device_lock);
2556 if (bitmap_end)
2557 bitmap_endwrite(conf->mddev->bitmap,
2558 sh->sector,
2559 STRIPE_SECTORS,
2560 !test_bit(STRIPE_DEGRADED, &sh->state),
2561 0);
2562 }
2563 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002564
2565 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2566 if (atomic_dec_and_test(&conf->pending_full_writes))
2567 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002568}
2569
NeilBrownd1688a62011-10-11 16:49:52 +11002570static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002571 struct stripe_head *sh,
2572 struct stripe_head_state *s,
2573 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002574{
2575 int rmw = 0, rcw = 0, i;
NeilBrownc8ac1802011-07-27 11:00:36 +10002576 if (conf->max_degraded == 2) {
2577 /* RAID6 requires 'rcw' in current implementation
2578 * Calculate the real rcw later - for now fake it
2579 * look like rcw is cheaper
2580 */
2581 rcw = 1; rmw = 2;
2582 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002583 /* would I have to read this buffer for read_modify_write */
2584 struct r5dev *dev = &sh->dev[i];
2585 if ((dev->towrite || i == sh->pd_idx) &&
2586 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002587 !(test_bit(R5_UPTODATE, &dev->flags) ||
2588 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002589 if (test_bit(R5_Insync, &dev->flags))
2590 rmw++;
2591 else
2592 rmw += 2*disks; /* cannot read it */
2593 }
2594 /* Would I have to read this buffer for reconstruct_write */
2595 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2596 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002597 !(test_bit(R5_UPTODATE, &dev->flags) ||
2598 test_bit(R5_Wantcompute, &dev->flags))) {
2599 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002600 else
2601 rcw += 2*disks;
2602 }
2603 }
Dan Williams45b42332007-07-09 11:56:43 -07002604 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002605 (unsigned long long)sh->sector, rmw, rcw);
2606 set_bit(STRIPE_HANDLE, &sh->state);
2607 if (rmw < rcw && rmw > 0)
2608 /* prefer read-modify-write, but need to get some data */
2609 for (i = disks; i--; ) {
2610 struct r5dev *dev = &sh->dev[i];
2611 if ((dev->towrite || i == sh->pd_idx) &&
2612 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002613 !(test_bit(R5_UPTODATE, &dev->flags) ||
2614 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002615 test_bit(R5_Insync, &dev->flags)) {
2616 if (
2617 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002618 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002619 "%d for r-m-w\n", i);
2620 set_bit(R5_LOCKED, &dev->flags);
2621 set_bit(R5_Wantread, &dev->flags);
2622 s->locked++;
2623 } else {
2624 set_bit(STRIPE_DELAYED, &sh->state);
2625 set_bit(STRIPE_HANDLE, &sh->state);
2626 }
2627 }
2628 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002629 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002630 /* want reconstruct write, but need to get some data */
NeilBrownc8ac1802011-07-27 11:00:36 +10002631 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002632 for (i = disks; i--; ) {
2633 struct r5dev *dev = &sh->dev[i];
2634 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002635 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002636 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002637 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002638 test_bit(R5_Wantcompute, &dev->flags))) {
2639 rcw++;
2640 if (!test_bit(R5_Insync, &dev->flags))
2641 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002642 if (
2643 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002644 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002645 "%d for Reconstruct\n", i);
2646 set_bit(R5_LOCKED, &dev->flags);
2647 set_bit(R5_Wantread, &dev->flags);
2648 s->locked++;
2649 } else {
2650 set_bit(STRIPE_DELAYED, &sh->state);
2651 set_bit(STRIPE_HANDLE, &sh->state);
2652 }
2653 }
2654 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002655 }
Dan Williamsa4456852007-07-09 11:56:43 -07002656 /* now if nothing is locked, and if we have enough data,
2657 * we can start a write request
2658 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002659 /* since handle_stripe can be called at any time we need to handle the
2660 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002661 * subsequent call wants to start a write request. raid_run_ops only
2662 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002663 * simultaneously. If this is not the case then new writes need to be
2664 * held off until the compute completes.
2665 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002666 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2667 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2668 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002669 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002670}
2671
NeilBrownd1688a62011-10-11 16:49:52 +11002672static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002673 struct stripe_head_state *s, int disks)
2674{
Dan Williamsecc65c92008-06-28 08:31:57 +10002675 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002676
Dan Williamsbd2ab672008-04-10 21:29:27 -07002677 set_bit(STRIPE_HANDLE, &sh->state);
2678
Dan Williamsecc65c92008-06-28 08:31:57 +10002679 switch (sh->check_state) {
2680 case check_state_idle:
2681 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002682 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002683 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002684 sh->check_state = check_state_run;
2685 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002686 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002687 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002688 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002689 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002690 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002691 /* fall through */
2692 case check_state_compute_result:
2693 sh->check_state = check_state_idle;
2694 if (!dev)
2695 dev = &sh->dev[sh->pd_idx];
2696
2697 /* check that a write has not made the stripe insync */
2698 if (test_bit(STRIPE_INSYNC, &sh->state))
2699 break;
2700
2701 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002702 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2703 BUG_ON(s->uptodate != disks);
2704
2705 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002706 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002707 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002708
Dan Williamsa4456852007-07-09 11:56:43 -07002709 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002710 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002711 break;
2712 case check_state_run:
2713 break; /* we will be called again upon completion */
2714 case check_state_check_result:
2715 sh->check_state = check_state_idle;
2716
2717 /* if a failure occurred during the check operation, leave
2718 * STRIPE_INSYNC not set and let the stripe be handled again
2719 */
2720 if (s->failed)
2721 break;
2722
2723 /* handle a successful check operation, if parity is correct
2724 * we are done. Otherwise update the mismatch count and repair
2725 * parity if !MD_RECOVERY_CHECK
2726 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002727 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002728 /* parity is correct (on disc,
2729 * not in buffer any more)
2730 */
2731 set_bit(STRIPE_INSYNC, &sh->state);
2732 else {
2733 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2734 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2735 /* don't try to repair!! */
2736 set_bit(STRIPE_INSYNC, &sh->state);
2737 else {
2738 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002739 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002740 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2741 set_bit(R5_Wantcompute,
2742 &sh->dev[sh->pd_idx].flags);
2743 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002744 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10002745 s->uptodate++;
2746 }
2747 }
2748 break;
2749 case check_state_compute_run:
2750 break;
2751 default:
2752 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2753 __func__, sh->check_state,
2754 (unsigned long long) sh->sector);
2755 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002756 }
2757}
2758
2759
NeilBrownd1688a62011-10-11 16:49:52 +11002760static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07002761 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10002762 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002763{
Dan Williamsa4456852007-07-09 11:56:43 -07002764 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11002765 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07002766 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07002767
2768 set_bit(STRIPE_HANDLE, &sh->state);
2769
2770 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002771
Dan Williamsa4456852007-07-09 11:56:43 -07002772 /* Want to check and possibly repair P and Q.
2773 * However there could be one 'failed' device, in which
2774 * case we can only check one of them, possibly using the
2775 * other to generate missing data
2776 */
2777
Dan Williamsd82dfee2009-07-14 13:40:57 -07002778 switch (sh->check_state) {
2779 case check_state_idle:
2780 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10002781 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002782 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07002783 * makes sense to check P (If anything else were failed,
2784 * we would have used P to recreate it).
2785 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002786 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07002787 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002788 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002789 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07002790 * anything, so it makes sense to check it
2791 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002792 if (sh->check_state == check_state_run)
2793 sh->check_state = check_state_run_pq;
2794 else
2795 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07002796 }
Dan Williams36d1c642009-07-14 11:48:22 -07002797
Dan Williamsd82dfee2009-07-14 13:40:57 -07002798 /* discard potentially stale zero_sum_result */
2799 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07002800
Dan Williamsd82dfee2009-07-14 13:40:57 -07002801 if (sh->check_state == check_state_run) {
2802 /* async_xor_zero_sum destroys the contents of P */
2803 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
2804 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07002805 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002806 if (sh->check_state >= check_state_run &&
2807 sh->check_state <= check_state_run_pq) {
2808 /* async_syndrome_zero_sum preserves P and Q, so
2809 * no need to mark them !uptodate here
2810 */
2811 set_bit(STRIPE_OP_CHECK, &s->ops_request);
2812 break;
2813 }
Dan Williams36d1c642009-07-14 11:48:22 -07002814
Dan Williamsd82dfee2009-07-14 13:40:57 -07002815 /* we have 2-disk failure */
2816 BUG_ON(s->failed != 2);
2817 /* fall through */
2818 case check_state_compute_result:
2819 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07002820
Dan Williamsd82dfee2009-07-14 13:40:57 -07002821 /* check that a write has not made the stripe insync */
2822 if (test_bit(STRIPE_INSYNC, &sh->state))
2823 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002824
2825 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07002826 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07002827 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002828 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07002829 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10002830 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07002831 s->locked++;
2832 set_bit(R5_LOCKED, &dev->flags);
2833 set_bit(R5_Wantwrite, &dev->flags);
2834 }
2835 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10002836 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07002837 s->locked++;
2838 set_bit(R5_LOCKED, &dev->flags);
2839 set_bit(R5_Wantwrite, &dev->flags);
2840 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002841 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002842 dev = &sh->dev[pd_idx];
2843 s->locked++;
2844 set_bit(R5_LOCKED, &dev->flags);
2845 set_bit(R5_Wantwrite, &dev->flags);
2846 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002847 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002848 dev = &sh->dev[qd_idx];
2849 s->locked++;
2850 set_bit(R5_LOCKED, &dev->flags);
2851 set_bit(R5_Wantwrite, &dev->flags);
2852 }
2853 clear_bit(STRIPE_DEGRADED, &sh->state);
2854
2855 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002856 break;
2857 case check_state_run:
2858 case check_state_run_q:
2859 case check_state_run_pq:
2860 break; /* we will be called again upon completion */
2861 case check_state_check_result:
2862 sh->check_state = check_state_idle;
2863
2864 /* handle a successful check operation, if parity is correct
2865 * we are done. Otherwise update the mismatch count and repair
2866 * parity if !MD_RECOVERY_CHECK
2867 */
2868 if (sh->ops.zero_sum_result == 0) {
2869 /* both parities are correct */
2870 if (!s->failed)
2871 set_bit(STRIPE_INSYNC, &sh->state);
2872 else {
2873 /* in contrast to the raid5 case we can validate
2874 * parity, but still have a failure to write
2875 * back
2876 */
2877 sh->check_state = check_state_compute_result;
2878 /* Returning at this point means that we may go
2879 * off and bring p and/or q uptodate again so
2880 * we make sure to check zero_sum_result again
2881 * to verify if p or q need writeback
2882 */
2883 }
2884 } else {
2885 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2886 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2887 /* don't try to repair!! */
2888 set_bit(STRIPE_INSYNC, &sh->state);
2889 else {
2890 int *target = &sh->ops.target;
2891
2892 sh->ops.target = -1;
2893 sh->ops.target2 = -1;
2894 sh->check_state = check_state_compute_run;
2895 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2896 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2897 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
2898 set_bit(R5_Wantcompute,
2899 &sh->dev[pd_idx].flags);
2900 *target = pd_idx;
2901 target = &sh->ops.target2;
2902 s->uptodate++;
2903 }
2904 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
2905 set_bit(R5_Wantcompute,
2906 &sh->dev[qd_idx].flags);
2907 *target = qd_idx;
2908 s->uptodate++;
2909 }
2910 }
2911 }
2912 break;
2913 case check_state_compute_run:
2914 break;
2915 default:
2916 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2917 __func__, sh->check_state,
2918 (unsigned long long) sh->sector);
2919 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002920 }
2921}
2922
NeilBrownd1688a62011-10-11 16:49:52 +11002923static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07002924{
2925 int i;
2926
2927 /* We have read all the blocks in this stripe and now we need to
2928 * copy some of them into a target stripe for expand.
2929 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07002930 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002931 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2932 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11002933 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11002934 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07002935 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07002936 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07002937
NeilBrown784052e2009-03-31 15:19:07 +11002938 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11002939 sector_t s = raid5_compute_sector(conf, bn, 0,
2940 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10002941 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07002942 if (sh2 == NULL)
2943 /* so far only the early blocks of this stripe
2944 * have been requested. When later blocks
2945 * get requested, we will try again
2946 */
2947 continue;
2948 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
2949 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
2950 /* must have already done this block */
2951 release_stripe(sh2);
2952 continue;
2953 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07002954
2955 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07002956 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002957 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07002958 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07002959 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002960
Dan Williamsa4456852007-07-09 11:56:43 -07002961 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
2962 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
2963 for (j = 0; j < conf->raid_disks; j++)
2964 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10002965 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002966 !test_bit(R5_Expanded, &sh2->dev[j].flags))
2967 break;
2968 if (j == conf->raid_disks) {
2969 set_bit(STRIPE_EXPAND_READY, &sh2->state);
2970 set_bit(STRIPE_HANDLE, &sh2->state);
2971 }
2972 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002973
Dan Williamsa4456852007-07-09 11:56:43 -07002974 }
NeilBrowna2e08552007-09-11 15:23:36 -07002975 /* done submitting copies, wait for them to complete */
2976 if (tx) {
2977 async_tx_ack(tx);
2978 dma_wait_for_async_tx(tx);
2979 }
Dan Williamsa4456852007-07-09 11:56:43 -07002980}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
Dan Williams6bfe0b42008-04-30 00:52:32 -07002982
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983/*
2984 * handle_stripe - do things to a stripe.
2985 *
2986 * We lock the stripe and then examine the state of various bits
2987 * to see what needs to be done.
2988 * Possible results:
2989 * return some read request which now have data
2990 * return some write requests which are safely on disc
2991 * schedule a read on some buffers
2992 * schedule a write of some buffers
2993 * return confirmation of parity correctness
2994 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 * buffers are taken off read_list or write_list, and bh_cache buffers
2996 * get BH_Lock set before the stripe lock is released.
2997 *
2998 */
Dan Williamsa4456852007-07-09 11:56:43 -07002999
NeilBrownacfe7262011-07-27 11:00:36 +10003000static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003001{
NeilBrownd1688a62011-10-11 16:49:52 +11003002 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003003 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003004 struct r5dev *dev;
3005 int i;
NeilBrown16a53ec2006-06-26 00:27:38 -07003006
NeilBrownacfe7262011-07-27 11:00:36 +10003007 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003008
NeilBrownacfe7262011-07-27 11:00:36 +10003009 s->syncing = test_bit(STRIPE_SYNCING, &sh->state);
3010 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3011 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3012 s->failed_num[0] = -1;
3013 s->failed_num[1] = -1;
3014
3015 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003016 rcu_read_lock();
NeilBrownc4c16632011-07-26 11:34:20 +10003017 spin_lock_irq(&conf->device_lock);
NeilBrown16a53ec2006-06-26 00:27:38 -07003018 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003019 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003020 sector_t first_bad;
3021 int bad_sectors;
3022 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003023
NeilBrown16a53ec2006-06-26 00:27:38 -07003024 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003025
Dan Williams45b42332007-07-09 11:56:43 -07003026 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown16a53ec2006-06-26 00:27:38 -07003027 i, dev->flags, dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003028 /* maybe we can reply to a read
3029 *
3030 * new wantfill requests are only permitted while
3031 * ops_complete_biofill is guaranteed to be inactive
3032 */
3033 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3034 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3035 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003036
3037 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003038 if (test_bit(R5_LOCKED, &dev->flags))
3039 s->locked++;
3040 if (test_bit(R5_UPTODATE, &dev->flags))
3041 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003042 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003043 s->compute++;
3044 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003045 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003046
NeilBrownacfe7262011-07-27 11:00:36 +10003047 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003048 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003049 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003050 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003051 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003052 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003053 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003054 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003055 }
Dan Williamsa4456852007-07-09 11:56:43 -07003056 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003057 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003058 /* Prefer to use the replacement for reads, but only
3059 * if it is recovered enough and has no bad blocks.
3060 */
3061 rdev = rcu_dereference(conf->disks[i].replacement);
3062 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3063 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3064 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3065 &first_bad, &bad_sectors))
3066 set_bit(R5_ReadRepl, &dev->flags);
3067 else {
3068 rdev = rcu_dereference(conf->disks[i].rdev);
3069 clear_bit(R5_ReadRepl, &dev->flags);
3070 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003071 if (rdev && test_bit(Faulty, &rdev->flags))
3072 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003073 if (rdev) {
3074 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3075 &first_bad, &bad_sectors);
3076 if (s->blocked_rdev == NULL
3077 && (test_bit(Blocked, &rdev->flags)
3078 || is_bad < 0)) {
3079 if (is_bad < 0)
3080 set_bit(BlockedBadBlocks,
3081 &rdev->flags);
3082 s->blocked_rdev = rdev;
3083 atomic_inc(&rdev->nr_pending);
3084 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003085 }
NeilBrown415e72d2010-06-17 17:25:21 +10003086 clear_bit(R5_Insync, &dev->flags);
3087 if (!rdev)
3088 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003089 else if (is_bad) {
3090 /* also not in-sync */
3091 if (!test_bit(WriteErrorSeen, &rdev->flags)) {
3092 /* treat as in-sync, but with a read error
3093 * which we can now try to correct
3094 */
3095 set_bit(R5_Insync, &dev->flags);
3096 set_bit(R5_ReadError, &dev->flags);
3097 }
3098 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003099 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003100 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003101 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003102 set_bit(R5_Insync, &dev->flags);
3103 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3104 test_bit(R5_Expanded, &dev->flags))
3105 /* If we've reshaped into here, we assume it is Insync.
3106 * We will shortly update recovery_offset to make
3107 * it official.
3108 */
3109 set_bit(R5_Insync, &dev->flags);
3110
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003111 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003112 /* This flag does not apply to '.replacement'
3113 * only to .rdev, so make sure to check that*/
3114 struct md_rdev *rdev2 = rcu_dereference(
3115 conf->disks[i].rdev);
3116 if (rdev2 == rdev)
3117 clear_bit(R5_Insync, &dev->flags);
3118 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003119 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003120 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003121 } else
3122 clear_bit(R5_WriteError, &dev->flags);
3123 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003124 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003125 /* This flag does not apply to '.replacement'
3126 * only to .rdev, so make sure to check that*/
3127 struct md_rdev *rdev2 = rcu_dereference(
3128 conf->disks[i].rdev);
3129 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003130 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003131 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003132 } else
3133 clear_bit(R5_MadeGood, &dev->flags);
3134 }
NeilBrown415e72d2010-06-17 17:25:21 +10003135 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003136 /* The ReadError flag will just be confusing now */
3137 clear_bit(R5_ReadError, &dev->flags);
3138 clear_bit(R5_ReWrite, &dev->flags);
3139 }
NeilBrown415e72d2010-06-17 17:25:21 +10003140 if (test_bit(R5_ReadError, &dev->flags))
3141 clear_bit(R5_Insync, &dev->flags);
3142 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003143 if (s->failed < 2)
3144 s->failed_num[s->failed] = i;
3145 s->failed++;
NeilBrown415e72d2010-06-17 17:25:21 +10003146 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003147 }
NeilBrownc4c16632011-07-26 11:34:20 +10003148 spin_unlock_irq(&conf->device_lock);
NeilBrown16a53ec2006-06-26 00:27:38 -07003149 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003150}
NeilBrownf4168852007-02-28 20:11:53 -08003151
NeilBrowncc940152011-07-26 11:35:35 +10003152static void handle_stripe(struct stripe_head *sh)
3153{
3154 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003155 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003156 int i;
NeilBrown84789552011-07-27 11:00:36 +10003157 int prexor;
3158 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003159 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003160
3161 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003162 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003163 /* already being handled, ensure it gets handled
3164 * again when current action finishes */
3165 set_bit(STRIPE_HANDLE, &sh->state);
3166 return;
3167 }
3168
3169 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3170 set_bit(STRIPE_SYNCING, &sh->state);
3171 clear_bit(STRIPE_INSYNC, &sh->state);
3172 }
3173 clear_bit(STRIPE_DELAYED, &sh->state);
3174
3175 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3176 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3177 (unsigned long long)sh->sector, sh->state,
3178 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3179 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003180
NeilBrownacfe7262011-07-27 11:00:36 +10003181 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003182
NeilBrownbc2607f2011-07-28 11:39:22 +10003183 if (s.handle_bad_blocks) {
3184 set_bit(STRIPE_HANDLE, &sh->state);
3185 goto finish;
3186 }
3187
NeilBrown474af965fe2011-07-27 11:00:36 +10003188 if (unlikely(s.blocked_rdev)) {
3189 if (s.syncing || s.expanding || s.expanded ||
3190 s.to_write || s.written) {
3191 set_bit(STRIPE_HANDLE, &sh->state);
3192 goto finish;
3193 }
3194 /* There is nothing for the blocked_rdev to block */
3195 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3196 s.blocked_rdev = NULL;
3197 }
3198
3199 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3200 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3201 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3202 }
3203
3204 pr_debug("locked=%d uptodate=%d to_read=%d"
3205 " to_write=%d failed=%d failed_num=%d,%d\n",
3206 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3207 s.failed_num[0], s.failed_num[1]);
3208 /* check if the array has lost more than max_degraded devices and,
3209 * if so, some requests might need to be failed.
3210 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003211 if (s.failed > conf->max_degraded) {
3212 sh->check_state = 0;
3213 sh->reconstruct_state = 0;
3214 if (s.to_read+s.to_write+s.written)
3215 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
3216 if (s.syncing)
3217 handle_failed_sync(conf, sh, &s);
3218 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003219
3220 /*
3221 * might be able to return some write requests if the parity blocks
3222 * are safe, or on a failed drive
3223 */
3224 pdev = &sh->dev[sh->pd_idx];
3225 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3226 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3227 qdev = &sh->dev[sh->qd_idx];
3228 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3229 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3230 || conf->level < 6;
3231
3232 if (s.written &&
3233 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3234 && !test_bit(R5_LOCKED, &pdev->flags)
3235 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3236 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3237 && !test_bit(R5_LOCKED, &qdev->flags)
3238 && test_bit(R5_UPTODATE, &qdev->flags)))))
3239 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3240
3241 /* Now we might consider reading some blocks, either to check/generate
3242 * parity, or to satisfy requests
3243 * or to load a block that is being partially written.
3244 */
3245 if (s.to_read || s.non_overwrite
3246 || (conf->level == 6 && s.to_write && s.failed)
3247 || (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
3248 handle_stripe_fill(sh, &s, disks);
3249
NeilBrown84789552011-07-27 11:00:36 +10003250 /* Now we check to see if any write operations have recently
3251 * completed
3252 */
3253 prexor = 0;
3254 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3255 prexor = 1;
3256 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3257 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3258 sh->reconstruct_state = reconstruct_state_idle;
3259
3260 /* All the 'written' buffers and the parity block are ready to
3261 * be written back to disk
3262 */
3263 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3264 BUG_ON(sh->qd_idx >= 0 &&
3265 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags));
3266 for (i = disks; i--; ) {
3267 struct r5dev *dev = &sh->dev[i];
3268 if (test_bit(R5_LOCKED, &dev->flags) &&
3269 (i == sh->pd_idx || i == sh->qd_idx ||
3270 dev->written)) {
3271 pr_debug("Writing block %d\n", i);
3272 set_bit(R5_Wantwrite, &dev->flags);
3273 if (prexor)
3274 continue;
3275 if (!test_bit(R5_Insync, &dev->flags) ||
3276 ((i == sh->pd_idx || i == sh->qd_idx) &&
3277 s.failed == 0))
3278 set_bit(STRIPE_INSYNC, &sh->state);
3279 }
3280 }
3281 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3282 s.dec_preread_active = 1;
3283 }
3284
3285 /* Now to consider new write requests and what else, if anything
3286 * should be read. We do not handle new writes when:
3287 * 1/ A 'write' operation (copy+xor) is already in flight.
3288 * 2/ A 'check' operation is in flight, as it may clobber the parity
3289 * block.
3290 */
3291 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3292 handle_stripe_dirtying(conf, sh, &s, disks);
3293
3294 /* maybe we need to check and possibly fix the parity for this stripe
3295 * Any reads will already have been scheduled, so we just see if enough
3296 * data is available. The parity check is held off while parity
3297 * dependent operations are in flight.
3298 */
3299 if (sh->check_state ||
3300 (s.syncing && s.locked == 0 &&
3301 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3302 !test_bit(STRIPE_INSYNC, &sh->state))) {
3303 if (conf->level == 6)
3304 handle_parity_checks6(conf, sh, &s, disks);
3305 else
3306 handle_parity_checks5(conf, sh, &s, disks);
3307 }
NeilBrownc5a31002011-07-27 11:00:36 +10003308
3309 if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
3310 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3311 clear_bit(STRIPE_SYNCING, &sh->state);
3312 }
3313
3314 /* If the failed drives are just a ReadError, then we might need
3315 * to progress the repair/check process
3316 */
3317 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3318 for (i = 0; i < s.failed; i++) {
3319 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3320 if (test_bit(R5_ReadError, &dev->flags)
3321 && !test_bit(R5_LOCKED, &dev->flags)
3322 && test_bit(R5_UPTODATE, &dev->flags)
3323 ) {
3324 if (!test_bit(R5_ReWrite, &dev->flags)) {
3325 set_bit(R5_Wantwrite, &dev->flags);
3326 set_bit(R5_ReWrite, &dev->flags);
3327 set_bit(R5_LOCKED, &dev->flags);
3328 s.locked++;
3329 } else {
3330 /* let's read it back */
3331 set_bit(R5_Wantread, &dev->flags);
3332 set_bit(R5_LOCKED, &dev->flags);
3333 s.locked++;
3334 }
3335 }
3336 }
3337
3338
NeilBrown3687c062011-07-27 11:00:36 +10003339 /* Finish reconstruct operations initiated by the expansion process */
3340 if (sh->reconstruct_state == reconstruct_state_result) {
3341 struct stripe_head *sh_src
3342 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3343 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3344 /* sh cannot be written until sh_src has been read.
3345 * so arrange for sh to be delayed a little
3346 */
3347 set_bit(STRIPE_DELAYED, &sh->state);
3348 set_bit(STRIPE_HANDLE, &sh->state);
3349 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3350 &sh_src->state))
3351 atomic_inc(&conf->preread_active_stripes);
3352 release_stripe(sh_src);
3353 goto finish;
3354 }
3355 if (sh_src)
3356 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003357
NeilBrown3687c062011-07-27 11:00:36 +10003358 sh->reconstruct_state = reconstruct_state_idle;
3359 clear_bit(STRIPE_EXPANDING, &sh->state);
3360 for (i = conf->raid_disks; i--; ) {
3361 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3362 set_bit(R5_LOCKED, &sh->dev[i].flags);
3363 s.locked++;
3364 }
3365 }
3366
3367 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3368 !sh->reconstruct_state) {
3369 /* Need to write out all blocks after computing parity */
3370 sh->disks = conf->raid_disks;
3371 stripe_set_idx(sh->sector, conf, 0, sh);
3372 schedule_reconstruction(sh, &s, 1, 1);
3373 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3374 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3375 atomic_dec(&conf->reshape_stripes);
3376 wake_up(&conf->wait_for_overlap);
3377 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3378 }
3379
3380 if (s.expanding && s.locked == 0 &&
3381 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3382 handle_stripe_expansion(conf, sh);
3383
3384finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003385 /* wait for this device to become unblocked */
NeilBrown43220aa2011-08-31 12:49:14 +10003386 if (conf->mddev->external && unlikely(s.blocked_rdev))
NeilBrownc5709ef2011-07-26 11:35:20 +10003387 md_wait_for_blocked_rdev(s.blocked_rdev, conf->mddev);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003388
NeilBrownbc2607f2011-07-28 11:39:22 +10003389 if (s.handle_bad_blocks)
3390 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003391 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003392 struct r5dev *dev = &sh->dev[i];
3393 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3394 /* We own a safe reference to the rdev */
3395 rdev = conf->disks[i].rdev;
3396 if (!rdev_set_badblocks(rdev, sh->sector,
3397 STRIPE_SECTORS, 0))
3398 md_error(conf->mddev, rdev);
3399 rdev_dec_pending(rdev, conf->mddev);
3400 }
NeilBrownb84db562011-07-28 11:39:23 +10003401 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3402 rdev = conf->disks[i].rdev;
3403 rdev_clear_badblocks(rdev, sh->sector,
3404 STRIPE_SECTORS);
3405 rdev_dec_pending(rdev, conf->mddev);
3406 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003407 }
3408
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003409 if (s.ops_request)
3410 raid_run_ops(sh, s.ops_request);
3411
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003412 ops_run_io(sh, &s);
3413
NeilBrownc5709ef2011-07-26 11:35:20 +10003414 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003415 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003416 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003417 * have actually been submitted.
3418 */
3419 atomic_dec(&conf->preread_active_stripes);
3420 if (atomic_read(&conf->preread_active_stripes) <
3421 IO_THRESHOLD)
3422 md_wakeup_thread(conf->mddev->thread);
3423 }
3424
NeilBrownc5709ef2011-07-26 11:35:20 +10003425 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003426
Dan Williams257a4b42011-11-08 16:22:06 +11003427 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003428}
3429
NeilBrownd1688a62011-10-11 16:49:52 +11003430static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431{
3432 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3433 while (!list_empty(&conf->delayed_list)) {
3434 struct list_head *l = conf->delayed_list.next;
3435 struct stripe_head *sh;
3436 sh = list_entry(l, struct stripe_head, lru);
3437 list_del_init(l);
3438 clear_bit(STRIPE_DELAYED, &sh->state);
3439 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3440 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003441 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 }
NeilBrown482c0832011-04-18 18:25:42 +10003443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444}
3445
NeilBrownd1688a62011-10-11 16:49:52 +11003446static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003447{
3448 /* device_lock is held */
3449 struct list_head head;
3450 list_add(&head, &conf->bitmap_list);
3451 list_del_init(&conf->bitmap_list);
3452 while (!list_empty(&head)) {
3453 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3454 list_del_init(&sh->lru);
3455 atomic_inc(&sh->count);
3456 __release_stripe(conf, sh);
3457 }
3458}
3459
NeilBrownfd01b882011-10-11 16:47:53 +11003460int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003461{
NeilBrownd1688a62011-10-11 16:49:52 +11003462 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003463
3464 /* No difference between reads and writes. Just check
3465 * how busy the stripe_cache is
3466 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003467
NeilBrownf022b2f2006-10-03 01:15:56 -07003468 if (conf->inactive_blocked)
3469 return 1;
3470 if (conf->quiesce)
3471 return 1;
3472 if (list_empty_careful(&conf->inactive_list))
3473 return 1;
3474
3475 return 0;
3476}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003477EXPORT_SYMBOL_GPL(md_raid5_congested);
3478
3479static int raid5_congested(void *data, int bits)
3480{
NeilBrownfd01b882011-10-11 16:47:53 +11003481 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003482
3483 return mddev_congested(mddev, bits) ||
3484 md_raid5_congested(mddev, bits);
3485}
NeilBrownf022b2f2006-10-03 01:15:56 -07003486
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003487/* We want read requests to align with chunks where possible,
3488 * but write requests don't need to.
3489 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003490static int raid5_mergeable_bvec(struct request_queue *q,
3491 struct bvec_merge_data *bvm,
3492 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003493{
NeilBrownfd01b882011-10-11 16:47:53 +11003494 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003495 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003496 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003497 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003498 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003499
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003500 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003501 return biovec->bv_len; /* always allow writes to be mergeable */
3502
Andre Noll664e7c42009-06-18 08:45:27 +10003503 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3504 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003505 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3506 if (max < 0) max = 0;
3507 if (max <= biovec->bv_len && bio_sectors == 0)
3508 return biovec->bv_len;
3509 else
3510 return max;
3511}
3512
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003513
NeilBrownfd01b882011-10-11 16:47:53 +11003514static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003515{
3516 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003517 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003518 unsigned int bio_sectors = bio->bi_size >> 9;
3519
Andre Noll664e7c42009-06-18 08:45:27 +10003520 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3521 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003522 return chunk_sectors >=
3523 ((sector & (chunk_sectors - 1)) + bio_sectors);
3524}
3525
3526/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003527 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3528 * later sampled by raid5d.
3529 */
NeilBrownd1688a62011-10-11 16:49:52 +11003530static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003531{
3532 unsigned long flags;
3533
3534 spin_lock_irqsave(&conf->device_lock, flags);
3535
3536 bi->bi_next = conf->retry_read_aligned_list;
3537 conf->retry_read_aligned_list = bi;
3538
3539 spin_unlock_irqrestore(&conf->device_lock, flags);
3540 md_wakeup_thread(conf->mddev->thread);
3541}
3542
3543
NeilBrownd1688a62011-10-11 16:49:52 +11003544static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003545{
3546 struct bio *bi;
3547
3548 bi = conf->retry_read_aligned;
3549 if (bi) {
3550 conf->retry_read_aligned = NULL;
3551 return bi;
3552 }
3553 bi = conf->retry_read_aligned_list;
3554 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003555 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003556 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003557 /*
3558 * this sets the active strip count to 1 and the processed
3559 * strip count to zero (upper 8 bits)
3560 */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003561 bi->bi_phys_segments = 1; /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003562 }
3563
3564 return bi;
3565}
3566
3567
3568/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003569 * The "raid5_align_endio" should check if the read succeeded and if it
3570 * did, call bio_endio on the original bio (having bio_put the new bio
3571 * first).
3572 * If the read failed..
3573 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003574static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003575{
3576 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003577 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003578 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003579 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003580 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003581
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003582 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003583
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003584 rdev = (void*)raid_bi->bi_next;
3585 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003586 mddev = rdev->mddev;
3587 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003588
3589 rdev_dec_pending(rdev, conf->mddev);
3590
3591 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003592 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003593 if (atomic_dec_and_test(&conf->active_aligned_reads))
3594 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003595 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003596 }
3597
3598
Dan Williams45b42332007-07-09 11:56:43 -07003599 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003600
3601 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003602}
3603
Neil Brown387bb172007-02-08 14:20:29 -08003604static int bio_fits_rdev(struct bio *bi)
3605{
Jens Axboe165125e2007-07-24 09:28:11 +02003606 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003607
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003608 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003609 return 0;
3610 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003611 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003612 return 0;
3613
3614 if (q->merge_bvec_fn)
3615 /* it's too hard to apply the merge_bvec_fn at this stage,
3616 * just just give up
3617 */
3618 return 0;
3619
3620 return 1;
3621}
3622
3623
NeilBrownfd01b882011-10-11 16:47:53 +11003624static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003625{
NeilBrownd1688a62011-10-11 16:49:52 +11003626 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11003627 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003628 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11003629 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11003630 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003631
3632 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003633 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003634 return 0;
3635 }
3636 /*
NeilBrowna167f662010-10-26 18:31:13 +11003637 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003638 */
NeilBrowna167f662010-10-26 18:31:13 +11003639 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003640 if (!align_bi)
3641 return 0;
3642 /*
3643 * set bi_end_io to a new function, and set bi_private to the
3644 * original bio.
3645 */
3646 align_bi->bi_end_io = raid5_align_endio;
3647 align_bi->bi_private = raid_bio;
3648 /*
3649 * compute position
3650 */
NeilBrown112bf892009-03-31 14:39:38 +11003651 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3652 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003653 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003654
NeilBrown671488c2011-12-23 10:17:52 +11003655 end_sector = align_bi->bi_sector + (align_bi->bi_size >> 9);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003656 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11003657 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
3658 if (!rdev || test_bit(Faulty, &rdev->flags) ||
3659 rdev->recovery_offset < end_sector) {
3660 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3661 if (rdev &&
3662 (test_bit(Faulty, &rdev->flags) ||
3663 !(test_bit(In_sync, &rdev->flags) ||
3664 rdev->recovery_offset >= end_sector)))
3665 rdev = NULL;
3666 }
3667 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10003668 sector_t first_bad;
3669 int bad_sectors;
3670
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003671 atomic_inc(&rdev->nr_pending);
3672 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003673 raid_bio->bi_next = (void*)rdev;
3674 align_bi->bi_bdev = rdev->bdev;
3675 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
3676 align_bi->bi_sector += rdev->data_offset;
3677
NeilBrown31c176e2011-07-28 11:39:22 +10003678 if (!bio_fits_rdev(align_bi) ||
3679 is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
3680 &first_bad, &bad_sectors)) {
3681 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08003682 bio_put(align_bi);
3683 rdev_dec_pending(rdev, mddev);
3684 return 0;
3685 }
3686
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003687 spin_lock_irq(&conf->device_lock);
3688 wait_event_lock_irq(conf->wait_for_stripe,
3689 conf->quiesce == 0,
3690 conf->device_lock, /* nothing */);
3691 atomic_inc(&conf->active_aligned_reads);
3692 spin_unlock_irq(&conf->device_lock);
3693
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003694 generic_make_request(align_bi);
3695 return 1;
3696 } else {
3697 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003698 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003699 return 0;
3700 }
3701}
3702
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003703/* __get_priority_stripe - get the next stripe to process
3704 *
3705 * Full stripe writes are allowed to pass preread active stripes up until
3706 * the bypass_threshold is exceeded. In general the bypass_count
3707 * increments when the handle_list is handled before the hold_list; however, it
3708 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3709 * stripe with in flight i/o. The bypass_count will be reset when the
3710 * head of the hold_list has changed, i.e. the head was promoted to the
3711 * handle_list.
3712 */
NeilBrownd1688a62011-10-11 16:49:52 +11003713static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003714{
3715 struct stripe_head *sh;
3716
3717 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3718 __func__,
3719 list_empty(&conf->handle_list) ? "empty" : "busy",
3720 list_empty(&conf->hold_list) ? "empty" : "busy",
3721 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3722
3723 if (!list_empty(&conf->handle_list)) {
3724 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3725
3726 if (list_empty(&conf->hold_list))
3727 conf->bypass_count = 0;
3728 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3729 if (conf->hold_list.next == conf->last_hold)
3730 conf->bypass_count++;
3731 else {
3732 conf->last_hold = conf->hold_list.next;
3733 conf->bypass_count -= conf->bypass_threshold;
3734 if (conf->bypass_count < 0)
3735 conf->bypass_count = 0;
3736 }
3737 }
3738 } else if (!list_empty(&conf->hold_list) &&
3739 ((conf->bypass_threshold &&
3740 conf->bypass_count > conf->bypass_threshold) ||
3741 atomic_read(&conf->pending_full_writes) == 0)) {
3742 sh = list_entry(conf->hold_list.next,
3743 typeof(*sh), lru);
3744 conf->bypass_count -= conf->bypass_threshold;
3745 if (conf->bypass_count < 0)
3746 conf->bypass_count = 0;
3747 } else
3748 return NULL;
3749
3750 list_del_init(&sh->lru);
3751 atomic_inc(&sh->count);
3752 BUG_ON(atomic_read(&sh->count) != 1);
3753 return sh;
3754}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003755
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07003756static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757{
NeilBrownd1688a62011-10-11 16:49:52 +11003758 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11003759 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 sector_t new_sector;
3761 sector_t logical_sector, last_sector;
3762 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01003763 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11003764 int remaining;
NeilBrown7c13edc2011-04-18 18:25:43 +10003765 int plugged;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766
Tejun Heoe9c74692010-09-03 11:56:18 +02003767 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
3768 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02003769 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07003770 }
3771
NeilBrown3d310eb2005-06-21 17:17:26 -07003772 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07003773
NeilBrown802ba062006-12-13 00:34:13 -08003774 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003775 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11003776 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02003777 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003778
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
3780 last_sector = bi->bi_sector + (bi->bi_size>>9);
3781 bi->bi_next = NULL;
3782 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07003783
NeilBrown7c13edc2011-04-18 18:25:43 +10003784 plugged = mddev_check_plugged(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
3786 DEFINE_WAIT(w);
NeilBrown16a53ec2006-06-26 00:27:38 -07003787 int disks, data_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003788 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08003789
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003790 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11003791 previous = 0;
NeilBrownb0f9ec02009-03-31 15:27:18 +11003792 disks = conf->raid_disks;
NeilBrownb578d552006-03-27 01:18:12 -08003793 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11003794 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11003795 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08003796 * 64bit on a 32bit platform, and so it might be
3797 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02003798 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08003799 * the lock is dropped, so once we get a reference
3800 * to the stripe that we think it is, we will have
3801 * to check again.
3802 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003803 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11003804 if (mddev->delta_disks < 0
3805 ? logical_sector < conf->reshape_progress
3806 : logical_sector >= conf->reshape_progress) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003807 disks = conf->previous_raid_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003808 previous = 1;
3809 } else {
NeilBrownfef9c612009-03-31 15:16:46 +11003810 if (mddev->delta_disks < 0
3811 ? logical_sector < conf->reshape_safe
3812 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08003813 spin_unlock_irq(&conf->device_lock);
3814 schedule();
3815 goto retry;
3816 }
3817 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003818 spin_unlock_irq(&conf->device_lock);
3819 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003820 data_disks = disks - conf->max_degraded;
3821
NeilBrown112bf892009-03-31 14:39:38 +11003822 new_sector = raid5_compute_sector(conf, logical_sector,
3823 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003824 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10003825 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 (unsigned long long)new_sector,
3827 (unsigned long long)logical_sector);
3828
NeilBrownb5663ba2009-03-31 14:39:38 +11003829 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10003830 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11003832 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003833 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08003834 * stripe, so we must do the range check again.
3835 * Expansion could still move past after this
3836 * test, but as we are holding a reference to
3837 * 'sh', we know that if that happens,
3838 * STRIPE_EXPANDING will get set and the expansion
3839 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003840 */
3841 int must_retry = 0;
3842 spin_lock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11003843 if (mddev->delta_disks < 0
3844 ? logical_sector >= conf->reshape_progress
3845 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003846 /* mismatch, need to try again */
3847 must_retry = 1;
3848 spin_unlock_irq(&conf->device_lock);
3849 if (must_retry) {
3850 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07003851 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003852 goto retry;
3853 }
3854 }
NeilBrowne62e58a2009-07-01 13:15:35 +10003855
Namhyung Kimffd96e32011-07-18 17:38:51 +10003856 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10003857 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08003858 logical_sector < mddev->suspend_hi) {
3859 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10003860 /* As the suspend_* range is controlled by
3861 * userspace, we want an interruptible
3862 * wait.
3863 */
3864 flush_signals(current);
3865 prepare_to_wait(&conf->wait_for_overlap,
3866 &w, TASK_INTERRUPTIBLE);
3867 if (logical_sector >= mddev->suspend_lo &&
3868 logical_sector < mddev->suspend_hi)
3869 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08003870 goto retry;
3871 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003872
3873 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10003874 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003875 /* Stripe is busy expanding or
3876 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 * and wait a while
3878 */
NeilBrown482c0832011-04-18 18:25:42 +10003879 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 release_stripe(sh);
3881 schedule();
3882 goto retry;
3883 }
3884 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08003885 set_bit(STRIPE_HANDLE, &sh->state);
3886 clear_bit(STRIPE_DELAYED, &sh->state);
Tejun Heoe9c74692010-09-03 11:56:18 +02003887 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11003888 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3889 atomic_inc(&conf->preread_active_stripes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 } else {
3892 /* cannot get stripe for read-ahead, just give-up */
3893 clear_bit(BIO_UPTODATE, &bi->bi_flags);
3894 finish_wait(&conf->wait_for_overlap, &w);
3895 break;
3896 }
3897
3898 }
NeilBrown7c13edc2011-04-18 18:25:43 +10003899 if (!plugged)
3900 md_wakeup_thread(mddev->thread);
3901
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02003903 remaining = raid5_dec_bi_phys_segments(bi);
NeilBrownf6344752006-03-27 01:18:17 -08003904 spin_unlock_irq(&conf->device_lock);
3905 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906
NeilBrown16a53ec2006-06-26 00:27:38 -07003907 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02003909
Neil Brown0e13fe232008-06-28 08:31:20 +10003910 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912}
3913
NeilBrownfd01b882011-10-11 16:47:53 +11003914static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11003915
NeilBrownfd01b882011-10-11 16:47:53 +11003916static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917{
NeilBrown52c03292006-06-26 00:27:43 -07003918 /* reshaping is quite different to recovery/resync so it is
3919 * handled quite separately ... here.
3920 *
3921 * On each call to sync_request, we gather one chunk worth of
3922 * destination stripes and flag them as expanding.
3923 * Then we find all the source stripes and request reads.
3924 * As the reads complete, handle_stripe will copy the data
3925 * into the destination stripe and release that stripe.
3926 */
NeilBrownd1688a62011-10-11 16:49:52 +11003927 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08003929 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08003930 int raid_disks = conf->previous_raid_disks;
3931 int data_disks = raid_disks - conf->max_degraded;
3932 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07003933 int i;
3934 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11003935 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11003936 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11003937 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11003938 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07003939
NeilBrownfef9c612009-03-31 15:16:46 +11003940 if (sector_nr == 0) {
3941 /* If restarting in the middle, skip the initial sectors */
3942 if (mddev->delta_disks < 0 &&
3943 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
3944 sector_nr = raid5_size(mddev, 0, 0)
3945 - conf->reshape_progress;
NeilBrowna6397552009-08-13 10:13:00 +10003946 } else if (mddev->delta_disks >= 0 &&
NeilBrownfef9c612009-03-31 15:16:46 +11003947 conf->reshape_progress > 0)
3948 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08003949 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11003950 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11003951 mddev->curr_resync_completed = sector_nr;
3952 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11003953 *skipped = 1;
3954 return sector_nr;
3955 }
NeilBrown52c03292006-06-26 00:27:43 -07003956 }
3957
NeilBrown7a661382009-03-31 15:21:40 +11003958 /* We need to process a full chunk at a time.
3959 * If old and new chunk sizes differ, we need to process the
3960 * largest of these
3961 */
Andre Noll664e7c42009-06-18 08:45:27 +10003962 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
3963 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11003964 else
Andre Noll9d8f0362009-06-18 08:45:01 +10003965 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11003966
NeilBrown52c03292006-06-26 00:27:43 -07003967 /* we update the metadata when there is more than 3Meg
3968 * in the block range (that is rather arbitrary, should
3969 * probably be time based) or when the data about to be
3970 * copied would over-write the source of the data at
3971 * the front of the range.
NeilBrownfef9c612009-03-31 15:16:46 +11003972 * i.e. one new_stripe along from reshape_progress new_maps
3973 * to after where reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07003974 */
NeilBrownfef9c612009-03-31 15:16:46 +11003975 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08003976 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11003977 readpos = conf->reshape_progress;
3978 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11003979 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08003980 sector_div(safepos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11003981 if (mddev->delta_disks < 0) {
NeilBrowned37d832009-05-27 21:39:05 +10003982 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11003983 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11003984 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11003985 } else {
NeilBrown7a661382009-03-31 15:21:40 +11003986 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10003987 readpos -= min_t(sector_t, reshape_sectors, readpos);
3988 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11003989 }
NeilBrown52c03292006-06-26 00:27:43 -07003990
NeilBrownc8f517c2009-03-31 15:28:40 +11003991 /* 'writepos' is the most advanced device address we might write.
3992 * 'readpos' is the least advanced device address we might read.
3993 * 'safepos' is the least address recorded in the metadata as having
3994 * been reshaped.
3995 * If 'readpos' is behind 'writepos', then there is no way that we can
3996 * ensure safety in the face of a crash - that must be done by userspace
3997 * making a backup of the data. So in that case there is no particular
3998 * rush to update metadata.
3999 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4000 * update the metadata to advance 'safepos' to match 'readpos' so that
4001 * we can be safe in the event of a crash.
4002 * So we insist on updating metadata if safepos is behind writepos and
4003 * readpos is beyond writepos.
4004 * In any case, update the metadata every 10 seconds.
4005 * Maybe that number should be configurable, but I'm not sure it is
4006 * worth it.... maybe it could be a multiple of safemode_delay???
4007 */
NeilBrownfef9c612009-03-31 15:16:46 +11004008 if ((mddev->delta_disks < 0
NeilBrownc8f517c2009-03-31 15:28:40 +11004009 ? (safepos > writepos && readpos < writepos)
4010 : (safepos < writepos && readpos > writepos)) ||
4011 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004012 /* Cannot proceed until we've updated the superblock... */
4013 wait_event(conf->wait_for_overlap,
4014 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004015 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004016 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004017 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07004018 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004019 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07004020 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004021 kthread_should_stop());
4022 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004023 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004024 spin_unlock_irq(&conf->device_lock);
4025 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004026 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004027 }
4028
NeilBrownec32a2b2009-03-31 15:17:38 +11004029 if (mddev->delta_disks < 0) {
4030 BUG_ON(conf->reshape_progress == 0);
4031 stripe_addr = writepos;
4032 BUG_ON((mddev->dev_sectors &
NeilBrown7a661382009-03-31 15:21:40 +11004033 ~((sector_t)reshape_sectors - 1))
4034 - reshape_sectors - stripe_addr
NeilBrownec32a2b2009-03-31 15:17:38 +11004035 != sector_nr);
4036 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004037 BUG_ON(writepos != sector_nr + reshape_sectors);
NeilBrownec32a2b2009-03-31 15:17:38 +11004038 stripe_addr = sector_nr;
4039 }
NeilBrownab69ae12009-03-31 15:26:47 +11004040 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004041 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004042 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004043 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004044 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004045 set_bit(STRIPE_EXPANDING, &sh->state);
4046 atomic_inc(&conf->reshape_stripes);
4047 /* If any of this stripe is beyond the end of the old
4048 * array, then we need to zero those blocks
4049 */
4050 for (j=sh->disks; j--;) {
4051 sector_t s;
4052 if (j == sh->pd_idx)
4053 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004054 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004055 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004056 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004057 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004058 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004059 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004060 continue;
4061 }
4062 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4063 set_bit(R5_Expanded, &sh->dev[j].flags);
4064 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4065 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004066 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004067 set_bit(STRIPE_EXPAND_READY, &sh->state);
4068 set_bit(STRIPE_HANDLE, &sh->state);
4069 }
NeilBrownab69ae12009-03-31 15:26:47 +11004070 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004071 }
4072 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004073 if (mddev->delta_disks < 0)
NeilBrown7a661382009-03-31 15:21:40 +11004074 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004075 else
NeilBrown7a661382009-03-31 15:21:40 +11004076 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004077 spin_unlock_irq(&conf->device_lock);
4078 /* Ok, those stripe are ready. We can start scheduling
4079 * reads on the source stripes.
4080 * The source stripes are determined by mapping the first and last
4081 * block on the destination stripes.
4082 */
NeilBrown52c03292006-06-26 00:27:43 -07004083 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004084 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004085 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004086 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004087 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004088 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004089 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004090 if (last_sector >= mddev->dev_sectors)
4091 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004092 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004093 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004094 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4095 set_bit(STRIPE_HANDLE, &sh->state);
4096 release_stripe(sh);
4097 first_sector += STRIPE_SECTORS;
4098 }
NeilBrownab69ae12009-03-31 15:26:47 +11004099 /* Now that the sources are clearly marked, we can release
4100 * the destination stripes
4101 */
4102 while (!list_empty(&stripes)) {
4103 sh = list_entry(stripes.next, struct stripe_head, lru);
4104 list_del_init(&sh->lru);
4105 release_stripe(sh);
4106 }
NeilBrownc6207272008-02-06 01:39:52 -08004107 /* If this takes us to the resync_max point where we have to pause,
4108 * then we need to write out the superblock.
4109 */
NeilBrown7a661382009-03-31 15:21:40 +11004110 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004111 if ((sector_nr - mddev->curr_resync_completed) * 2
4112 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004113 /* Cannot proceed until we've updated the superblock... */
4114 wait_event(conf->wait_for_overlap,
4115 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004116 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004117 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004118 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004119 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4120 md_wakeup_thread(mddev->thread);
4121 wait_event(mddev->sb_wait,
4122 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4123 || kthread_should_stop());
4124 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004125 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004126 spin_unlock_irq(&conf->device_lock);
4127 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004128 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004129 }
NeilBrown7a661382009-03-31 15:21:40 +11004130 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004131}
4132
4133/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004134static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004135{
NeilBrownd1688a62011-10-11 16:49:52 +11004136 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004137 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004138 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004139 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004140 int still_degraded = 0;
4141 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142
NeilBrown72626682005-09-09 16:23:54 -07004143 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004145
NeilBrown29269552006-03-27 01:18:10 -08004146 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4147 end_reshape(conf);
4148 return 0;
4149 }
NeilBrown72626682005-09-09 16:23:54 -07004150
4151 if (mddev->curr_resync < max_sector) /* aborted */
4152 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4153 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004154 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004155 conf->fullsync = 0;
4156 bitmap_close_sync(mddev->bitmap);
4157
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 return 0;
4159 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004160
NeilBrown64bd6602009-08-03 10:59:58 +10004161 /* Allow raid5_quiesce to complete */
4162 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4163
NeilBrown52c03292006-06-26 00:27:43 -07004164 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4165 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004166
NeilBrownc6207272008-02-06 01:39:52 -08004167 /* No need to check resync_max as we never do more than one
4168 * stripe, and as resync_max will always be on a chunk boundary,
4169 * if the check in md_do_sync didn't fire, there is no chance
4170 * of overstepping resync_max here
4171 */
4172
NeilBrown16a53ec2006-06-26 00:27:38 -07004173 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 * to resync, then assert that we are finished, because there is
4175 * nothing we can do.
4176 */
NeilBrown3285edf2006-06-26 00:27:55 -07004177 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004178 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004179 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004180 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 return rv;
4182 }
NeilBrown72626682005-09-09 16:23:54 -07004183 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004184 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004185 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4186 /* we can skip this block, and probably more */
4187 sync_blocks /= STRIPE_SECTORS;
4188 *skipped = 1;
4189 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191
NeilBrownb47490c2008-02-06 01:39:50 -08004192
4193 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4194
NeilBrowna8c906c2009-06-09 14:39:59 +10004195 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004197 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004199 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004201 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004203 /* Need to check if array will still be degraded after recovery/resync
4204 * We don't need to check the 'failed' flag as when that gets set,
4205 * recovery aborts.
4206 */
NeilBrownf001a702009-06-09 14:30:31 +10004207 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004208 if (conf->disks[i].rdev == NULL)
4209 still_degraded = 1;
4210
4211 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4212
NeilBrown83206d62011-07-26 11:19:49 +10004213 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214
NeilBrown14425772009-10-16 15:55:25 +11004215 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 release_stripe(sh);
4217
4218 return STRIPE_SECTORS;
4219}
4220
NeilBrownd1688a62011-10-11 16:49:52 +11004221static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004222{
4223 /* We may not be able to submit a whole bio at once as there
4224 * may not be enough stripe_heads available.
4225 * We cannot pre-allocate enough stripe_heads as we may need
4226 * more than exist in the cache (if we allow ever large chunks).
4227 * So we do one stripe head at a time and record in
4228 * ->bi_hw_segments how many have been done.
4229 *
4230 * We *know* that this entire raid_bio is in one chunk, so
4231 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4232 */
4233 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004234 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004235 sector_t sector, logical_sector, last_sector;
4236 int scnt = 0;
4237 int remaining;
4238 int handled = 0;
4239
4240 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004241 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004242 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004243 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4244
4245 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004246 logical_sector += STRIPE_SECTORS,
4247 sector += STRIPE_SECTORS,
4248 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004249
Jens Axboe960e7392008-08-15 10:41:18 +02004250 if (scnt < raid5_bi_hw_segments(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004251 /* already done this stripe */
4252 continue;
4253
NeilBrowna8c906c2009-06-09 14:39:59 +10004254 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004255
4256 if (!sh) {
4257 /* failed to get a stripe - must wait */
Jens Axboe960e7392008-08-15 10:41:18 +02004258 raid5_set_bi_hw_segments(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004259 conf->retry_read_aligned = raid_bio;
4260 return handled;
4261 }
4262
Neil Brown387bb172007-02-08 14:20:29 -08004263 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4264 release_stripe(sh);
Jens Axboe960e7392008-08-15 10:41:18 +02004265 raid5_set_bi_hw_segments(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004266 conf->retry_read_aligned = raid_bio;
4267 return handled;
4268 }
4269
Dan Williams36d1c642009-07-14 11:48:22 -07004270 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004271 release_stripe(sh);
4272 handled++;
4273 }
4274 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004275 remaining = raid5_dec_bi_phys_segments(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004276 spin_unlock_irq(&conf->device_lock);
Neil Brown0e13fe232008-06-28 08:31:20 +10004277 if (remaining == 0)
4278 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004279 if (atomic_dec_and_test(&conf->active_aligned_reads))
4280 wake_up(&conf->wait_for_stripe);
4281 return handled;
4282}
4283
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004284
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285/*
4286 * This is our raid5 kernel thread.
4287 *
4288 * We scan the hash table for stripes which can be handled now.
4289 * During the scan, completed stripes are saved for us by the interrupt
4290 * handler, so that they will not have to wait for our next wakeup.
4291 */
NeilBrownfd01b882011-10-11 16:47:53 +11004292static void raid5d(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293{
4294 struct stripe_head *sh;
NeilBrownd1688a62011-10-11 16:49:52 +11004295 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004297 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298
Dan Williams45b42332007-07-09 11:56:43 -07004299 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300
4301 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004303 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 handled = 0;
4305 spin_lock_irq(&conf->device_lock);
4306 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004307 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308
NeilBrown7c13edc2011-04-18 18:25:43 +10004309 if (atomic_read(&mddev->plug_cnt) == 0 &&
4310 !list_empty(&conf->bitmap_list)) {
4311 /* Now is a good time to flush some bitmap updates */
4312 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004313 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004314 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004315 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004316 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004317 activate_bit_delay(conf);
4318 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004319 if (atomic_read(&mddev->plug_cnt) == 0)
4320 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004321
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004322 while ((bio = remove_bio_from_retry(conf))) {
4323 int ok;
4324 spin_unlock_irq(&conf->device_lock);
4325 ok = retry_aligned_read(conf, bio);
4326 spin_lock_irq(&conf->device_lock);
4327 if (!ok)
4328 break;
4329 handled++;
4330 }
4331
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004332 sh = __get_priority_stripe(conf);
4333
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004334 if (!sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 spin_unlock_irq(&conf->device_lock);
4337
4338 handled++;
Dan Williams417b8d42009-10-16 16:25:22 +11004339 handle_stripe(sh);
4340 release_stripe(sh);
4341 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342
NeilBrownde393cd2011-07-28 11:31:48 +10004343 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
4344 md_check_recovery(mddev);
4345
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 spin_lock_irq(&conf->device_lock);
4347 }
Dan Williams45b42332007-07-09 11:56:43 -07004348 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349
4350 spin_unlock_irq(&conf->device_lock);
4351
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004352 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004353 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354
Dan Williams45b42332007-07-09 11:56:43 -07004355 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356}
4357
NeilBrown3f294f42005-11-08 21:39:25 -08004358static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004359raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004360{
NeilBrownd1688a62011-10-11 16:49:52 +11004361 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004362 if (conf)
4363 return sprintf(page, "%d\n", conf->max_nr_stripes);
4364 else
4365 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004366}
4367
NeilBrownc41d4ac2010-06-01 19:37:24 +10004368int
NeilBrownfd01b882011-10-11 16:47:53 +11004369raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004370{
NeilBrownd1688a62011-10-11 16:49:52 +11004371 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004372 int err;
4373
4374 if (size <= 16 || size > 32768)
4375 return -EINVAL;
4376 while (size < conf->max_nr_stripes) {
4377 if (drop_one_stripe(conf))
4378 conf->max_nr_stripes--;
4379 else
4380 break;
4381 }
4382 err = md_allow_write(mddev);
4383 if (err)
4384 return err;
4385 while (size > conf->max_nr_stripes) {
4386 if (grow_one_stripe(conf))
4387 conf->max_nr_stripes++;
4388 else break;
4389 }
4390 return 0;
4391}
4392EXPORT_SYMBOL(raid5_set_cache_size);
4393
NeilBrown3f294f42005-11-08 21:39:25 -08004394static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004395raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004396{
NeilBrownd1688a62011-10-11 16:49:52 +11004397 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004398 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004399 int err;
4400
NeilBrown3f294f42005-11-08 21:39:25 -08004401 if (len >= PAGE_SIZE)
4402 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004403 if (!conf)
4404 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004405
Dan Williams4ef197d82008-04-28 02:15:54 -07004406 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004407 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004408 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004409 if (err)
4410 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004411 return len;
4412}
NeilBrown007583c2005-11-08 21:39:30 -08004413
NeilBrown96de1e62005-11-08 21:39:39 -08004414static struct md_sysfs_entry
4415raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4416 raid5_show_stripe_cache_size,
4417 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004418
4419static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004420raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004421{
NeilBrownd1688a62011-10-11 16:49:52 +11004422 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004423 if (conf)
4424 return sprintf(page, "%d\n", conf->bypass_threshold);
4425 else
4426 return 0;
4427}
4428
4429static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004430raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004431{
NeilBrownd1688a62011-10-11 16:49:52 +11004432 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004433 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004434 if (len >= PAGE_SIZE)
4435 return -EINVAL;
4436 if (!conf)
4437 return -ENODEV;
4438
Dan Williams4ef197d82008-04-28 02:15:54 -07004439 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004440 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004441 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004442 return -EINVAL;
4443 conf->bypass_threshold = new;
4444 return len;
4445}
4446
4447static struct md_sysfs_entry
4448raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4449 S_IRUGO | S_IWUSR,
4450 raid5_show_preread_threshold,
4451 raid5_store_preread_threshold);
4452
4453static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004454stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004455{
NeilBrownd1688a62011-10-11 16:49:52 +11004456 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004457 if (conf)
4458 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4459 else
4460 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004461}
4462
NeilBrown96de1e62005-11-08 21:39:39 -08004463static struct md_sysfs_entry
4464raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004465
NeilBrown007583c2005-11-08 21:39:30 -08004466static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004467 &raid5_stripecache_size.attr,
4468 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004469 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004470 NULL,
4471};
NeilBrown007583c2005-11-08 21:39:30 -08004472static struct attribute_group raid5_attrs_group = {
4473 .name = NULL,
4474 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004475};
4476
Dan Williams80c3a6c2009-03-17 18:10:40 -07004477static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11004478raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07004479{
NeilBrownd1688a62011-10-11 16:49:52 +11004480 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004481
4482 if (!sectors)
4483 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004484 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11004485 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11004486 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004487
Andre Noll9d8f0362009-06-18 08:45:01 +10004488 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004489 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004490 return sectors * (raid_disks - conf->max_degraded);
4491}
4492
NeilBrownd1688a62011-10-11 16:49:52 +11004493static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004494{
4495 struct raid5_percpu *percpu;
4496 unsigned long cpu;
4497
4498 if (!conf->percpu)
4499 return;
4500
4501 get_online_cpus();
4502 for_each_possible_cpu(cpu) {
4503 percpu = per_cpu_ptr(conf->percpu, cpu);
4504 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004505 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004506 }
4507#ifdef CONFIG_HOTPLUG_CPU
4508 unregister_cpu_notifier(&conf->cpu_notify);
4509#endif
4510 put_online_cpus();
4511
4512 free_percpu(conf->percpu);
4513}
4514
NeilBrownd1688a62011-10-11 16:49:52 +11004515static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10004516{
4517 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004518 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10004519 kfree(conf->disks);
4520 kfree(conf->stripe_hashtbl);
4521 kfree(conf);
4522}
4523
Dan Williams36d1c642009-07-14 11:48:22 -07004524#ifdef CONFIG_HOTPLUG_CPU
4525static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4526 void *hcpu)
4527{
NeilBrownd1688a62011-10-11 16:49:52 +11004528 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07004529 long cpu = (long)hcpu;
4530 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4531
4532 switch (action) {
4533 case CPU_UP_PREPARE:
4534 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07004535 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07004536 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004537 if (!percpu->scribble)
4538 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4539
4540 if (!percpu->scribble ||
4541 (conf->level == 6 && !percpu->spare_page)) {
4542 safe_put_page(percpu->spare_page);
4543 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004544 pr_err("%s: failed memory allocation for cpu%ld\n",
4545 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07004546 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07004547 }
4548 break;
4549 case CPU_DEAD:
4550 case CPU_DEAD_FROZEN:
4551 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004552 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004553 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004554 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07004555 break;
4556 default:
4557 break;
4558 }
4559 return NOTIFY_OK;
4560}
4561#endif
4562
NeilBrownd1688a62011-10-11 16:49:52 +11004563static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004564{
4565 unsigned long cpu;
4566 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09004567 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004568 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004569 int err;
4570
Dan Williams36d1c642009-07-14 11:48:22 -07004571 allcpus = alloc_percpu(struct raid5_percpu);
4572 if (!allcpus)
4573 return -ENOMEM;
4574 conf->percpu = allcpus;
4575
4576 get_online_cpus();
4577 err = 0;
4578 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07004579 if (conf->level == 6) {
4580 spare_page = alloc_page(GFP_KERNEL);
4581 if (!spare_page) {
4582 err = -ENOMEM;
4583 break;
4584 }
4585 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
4586 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11004587 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004588 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07004589 err = -ENOMEM;
4590 break;
4591 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07004592 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004593 }
4594#ifdef CONFIG_HOTPLUG_CPU
4595 conf->cpu_notify.notifier_call = raid456_cpu_notify;
4596 conf->cpu_notify.priority = 0;
4597 if (err == 0)
4598 err = register_cpu_notifier(&conf->cpu_notify);
4599#endif
4600 put_online_cpus();
4601
4602 return err;
4603}
4604
NeilBrownd1688a62011-10-11 16:49:52 +11004605static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606{
NeilBrownd1688a62011-10-11 16:49:52 +11004607 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004608 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11004609 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 struct disk_info *disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611
NeilBrown91adb562009-03-31 14:39:39 +11004612 if (mddev->new_level != 5
4613 && mddev->new_level != 4
4614 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10004615 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004616 mdname(mddev), mddev->new_level);
4617 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 }
NeilBrown91adb562009-03-31 14:39:39 +11004619 if ((mddev->new_level == 5
4620 && !algorithm_valid_raid5(mddev->new_layout)) ||
4621 (mddev->new_level == 6
4622 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004623 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11004624 mdname(mddev), mddev->new_layout);
4625 return ERR_PTR(-EIO);
4626 }
4627 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10004628 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004629 mdname(mddev), mddev->raid_disks);
4630 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11004631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632
Andre Noll664e7c42009-06-18 08:45:27 +10004633 if (!mddev->new_chunk_sectors ||
4634 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
4635 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10004636 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
4637 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11004638 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11004639 }
4640
NeilBrownd1688a62011-10-11 16:49:52 +11004641 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11004642 if (conf == NULL)
4643 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11004644 spin_lock_init(&conf->device_lock);
4645 init_waitqueue_head(&conf->wait_for_stripe);
4646 init_waitqueue_head(&conf->wait_for_overlap);
4647 INIT_LIST_HEAD(&conf->handle_list);
4648 INIT_LIST_HEAD(&conf->hold_list);
4649 INIT_LIST_HEAD(&conf->delayed_list);
4650 INIT_LIST_HEAD(&conf->bitmap_list);
4651 INIT_LIST_HEAD(&conf->inactive_list);
4652 atomic_set(&conf->active_stripes, 0);
4653 atomic_set(&conf->preread_active_stripes, 0);
4654 atomic_set(&conf->active_aligned_reads, 0);
4655 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11004656 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11004657
4658 conf->raid_disks = mddev->raid_disks;
4659 if (mddev->reshape_position == MaxSector)
4660 conf->previous_raid_disks = mddev->raid_disks;
4661 else
4662 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004663 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
4664 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11004665
NeilBrown5e5e3e72009-10-16 16:35:30 +11004666 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11004667 GFP_KERNEL);
4668 if (!conf->disks)
4669 goto abort;
4670
4671 conf->mddev = mddev;
4672
4673 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
4674 goto abort;
4675
Dan Williams36d1c642009-07-14 11:48:22 -07004676 conf->level = mddev->new_level;
4677 if (raid5_alloc_percpu(conf) != 0)
4678 goto abort;
4679
NeilBrown0c55e022010-05-03 14:09:02 +10004680 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11004681
4682 list_for_each_entry(rdev, &mddev->disks, same_set) {
4683 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004684 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11004685 || raid_disk < 0)
4686 continue;
4687 disk = conf->disks + raid_disk;
4688
4689 disk->rdev = rdev;
4690
4691 if (test_bit(In_sync, &rdev->flags)) {
4692 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10004693 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
4694 " disk %d\n",
4695 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05004696 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11004697 /* Cannot rely on bitmap to complete recovery */
4698 conf->fullsync = 1;
4699 }
4700
Andre Noll09c9e5f2009-06-18 08:45:55 +10004701 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11004702 conf->level = mddev->new_level;
4703 if (conf->level == 6)
4704 conf->max_degraded = 2;
4705 else
4706 conf->max_degraded = 1;
4707 conf->algorithm = mddev->new_layout;
4708 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11004709 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11004710 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10004711 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11004712 conf->prev_algo = mddev->layout;
4713 }
NeilBrown91adb562009-03-31 14:39:39 +11004714
4715 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11004716 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11004717 if (grow_stripes(conf, conf->max_nr_stripes)) {
4718 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004719 "md/raid:%s: couldn't allocate %dkB for buffers\n",
4720 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004721 goto abort;
4722 } else
NeilBrown0c55e022010-05-03 14:09:02 +10004723 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
4724 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004725
NeilBrown0da3c612009-09-23 18:09:45 +10004726 conf->thread = md_register_thread(raid5d, mddev, NULL);
NeilBrown91adb562009-03-31 14:39:39 +11004727 if (!conf->thread) {
4728 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004729 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11004730 mdname(mddev));
4731 goto abort;
4732 }
4733
4734 return conf;
4735
4736 abort:
4737 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10004738 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11004739 return ERR_PTR(-EIO);
4740 } else
4741 return ERR_PTR(-ENOMEM);
4742}
4743
NeilBrownc148ffd2009-11-13 17:47:00 +11004744
4745static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
4746{
4747 switch (algo) {
4748 case ALGORITHM_PARITY_0:
4749 if (raid_disk < max_degraded)
4750 return 1;
4751 break;
4752 case ALGORITHM_PARITY_N:
4753 if (raid_disk >= raid_disks - max_degraded)
4754 return 1;
4755 break;
4756 case ALGORITHM_PARITY_0_6:
4757 if (raid_disk == 0 ||
4758 raid_disk == raid_disks - 1)
4759 return 1;
4760 break;
4761 case ALGORITHM_LEFT_ASYMMETRIC_6:
4762 case ALGORITHM_RIGHT_ASYMMETRIC_6:
4763 case ALGORITHM_LEFT_SYMMETRIC_6:
4764 case ALGORITHM_RIGHT_SYMMETRIC_6:
4765 if (raid_disk == raid_disks - 1)
4766 return 1;
4767 }
4768 return 0;
4769}
4770
NeilBrownfd01b882011-10-11 16:47:53 +11004771static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11004772{
NeilBrownd1688a62011-10-11 16:49:52 +11004773 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10004774 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11004775 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11004776 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11004777 sector_t reshape_offset = 0;
NeilBrown91adb562009-03-31 14:39:39 +11004778
Andre Noll8c6ac862009-06-18 08:48:06 +10004779 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10004780 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10004781 " -- starting background reconstruction\n",
4782 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004783 if (mddev->reshape_position != MaxSector) {
4784 /* Check that we can continue the reshape.
4785 * Currently only disks can change, it must
4786 * increase, and we must be past the point where
4787 * a stripe over-writes itself
4788 */
4789 sector_t here_new, here_old;
4790 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11004791 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08004792
NeilBrown88ce4932009-03-31 15:24:23 +11004793 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10004794 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08004795 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08004796 mdname(mddev));
4797 return -EINVAL;
4798 }
NeilBrownf6705572006-03-27 01:18:11 -08004799 old_disks = mddev->raid_disks - mddev->delta_disks;
4800 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08004801 * further up in new geometry must map after here in old
4802 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08004803 */
4804 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10004805 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08004806 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004807 printk(KERN_ERR "md/raid:%s: reshape_position not "
4808 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004809 return -EINVAL;
4810 }
NeilBrownc148ffd2009-11-13 17:47:00 +11004811 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08004812 /* here_new is the stripe we will write to */
4813 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10004814 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08004815 (old_disks-max_degraded));
4816 /* here_old is the first stripe that we might need to read
4817 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10004818 if (mddev->delta_disks == 0) {
4819 /* We cannot be sure it is safe to start an in-place
4820 * reshape. It is only safe if user-space if monitoring
4821 * and taking constant backups.
4822 * mdadm always starts a situation like this in
4823 * readonly mode so it can take control before
4824 * allowing any writes. So just check for that.
4825 */
4826 if ((here_new * mddev->new_chunk_sectors !=
4827 here_old * mddev->chunk_sectors) ||
4828 mddev->ro == 0) {
NeilBrown0c55e022010-05-03 14:09:02 +10004829 printk(KERN_ERR "md/raid:%s: in-place reshape must be started"
4830 " in read-only mode - aborting\n",
4831 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10004832 return -EINVAL;
4833 }
4834 } else if (mddev->delta_disks < 0
4835 ? (here_new * mddev->new_chunk_sectors <=
4836 here_old * mddev->chunk_sectors)
4837 : (here_new * mddev->new_chunk_sectors >=
4838 here_old * mddev->chunk_sectors)) {
NeilBrownf6705572006-03-27 01:18:11 -08004839 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10004840 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
4841 "auto-recovery - aborting.\n",
4842 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004843 return -EINVAL;
4844 }
NeilBrown0c55e022010-05-03 14:09:02 +10004845 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
4846 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004847 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08004848 } else {
NeilBrown91adb562009-03-31 14:39:39 +11004849 BUG_ON(mddev->level != mddev->new_level);
4850 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10004851 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11004852 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08004853 }
4854
NeilBrown245f46c2009-03-31 14:39:39 +11004855 if (mddev->private == NULL)
4856 conf = setup_conf(mddev);
4857 else
4858 conf = mddev->private;
4859
NeilBrown91adb562009-03-31 14:39:39 +11004860 if (IS_ERR(conf))
4861 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08004862
NeilBrown91adb562009-03-31 14:39:39 +11004863 mddev->thread = conf->thread;
4864 conf->thread = NULL;
4865 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07004868 * 0 for a fully functional array, 1 or 2 for a degraded array.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 */
NeilBrownc148ffd2009-11-13 17:47:00 +11004870 list_for_each_entry(rdev, &mddev->disks, same_set) {
4871 if (rdev->raid_disk < 0)
4872 continue;
NeilBrown2f115882010-06-17 17:41:03 +10004873 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11004874 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10004875 continue;
4876 }
NeilBrownc148ffd2009-11-13 17:47:00 +11004877 /* This disc is not fully in-sync. However if it
4878 * just stored parity (beyond the recovery_offset),
4879 * when we don't need to be concerned about the
4880 * array being dirty.
4881 * When reshape goes 'backwards', we never have
4882 * partially completed devices, so we only need
4883 * to worry about reshape going forwards.
4884 */
4885 /* Hack because v0.91 doesn't store recovery_offset properly. */
4886 if (mddev->major_version == 0 &&
4887 mddev->minor_version > 90)
4888 rdev->recovery_offset = reshape_offset;
4889
NeilBrownc148ffd2009-11-13 17:47:00 +11004890 if (rdev->recovery_offset < reshape_offset) {
4891 /* We need to check old and new layout */
4892 if (!only_parity(rdev->raid_disk,
4893 conf->algorithm,
4894 conf->raid_disks,
4895 conf->max_degraded))
4896 continue;
4897 }
4898 if (!only_parity(rdev->raid_disk,
4899 conf->prev_algo,
4900 conf->previous_raid_disks,
4901 conf->max_degraded))
4902 continue;
4903 dirty_parity_disks++;
4904 }
NeilBrown91adb562009-03-31 14:39:39 +11004905
NeilBrown908f4fb2011-12-23 10:17:50 +11004906 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907
NeilBrown674806d2010-06-16 17:17:53 +10004908 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10004909 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07004911 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 goto abort;
4913 }
4914
NeilBrown91adb562009-03-31 14:39:39 +11004915 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004916 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11004917 mddev->resync_max_sectors = mddev->dev_sectors;
4918
NeilBrownc148ffd2009-11-13 17:47:00 +11004919 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004921 if (mddev->ok_start_degraded)
4922 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10004923 "md/raid:%s: starting dirty degraded array"
4924 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004925 mdname(mddev));
4926 else {
4927 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004928 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004929 mdname(mddev));
4930 goto abort;
4931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 }
4933
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10004935 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
4936 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11004937 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
4938 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 else
NeilBrown0c55e022010-05-03 14:09:02 +10004940 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
4941 " out of %d devices, algorithm %d\n",
4942 mdname(mddev), conf->level,
4943 mddev->raid_disks - mddev->degraded,
4944 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945
4946 print_raid5_conf(conf);
4947
NeilBrownfef9c612009-03-31 15:16:46 +11004948 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11004949 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08004950 atomic_set(&conf->reshape_stripes, 0);
4951 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4952 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4953 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4954 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4955 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10004956 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08004957 }
4958
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959
4960 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10004961 if (mddev->to_remove == &raid5_attrs_group)
4962 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10004963 else if (mddev->kobj.sd &&
4964 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08004965 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10004966 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08004967 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10004968 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
4969
4970 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10004971 int chunk_size;
NeilBrown4a5add42010-06-01 19:37:28 +10004972 /* read-ahead size must cover two whole stripes, which
4973 * is 2 * (datadisks) * chunksize where 'n' is the
4974 * number of raid devices
4975 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 int data_disks = conf->previous_raid_disks - conf->max_degraded;
4977 int stripe = data_disks *
4978 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
4979 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
4980 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10004981
4982 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10004983
4984 mddev->queue->backing_dev_info.congested_data = mddev;
4985 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10004986
4987 chunk_size = mddev->chunk_sectors << 9;
4988 blk_queue_io_min(mddev->queue, chunk_size);
4989 blk_queue_io_opt(mddev->queue, chunk_size *
4990 (conf->raid_disks - conf->max_degraded));
4991
4992 list_for_each_entry(rdev, &mddev->disks, same_set)
4993 disk_stack_limits(mddev->gendisk, rdev->bdev,
4994 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 }
4996
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 return 0;
4998abort:
NeilBrown01f96c02011-09-21 15:30:20 +10004999 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11005000 print_raid5_conf(conf);
5001 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005003 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 return -EIO;
5005}
5006
NeilBrownfd01b882011-10-11 16:47:53 +11005007static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008{
NeilBrownd1688a62011-10-11 16:49:52 +11005009 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010
NeilBrown01f96c02011-09-21 15:30:20 +10005011 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005012 if (mddev->queue)
5013 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10005014 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005015 mddev->private = NULL;
5016 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 return 0;
5018}
5019
NeilBrownfd01b882011-10-11 16:47:53 +11005020static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021{
NeilBrownd1688a62011-10-11 16:49:52 +11005022 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 int i;
5024
Andre Noll9d8f0362009-06-18 08:45:01 +10005025 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5026 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005027 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 for (i = 0; i < conf->raid_disks; i++)
5029 seq_printf (seq, "%s",
5030 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005031 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033}
5034
NeilBrownd1688a62011-10-11 16:49:52 +11005035static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036{
5037 int i;
5038 struct disk_info *tmp;
5039
NeilBrown0c55e022010-05-03 14:09:02 +10005040 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 if (!conf) {
5042 printk("(conf==NULL)\n");
5043 return;
5044 }
NeilBrown0c55e022010-05-03 14:09:02 +10005045 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5046 conf->raid_disks,
5047 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048
5049 for (i = 0; i < conf->raid_disks; i++) {
5050 char b[BDEVNAME_SIZE];
5051 tmp = conf->disks + i;
5052 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005053 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5054 i, !test_bit(Faulty, &tmp->rdev->flags),
5055 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 }
5057}
5058
NeilBrownfd01b882011-10-11 16:47:53 +11005059static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060{
5061 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005062 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005064 int count = 0;
5065 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066
5067 for (i = 0; i < conf->raid_disks; i++) {
5068 tmp = conf->disks + i;
5069 if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005070 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005071 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005072 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005073 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005074 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 }
5076 }
NeilBrown6b965622010-08-18 11:56:59 +10005077 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005078 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005079 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005081 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082}
5083
NeilBrownb8321b62011-12-23 10:17:51 +11005084static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085{
NeilBrownd1688a62011-10-11 16:49:52 +11005086 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11005088 int number = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 struct disk_info *p = conf->disks + number;
5090
5091 print_raid5_conf(conf);
NeilBrownb8321b62011-12-23 10:17:51 +11005092 if (rdev == p->rdev) {
NeilBrownec32a2b2009-03-31 15:17:38 +11005093 if (number >= conf->raid_disks &&
5094 conf->reshape_progress == MaxSector)
5095 clear_bit(In_sync, &rdev->flags);
5096
NeilBrownb2d444d2005-11-08 21:39:31 -08005097 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 atomic_read(&rdev->nr_pending)) {
5099 err = -EBUSY;
5100 goto abort;
5101 }
NeilBrowndfc70642008-05-23 13:04:39 -07005102 /* Only remove non-faulty devices if recovery
5103 * isn't possible.
5104 */
5105 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7f0da592011-07-28 11:39:22 +10005106 mddev->recovery_disabled != conf->recovery_disabled &&
NeilBrown674806d2010-06-16 17:17:53 +10005107 !has_failed(conf) &&
NeilBrownec32a2b2009-03-31 15:17:38 +11005108 number < conf->raid_disks) {
NeilBrowndfc70642008-05-23 13:04:39 -07005109 err = -EBUSY;
5110 goto abort;
5111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07005113 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 if (atomic_read(&rdev->nr_pending)) {
5115 /* lost the race, try later */
5116 err = -EBUSY;
5117 p->rdev = rdev;
5118 }
5119 }
5120abort:
5121
5122 print_raid5_conf(conf);
5123 return err;
5124}
5125
NeilBrownfd01b882011-10-11 16:47:53 +11005126static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127{
NeilBrownd1688a62011-10-11 16:49:52 +11005128 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005129 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 int disk;
5131 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005132 int first = 0;
5133 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134
NeilBrown7f0da592011-07-28 11:39:22 +10005135 if (mddev->recovery_disabled == conf->recovery_disabled)
5136 return -EBUSY;
5137
NeilBrown674806d2010-06-16 17:17:53 +10005138 if (has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005140 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141
Neil Brown6c2fce22008-06-28 08:31:31 +10005142 if (rdev->raid_disk >= 0)
5143 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144
5145 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005146 * find the disk ... but prefer rdev->saved_raid_disk
5147 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005149 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005150 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005151 conf->disks[rdev->saved_raid_disk].rdev == NULL)
5152 disk = rdev->saved_raid_disk;
5153 else
Neil Brown6c2fce22008-06-28 08:31:31 +10005154 disk = first;
5155 for ( ; disk <= last ; disk++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 if ((p=conf->disks + disk)->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005157 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005159 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005160 if (rdev->saved_raid_disk != disk)
5161 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005162 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 break;
5164 }
5165 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005166 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167}
5168
NeilBrownfd01b882011-10-11 16:47:53 +11005169static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170{
5171 /* no resync is happening, and there is enough space
5172 * on all devices, so we can resize.
5173 * We need to make sure resync covers any new space.
5174 * If the array is shrinking we should possibly wait until
5175 * any io in the removed space completes, but it hardly seems
5176 * worth it.
5177 */
Andre Noll9d8f0362009-06-18 08:45:01 +10005178 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Dan Williams1f403622009-03-31 14:59:03 +11005179 md_set_array_sectors(mddev, raid5_size(mddev, sectors,
5180 mddev->raid_disks));
Dan Williamsb522adc2009-03-31 15:00:31 +11005181 if (mddev->array_sectors >
5182 raid5_size(mddev, sectors, mddev->raid_disks))
5183 return -EINVAL;
Andre Nollf233ea52008-07-21 17:05:22 +10005184 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005185 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005186 if (sectors > mddev->dev_sectors &&
5187 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005188 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5190 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005191 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005192 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 return 0;
5194}
5195
NeilBrownfd01b882011-10-11 16:47:53 +11005196static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005197{
5198 /* Can only proceed if there are plenty of stripe_heads.
5199 * We need a minimum of one full stripe,, and for sensible progress
5200 * it is best to have about 4 times that.
5201 * If we require 4 times, then the default 256 4K stripe_heads will
5202 * allow for chunk sizes up to 256K, which is probably OK.
5203 * If the chunk size is greater, user-space should request more
5204 * stripe_heads first.
5205 */
NeilBrownd1688a62011-10-11 16:49:52 +11005206 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005207 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5208 > conf->max_nr_stripes ||
5209 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5210 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005211 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5212 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005213 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5214 / STRIPE_SIZE)*4);
5215 return 0;
5216 }
5217 return 1;
5218}
5219
NeilBrownfd01b882011-10-11 16:47:53 +11005220static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005221{
NeilBrownd1688a62011-10-11 16:49:52 +11005222 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005223
NeilBrown88ce4932009-03-31 15:24:23 +11005224 if (mddev->delta_disks == 0 &&
5225 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005226 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005227 return 0; /* nothing to do */
NeilBrowndba034e2008-08-05 15:54:13 +10005228 if (mddev->bitmap)
5229 /* Cannot grow a bitmap yet */
5230 return -EBUSY;
NeilBrown674806d2010-06-16 17:17:53 +10005231 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005232 return -EINVAL;
5233 if (mddev->delta_disks < 0) {
5234 /* We might be able to shrink, but the devices must
5235 * be made bigger first.
5236 * For raid6, 4 is the minimum size.
5237 * Otherwise 2 is the minimum
5238 */
5239 int min = 2;
5240 if (mddev->level == 6)
5241 min = 4;
5242 if (mddev->raid_disks + mddev->delta_disks < min)
5243 return -EINVAL;
5244 }
NeilBrown29269552006-03-27 01:18:10 -08005245
NeilBrown01ee22b2009-06-18 08:47:20 +10005246 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005247 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005248
NeilBrownec32a2b2009-03-31 15:17:38 +11005249 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
NeilBrown63c70c42006-03-27 01:18:13 -08005250}
5251
NeilBrownfd01b882011-10-11 16:47:53 +11005252static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005253{
NeilBrownd1688a62011-10-11 16:49:52 +11005254 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005255 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005256 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005257 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005258
NeilBrownf4168852007-02-28 20:11:53 -08005259 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005260 return -EBUSY;
5261
NeilBrown01ee22b2009-06-18 08:47:20 +10005262 if (!check_stripe_cache(mddev))
5263 return -ENOSPC;
5264
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005265 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown469518a2011-01-31 11:57:43 +11005266 if (!test_bit(In_sync, &rdev->flags)
5267 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005268 spares++;
NeilBrown63c70c42006-03-27 01:18:13 -08005269
NeilBrownf4168852007-02-28 20:11:53 -08005270 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005271 /* Not enough devices even to make a degraded array
5272 * of that size
5273 */
5274 return -EINVAL;
5275
NeilBrownec32a2b2009-03-31 15:17:38 +11005276 /* Refuse to reduce size of the array. Any reductions in
5277 * array size must be through explicit setting of array_size
5278 * attribute.
5279 */
5280 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5281 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10005282 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11005283 "before number of disks\n", mdname(mddev));
5284 return -EINVAL;
5285 }
5286
NeilBrownf6705572006-03-27 01:18:11 -08005287 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005288 spin_lock_irq(&conf->device_lock);
5289 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005290 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005291 conf->prev_chunk_sectors = conf->chunk_sectors;
5292 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005293 conf->prev_algo = conf->algorithm;
5294 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11005295 if (mddev->delta_disks < 0)
5296 conf->reshape_progress = raid5_size(mddev, 0, 0);
5297 else
5298 conf->reshape_progress = 0;
5299 conf->reshape_safe = conf->reshape_progress;
NeilBrown86b42c72009-03-31 15:19:03 +11005300 conf->generation++;
NeilBrown29269552006-03-27 01:18:10 -08005301 spin_unlock_irq(&conf->device_lock);
5302
5303 /* Add some new drives, as many as will fit.
5304 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10005305 * Don't add devices if we are reducing the number of
5306 * devices in the array. This is because it is not possible
5307 * to correctly record the "partially reconstructed" state of
5308 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08005309 */
NeilBrown87a8dec2011-01-31 11:57:43 +11005310 if (mddev->delta_disks >= 0) {
5311 int added_devices = 0;
5312 list_for_each_entry(rdev, &mddev->disks, same_set)
5313 if (rdev->raid_disk < 0 &&
5314 !test_bit(Faulty, &rdev->flags)) {
5315 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11005316 if (rdev->raid_disk
5317 >= conf->previous_raid_disks) {
5318 set_bit(In_sync, &rdev->flags);
5319 added_devices++;
5320 } else
5321 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10005322
5323 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11005324 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11005325 }
NeilBrown87a8dec2011-01-31 11:57:43 +11005326 } else if (rdev->raid_disk >= conf->previous_raid_disks
5327 && !test_bit(Faulty, &rdev->flags)) {
5328 /* This is a spare that was manually added */
5329 set_bit(In_sync, &rdev->flags);
5330 added_devices++;
5331 }
NeilBrown29269552006-03-27 01:18:10 -08005332
NeilBrown87a8dec2011-01-31 11:57:43 +11005333 /* When a reshape changes the number of devices,
5334 * ->degraded is measured against the larger of the
5335 * pre and post number of devices.
5336 */
NeilBrownec32a2b2009-03-31 15:17:38 +11005337 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005338 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11005339 spin_unlock_irqrestore(&conf->device_lock, flags);
5340 }
NeilBrown63c70c42006-03-27 01:18:13 -08005341 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10005342 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07005343 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005344
NeilBrown29269552006-03-27 01:18:10 -08005345 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5346 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5347 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5348 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5349 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005350 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08005351 if (!mddev->sync_thread) {
5352 mddev->recovery = 0;
5353 spin_lock_irq(&conf->device_lock);
5354 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005355 conf->reshape_progress = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005356 spin_unlock_irq(&conf->device_lock);
5357 return -EAGAIN;
5358 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005359 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005360 md_wakeup_thread(mddev->sync_thread);
5361 md_new_event(mddev);
5362 return 0;
5363}
NeilBrown29269552006-03-27 01:18:10 -08005364
NeilBrownec32a2b2009-03-31 15:17:38 +11005365/* This is called from the reshape thread and should make any
5366 * changes needed in 'conf'
5367 */
NeilBrownd1688a62011-10-11 16:49:52 +11005368static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08005369{
NeilBrown29269552006-03-27 01:18:10 -08005370
NeilBrownf6705572006-03-27 01:18:11 -08005371 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
Dan Williams80c3a6c2009-03-17 18:10:40 -07005372
NeilBrownf6705572006-03-27 01:18:11 -08005373 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11005374 conf->previous_raid_disks = conf->raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005375 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08005376 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005377 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07005378
5379 /* read-ahead size must cover two whole stripes, which is
5380 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5381 */
NeilBrown4a5add42010-06-01 19:37:28 +10005382 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11005383 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005384 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11005385 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07005386 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5387 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5388 }
NeilBrown29269552006-03-27 01:18:10 -08005389 }
NeilBrown29269552006-03-27 01:18:10 -08005390}
5391
NeilBrownec32a2b2009-03-31 15:17:38 +11005392/* This is called from the raid5d thread with mddev_lock held.
5393 * It makes config changes to the device.
5394 */
NeilBrownfd01b882011-10-11 16:47:53 +11005395static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11005396{
NeilBrownd1688a62011-10-11 16:49:52 +11005397 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11005398
5399 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5400
NeilBrownec32a2b2009-03-31 15:17:38 +11005401 if (mddev->delta_disks > 0) {
5402 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5403 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005404 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11005405 } else {
5406 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11005407 spin_lock_irq(&conf->device_lock);
5408 mddev->degraded = calc_degraded(conf);
5409 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11005410 for (d = conf->raid_disks ;
5411 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10005412 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11005413 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownb8321b62011-12-23 10:17:51 +11005414 if (rdev &&
5415 raid5_remove_disk(mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10005416 sysfs_unlink_rdev(mddev, rdev);
NeilBrown1a67dde2009-08-13 10:41:49 +10005417 rdev->raid_disk = -1;
5418 }
5419 }
NeilBrowncea9c222009-03-31 15:15:05 +11005420 }
NeilBrown88ce4932009-03-31 15:24:23 +11005421 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005422 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11005423 mddev->reshape_position = MaxSector;
5424 mddev->delta_disks = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11005425 }
5426}
5427
NeilBrownfd01b882011-10-11 16:47:53 +11005428static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07005429{
NeilBrownd1688a62011-10-11 16:49:52 +11005430 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07005431
5432 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08005433 case 2: /* resume for a suspend */
5434 wake_up(&conf->wait_for_overlap);
5435 break;
5436
NeilBrown72626682005-09-09 16:23:54 -07005437 case 1: /* stop all writes */
5438 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005439 /* '2' tells resync/reshape to pause so that all
5440 * active stripes can drain
5441 */
5442 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07005443 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005444 atomic_read(&conf->active_stripes) == 0 &&
5445 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07005446 conf->device_lock, /* nothing */);
NeilBrown64bd6602009-08-03 10:59:58 +10005447 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07005448 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005449 /* allow reshape to continue */
5450 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005451 break;
5452
5453 case 0: /* re-enable writes */
5454 spin_lock_irq(&conf->device_lock);
5455 conf->quiesce = 0;
5456 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08005457 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005458 spin_unlock_irq(&conf->device_lock);
5459 break;
5460 }
NeilBrown72626682005-09-09 16:23:54 -07005461}
NeilBrownb15c2e52006-01-06 00:20:16 -08005462
NeilBrownd562b0c2009-03-31 14:39:39 +11005463
NeilBrownfd01b882011-10-11 16:47:53 +11005464static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11005465{
NeilBrowne373ab12011-10-11 16:48:59 +11005466 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07005467 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11005468
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005469 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11005470 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10005471 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
5472 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005473 return ERR_PTR(-EINVAL);
5474 }
5475
NeilBrowne373ab12011-10-11 16:48:59 +11005476 sectors = raid0_conf->strip_zone[0].zone_end;
5477 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10005478 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005479 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11005480 mddev->new_layout = ALGORITHM_PARITY_N;
5481 mddev->new_chunk_sectors = mddev->chunk_sectors;
5482 mddev->raid_disks += 1;
5483 mddev->delta_disks = 1;
5484 /* make sure it will be not marked as dirty */
5485 mddev->recovery_cp = MaxSector;
5486
5487 return setup_conf(mddev);
5488}
5489
5490
NeilBrownfd01b882011-10-11 16:47:53 +11005491static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005492{
5493 int chunksect;
5494
5495 if (mddev->raid_disks != 2 ||
5496 mddev->degraded > 1)
5497 return ERR_PTR(-EINVAL);
5498
5499 /* Should check if there are write-behind devices? */
5500
5501 chunksect = 64*2; /* 64K by default */
5502
5503 /* The array must be an exact multiple of chunksize */
5504 while (chunksect && (mddev->array_sectors & (chunksect-1)))
5505 chunksect >>= 1;
5506
5507 if ((chunksect<<9) < STRIPE_SIZE)
5508 /* array size does not allow a suitable chunk size */
5509 return ERR_PTR(-EINVAL);
5510
5511 mddev->new_level = 5;
5512 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10005513 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11005514
5515 return setup_conf(mddev);
5516}
5517
NeilBrownfd01b882011-10-11 16:47:53 +11005518static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11005519{
5520 int new_layout;
5521
5522 switch (mddev->layout) {
5523 case ALGORITHM_LEFT_ASYMMETRIC_6:
5524 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
5525 break;
5526 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5527 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
5528 break;
5529 case ALGORITHM_LEFT_SYMMETRIC_6:
5530 new_layout = ALGORITHM_LEFT_SYMMETRIC;
5531 break;
5532 case ALGORITHM_RIGHT_SYMMETRIC_6:
5533 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
5534 break;
5535 case ALGORITHM_PARITY_0_6:
5536 new_layout = ALGORITHM_PARITY_0;
5537 break;
5538 case ALGORITHM_PARITY_N:
5539 new_layout = ALGORITHM_PARITY_N;
5540 break;
5541 default:
5542 return ERR_PTR(-EINVAL);
5543 }
5544 mddev->new_level = 5;
5545 mddev->new_layout = new_layout;
5546 mddev->delta_disks = -1;
5547 mddev->raid_disks -= 1;
5548 return setup_conf(mddev);
5549}
5550
NeilBrownd562b0c2009-03-31 14:39:39 +11005551
NeilBrownfd01b882011-10-11 16:47:53 +11005552static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11005553{
NeilBrown88ce4932009-03-31 15:24:23 +11005554 /* For a 2-drive array, the layout and chunk size can be changed
5555 * immediately as not restriping is needed.
5556 * For larger arrays we record the new value - after validation
5557 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11005558 */
NeilBrownd1688a62011-10-11 16:49:52 +11005559 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10005560 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11005561
NeilBrown597a7112009-06-18 08:47:42 +10005562 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11005563 return -EINVAL;
5564 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005565 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11005566 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005567 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11005568 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005569 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11005570 /* not factor of array size */
5571 return -EINVAL;
5572 }
5573
5574 /* They look valid */
5575
NeilBrown88ce4932009-03-31 15:24:23 +11005576 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10005577 /* can make the change immediately */
5578 if (mddev->new_layout >= 0) {
5579 conf->algorithm = mddev->new_layout;
5580 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11005581 }
5582 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10005583 conf->chunk_sectors = new_chunk ;
5584 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11005585 }
5586 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5587 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11005588 }
NeilBrown50ac1682009-06-18 08:47:55 +10005589 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11005590}
5591
NeilBrownfd01b882011-10-11 16:47:53 +11005592static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11005593{
NeilBrown597a7112009-06-18 08:47:42 +10005594 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10005595
NeilBrown597a7112009-06-18 08:47:42 +10005596 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11005597 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005598 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005599 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11005600 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005601 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11005602 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005603 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11005604 /* not factor of array size */
5605 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005606 }
NeilBrown88ce4932009-03-31 15:24:23 +11005607
5608 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10005609 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11005610}
5611
NeilBrownfd01b882011-10-11 16:47:53 +11005612static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005613{
5614 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005615 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005616 * raid1 - if there are two drives. We need to know the chunk size
5617 * raid4 - trivial - just use a raid4 layout.
5618 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005619 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005620 if (mddev->level == 0)
5621 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11005622 if (mddev->level == 1)
5623 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11005624 if (mddev->level == 4) {
5625 mddev->new_layout = ALGORITHM_PARITY_N;
5626 mddev->new_level = 5;
5627 return setup_conf(mddev);
5628 }
NeilBrownfc9739c2009-03-31 14:57:20 +11005629 if (mddev->level == 6)
5630 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11005631
5632 return ERR_PTR(-EINVAL);
5633}
5634
NeilBrownfd01b882011-10-11 16:47:53 +11005635static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11005636{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005637 /* raid4 can take over:
5638 * raid0 - if there is only one strip zone
5639 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11005640 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005641 if (mddev->level == 0)
5642 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11005643 if (mddev->level == 5 &&
5644 mddev->layout == ALGORITHM_PARITY_N) {
5645 mddev->new_layout = 0;
5646 mddev->new_level = 4;
5647 return setup_conf(mddev);
5648 }
5649 return ERR_PTR(-EINVAL);
5650}
NeilBrownd562b0c2009-03-31 14:39:39 +11005651
NeilBrown84fc4b52011-10-11 16:49:58 +11005652static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11005653
NeilBrownfd01b882011-10-11 16:47:53 +11005654static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11005655{
5656 /* Currently can only take over a raid5. We map the
5657 * personality to an equivalent raid6 personality
5658 * with the Q block at the end.
5659 */
5660 int new_layout;
5661
5662 if (mddev->pers != &raid5_personality)
5663 return ERR_PTR(-EINVAL);
5664 if (mddev->degraded > 1)
5665 return ERR_PTR(-EINVAL);
5666 if (mddev->raid_disks > 253)
5667 return ERR_PTR(-EINVAL);
5668 if (mddev->raid_disks < 3)
5669 return ERR_PTR(-EINVAL);
5670
5671 switch (mddev->layout) {
5672 case ALGORITHM_LEFT_ASYMMETRIC:
5673 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
5674 break;
5675 case ALGORITHM_RIGHT_ASYMMETRIC:
5676 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
5677 break;
5678 case ALGORITHM_LEFT_SYMMETRIC:
5679 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
5680 break;
5681 case ALGORITHM_RIGHT_SYMMETRIC:
5682 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
5683 break;
5684 case ALGORITHM_PARITY_0:
5685 new_layout = ALGORITHM_PARITY_0_6;
5686 break;
5687 case ALGORITHM_PARITY_N:
5688 new_layout = ALGORITHM_PARITY_N;
5689 break;
5690 default:
5691 return ERR_PTR(-EINVAL);
5692 }
5693 mddev->new_level = 6;
5694 mddev->new_layout = new_layout;
5695 mddev->delta_disks = 1;
5696 mddev->raid_disks += 1;
5697 return setup_conf(mddev);
5698}
5699
5700
NeilBrown84fc4b52011-10-11 16:49:58 +11005701static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07005702{
5703 .name = "raid6",
5704 .level = 6,
5705 .owner = THIS_MODULE,
5706 .make_request = make_request,
5707 .run = run,
5708 .stop = stop,
5709 .status = status,
5710 .error_handler = error,
5711 .hot_add_disk = raid5_add_disk,
5712 .hot_remove_disk= raid5_remove_disk,
5713 .spare_active = raid5_spare_active,
5714 .sync_request = sync_request,
5715 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005716 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10005717 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08005718 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005719 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07005720 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11005721 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07005722};
NeilBrown84fc4b52011-10-11 16:49:58 +11005723static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724{
5725 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08005726 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 .owner = THIS_MODULE,
5728 .make_request = make_request,
5729 .run = run,
5730 .stop = stop,
5731 .status = status,
5732 .error_handler = error,
5733 .hot_add_disk = raid5_add_disk,
5734 .hot_remove_disk= raid5_remove_disk,
5735 .spare_active = raid5_spare_active,
5736 .sync_request = sync_request,
5737 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005738 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08005739 .check_reshape = raid5_check_reshape,
5740 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005741 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07005742 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11005743 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744};
5745
NeilBrown84fc4b52011-10-11 16:49:58 +11005746static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747{
NeilBrown2604b702006-01-06 00:20:36 -08005748 .name = "raid4",
5749 .level = 4,
5750 .owner = THIS_MODULE,
5751 .make_request = make_request,
5752 .run = run,
5753 .stop = stop,
5754 .status = status,
5755 .error_handler = error,
5756 .hot_add_disk = raid5_add_disk,
5757 .hot_remove_disk= raid5_remove_disk,
5758 .spare_active = raid5_spare_active,
5759 .sync_request = sync_request,
5760 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005761 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08005762 .check_reshape = raid5_check_reshape,
5763 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005764 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08005765 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11005766 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08005767};
5768
5769static int __init raid5_init(void)
5770{
NeilBrown16a53ec2006-06-26 00:27:38 -07005771 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08005772 register_md_personality(&raid5_personality);
5773 register_md_personality(&raid4_personality);
5774 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775}
5776
NeilBrown2604b702006-01-06 00:20:36 -08005777static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778{
NeilBrown16a53ec2006-06-26 00:27:38 -07005779 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08005780 unregister_md_personality(&raid5_personality);
5781 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782}
5783
5784module_init(raid5_init);
5785module_exit(raid5_exit);
5786MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11005787MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08005789MODULE_ALIAS("md-raid5");
5790MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08005791MODULE_ALIAS("md-level-5");
5792MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07005793MODULE_ALIAS("md-personality-8"); /* RAID6 */
5794MODULE_ALIAS("md-raid6");
5795MODULE_ALIAS("md-level-6");
5796
5797/* This used to be two separate modules, they were: */
5798MODULE_ALIAS("raid5");
5799MODULE_ALIAS("raid6");