blob: 516baf49a1fa6537259c791ec97c801e9728dc10 [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;
NeilBrown9a3e1102011-12-23 10:17:53 +1100506 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100507 struct bio *bi, *rbi;
508 struct md_rdev *rdev, *rrdev = NULL;
Tejun Heoe9c74692010-09-03 11:56:18 +0200509 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
510 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
511 rw = WRITE_FUA;
512 else
513 rw = WRITE;
514 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700515 rw = READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100516 else if (test_and_clear_bit(R5_WantReplace,
517 &sh->dev[i].flags)) {
518 rw = WRITE;
519 replace_only = 1;
520 } else
Dan Williams91c00922007-01-02 13:52:30 -0700521 continue;
522
523 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100524 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700525
526 bi->bi_rw = rw;
NeilBrown977df362011-12-23 10:17:53 +1100527 rbi->bi_rw = rw;
528 if (rw & WRITE) {
Dan Williams91c00922007-01-02 13:52:30 -0700529 bi->bi_end_io = raid5_end_write_request;
NeilBrown977df362011-12-23 10:17:53 +1100530 rbi->bi_end_io = raid5_end_write_request;
531 } else
Dan Williams91c00922007-01-02 13:52:30 -0700532 bi->bi_end_io = raid5_end_read_request;
533
534 rcu_read_lock();
NeilBrown977df362011-12-23 10:17:53 +1100535 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown9a3e1102011-12-23 10:17:53 +1100536 rrdev = rcu_dereference(conf->disks[i].replacement);
537 if (rw & WRITE) {
538 if (replace_only)
539 rdev = NULL;
540 } else {
541 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
542 rdev = rrdev;
543 rrdev = NULL;
544 }
NeilBrown977df362011-12-23 10:17:53 +1100545
Dan Williams91c00922007-01-02 13:52:30 -0700546 if (rdev && test_bit(Faulty, &rdev->flags))
547 rdev = NULL;
548 if (rdev)
549 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100550 if (rrdev && test_bit(Faulty, &rrdev->flags))
551 rrdev = NULL;
552 if (rrdev)
553 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700554 rcu_read_unlock();
555
NeilBrown73e92e52011-07-28 11:39:22 +1000556 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100557 * need to check for writes. We never accept write errors
558 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000559 */
560 while ((rw & WRITE) && rdev &&
561 test_bit(WriteErrorSeen, &rdev->flags)) {
562 sector_t first_bad;
563 int bad_sectors;
564 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
565 &first_bad, &bad_sectors);
566 if (!bad)
567 break;
568
569 if (bad < 0) {
570 set_bit(BlockedBadBlocks, &rdev->flags);
571 if (!conf->mddev->external &&
572 conf->mddev->flags) {
573 /* It is very unlikely, but we might
574 * still need to write out the
575 * bad block log - better give it
576 * a chance*/
577 md_check_recovery(conf->mddev);
578 }
579 md_wait_for_blocked_rdev(rdev, conf->mddev);
580 } else {
581 /* Acknowledged bad block - skip the write */
582 rdev_dec_pending(rdev, conf->mddev);
583 rdev = NULL;
584 }
585 }
586
Dan Williams91c00922007-01-02 13:52:30 -0700587 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100588 if (s->syncing || s->expanding || s->expanded
589 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700590 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
591
Dan Williams2b7497f2008-06-28 08:31:52 +1000592 set_bit(STRIPE_IO_STARTED, &sh->state);
593
Dan Williams91c00922007-01-02 13:52:30 -0700594 bi->bi_bdev = rdev->bdev;
595 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700596 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700597 bi->bi_rw, i);
598 atomic_inc(&sh->count);
599 bi->bi_sector = sh->sector + rdev->data_offset;
600 bi->bi_flags = 1 << BIO_UPTODATE;
Dan Williams91c00922007-01-02 13:52:30 -0700601 bi->bi_idx = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700602 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
603 bi->bi_io_vec[0].bv_offset = 0;
604 bi->bi_size = STRIPE_SIZE;
605 bi->bi_next = NULL;
NeilBrown977df362011-12-23 10:17:53 +1100606 if (rrdev)
607 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Dan Williams91c00922007-01-02 13:52:30 -0700608 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100609 }
610 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100611 if (s->syncing || s->expanding || s->expanded
612 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +1100613 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
614
615 set_bit(STRIPE_IO_STARTED, &sh->state);
616
617 rbi->bi_bdev = rrdev->bdev;
618 pr_debug("%s: for %llu schedule op %ld on "
619 "replacement disc %d\n",
620 __func__, (unsigned long long)sh->sector,
621 rbi->bi_rw, i);
622 atomic_inc(&sh->count);
623 rbi->bi_sector = sh->sector + rrdev->data_offset;
624 rbi->bi_flags = 1 << BIO_UPTODATE;
625 rbi->bi_idx = 0;
626 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
627 rbi->bi_io_vec[0].bv_offset = 0;
628 rbi->bi_size = STRIPE_SIZE;
629 rbi->bi_next = NULL;
630 generic_make_request(rbi);
631 }
632 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000633 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700634 set_bit(STRIPE_DEGRADED, &sh->state);
635 pr_debug("skip op %ld on disc %d for sector %llu\n",
636 bi->bi_rw, i, (unsigned long long)sh->sector);
637 clear_bit(R5_LOCKED, &sh->dev[i].flags);
638 set_bit(STRIPE_HANDLE, &sh->state);
639 }
640 }
641}
642
643static struct dma_async_tx_descriptor *
644async_copy_data(int frombio, struct bio *bio, struct page *page,
645 sector_t sector, struct dma_async_tx_descriptor *tx)
646{
647 struct bio_vec *bvl;
648 struct page *bio_page;
649 int i;
650 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700651 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700652 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700653
654 if (bio->bi_sector >= sector)
655 page_offset = (signed)(bio->bi_sector - sector) * 512;
656 else
657 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700658
Dan Williams0403e382009-09-08 17:42:50 -0700659 if (frombio)
660 flags |= ASYNC_TX_FENCE;
661 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
662
Dan Williams91c00922007-01-02 13:52:30 -0700663 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000664 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700665 int clen;
666 int b_offset = 0;
667
668 if (page_offset < 0) {
669 b_offset = -page_offset;
670 page_offset += b_offset;
671 len -= b_offset;
672 }
673
674 if (len > 0 && page_offset + len > STRIPE_SIZE)
675 clen = STRIPE_SIZE - page_offset;
676 else
677 clen = len;
678
679 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000680 b_offset += bvl->bv_offset;
681 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700682 if (frombio)
683 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700684 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700685 else
686 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700687 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700688 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700689 /* chain the operations */
690 submit.depend_tx = tx;
691
Dan Williams91c00922007-01-02 13:52:30 -0700692 if (clen < len) /* hit end of page */
693 break;
694 page_offset += len;
695 }
696
697 return tx;
698}
699
700static void ops_complete_biofill(void *stripe_head_ref)
701{
702 struct stripe_head *sh = stripe_head_ref;
703 struct bio *return_bi = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +1100704 struct r5conf *conf = sh->raid_conf;
Dan Williamse4d84902007-09-24 10:06:13 -0700705 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700706
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700707 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700708 (unsigned long long)sh->sector);
709
710 /* clear completed biofills */
Dan Williams83de75c2008-06-28 08:31:58 +1000711 spin_lock_irq(&conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700712 for (i = sh->disks; i--; ) {
713 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700714
715 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700716 /* and check if we need to reply to a read request,
717 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000718 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700719 */
720 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700721 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700722
Dan Williams91c00922007-01-02 13:52:30 -0700723 BUG_ON(!dev->read);
724 rbi = dev->read;
725 dev->read = NULL;
726 while (rbi && rbi->bi_sector <
727 dev->sector + STRIPE_SECTORS) {
728 rbi2 = r5_next_bio(rbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +0200729 if (!raid5_dec_bi_phys_segments(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700730 rbi->bi_next = return_bi;
731 return_bi = rbi;
732 }
Dan Williams91c00922007-01-02 13:52:30 -0700733 rbi = rbi2;
734 }
735 }
736 }
Dan Williams83de75c2008-06-28 08:31:58 +1000737 spin_unlock_irq(&conf->device_lock);
738 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700739
740 return_io(return_bi);
741
Dan Williamse4d84902007-09-24 10:06:13 -0700742 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700743 release_stripe(sh);
744}
745
746static void ops_run_biofill(struct stripe_head *sh)
747{
748 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +1100749 struct r5conf *conf = sh->raid_conf;
Dan Williamsa08abd82009-06-03 11:43:59 -0700750 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700751 int i;
752
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700753 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700754 (unsigned long long)sh->sector);
755
756 for (i = sh->disks; i--; ) {
757 struct r5dev *dev = &sh->dev[i];
758 if (test_bit(R5_Wantfill, &dev->flags)) {
759 struct bio *rbi;
760 spin_lock_irq(&conf->device_lock);
761 dev->read = rbi = dev->toread;
762 dev->toread = NULL;
763 spin_unlock_irq(&conf->device_lock);
764 while (rbi && rbi->bi_sector <
765 dev->sector + STRIPE_SECTORS) {
766 tx = async_copy_data(0, rbi, dev->page,
767 dev->sector, tx);
768 rbi = r5_next_bio(rbi, dev->sector);
769 }
770 }
771 }
772
773 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700774 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
775 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700776}
777
Dan Williams4e7d2c02009-08-29 19:13:11 -0700778static void mark_target_uptodate(struct stripe_head *sh, int target)
779{
780 struct r5dev *tgt;
781
782 if (target < 0)
783 return;
784
785 tgt = &sh->dev[target];
786 set_bit(R5_UPTODATE, &tgt->flags);
787 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
788 clear_bit(R5_Wantcompute, &tgt->flags);
789}
790
Dan Williamsac6b53b2009-07-14 13:40:19 -0700791static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700792{
793 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700794
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700795 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700796 (unsigned long long)sh->sector);
797
Dan Williamsac6b53b2009-07-14 13:40:19 -0700798 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700799 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700800 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700801
Dan Williamsecc65c92008-06-28 08:31:57 +1000802 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
803 if (sh->check_state == check_state_compute_run)
804 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700805 set_bit(STRIPE_HANDLE, &sh->state);
806 release_stripe(sh);
807}
808
Dan Williamsd6f38f32009-07-14 11:50:52 -0700809/* return a pointer to the address conversion region of the scribble buffer */
810static addr_conv_t *to_addr_conv(struct stripe_head *sh,
811 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700812{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700813 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
814}
815
816static struct dma_async_tx_descriptor *
817ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
818{
Dan Williams91c00922007-01-02 13:52:30 -0700819 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700820 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700821 int target = sh->ops.target;
822 struct r5dev *tgt = &sh->dev[target];
823 struct page *xor_dest = tgt->page;
824 int count = 0;
825 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700826 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700827 int i;
828
829 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700830 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700831 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
832
833 for (i = disks; i--; )
834 if (i != target)
835 xor_srcs[count++] = sh->dev[i].page;
836
837 atomic_inc(&sh->count);
838
Dan Williams0403e382009-09-08 17:42:50 -0700839 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700840 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700841 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700842 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700843 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700844 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700845
Dan Williams91c00922007-01-02 13:52:30 -0700846 return tx;
847}
848
Dan Williamsac6b53b2009-07-14 13:40:19 -0700849/* set_syndrome_sources - populate source buffers for gen_syndrome
850 * @srcs - (struct page *) array of size sh->disks
851 * @sh - stripe_head to parse
852 *
853 * Populates srcs in proper layout order for the stripe and returns the
854 * 'count' of sources to be used in a call to async_gen_syndrome. The P
855 * destination buffer is recorded in srcs[count] and the Q destination
856 * is recorded in srcs[count+1]].
857 */
858static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
859{
860 int disks = sh->disks;
861 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
862 int d0_idx = raid6_d0(sh);
863 int count;
864 int i;
865
866 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100867 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700868
869 count = 0;
870 i = d0_idx;
871 do {
872 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
873
874 srcs[slot] = sh->dev[i].page;
875 i = raid6_next_disk(i, disks);
876 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700877
NeilBrowne4424fe2009-10-16 16:27:34 +1100878 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700879}
880
881static struct dma_async_tx_descriptor *
882ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
883{
884 int disks = sh->disks;
885 struct page **blocks = percpu->scribble;
886 int target;
887 int qd_idx = sh->qd_idx;
888 struct dma_async_tx_descriptor *tx;
889 struct async_submit_ctl submit;
890 struct r5dev *tgt;
891 struct page *dest;
892 int i;
893 int count;
894
895 if (sh->ops.target < 0)
896 target = sh->ops.target2;
897 else if (sh->ops.target2 < 0)
898 target = sh->ops.target;
899 else
900 /* we should only have one valid target */
901 BUG();
902 BUG_ON(target < 0);
903 pr_debug("%s: stripe %llu block: %d\n",
904 __func__, (unsigned long long)sh->sector, target);
905
906 tgt = &sh->dev[target];
907 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
908 dest = tgt->page;
909
910 atomic_inc(&sh->count);
911
912 if (target == qd_idx) {
913 count = set_syndrome_sources(blocks, sh);
914 blocks[count] = NULL; /* regenerating p is not necessary */
915 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700916 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
917 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700918 to_addr_conv(sh, percpu));
919 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
920 } else {
921 /* Compute any data- or p-drive using XOR */
922 count = 0;
923 for (i = disks; i-- ; ) {
924 if (i == target || i == qd_idx)
925 continue;
926 blocks[count++] = sh->dev[i].page;
927 }
928
Dan Williams0403e382009-09-08 17:42:50 -0700929 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
930 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700931 to_addr_conv(sh, percpu));
932 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
933 }
934
935 return tx;
936}
937
938static struct dma_async_tx_descriptor *
939ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
940{
941 int i, count, disks = sh->disks;
942 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
943 int d0_idx = raid6_d0(sh);
944 int faila = -1, failb = -1;
945 int target = sh->ops.target;
946 int target2 = sh->ops.target2;
947 struct r5dev *tgt = &sh->dev[target];
948 struct r5dev *tgt2 = &sh->dev[target2];
949 struct dma_async_tx_descriptor *tx;
950 struct page **blocks = percpu->scribble;
951 struct async_submit_ctl submit;
952
953 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
954 __func__, (unsigned long long)sh->sector, target, target2);
955 BUG_ON(target < 0 || target2 < 0);
956 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
957 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
958
Dan Williams6c910a72009-09-16 12:24:54 -0700959 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -0700960 * slot number conversion for 'faila' and 'failb'
961 */
962 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100963 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700964 count = 0;
965 i = d0_idx;
966 do {
967 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
968
969 blocks[slot] = sh->dev[i].page;
970
971 if (i == target)
972 faila = slot;
973 if (i == target2)
974 failb = slot;
975 i = raid6_next_disk(i, disks);
976 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700977
978 BUG_ON(faila == failb);
979 if (failb < faila)
980 swap(faila, failb);
981 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
982 __func__, (unsigned long long)sh->sector, faila, failb);
983
984 atomic_inc(&sh->count);
985
986 if (failb == syndrome_disks+1) {
987 /* Q disk is one of the missing disks */
988 if (faila == syndrome_disks) {
989 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -0700990 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
991 ops_complete_compute, sh,
992 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +1100993 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700994 STRIPE_SIZE, &submit);
995 } else {
996 struct page *dest;
997 int data_target;
998 int qd_idx = sh->qd_idx;
999
1000 /* Missing D+Q: recompute D from P, then recompute Q */
1001 if (target == qd_idx)
1002 data_target = target2;
1003 else
1004 data_target = target;
1005
1006 count = 0;
1007 for (i = disks; i-- ; ) {
1008 if (i == data_target || i == qd_idx)
1009 continue;
1010 blocks[count++] = sh->dev[i].page;
1011 }
1012 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001013 init_async_submit(&submit,
1014 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1015 NULL, NULL, NULL,
1016 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001017 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1018 &submit);
1019
1020 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001021 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1022 ops_complete_compute, sh,
1023 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001024 return async_gen_syndrome(blocks, 0, count+2,
1025 STRIPE_SIZE, &submit);
1026 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001027 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001028 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1029 ops_complete_compute, sh,
1030 to_addr_conv(sh, percpu));
1031 if (failb == syndrome_disks) {
1032 /* We're missing D+P. */
1033 return async_raid6_datap_recov(syndrome_disks+2,
1034 STRIPE_SIZE, faila,
1035 blocks, &submit);
1036 } else {
1037 /* We're missing D+D. */
1038 return async_raid6_2data_recov(syndrome_disks+2,
1039 STRIPE_SIZE, faila, failb,
1040 blocks, &submit);
1041 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001042 }
1043}
1044
1045
Dan Williams91c00922007-01-02 13:52:30 -07001046static void ops_complete_prexor(void *stripe_head_ref)
1047{
1048 struct stripe_head *sh = stripe_head_ref;
1049
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001050 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001051 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001052}
1053
1054static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001055ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1056 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001057{
Dan Williams91c00922007-01-02 13:52:30 -07001058 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001059 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001060 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001061 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001062
1063 /* existing parity data subtracted */
1064 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1065
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001066 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001067 (unsigned long long)sh->sector);
1068
1069 for (i = disks; i--; ) {
1070 struct r5dev *dev = &sh->dev[i];
1071 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001072 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001073 xor_srcs[count++] = dev->page;
1074 }
1075
Dan Williams0403e382009-09-08 17:42:50 -07001076 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001077 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001078 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001079
1080 return tx;
1081}
1082
1083static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001084ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001085{
1086 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001087 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001088
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001089 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001090 (unsigned long long)sh->sector);
1091
1092 for (i = disks; i--; ) {
1093 struct r5dev *dev = &sh->dev[i];
1094 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001095
Dan Williamsd8ee0722008-06-28 08:32:06 +10001096 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001097 struct bio *wbi;
1098
NeilBrowncbe47ec2011-07-26 11:20:35 +10001099 spin_lock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001100 chosen = dev->towrite;
1101 dev->towrite = NULL;
1102 BUG_ON(dev->written);
1103 wbi = dev->written = chosen;
NeilBrowncbe47ec2011-07-26 11:20:35 +10001104 spin_unlock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001105
1106 while (wbi && wbi->bi_sector <
1107 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001108 if (wbi->bi_rw & REQ_FUA)
1109 set_bit(R5_WantFUA, &dev->flags);
Dan Williams91c00922007-01-02 13:52:30 -07001110 tx = async_copy_data(1, wbi, dev->page,
1111 dev->sector, tx);
1112 wbi = r5_next_bio(wbi, dev->sector);
1113 }
1114 }
1115 }
1116
1117 return tx;
1118}
1119
Dan Williamsac6b53b2009-07-14 13:40:19 -07001120static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001121{
1122 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001123 int disks = sh->disks;
1124 int pd_idx = sh->pd_idx;
1125 int qd_idx = sh->qd_idx;
1126 int i;
Tejun Heoe9c74692010-09-03 11:56:18 +02001127 bool fua = false;
Dan Williams91c00922007-01-02 13:52:30 -07001128
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001129 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001130 (unsigned long long)sh->sector);
1131
Tejun Heoe9c74692010-09-03 11:56:18 +02001132 for (i = disks; i--; )
1133 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
1134
Dan Williams91c00922007-01-02 13:52:30 -07001135 for (i = disks; i--; ) {
1136 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001137
Tejun Heoe9c74692010-09-03 11:56:18 +02001138 if (dev->written || i == pd_idx || i == qd_idx) {
Dan Williams91c00922007-01-02 13:52:30 -07001139 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001140 if (fua)
1141 set_bit(R5_WantFUA, &dev->flags);
1142 }
Dan Williams91c00922007-01-02 13:52:30 -07001143 }
1144
Dan Williamsd8ee0722008-06-28 08:32:06 +10001145 if (sh->reconstruct_state == reconstruct_state_drain_run)
1146 sh->reconstruct_state = reconstruct_state_drain_result;
1147 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1148 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1149 else {
1150 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1151 sh->reconstruct_state = reconstruct_state_result;
1152 }
Dan Williams91c00922007-01-02 13:52:30 -07001153
1154 set_bit(STRIPE_HANDLE, &sh->state);
1155 release_stripe(sh);
1156}
1157
1158static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001159ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1160 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001161{
Dan Williams91c00922007-01-02 13:52:30 -07001162 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001163 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001164 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001165 int count = 0, pd_idx = sh->pd_idx, i;
1166 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001167 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001168 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001169
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001170 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001171 (unsigned long long)sh->sector);
1172
1173 /* check if prexor is active which means only process blocks
1174 * that are part of a read-modify-write (written)
1175 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001176 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1177 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001178 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1179 for (i = disks; i--; ) {
1180 struct r5dev *dev = &sh->dev[i];
1181 if (dev->written)
1182 xor_srcs[count++] = dev->page;
1183 }
1184 } else {
1185 xor_dest = sh->dev[pd_idx].page;
1186 for (i = disks; i--; ) {
1187 struct r5dev *dev = &sh->dev[i];
1188 if (i != pd_idx)
1189 xor_srcs[count++] = dev->page;
1190 }
1191 }
1192
Dan Williams91c00922007-01-02 13:52:30 -07001193 /* 1/ if we prexor'd then the dest is reused as a source
1194 * 2/ if we did not prexor then we are redoing the parity
1195 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1196 * for the synchronous xor case
1197 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001198 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001199 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1200
1201 atomic_inc(&sh->count);
1202
Dan Williamsac6b53b2009-07-14 13:40:19 -07001203 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001204 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001205 if (unlikely(count == 1))
1206 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1207 else
1208 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001209}
1210
Dan Williamsac6b53b2009-07-14 13:40:19 -07001211static void
1212ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1213 struct dma_async_tx_descriptor *tx)
1214{
1215 struct async_submit_ctl submit;
1216 struct page **blocks = percpu->scribble;
1217 int count;
1218
1219 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1220
1221 count = set_syndrome_sources(blocks, sh);
1222
1223 atomic_inc(&sh->count);
1224
1225 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1226 sh, to_addr_conv(sh, percpu));
1227 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001228}
1229
1230static void ops_complete_check(void *stripe_head_ref)
1231{
1232 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001233
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001234 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001235 (unsigned long long)sh->sector);
1236
Dan Williamsecc65c92008-06-28 08:31:57 +10001237 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001238 set_bit(STRIPE_HANDLE, &sh->state);
1239 release_stripe(sh);
1240}
1241
Dan Williamsac6b53b2009-07-14 13:40:19 -07001242static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001243{
Dan Williams91c00922007-01-02 13:52:30 -07001244 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001245 int pd_idx = sh->pd_idx;
1246 int qd_idx = sh->qd_idx;
1247 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001248 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001249 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001250 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001251 int count;
1252 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001253
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001254 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001255 (unsigned long long)sh->sector);
1256
Dan Williamsac6b53b2009-07-14 13:40:19 -07001257 count = 0;
1258 xor_dest = sh->dev[pd_idx].page;
1259 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001260 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001261 if (i == pd_idx || i == qd_idx)
1262 continue;
1263 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001264 }
1265
Dan Williamsd6f38f32009-07-14 11:50:52 -07001266 init_async_submit(&submit, 0, NULL, NULL, NULL,
1267 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001268 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001269 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001270
Dan Williams91c00922007-01-02 13:52:30 -07001271 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001272 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1273 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001274}
1275
Dan Williamsac6b53b2009-07-14 13:40:19 -07001276static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1277{
1278 struct page **srcs = percpu->scribble;
1279 struct async_submit_ctl submit;
1280 int count;
1281
1282 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1283 (unsigned long long)sh->sector, checkp);
1284
1285 count = set_syndrome_sources(srcs, sh);
1286 if (!checkp)
1287 srcs[count] = NULL;
1288
1289 atomic_inc(&sh->count);
1290 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1291 sh, to_addr_conv(sh, percpu));
1292 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1293 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1294}
1295
Dan Williams417b8d42009-10-16 16:25:22 +11001296static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001297{
1298 int overlap_clear = 0, i, disks = sh->disks;
1299 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001300 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001301 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001302 struct raid5_percpu *percpu;
1303 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001304
Dan Williamsd6f38f32009-07-14 11:50:52 -07001305 cpu = get_cpu();
1306 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001307 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001308 ops_run_biofill(sh);
1309 overlap_clear++;
1310 }
1311
Dan Williams7b3a8712008-06-28 08:32:09 +10001312 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001313 if (level < 6)
1314 tx = ops_run_compute5(sh, percpu);
1315 else {
1316 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1317 tx = ops_run_compute6_1(sh, percpu);
1318 else
1319 tx = ops_run_compute6_2(sh, percpu);
1320 }
1321 /* terminate the chain if reconstruct is not set to be run */
1322 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001323 async_tx_ack(tx);
1324 }
Dan Williams91c00922007-01-02 13:52:30 -07001325
Dan Williams600aa102008-06-28 08:32:05 +10001326 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001327 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001328
Dan Williams600aa102008-06-28 08:32:05 +10001329 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001330 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001331 overlap_clear++;
1332 }
1333
Dan Williamsac6b53b2009-07-14 13:40:19 -07001334 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1335 if (level < 6)
1336 ops_run_reconstruct5(sh, percpu, tx);
1337 else
1338 ops_run_reconstruct6(sh, percpu, tx);
1339 }
Dan Williams91c00922007-01-02 13:52:30 -07001340
Dan Williamsac6b53b2009-07-14 13:40:19 -07001341 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1342 if (sh->check_state == check_state_run)
1343 ops_run_check_p(sh, percpu);
1344 else if (sh->check_state == check_state_run_q)
1345 ops_run_check_pq(sh, percpu, 0);
1346 else if (sh->check_state == check_state_run_pq)
1347 ops_run_check_pq(sh, percpu, 1);
1348 else
1349 BUG();
1350 }
Dan Williams91c00922007-01-02 13:52:30 -07001351
Dan Williams91c00922007-01-02 13:52:30 -07001352 if (overlap_clear)
1353 for (i = disks; i--; ) {
1354 struct r5dev *dev = &sh->dev[i];
1355 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1356 wake_up(&sh->raid_conf->wait_for_overlap);
1357 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001358 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001359}
1360
Dan Williams417b8d42009-10-16 16:25:22 +11001361#ifdef CONFIG_MULTICORE_RAID456
1362static void async_run_ops(void *param, async_cookie_t cookie)
1363{
1364 struct stripe_head *sh = param;
1365 unsigned long ops_request = sh->ops.request;
1366
1367 clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
1368 wake_up(&sh->ops.wait_for_ops);
1369
1370 __raid_run_ops(sh, ops_request);
1371 release_stripe(sh);
1372}
1373
1374static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1375{
1376 /* since handle_stripe can be called outside of raid5d context
1377 * we need to ensure sh->ops.request is de-staged before another
1378 * request arrives
1379 */
1380 wait_event(sh->ops.wait_for_ops,
1381 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
1382 sh->ops.request = ops_request;
1383
1384 atomic_inc(&sh->count);
1385 async_schedule(async_run_ops, sh);
1386}
1387#else
1388#define raid_run_ops __raid_run_ops
1389#endif
1390
NeilBrownd1688a62011-10-11 16:49:52 +11001391static int grow_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
1393 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001394 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001395 if (!sh)
1396 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001397
NeilBrown3f294f42005-11-08 21:39:25 -08001398 sh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001399 #ifdef CONFIG_MULTICORE_RAID456
1400 init_waitqueue_head(&sh->ops.wait_for_ops);
1401 #endif
NeilBrown3f294f42005-11-08 21:39:25 -08001402
NeilBrowne4e11e32010-06-16 16:45:16 +10001403 if (grow_buffers(sh)) {
1404 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001405 kmem_cache_free(conf->slab_cache, sh);
1406 return 0;
1407 }
1408 /* we just created an active stripe so... */
1409 atomic_set(&sh->count, 1);
1410 atomic_inc(&conf->active_stripes);
1411 INIT_LIST_HEAD(&sh->lru);
1412 release_stripe(sh);
1413 return 1;
1414}
1415
NeilBrownd1688a62011-10-11 16:49:52 +11001416static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001417{
Christoph Lametere18b8902006-12-06 20:33:20 -08001418 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001419 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
NeilBrownf4be6b42010-06-01 19:37:25 +10001421 if (conf->mddev->gendisk)
1422 sprintf(conf->cache_name[0],
1423 "raid%d-%s", conf->level, mdname(conf->mddev));
1424 else
1425 sprintf(conf->cache_name[0],
1426 "raid%d-%p", conf->level, conf->mddev);
1427 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1428
NeilBrownad01c9e2006-03-27 01:18:07 -08001429 conf->active_name = 0;
1430 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001432 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 if (!sc)
1434 return 1;
1435 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001436 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001437 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001438 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 return 0;
1441}
NeilBrown29269552006-03-27 01:18:10 -08001442
Dan Williamsd6f38f32009-07-14 11:50:52 -07001443/**
1444 * scribble_len - return the required size of the scribble region
1445 * @num - total number of disks in the array
1446 *
1447 * The size must be enough to contain:
1448 * 1/ a struct page pointer for each device in the array +2
1449 * 2/ room to convert each entry in (1) to its corresponding dma
1450 * (dma_map_page()) or page (page_address()) address.
1451 *
1452 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1453 * calculate over all devices (not just the data blocks), using zeros in place
1454 * of the P and Q blocks.
1455 */
1456static size_t scribble_len(int num)
1457{
1458 size_t len;
1459
1460 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1461
1462 return len;
1463}
1464
NeilBrownd1688a62011-10-11 16:49:52 +11001465static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001466{
1467 /* Make all the stripes able to hold 'newsize' devices.
1468 * New slots in each stripe get 'page' set to a new page.
1469 *
1470 * This happens in stages:
1471 * 1/ create a new kmem_cache and allocate the required number of
1472 * stripe_heads.
1473 * 2/ gather all the old stripe_heads and tranfer the pages across
1474 * to the new stripe_heads. This will have the side effect of
1475 * freezing the array as once all stripe_heads have been collected,
1476 * no IO will be possible. Old stripe heads are freed once their
1477 * pages have been transferred over, and the old kmem_cache is
1478 * freed when all stripes are done.
1479 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1480 * we simple return a failre status - no need to clean anything up.
1481 * 4/ allocate new pages for the new slots in the new stripe_heads.
1482 * If this fails, we don't bother trying the shrink the
1483 * stripe_heads down again, we just leave them as they are.
1484 * As each stripe_head is processed the new one is released into
1485 * active service.
1486 *
1487 * Once step2 is started, we cannot afford to wait for a write,
1488 * so we use GFP_NOIO allocations.
1489 */
1490 struct stripe_head *osh, *nsh;
1491 LIST_HEAD(newstripes);
1492 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001493 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001494 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001495 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001496 int i;
1497
1498 if (newsize <= conf->pool_size)
1499 return 0; /* never bother to shrink */
1500
Dan Williamsb5470dc2008-06-27 21:44:04 -07001501 err = md_allow_write(conf->mddev);
1502 if (err)
1503 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001504
NeilBrownad01c9e2006-03-27 01:18:07 -08001505 /* Step 1 */
1506 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1507 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001508 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001509 if (!sc)
1510 return -ENOMEM;
1511
1512 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001513 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001514 if (!nsh)
1515 break;
1516
NeilBrownad01c9e2006-03-27 01:18:07 -08001517 nsh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001518 #ifdef CONFIG_MULTICORE_RAID456
1519 init_waitqueue_head(&nsh->ops.wait_for_ops);
1520 #endif
NeilBrownad01c9e2006-03-27 01:18:07 -08001521
1522 list_add(&nsh->lru, &newstripes);
1523 }
1524 if (i) {
1525 /* didn't get enough, give up */
1526 while (!list_empty(&newstripes)) {
1527 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1528 list_del(&nsh->lru);
1529 kmem_cache_free(sc, nsh);
1530 }
1531 kmem_cache_destroy(sc);
1532 return -ENOMEM;
1533 }
1534 /* Step 2 - Must use GFP_NOIO now.
1535 * OK, we have enough stripes, start collecting inactive
1536 * stripes and copying them over
1537 */
1538 list_for_each_entry(nsh, &newstripes, lru) {
1539 spin_lock_irq(&conf->device_lock);
1540 wait_event_lock_irq(conf->wait_for_stripe,
1541 !list_empty(&conf->inactive_list),
1542 conf->device_lock,
NeilBrown482c0832011-04-18 18:25:42 +10001543 );
NeilBrownad01c9e2006-03-27 01:18:07 -08001544 osh = get_free_stripe(conf);
1545 spin_unlock_irq(&conf->device_lock);
1546 atomic_set(&nsh->count, 1);
1547 for(i=0; i<conf->pool_size; i++)
1548 nsh->dev[i].page = osh->dev[i].page;
1549 for( ; i<newsize; i++)
1550 nsh->dev[i].page = NULL;
1551 kmem_cache_free(conf->slab_cache, osh);
1552 }
1553 kmem_cache_destroy(conf->slab_cache);
1554
1555 /* Step 3.
1556 * At this point, we are holding all the stripes so the array
1557 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001558 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001559 */
1560 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1561 if (ndisks) {
1562 for (i=0; i<conf->raid_disks; i++)
1563 ndisks[i] = conf->disks[i];
1564 kfree(conf->disks);
1565 conf->disks = ndisks;
1566 } else
1567 err = -ENOMEM;
1568
Dan Williamsd6f38f32009-07-14 11:50:52 -07001569 get_online_cpus();
1570 conf->scribble_len = scribble_len(newsize);
1571 for_each_present_cpu(cpu) {
1572 struct raid5_percpu *percpu;
1573 void *scribble;
1574
1575 percpu = per_cpu_ptr(conf->percpu, cpu);
1576 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1577
1578 if (scribble) {
1579 kfree(percpu->scribble);
1580 percpu->scribble = scribble;
1581 } else {
1582 err = -ENOMEM;
1583 break;
1584 }
1585 }
1586 put_online_cpus();
1587
NeilBrownad01c9e2006-03-27 01:18:07 -08001588 /* Step 4, return new stripes to service */
1589 while(!list_empty(&newstripes)) {
1590 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1591 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001592
NeilBrownad01c9e2006-03-27 01:18:07 -08001593 for (i=conf->raid_disks; i < newsize; i++)
1594 if (nsh->dev[i].page == NULL) {
1595 struct page *p = alloc_page(GFP_NOIO);
1596 nsh->dev[i].page = p;
1597 if (!p)
1598 err = -ENOMEM;
1599 }
1600 release_stripe(nsh);
1601 }
1602 /* critical section pass, GFP_NOIO no longer needed */
1603
1604 conf->slab_cache = sc;
1605 conf->active_name = 1-conf->active_name;
1606 conf->pool_size = newsize;
1607 return err;
1608}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
NeilBrownd1688a62011-10-11 16:49:52 +11001610static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611{
1612 struct stripe_head *sh;
1613
NeilBrown3f294f42005-11-08 21:39:25 -08001614 spin_lock_irq(&conf->device_lock);
1615 sh = get_free_stripe(conf);
1616 spin_unlock_irq(&conf->device_lock);
1617 if (!sh)
1618 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001619 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001620 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001621 kmem_cache_free(conf->slab_cache, sh);
1622 atomic_dec(&conf->active_stripes);
1623 return 1;
1624}
1625
NeilBrownd1688a62011-10-11 16:49:52 +11001626static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001627{
1628 while (drop_one_stripe(conf))
1629 ;
1630
NeilBrown29fc7e32006-02-03 03:03:41 -08001631 if (conf->slab_cache)
1632 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 conf->slab_cache = NULL;
1634}
1635
NeilBrown6712ecf2007-09-27 12:47:43 +02001636static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
NeilBrown99c0fb52009-03-31 14:39:38 +11001638 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001639 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001640 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001642 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11001643 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
1646 for (i=0 ; i<disks; i++)
1647 if (bi == &sh->dev[i].req)
1648 break;
1649
Dan Williams45b42332007-07-09 11:56:43 -07001650 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1651 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 uptodate);
1653 if (i == disks) {
1654 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001655 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 }
NeilBrown14a75d32011-12-23 10:17:52 +11001657 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1658 rdev = conf->disks[i].replacement;
1659 else
1660 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
1662 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001664 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11001665 /* Note that this cannot happen on a
1666 * replacement device. We just fail those on
1667 * any error
1668 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001669 printk_ratelimited(
1670 KERN_INFO
1671 "md/raid:%s: read error corrected"
1672 " (%lu sectors at %llu on %s)\n",
1673 mdname(conf->mddev), STRIPE_SECTORS,
1674 (unsigned long long)(sh->sector
1675 + rdev->data_offset),
1676 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001677 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001678 clear_bit(R5_ReadError, &sh->dev[i].flags);
1679 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1680 }
NeilBrown14a75d32011-12-23 10:17:52 +11001681 if (atomic_read(&rdev->read_errors))
1682 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11001684 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001685 int retry = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001688 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11001689 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1690 printk_ratelimited(
1691 KERN_WARNING
1692 "md/raid:%s: read error on replacement device "
1693 "(sector %llu on %s).\n",
1694 mdname(conf->mddev),
1695 (unsigned long long)(sh->sector
1696 + rdev->data_offset),
1697 bdn);
1698 else if (conf->mddev->degraded >= conf->max_degraded)
Christian Dietrich8bda4702011-07-27 11:00:36 +10001699 printk_ratelimited(
1700 KERN_WARNING
1701 "md/raid:%s: read error not correctable "
1702 "(sector %llu on %s).\n",
1703 mdname(conf->mddev),
1704 (unsigned long long)(sh->sector
1705 + rdev->data_offset),
1706 bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001707 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
NeilBrown4e5314b2005-11-08 21:39:22 -08001708 /* Oh, no!!! */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001709 printk_ratelimited(
1710 KERN_WARNING
1711 "md/raid:%s: read error NOT corrected!! "
1712 "(sector %llu on %s).\n",
1713 mdname(conf->mddev),
1714 (unsigned long long)(sh->sector
1715 + rdev->data_offset),
1716 bdn);
NeilBrownd6950432006-07-10 04:44:20 -07001717 else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001718 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001719 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001720 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001721 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001722 else
1723 retry = 1;
1724 if (retry)
1725 set_bit(R5_ReadError, &sh->dev[i].flags);
1726 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001727 clear_bit(R5_ReadError, &sh->dev[i].flags);
1728 clear_bit(R5_ReWrite, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001729 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 }
NeilBrown14a75d32011-12-23 10:17:52 +11001732 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1734 set_bit(STRIPE_HANDLE, &sh->state);
1735 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736}
1737
NeilBrownd710e132008-10-13 11:55:12 +11001738static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
NeilBrown99c0fb52009-03-31 14:39:38 +11001740 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001741 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001742 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11001743 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001745 sector_t first_bad;
1746 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11001747 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
NeilBrown977df362011-12-23 10:17:53 +11001749 for (i = 0 ; i < disks; i++) {
1750 if (bi == &sh->dev[i].req) {
1751 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 break;
NeilBrown977df362011-12-23 10:17:53 +11001753 }
1754 if (bi == &sh->dev[i].rreq) {
1755 rdev = conf->disks[i].replacement;
1756 replacement = 1;
1757 break;
1758 }
1759 }
Dan Williams45b42332007-07-09 11:56:43 -07001760 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1762 uptodate);
1763 if (i == disks) {
1764 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001765 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 }
1767
NeilBrown977df362011-12-23 10:17:53 +11001768 if (replacement) {
1769 if (!uptodate)
1770 md_error(conf->mddev, rdev);
1771 else if (is_badblock(rdev, sh->sector,
1772 STRIPE_SECTORS,
1773 &first_bad, &bad_sectors))
1774 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1775 } else {
1776 if (!uptodate) {
1777 set_bit(WriteErrorSeen, &rdev->flags);
1778 set_bit(R5_WriteError, &sh->dev[i].flags);
1779 } else if (is_badblock(rdev, sh->sector,
1780 STRIPE_SECTORS,
1781 &first_bad, &bad_sectors))
1782 set_bit(R5_MadeGood, &sh->dev[i].flags);
1783 }
1784 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
NeilBrown977df362011-12-23 10:17:53 +11001786 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1787 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001789 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790}
1791
NeilBrown784052e2009-03-31 15:19:07 +11001792static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
NeilBrown784052e2009-03-31 15:19:07 +11001794static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795{
1796 struct r5dev *dev = &sh->dev[i];
1797
1798 bio_init(&dev->req);
1799 dev->req.bi_io_vec = &dev->vec;
1800 dev->req.bi_vcnt++;
1801 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11001803 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
NeilBrown977df362011-12-23 10:17:53 +11001805 bio_init(&dev->rreq);
1806 dev->rreq.bi_io_vec = &dev->rvec;
1807 dev->rreq.bi_vcnt++;
1808 dev->rreq.bi_max_vecs++;
1809 dev->rreq.bi_private = sh;
1810 dev->rvec.bv_page = dev->page;
1811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001813 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814}
1815
NeilBrownfd01b882011-10-11 16:47:53 +11001816static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
1818 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11001819 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11001820 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10001821 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
NeilBrown908f4fb2011-12-23 10:17:50 +11001823 spin_lock_irqsave(&conf->device_lock, flags);
1824 clear_bit(In_sync, &rdev->flags);
1825 mddev->degraded = calc_degraded(conf);
1826 spin_unlock_irqrestore(&conf->device_lock, flags);
1827 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1828
NeilBrownde393cd2011-07-28 11:31:48 +10001829 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001830 set_bit(Faulty, &rdev->flags);
1831 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1832 printk(KERN_ALERT
1833 "md/raid:%s: Disk failure on %s, disabling device.\n"
1834 "md/raid:%s: Operation continuing on %d devices.\n",
1835 mdname(mddev),
1836 bdevname(rdev->bdev, b),
1837 mdname(mddev),
1838 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001839}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
1841/*
1842 * Input: a 'big' sector number,
1843 * Output: index of the data and parity disk, and the sector # in them.
1844 */
NeilBrownd1688a62011-10-11 16:49:52 +11001845static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001846 int previous, int *dd_idx,
1847 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001849 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001850 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001852 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001853 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001855 int algorithm = previous ? conf->prev_algo
1856 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001857 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1858 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001859 int raid_disks = previous ? conf->previous_raid_disks
1860 : conf->raid_disks;
1861 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863 /* First compute the information on this sector */
1864
1865 /*
1866 * Compute the chunk number and the sector offset inside the chunk
1867 */
1868 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1869 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
1871 /*
1872 * Compute the stripe number
1873 */
NeilBrown35f2a592010-04-20 14:13:34 +10001874 stripe = chunk_number;
1875 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10001876 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 /*
1878 * Select the parity disk based on the user selected algorithm.
1879 */
NeilBrown84789552011-07-27 11:00:36 +10001880 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07001881 switch(conf->level) {
1882 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001883 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001884 break;
1885 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001886 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001888 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001889 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 (*dd_idx)++;
1891 break;
1892 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001893 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001894 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 (*dd_idx)++;
1896 break;
1897 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001898 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001899 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 break;
1901 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001902 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001903 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001905 case ALGORITHM_PARITY_0:
1906 pd_idx = 0;
1907 (*dd_idx)++;
1908 break;
1909 case ALGORITHM_PARITY_N:
1910 pd_idx = data_disks;
1911 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11001913 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001914 }
1915 break;
1916 case 6:
1917
NeilBrowne183eae2009-03-31 15:20:22 +11001918 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001919 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001920 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001921 qd_idx = pd_idx + 1;
1922 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001923 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001924 qd_idx = 0;
1925 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001926 (*dd_idx) += 2; /* D D P Q D */
1927 break;
1928 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001929 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001930 qd_idx = pd_idx + 1;
1931 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001932 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001933 qd_idx = 0;
1934 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001935 (*dd_idx) += 2; /* D D P Q D */
1936 break;
1937 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001938 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001939 qd_idx = (pd_idx + 1) % raid_disks;
1940 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001941 break;
1942 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001943 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001944 qd_idx = (pd_idx + 1) % raid_disks;
1945 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001946 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001947
1948 case ALGORITHM_PARITY_0:
1949 pd_idx = 0;
1950 qd_idx = 1;
1951 (*dd_idx) += 2;
1952 break;
1953 case ALGORITHM_PARITY_N:
1954 pd_idx = data_disks;
1955 qd_idx = data_disks + 1;
1956 break;
1957
1958 case ALGORITHM_ROTATING_ZERO_RESTART:
1959 /* Exactly the same as RIGHT_ASYMMETRIC, but or
1960 * of blocks for computing Q is different.
1961 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001962 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11001963 qd_idx = pd_idx + 1;
1964 if (pd_idx == raid_disks-1) {
1965 (*dd_idx)++; /* Q D D D P */
1966 qd_idx = 0;
1967 } else if (*dd_idx >= pd_idx)
1968 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001969 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001970 break;
1971
1972 case ALGORITHM_ROTATING_N_RESTART:
1973 /* Same a left_asymmetric, by first stripe is
1974 * D D D P Q rather than
1975 * Q D D D P
1976 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001977 stripe2 += 1;
1978 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11001979 qd_idx = pd_idx + 1;
1980 if (pd_idx == raid_disks-1) {
1981 (*dd_idx)++; /* Q D D D P */
1982 qd_idx = 0;
1983 } else if (*dd_idx >= pd_idx)
1984 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001985 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001986 break;
1987
1988 case ALGORITHM_ROTATING_N_CONTINUE:
1989 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001990 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11001991 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
1992 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11001993 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001994 break;
1995
1996 case ALGORITHM_LEFT_ASYMMETRIC_6:
1997 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001998 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11001999 if (*dd_idx >= pd_idx)
2000 (*dd_idx)++;
2001 qd_idx = raid_disks - 1;
2002 break;
2003
2004 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002005 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002006 if (*dd_idx >= pd_idx)
2007 (*dd_idx)++;
2008 qd_idx = raid_disks - 1;
2009 break;
2010
2011 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002012 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002013 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2014 qd_idx = raid_disks - 1;
2015 break;
2016
2017 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002018 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002019 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2020 qd_idx = raid_disks - 1;
2021 break;
2022
2023 case ALGORITHM_PARITY_0_6:
2024 pd_idx = 0;
2025 (*dd_idx)++;
2026 qd_idx = raid_disks - 1;
2027 break;
2028
NeilBrown16a53ec2006-06-26 00:27:38 -07002029 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002030 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002031 }
2032 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 }
2034
NeilBrown911d4ee2009-03-31 14:39:38 +11002035 if (sh) {
2036 sh->pd_idx = pd_idx;
2037 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002038 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 /*
2041 * Finally, compute the new sector number
2042 */
2043 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2044 return new_sector;
2045}
2046
2047
NeilBrown784052e2009-03-31 15:19:07 +11002048static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049{
NeilBrownd1688a62011-10-11 16:49:52 +11002050 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002051 int raid_disks = sh->disks;
2052 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002054 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2055 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002056 int algorithm = previous ? conf->prev_algo
2057 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 sector_t stripe;
2059 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002060 sector_t chunk_number;
2061 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002063 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
NeilBrown16a53ec2006-06-26 00:27:38 -07002065
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2067 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
NeilBrown16a53ec2006-06-26 00:27:38 -07002069 if (i == sh->pd_idx)
2070 return 0;
2071 switch(conf->level) {
2072 case 4: break;
2073 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002074 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 case ALGORITHM_LEFT_ASYMMETRIC:
2076 case ALGORITHM_RIGHT_ASYMMETRIC:
2077 if (i > sh->pd_idx)
2078 i--;
2079 break;
2080 case ALGORITHM_LEFT_SYMMETRIC:
2081 case ALGORITHM_RIGHT_SYMMETRIC:
2082 if (i < sh->pd_idx)
2083 i += raid_disks;
2084 i -= (sh->pd_idx + 1);
2085 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002086 case ALGORITHM_PARITY_0:
2087 i -= 1;
2088 break;
2089 case ALGORITHM_PARITY_N:
2090 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002092 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002093 }
2094 break;
2095 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002096 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002097 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002098 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002099 case ALGORITHM_LEFT_ASYMMETRIC:
2100 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002101 case ALGORITHM_ROTATING_ZERO_RESTART:
2102 case ALGORITHM_ROTATING_N_RESTART:
2103 if (sh->pd_idx == raid_disks-1)
2104 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002105 else if (i > sh->pd_idx)
2106 i -= 2; /* D D P Q D */
2107 break;
2108 case ALGORITHM_LEFT_SYMMETRIC:
2109 case ALGORITHM_RIGHT_SYMMETRIC:
2110 if (sh->pd_idx == raid_disks-1)
2111 i--; /* Q D D D P */
2112 else {
2113 /* D D P Q D */
2114 if (i < sh->pd_idx)
2115 i += raid_disks;
2116 i -= (sh->pd_idx + 2);
2117 }
2118 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002119 case ALGORITHM_PARITY_0:
2120 i -= 2;
2121 break;
2122 case ALGORITHM_PARITY_N:
2123 break;
2124 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002125 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002126 if (sh->pd_idx == 0)
2127 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002128 else {
2129 /* D D Q P D */
2130 if (i < sh->pd_idx)
2131 i += raid_disks;
2132 i -= (sh->pd_idx + 1);
2133 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002134 break;
2135 case ALGORITHM_LEFT_ASYMMETRIC_6:
2136 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2137 if (i > sh->pd_idx)
2138 i--;
2139 break;
2140 case ALGORITHM_LEFT_SYMMETRIC_6:
2141 case ALGORITHM_RIGHT_SYMMETRIC_6:
2142 if (i < sh->pd_idx)
2143 i += data_disks + 1;
2144 i -= (sh->pd_idx + 1);
2145 break;
2146 case ALGORITHM_PARITY_0_6:
2147 i -= 1;
2148 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002149 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002150 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002151 }
2152 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 }
2154
2155 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002156 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
NeilBrown112bf892009-03-31 14:39:38 +11002158 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002159 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002160 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2161 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002162 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2163 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 return 0;
2165 }
2166 return r_sector;
2167}
2168
2169
Dan Williams600aa102008-06-28 08:32:05 +10002170static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002171schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002172 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002173{
2174 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002175 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002176 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002177
Dan Williamse33129d2007-01-02 13:52:30 -07002178 if (rcw) {
2179 /* if we are not expanding this is a proper write request, and
2180 * there will be bios with new data to be drained into the
2181 * stripe cache
2182 */
2183 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10002184 sh->reconstruct_state = reconstruct_state_drain_run;
2185 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2186 } else
2187 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07002188
Dan Williamsac6b53b2009-07-14 13:40:19 -07002189 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002190
2191 for (i = disks; i--; ) {
2192 struct r5dev *dev = &sh->dev[i];
2193
2194 if (dev->towrite) {
2195 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002196 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002197 if (!expand)
2198 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002199 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002200 }
2201 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002202 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002203 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002204 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002205 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002206 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002207 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2208 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2209
Dan Williamsd8ee0722008-06-28 08:32:06 +10002210 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002211 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2212 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002213 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002214
2215 for (i = disks; i--; ) {
2216 struct r5dev *dev = &sh->dev[i];
2217 if (i == pd_idx)
2218 continue;
2219
Dan Williamse33129d2007-01-02 13:52:30 -07002220 if (dev->towrite &&
2221 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002222 test_bit(R5_Wantcompute, &dev->flags))) {
2223 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002224 set_bit(R5_LOCKED, &dev->flags);
2225 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002226 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002227 }
2228 }
2229 }
2230
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002231 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002232 * are in flight
2233 */
2234 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2235 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002236 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002237
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002238 if (level == 6) {
2239 int qd_idx = sh->qd_idx;
2240 struct r5dev *dev = &sh->dev[qd_idx];
2241
2242 set_bit(R5_LOCKED, &dev->flags);
2243 clear_bit(R5_UPTODATE, &dev->flags);
2244 s->locked++;
2245 }
2246
Dan Williams600aa102008-06-28 08:32:05 +10002247 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07002248 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002249 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002250}
NeilBrown16a53ec2006-06-26 00:27:38 -07002251
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252/*
2253 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002254 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 * The bi_next chain must be in order.
2256 */
2257static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2258{
2259 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002260 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002261 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
NeilBrowncbe47ec2011-07-26 11:20:35 +10002263 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 (unsigned long long)bi->bi_sector,
2265 (unsigned long long)sh->sector);
2266
2267
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07002269 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -07002271 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
2272 firstwrite = 1;
2273 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 bip = &sh->dev[dd_idx].toread;
2275 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2276 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2277 goto overlap;
2278 bip = & (*bip)->bi_next;
2279 }
2280 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2281 goto overlap;
2282
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002283 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 if (*bip)
2285 bi->bi_next = *bip;
2286 *bip = bi;
Jens Axboe960e7392008-08-15 10:41:18 +02002287 bi->bi_phys_segments++;
NeilBrown72626682005-09-09 16:23:54 -07002288
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 if (forwrite) {
2290 /* check if page is covered */
2291 sector_t sector = sh->dev[dd_idx].sector;
2292 for (bi=sh->dev[dd_idx].towrite;
2293 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2294 bi && bi->bi_sector <= sector;
2295 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2296 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2297 sector = bi->bi_sector + (bi->bi_size>>9);
2298 }
2299 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2300 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2301 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002302 spin_unlock_irq(&conf->device_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002303
2304 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2305 (unsigned long long)(*bip)->bi_sector,
2306 (unsigned long long)sh->sector, dd_idx);
2307
2308 if (conf->mddev->bitmap && firstwrite) {
2309 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2310 STRIPE_SECTORS, 0);
2311 sh->bm_seq = conf->seq_flush+1;
2312 set_bit(STRIPE_BIT_DELAY, &sh->state);
2313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 return 1;
2315
2316 overlap:
2317 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
2318 spin_unlock_irq(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 return 0;
2320}
2321
NeilBrownd1688a62011-10-11 16:49:52 +11002322static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002323
NeilBrownd1688a62011-10-11 16:49:52 +11002324static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002325 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002326{
NeilBrown784052e2009-03-31 15:19:07 +11002327 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002328 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002329 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002330 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002331 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002332
NeilBrown112bf892009-03-31 14:39:38 +11002333 raid5_compute_sector(conf,
2334 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002335 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002336 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002337 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002338}
2339
Dan Williamsa4456852007-07-09 11:56:43 -07002340static void
NeilBrownd1688a62011-10-11 16:49:52 +11002341handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002342 struct stripe_head_state *s, int disks,
2343 struct bio **return_bi)
2344{
2345 int i;
2346 for (i = disks; i--; ) {
2347 struct bio *bi;
2348 int bitmap_end = 0;
2349
2350 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002351 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002352 rcu_read_lock();
2353 rdev = rcu_dereference(conf->disks[i].rdev);
2354 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002355 atomic_inc(&rdev->nr_pending);
2356 else
2357 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002358 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002359 if (rdev) {
2360 if (!rdev_set_badblocks(
2361 rdev,
2362 sh->sector,
2363 STRIPE_SECTORS, 0))
2364 md_error(conf->mddev, rdev);
2365 rdev_dec_pending(rdev, conf->mddev);
2366 }
Dan Williamsa4456852007-07-09 11:56:43 -07002367 }
2368 spin_lock_irq(&conf->device_lock);
2369 /* fail all writes first */
2370 bi = sh->dev[i].towrite;
2371 sh->dev[i].towrite = NULL;
2372 if (bi) {
2373 s->to_write--;
2374 bitmap_end = 1;
2375 }
2376
2377 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2378 wake_up(&conf->wait_for_overlap);
2379
2380 while (bi && bi->bi_sector <
2381 sh->dev[i].sector + STRIPE_SECTORS) {
2382 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2383 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002384 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002385 md_write_end(conf->mddev);
2386 bi->bi_next = *return_bi;
2387 *return_bi = bi;
2388 }
2389 bi = nextbi;
2390 }
2391 /* and fail all 'written' */
2392 bi = sh->dev[i].written;
2393 sh->dev[i].written = NULL;
2394 if (bi) bitmap_end = 1;
2395 while (bi && bi->bi_sector <
2396 sh->dev[i].sector + STRIPE_SECTORS) {
2397 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2398 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002399 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002400 md_write_end(conf->mddev);
2401 bi->bi_next = *return_bi;
2402 *return_bi = bi;
2403 }
2404 bi = bi2;
2405 }
2406
Dan Williamsb5e98d62007-01-02 13:52:31 -07002407 /* fail any reads if this device is non-operational and
2408 * the data has not reached the cache yet.
2409 */
2410 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2411 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2412 test_bit(R5_ReadError, &sh->dev[i].flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002413 bi = sh->dev[i].toread;
2414 sh->dev[i].toread = NULL;
2415 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2416 wake_up(&conf->wait_for_overlap);
2417 if (bi) s->to_read--;
2418 while (bi && bi->bi_sector <
2419 sh->dev[i].sector + STRIPE_SECTORS) {
2420 struct bio *nextbi =
2421 r5_next_bio(bi, sh->dev[i].sector);
2422 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002423 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002424 bi->bi_next = *return_bi;
2425 *return_bi = bi;
2426 }
2427 bi = nextbi;
2428 }
2429 }
2430 spin_unlock_irq(&conf->device_lock);
2431 if (bitmap_end)
2432 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2433 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002434 /* If we were in the middle of a write the parity block might
2435 * still be locked - so just clear all R5_LOCKED flags
2436 */
2437 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002438 }
2439
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002440 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2441 if (atomic_dec_and_test(&conf->pending_full_writes))
2442 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002443}
2444
NeilBrown7f0da592011-07-28 11:39:22 +10002445static void
NeilBrownd1688a62011-10-11 16:49:52 +11002446handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002447 struct stripe_head_state *s)
2448{
2449 int abort = 0;
2450 int i;
2451
2452 md_done_sync(conf->mddev, STRIPE_SECTORS, 0);
2453 clear_bit(STRIPE_SYNCING, &sh->state);
2454 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002455 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002456 /* There is nothing more to do for sync/check/repair.
NeilBrown9a3e1102011-12-23 10:17:53 +11002457 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002458 * non-sync devices, or abort the recovery
2459 */
2460 if (!test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery))
2461 return;
2462 /* During recovery devices cannot be removed, so locking and
2463 * refcounting of rdevs is not needed
2464 */
2465 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +11002466 struct md_rdev *rdev = conf->disks[i].rdev;
NeilBrown9a3e1102011-12-23 10:17:53 +11002467 if (rdev
2468 && !test_bit(Faulty, &rdev->flags)
2469 && !test_bit(In_sync, &rdev->flags)
2470 && !rdev_set_badblocks(rdev, sh->sector,
2471 STRIPE_SECTORS, 0))
2472 abort = 1;
2473 rdev = conf->disks[i].replacement;
2474 if (rdev
2475 && !test_bit(Faulty, &rdev->flags)
2476 && !test_bit(In_sync, &rdev->flags)
2477 && !rdev_set_badblocks(rdev, sh->sector,
2478 STRIPE_SECTORS, 0))
NeilBrown7f0da592011-07-28 11:39:22 +10002479 abort = 1;
2480 }
2481 if (abort) {
2482 conf->recovery_disabled = conf->mddev->recovery_disabled;
2483 set_bit(MD_RECOVERY_INTR, &conf->mddev->recovery);
2484 }
2485}
2486
NeilBrown9a3e1102011-12-23 10:17:53 +11002487static int want_replace(struct stripe_head *sh, int disk_idx)
2488{
2489 struct md_rdev *rdev;
2490 int rv = 0;
2491 /* Doing recovery so rcu locking not required */
2492 rdev = sh->raid_conf->disks[disk_idx].replacement;
2493 if (rdev
2494 && !test_bit(Faulty, &rdev->flags)
2495 && !test_bit(In_sync, &rdev->flags)
2496 && (rdev->recovery_offset <= sh->sector
2497 || rdev->mddev->recovery_cp <= sh->sector))
2498 rv = 1;
2499
2500 return rv;
2501}
2502
NeilBrown93b3dbc2011-07-27 11:00:36 +10002503/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002504 * to be read or computed to satisfy a request.
2505 *
2506 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002507 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002508 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002509static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2510 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002511{
2512 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002513 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2514 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002515
Dan Williamsf38e1212007-01-02 13:52:30 -07002516 /* is the data in this block needed, and can we get it? */
2517 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002518 !test_bit(R5_UPTODATE, &dev->flags) &&
2519 (dev->toread ||
2520 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2521 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002522 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002523 (s->failed >= 1 && fdev[0]->toread) ||
2524 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002525 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2526 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2527 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002528 /* we would like to get this block, possibly by computing it,
2529 * otherwise read it if the backing disk is insync
2530 */
2531 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2532 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2533 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002534 (s->failed && (disk_idx == s->failed_num[0] ||
2535 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002536 /* have disk failed, and we're requested to fetch it;
2537 * do compute it
2538 */
2539 pr_debug("Computing stripe %llu block %d\n",
2540 (unsigned long long)sh->sector, disk_idx);
2541 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2542 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2543 set_bit(R5_Wantcompute, &dev->flags);
2544 sh->ops.target = disk_idx;
2545 sh->ops.target2 = -1; /* no 2nd target */
2546 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002547 /* Careful: from this point on 'uptodate' is in the eye
2548 * of raid_run_ops which services 'compute' operations
2549 * before writes. R5_Wantcompute flags a block that will
2550 * be R5_UPTODATE by the time it is needed for a
2551 * subsequent operation.
2552 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002553 s->uptodate++;
2554 return 1;
2555 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2556 /* Computing 2-failure is *very* expensive; only
2557 * do it if failed >= 2
2558 */
2559 int other;
2560 for (other = disks; other--; ) {
2561 if (other == disk_idx)
2562 continue;
2563 if (!test_bit(R5_UPTODATE,
2564 &sh->dev[other].flags))
2565 break;
2566 }
2567 BUG_ON(other < 0);
2568 pr_debug("Computing stripe %llu blocks %d,%d\n",
2569 (unsigned long long)sh->sector,
2570 disk_idx, other);
2571 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2572 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2573 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2574 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2575 sh->ops.target = disk_idx;
2576 sh->ops.target2 = other;
2577 s->uptodate += 2;
2578 s->req_compute = 1;
2579 return 1;
2580 } else if (test_bit(R5_Insync, &dev->flags)) {
2581 set_bit(R5_LOCKED, &dev->flags);
2582 set_bit(R5_Wantread, &dev->flags);
2583 s->locked++;
2584 pr_debug("Reading block %d (sync=%d)\n",
2585 disk_idx, s->syncing);
2586 }
2587 }
2588
2589 return 0;
2590}
2591
2592/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002593 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002594 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002595static void handle_stripe_fill(struct stripe_head *sh,
2596 struct stripe_head_state *s,
2597 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002598{
2599 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002600
2601 /* look for blocks to read/compute, skip this if a compute
2602 * is already in flight, or if the stripe contents are in the
2603 * midst of changing due to a write
2604 */
2605 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2606 !sh->reconstruct_state)
2607 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002608 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002609 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002610 set_bit(STRIPE_HANDLE, &sh->state);
2611}
2612
2613
Dan Williams1fe797e2008-06-28 09:16:30 +10002614/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002615 * any written block on an uptodate or failed drive can be returned.
2616 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2617 * never LOCKED, so we don't need to test 'failed' directly.
2618 */
NeilBrownd1688a62011-10-11 16:49:52 +11002619static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002620 struct stripe_head *sh, int disks, struct bio **return_bi)
2621{
2622 int i;
2623 struct r5dev *dev;
2624
2625 for (i = disks; i--; )
2626 if (sh->dev[i].written) {
2627 dev = &sh->dev[i];
2628 if (!test_bit(R5_LOCKED, &dev->flags) &&
2629 test_bit(R5_UPTODATE, &dev->flags)) {
2630 /* We can return any write requests */
2631 struct bio *wbi, *wbi2;
2632 int bitmap_end = 0;
Dan Williams45b42332007-07-09 11:56:43 -07002633 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002634 spin_lock_irq(&conf->device_lock);
2635 wbi = dev->written;
2636 dev->written = NULL;
2637 while (wbi && wbi->bi_sector <
2638 dev->sector + STRIPE_SECTORS) {
2639 wbi2 = r5_next_bio(wbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +02002640 if (!raid5_dec_bi_phys_segments(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002641 md_write_end(conf->mddev);
2642 wbi->bi_next = *return_bi;
2643 *return_bi = wbi;
2644 }
2645 wbi = wbi2;
2646 }
2647 if (dev->towrite == NULL)
2648 bitmap_end = 1;
2649 spin_unlock_irq(&conf->device_lock);
2650 if (bitmap_end)
2651 bitmap_endwrite(conf->mddev->bitmap,
2652 sh->sector,
2653 STRIPE_SECTORS,
2654 !test_bit(STRIPE_DEGRADED, &sh->state),
2655 0);
2656 }
2657 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002658
2659 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2660 if (atomic_dec_and_test(&conf->pending_full_writes))
2661 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002662}
2663
NeilBrownd1688a62011-10-11 16:49:52 +11002664static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002665 struct stripe_head *sh,
2666 struct stripe_head_state *s,
2667 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002668{
2669 int rmw = 0, rcw = 0, i;
NeilBrownc8ac1802011-07-27 11:00:36 +10002670 if (conf->max_degraded == 2) {
2671 /* RAID6 requires 'rcw' in current implementation
2672 * Calculate the real rcw later - for now fake it
2673 * look like rcw is cheaper
2674 */
2675 rcw = 1; rmw = 2;
2676 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002677 /* would I have to read this buffer for read_modify_write */
2678 struct r5dev *dev = &sh->dev[i];
2679 if ((dev->towrite || i == sh->pd_idx) &&
2680 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002681 !(test_bit(R5_UPTODATE, &dev->flags) ||
2682 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002683 if (test_bit(R5_Insync, &dev->flags))
2684 rmw++;
2685 else
2686 rmw += 2*disks; /* cannot read it */
2687 }
2688 /* Would I have to read this buffer for reconstruct_write */
2689 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2690 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002691 !(test_bit(R5_UPTODATE, &dev->flags) ||
2692 test_bit(R5_Wantcompute, &dev->flags))) {
2693 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002694 else
2695 rcw += 2*disks;
2696 }
2697 }
Dan Williams45b42332007-07-09 11:56:43 -07002698 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002699 (unsigned long long)sh->sector, rmw, rcw);
2700 set_bit(STRIPE_HANDLE, &sh->state);
2701 if (rmw < rcw && rmw > 0)
2702 /* prefer read-modify-write, but need to get some data */
2703 for (i = disks; i--; ) {
2704 struct r5dev *dev = &sh->dev[i];
2705 if ((dev->towrite || i == sh->pd_idx) &&
2706 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002707 !(test_bit(R5_UPTODATE, &dev->flags) ||
2708 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002709 test_bit(R5_Insync, &dev->flags)) {
2710 if (
2711 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002712 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002713 "%d for r-m-w\n", i);
2714 set_bit(R5_LOCKED, &dev->flags);
2715 set_bit(R5_Wantread, &dev->flags);
2716 s->locked++;
2717 } else {
2718 set_bit(STRIPE_DELAYED, &sh->state);
2719 set_bit(STRIPE_HANDLE, &sh->state);
2720 }
2721 }
2722 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002723 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002724 /* want reconstruct write, but need to get some data */
NeilBrownc8ac1802011-07-27 11:00:36 +10002725 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002726 for (i = disks; i--; ) {
2727 struct r5dev *dev = &sh->dev[i];
2728 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002729 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002730 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002731 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002732 test_bit(R5_Wantcompute, &dev->flags))) {
2733 rcw++;
2734 if (!test_bit(R5_Insync, &dev->flags))
2735 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002736 if (
2737 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002738 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002739 "%d for Reconstruct\n", i);
2740 set_bit(R5_LOCKED, &dev->flags);
2741 set_bit(R5_Wantread, &dev->flags);
2742 s->locked++;
2743 } else {
2744 set_bit(STRIPE_DELAYED, &sh->state);
2745 set_bit(STRIPE_HANDLE, &sh->state);
2746 }
2747 }
2748 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002749 }
Dan Williamsa4456852007-07-09 11:56:43 -07002750 /* now if nothing is locked, and if we have enough data,
2751 * we can start a write request
2752 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002753 /* since handle_stripe can be called at any time we need to handle the
2754 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002755 * subsequent call wants to start a write request. raid_run_ops only
2756 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002757 * simultaneously. If this is not the case then new writes need to be
2758 * held off until the compute completes.
2759 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002760 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2761 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2762 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002763 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002764}
2765
NeilBrownd1688a62011-10-11 16:49:52 +11002766static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002767 struct stripe_head_state *s, int disks)
2768{
Dan Williamsecc65c92008-06-28 08:31:57 +10002769 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002770
Dan Williamsbd2ab672008-04-10 21:29:27 -07002771 set_bit(STRIPE_HANDLE, &sh->state);
2772
Dan Williamsecc65c92008-06-28 08:31:57 +10002773 switch (sh->check_state) {
2774 case check_state_idle:
2775 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002776 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002777 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002778 sh->check_state = check_state_run;
2779 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002780 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002781 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002782 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002783 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002784 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002785 /* fall through */
2786 case check_state_compute_result:
2787 sh->check_state = check_state_idle;
2788 if (!dev)
2789 dev = &sh->dev[sh->pd_idx];
2790
2791 /* check that a write has not made the stripe insync */
2792 if (test_bit(STRIPE_INSYNC, &sh->state))
2793 break;
2794
2795 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002796 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2797 BUG_ON(s->uptodate != disks);
2798
2799 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002800 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002801 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002802
Dan Williamsa4456852007-07-09 11:56:43 -07002803 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002804 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002805 break;
2806 case check_state_run:
2807 break; /* we will be called again upon completion */
2808 case check_state_check_result:
2809 sh->check_state = check_state_idle;
2810
2811 /* if a failure occurred during the check operation, leave
2812 * STRIPE_INSYNC not set and let the stripe be handled again
2813 */
2814 if (s->failed)
2815 break;
2816
2817 /* handle a successful check operation, if parity is correct
2818 * we are done. Otherwise update the mismatch count and repair
2819 * parity if !MD_RECOVERY_CHECK
2820 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002821 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002822 /* parity is correct (on disc,
2823 * not in buffer any more)
2824 */
2825 set_bit(STRIPE_INSYNC, &sh->state);
2826 else {
2827 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2828 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2829 /* don't try to repair!! */
2830 set_bit(STRIPE_INSYNC, &sh->state);
2831 else {
2832 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002833 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002834 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2835 set_bit(R5_Wantcompute,
2836 &sh->dev[sh->pd_idx].flags);
2837 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002838 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10002839 s->uptodate++;
2840 }
2841 }
2842 break;
2843 case check_state_compute_run:
2844 break;
2845 default:
2846 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2847 __func__, sh->check_state,
2848 (unsigned long long) sh->sector);
2849 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002850 }
2851}
2852
2853
NeilBrownd1688a62011-10-11 16:49:52 +11002854static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07002855 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10002856 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002857{
Dan Williamsa4456852007-07-09 11:56:43 -07002858 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11002859 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07002860 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07002861
2862 set_bit(STRIPE_HANDLE, &sh->state);
2863
2864 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002865
Dan Williamsa4456852007-07-09 11:56:43 -07002866 /* Want to check and possibly repair P and Q.
2867 * However there could be one 'failed' device, in which
2868 * case we can only check one of them, possibly using the
2869 * other to generate missing data
2870 */
2871
Dan Williamsd82dfee2009-07-14 13:40:57 -07002872 switch (sh->check_state) {
2873 case check_state_idle:
2874 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10002875 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002876 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07002877 * makes sense to check P (If anything else were failed,
2878 * we would have used P to recreate it).
2879 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002880 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07002881 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002882 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002883 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07002884 * anything, so it makes sense to check it
2885 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002886 if (sh->check_state == check_state_run)
2887 sh->check_state = check_state_run_pq;
2888 else
2889 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07002890 }
Dan Williams36d1c642009-07-14 11:48:22 -07002891
Dan Williamsd82dfee2009-07-14 13:40:57 -07002892 /* discard potentially stale zero_sum_result */
2893 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07002894
Dan Williamsd82dfee2009-07-14 13:40:57 -07002895 if (sh->check_state == check_state_run) {
2896 /* async_xor_zero_sum destroys the contents of P */
2897 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
2898 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07002899 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002900 if (sh->check_state >= check_state_run &&
2901 sh->check_state <= check_state_run_pq) {
2902 /* async_syndrome_zero_sum preserves P and Q, so
2903 * no need to mark them !uptodate here
2904 */
2905 set_bit(STRIPE_OP_CHECK, &s->ops_request);
2906 break;
2907 }
Dan Williams36d1c642009-07-14 11:48:22 -07002908
Dan Williamsd82dfee2009-07-14 13:40:57 -07002909 /* we have 2-disk failure */
2910 BUG_ON(s->failed != 2);
2911 /* fall through */
2912 case check_state_compute_result:
2913 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07002914
Dan Williamsd82dfee2009-07-14 13:40:57 -07002915 /* check that a write has not made the stripe insync */
2916 if (test_bit(STRIPE_INSYNC, &sh->state))
2917 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002918
2919 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07002920 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07002921 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002922 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07002923 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10002924 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07002925 s->locked++;
2926 set_bit(R5_LOCKED, &dev->flags);
2927 set_bit(R5_Wantwrite, &dev->flags);
2928 }
2929 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10002930 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07002931 s->locked++;
2932 set_bit(R5_LOCKED, &dev->flags);
2933 set_bit(R5_Wantwrite, &dev->flags);
2934 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002935 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002936 dev = &sh->dev[pd_idx];
2937 s->locked++;
2938 set_bit(R5_LOCKED, &dev->flags);
2939 set_bit(R5_Wantwrite, &dev->flags);
2940 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002941 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002942 dev = &sh->dev[qd_idx];
2943 s->locked++;
2944 set_bit(R5_LOCKED, &dev->flags);
2945 set_bit(R5_Wantwrite, &dev->flags);
2946 }
2947 clear_bit(STRIPE_DEGRADED, &sh->state);
2948
2949 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002950 break;
2951 case check_state_run:
2952 case check_state_run_q:
2953 case check_state_run_pq:
2954 break; /* we will be called again upon completion */
2955 case check_state_check_result:
2956 sh->check_state = check_state_idle;
2957
2958 /* handle a successful check operation, if parity is correct
2959 * we are done. Otherwise update the mismatch count and repair
2960 * parity if !MD_RECOVERY_CHECK
2961 */
2962 if (sh->ops.zero_sum_result == 0) {
2963 /* both parities are correct */
2964 if (!s->failed)
2965 set_bit(STRIPE_INSYNC, &sh->state);
2966 else {
2967 /* in contrast to the raid5 case we can validate
2968 * parity, but still have a failure to write
2969 * back
2970 */
2971 sh->check_state = check_state_compute_result;
2972 /* Returning at this point means that we may go
2973 * off and bring p and/or q uptodate again so
2974 * we make sure to check zero_sum_result again
2975 * to verify if p or q need writeback
2976 */
2977 }
2978 } else {
2979 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2980 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2981 /* don't try to repair!! */
2982 set_bit(STRIPE_INSYNC, &sh->state);
2983 else {
2984 int *target = &sh->ops.target;
2985
2986 sh->ops.target = -1;
2987 sh->ops.target2 = -1;
2988 sh->check_state = check_state_compute_run;
2989 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2990 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2991 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
2992 set_bit(R5_Wantcompute,
2993 &sh->dev[pd_idx].flags);
2994 *target = pd_idx;
2995 target = &sh->ops.target2;
2996 s->uptodate++;
2997 }
2998 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
2999 set_bit(R5_Wantcompute,
3000 &sh->dev[qd_idx].flags);
3001 *target = qd_idx;
3002 s->uptodate++;
3003 }
3004 }
3005 }
3006 break;
3007 case check_state_compute_run:
3008 break;
3009 default:
3010 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3011 __func__, sh->check_state,
3012 (unsigned long long) sh->sector);
3013 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003014 }
3015}
3016
NeilBrownd1688a62011-10-11 16:49:52 +11003017static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003018{
3019 int i;
3020
3021 /* We have read all the blocks in this stripe and now we need to
3022 * copy some of them into a target stripe for expand.
3023 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003024 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003025 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3026 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003027 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003028 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003029 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003030 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003031
NeilBrown784052e2009-03-31 15:19:07 +11003032 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003033 sector_t s = raid5_compute_sector(conf, bn, 0,
3034 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003035 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003036 if (sh2 == NULL)
3037 /* so far only the early blocks of this stripe
3038 * have been requested. When later blocks
3039 * get requested, we will try again
3040 */
3041 continue;
3042 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3043 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3044 /* must have already done this block */
3045 release_stripe(sh2);
3046 continue;
3047 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003048
3049 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003050 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003051 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003052 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003053 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003054
Dan Williamsa4456852007-07-09 11:56:43 -07003055 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3056 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3057 for (j = 0; j < conf->raid_disks; j++)
3058 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003059 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003060 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3061 break;
3062 if (j == conf->raid_disks) {
3063 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3064 set_bit(STRIPE_HANDLE, &sh2->state);
3065 }
3066 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003067
Dan Williamsa4456852007-07-09 11:56:43 -07003068 }
NeilBrowna2e08552007-09-11 15:23:36 -07003069 /* done submitting copies, wait for them to complete */
3070 if (tx) {
3071 async_tx_ack(tx);
3072 dma_wait_for_async_tx(tx);
3073 }
Dan Williamsa4456852007-07-09 11:56:43 -07003074}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
3076/*
3077 * handle_stripe - do things to a stripe.
3078 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003079 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3080 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003082 * return some read requests which now have data
3083 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 * schedule a read on some buffers
3085 * schedule a write of some buffers
3086 * return confirmation of parity correctness
3087 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 */
Dan Williamsa4456852007-07-09 11:56:43 -07003089
NeilBrownacfe7262011-07-27 11:00:36 +10003090static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003091{
NeilBrownd1688a62011-10-11 16:49:52 +11003092 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003093 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003094 struct r5dev *dev;
3095 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003096 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003097
NeilBrownacfe7262011-07-27 11:00:36 +10003098 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003099
NeilBrownacfe7262011-07-27 11:00:36 +10003100 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3101 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3102 s->failed_num[0] = -1;
3103 s->failed_num[1] = -1;
3104
3105 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003106 rcu_read_lock();
NeilBrownc4c16632011-07-26 11:34:20 +10003107 spin_lock_irq(&conf->device_lock);
NeilBrown16a53ec2006-06-26 00:27:38 -07003108 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003109 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003110 sector_t first_bad;
3111 int bad_sectors;
3112 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003113
NeilBrown16a53ec2006-06-26 00:27:38 -07003114 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003115
Dan Williams45b42332007-07-09 11:56:43 -07003116 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003117 i, dev->flags,
3118 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003119 /* maybe we can reply to a read
3120 *
3121 * new wantfill requests are only permitted while
3122 * ops_complete_biofill is guaranteed to be inactive
3123 */
3124 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3125 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3126 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003127
3128 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003129 if (test_bit(R5_LOCKED, &dev->flags))
3130 s->locked++;
3131 if (test_bit(R5_UPTODATE, &dev->flags))
3132 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003133 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003134 s->compute++;
3135 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003136 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003137
NeilBrownacfe7262011-07-27 11:00:36 +10003138 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003139 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003140 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003141 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003142 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003143 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003144 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003145 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003146 }
Dan Williamsa4456852007-07-09 11:56:43 -07003147 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003148 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003149 /* Prefer to use the replacement for reads, but only
3150 * if it is recovered enough and has no bad blocks.
3151 */
3152 rdev = rcu_dereference(conf->disks[i].replacement);
3153 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3154 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3155 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3156 &first_bad, &bad_sectors))
3157 set_bit(R5_ReadRepl, &dev->flags);
3158 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003159 if (rdev)
3160 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003161 rdev = rcu_dereference(conf->disks[i].rdev);
3162 clear_bit(R5_ReadRepl, &dev->flags);
3163 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003164 if (rdev && test_bit(Faulty, &rdev->flags))
3165 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003166 if (rdev) {
3167 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3168 &first_bad, &bad_sectors);
3169 if (s->blocked_rdev == NULL
3170 && (test_bit(Blocked, &rdev->flags)
3171 || is_bad < 0)) {
3172 if (is_bad < 0)
3173 set_bit(BlockedBadBlocks,
3174 &rdev->flags);
3175 s->blocked_rdev = rdev;
3176 atomic_inc(&rdev->nr_pending);
3177 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003178 }
NeilBrown415e72d2010-06-17 17:25:21 +10003179 clear_bit(R5_Insync, &dev->flags);
3180 if (!rdev)
3181 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003182 else if (is_bad) {
3183 /* also not in-sync */
3184 if (!test_bit(WriteErrorSeen, &rdev->flags)) {
3185 /* treat as in-sync, but with a read error
3186 * which we can now try to correct
3187 */
3188 set_bit(R5_Insync, &dev->flags);
3189 set_bit(R5_ReadError, &dev->flags);
3190 }
3191 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003192 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003193 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003194 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003195 set_bit(R5_Insync, &dev->flags);
3196 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3197 test_bit(R5_Expanded, &dev->flags))
3198 /* If we've reshaped into here, we assume it is Insync.
3199 * We will shortly update recovery_offset to make
3200 * it official.
3201 */
3202 set_bit(R5_Insync, &dev->flags);
3203
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003204 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003205 /* This flag does not apply to '.replacement'
3206 * only to .rdev, so make sure to check that*/
3207 struct md_rdev *rdev2 = rcu_dereference(
3208 conf->disks[i].rdev);
3209 if (rdev2 == rdev)
3210 clear_bit(R5_Insync, &dev->flags);
3211 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003212 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003213 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003214 } else
3215 clear_bit(R5_WriteError, &dev->flags);
3216 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003217 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003218 /* This flag does not apply to '.replacement'
3219 * only to .rdev, so make sure to check that*/
3220 struct md_rdev *rdev2 = rcu_dereference(
3221 conf->disks[i].rdev);
3222 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003223 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003224 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003225 } else
3226 clear_bit(R5_MadeGood, &dev->flags);
3227 }
NeilBrown977df362011-12-23 10:17:53 +11003228 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3229 struct md_rdev *rdev2 = rcu_dereference(
3230 conf->disks[i].replacement);
3231 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3232 s->handle_bad_blocks = 1;
3233 atomic_inc(&rdev2->nr_pending);
3234 } else
3235 clear_bit(R5_MadeGoodRepl, &dev->flags);
3236 }
NeilBrown415e72d2010-06-17 17:25:21 +10003237 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003238 /* The ReadError flag will just be confusing now */
3239 clear_bit(R5_ReadError, &dev->flags);
3240 clear_bit(R5_ReWrite, &dev->flags);
3241 }
NeilBrown415e72d2010-06-17 17:25:21 +10003242 if (test_bit(R5_ReadError, &dev->flags))
3243 clear_bit(R5_Insync, &dev->flags);
3244 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003245 if (s->failed < 2)
3246 s->failed_num[s->failed] = i;
3247 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003248 if (rdev && !test_bit(Faulty, &rdev->flags))
3249 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003250 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003251 }
NeilBrownc4c16632011-07-26 11:34:20 +10003252 spin_unlock_irq(&conf->device_lock);
NeilBrown9a3e1102011-12-23 10:17:53 +11003253 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3254 /* If there is a failed device being replaced,
3255 * we must be recovering.
3256 * else if we are after recovery_cp, we must be syncing
3257 * else we can only be replacing
3258 * sync and recovery both need to read all devices, and so
3259 * use the same flag.
3260 */
3261 if (do_recovery ||
3262 sh->sector >= conf->mddev->recovery_cp)
3263 s->syncing = 1;
3264 else
3265 s->replacing = 1;
3266 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003267 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003268}
NeilBrownf4168852007-02-28 20:11:53 -08003269
NeilBrowncc940152011-07-26 11:35:35 +10003270static void handle_stripe(struct stripe_head *sh)
3271{
3272 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003273 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003274 int i;
NeilBrown84789552011-07-27 11:00:36 +10003275 int prexor;
3276 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003277 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003278
3279 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003280 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003281 /* already being handled, ensure it gets handled
3282 * again when current action finishes */
3283 set_bit(STRIPE_HANDLE, &sh->state);
3284 return;
3285 }
3286
3287 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3288 set_bit(STRIPE_SYNCING, &sh->state);
3289 clear_bit(STRIPE_INSYNC, &sh->state);
3290 }
3291 clear_bit(STRIPE_DELAYED, &sh->state);
3292
3293 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3294 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3295 (unsigned long long)sh->sector, sh->state,
3296 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3297 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003298
NeilBrownacfe7262011-07-27 11:00:36 +10003299 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003300
NeilBrownbc2607f2011-07-28 11:39:22 +10003301 if (s.handle_bad_blocks) {
3302 set_bit(STRIPE_HANDLE, &sh->state);
3303 goto finish;
3304 }
3305
NeilBrown474af965fe2011-07-27 11:00:36 +10003306 if (unlikely(s.blocked_rdev)) {
3307 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003308 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003309 set_bit(STRIPE_HANDLE, &sh->state);
3310 goto finish;
3311 }
3312 /* There is nothing for the blocked_rdev to block */
3313 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3314 s.blocked_rdev = NULL;
3315 }
3316
3317 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3318 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3319 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3320 }
3321
3322 pr_debug("locked=%d uptodate=%d to_read=%d"
3323 " to_write=%d failed=%d failed_num=%d,%d\n",
3324 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3325 s.failed_num[0], s.failed_num[1]);
3326 /* check if the array has lost more than max_degraded devices and,
3327 * if so, some requests might need to be failed.
3328 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003329 if (s.failed > conf->max_degraded) {
3330 sh->check_state = 0;
3331 sh->reconstruct_state = 0;
3332 if (s.to_read+s.to_write+s.written)
3333 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003334 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003335 handle_failed_sync(conf, sh, &s);
3336 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003337
3338 /*
3339 * might be able to return some write requests if the parity blocks
3340 * are safe, or on a failed drive
3341 */
3342 pdev = &sh->dev[sh->pd_idx];
3343 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3344 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3345 qdev = &sh->dev[sh->qd_idx];
3346 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3347 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3348 || conf->level < 6;
3349
3350 if (s.written &&
3351 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3352 && !test_bit(R5_LOCKED, &pdev->flags)
3353 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3354 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3355 && !test_bit(R5_LOCKED, &qdev->flags)
3356 && test_bit(R5_UPTODATE, &qdev->flags)))))
3357 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3358
3359 /* Now we might consider reading some blocks, either to check/generate
3360 * parity, or to satisfy requests
3361 * or to load a block that is being partially written.
3362 */
3363 if (s.to_read || s.non_overwrite
3364 || (conf->level == 6 && s.to_write && s.failed)
NeilBrown9a3e1102011-12-23 10:17:53 +11003365 || (s.syncing && (s.uptodate + s.compute < disks))
3366 || s.replacing
3367 || s.expanding)
NeilBrown474af965fe2011-07-27 11:00:36 +10003368 handle_stripe_fill(sh, &s, disks);
3369
NeilBrown84789552011-07-27 11:00:36 +10003370 /* Now we check to see if any write operations have recently
3371 * completed
3372 */
3373 prexor = 0;
3374 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3375 prexor = 1;
3376 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3377 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3378 sh->reconstruct_state = reconstruct_state_idle;
3379
3380 /* All the 'written' buffers and the parity block are ready to
3381 * be written back to disk
3382 */
3383 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3384 BUG_ON(sh->qd_idx >= 0 &&
3385 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags));
3386 for (i = disks; i--; ) {
3387 struct r5dev *dev = &sh->dev[i];
3388 if (test_bit(R5_LOCKED, &dev->flags) &&
3389 (i == sh->pd_idx || i == sh->qd_idx ||
3390 dev->written)) {
3391 pr_debug("Writing block %d\n", i);
3392 set_bit(R5_Wantwrite, &dev->flags);
3393 if (prexor)
3394 continue;
3395 if (!test_bit(R5_Insync, &dev->flags) ||
3396 ((i == sh->pd_idx || i == sh->qd_idx) &&
3397 s.failed == 0))
3398 set_bit(STRIPE_INSYNC, &sh->state);
3399 }
3400 }
3401 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3402 s.dec_preread_active = 1;
3403 }
3404
3405 /* Now to consider new write requests and what else, if anything
3406 * should be read. We do not handle new writes when:
3407 * 1/ A 'write' operation (copy+xor) is already in flight.
3408 * 2/ A 'check' operation is in flight, as it may clobber the parity
3409 * block.
3410 */
3411 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3412 handle_stripe_dirtying(conf, sh, &s, disks);
3413
3414 /* maybe we need to check and possibly fix the parity for this stripe
3415 * Any reads will already have been scheduled, so we just see if enough
3416 * data is available. The parity check is held off while parity
3417 * dependent operations are in flight.
3418 */
3419 if (sh->check_state ||
3420 (s.syncing && s.locked == 0 &&
3421 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3422 !test_bit(STRIPE_INSYNC, &sh->state))) {
3423 if (conf->level == 6)
3424 handle_parity_checks6(conf, sh, &s, disks);
3425 else
3426 handle_parity_checks5(conf, sh, &s, disks);
3427 }
NeilBrownc5a31002011-07-27 11:00:36 +10003428
NeilBrown9a3e1102011-12-23 10:17:53 +11003429 if (s.replacing && s.locked == 0
3430 && !test_bit(STRIPE_INSYNC, &sh->state)) {
3431 /* Write out to replacement devices where possible */
3432 for (i = 0; i < conf->raid_disks; i++)
3433 if (test_bit(R5_UPTODATE, &sh->dev[i].flags) &&
3434 test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3435 set_bit(R5_WantReplace, &sh->dev[i].flags);
3436 set_bit(R5_LOCKED, &sh->dev[i].flags);
3437 s.locked++;
3438 }
3439 set_bit(STRIPE_INSYNC, &sh->state);
3440 }
3441 if ((s.syncing || s.replacing) && s.locked == 0 &&
3442 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003443 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3444 clear_bit(STRIPE_SYNCING, &sh->state);
3445 }
3446
3447 /* If the failed drives are just a ReadError, then we might need
3448 * to progress the repair/check process
3449 */
3450 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3451 for (i = 0; i < s.failed; i++) {
3452 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3453 if (test_bit(R5_ReadError, &dev->flags)
3454 && !test_bit(R5_LOCKED, &dev->flags)
3455 && test_bit(R5_UPTODATE, &dev->flags)
3456 ) {
3457 if (!test_bit(R5_ReWrite, &dev->flags)) {
3458 set_bit(R5_Wantwrite, &dev->flags);
3459 set_bit(R5_ReWrite, &dev->flags);
3460 set_bit(R5_LOCKED, &dev->flags);
3461 s.locked++;
3462 } else {
3463 /* let's read it back */
3464 set_bit(R5_Wantread, &dev->flags);
3465 set_bit(R5_LOCKED, &dev->flags);
3466 s.locked++;
3467 }
3468 }
3469 }
3470
3471
NeilBrown3687c062011-07-27 11:00:36 +10003472 /* Finish reconstruct operations initiated by the expansion process */
3473 if (sh->reconstruct_state == reconstruct_state_result) {
3474 struct stripe_head *sh_src
3475 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3476 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3477 /* sh cannot be written until sh_src has been read.
3478 * so arrange for sh to be delayed a little
3479 */
3480 set_bit(STRIPE_DELAYED, &sh->state);
3481 set_bit(STRIPE_HANDLE, &sh->state);
3482 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3483 &sh_src->state))
3484 atomic_inc(&conf->preread_active_stripes);
3485 release_stripe(sh_src);
3486 goto finish;
3487 }
3488 if (sh_src)
3489 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003490
NeilBrown3687c062011-07-27 11:00:36 +10003491 sh->reconstruct_state = reconstruct_state_idle;
3492 clear_bit(STRIPE_EXPANDING, &sh->state);
3493 for (i = conf->raid_disks; i--; ) {
3494 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3495 set_bit(R5_LOCKED, &sh->dev[i].flags);
3496 s.locked++;
3497 }
3498 }
3499
3500 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3501 !sh->reconstruct_state) {
3502 /* Need to write out all blocks after computing parity */
3503 sh->disks = conf->raid_disks;
3504 stripe_set_idx(sh->sector, conf, 0, sh);
3505 schedule_reconstruction(sh, &s, 1, 1);
3506 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3507 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3508 atomic_dec(&conf->reshape_stripes);
3509 wake_up(&conf->wait_for_overlap);
3510 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3511 }
3512
3513 if (s.expanding && s.locked == 0 &&
3514 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3515 handle_stripe_expansion(conf, sh);
3516
3517finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003518 /* wait for this device to become unblocked */
NeilBrown43220aa2011-08-31 12:49:14 +10003519 if (conf->mddev->external && unlikely(s.blocked_rdev))
NeilBrownc5709ef2011-07-26 11:35:20 +10003520 md_wait_for_blocked_rdev(s.blocked_rdev, conf->mddev);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003521
NeilBrownbc2607f2011-07-28 11:39:22 +10003522 if (s.handle_bad_blocks)
3523 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003524 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003525 struct r5dev *dev = &sh->dev[i];
3526 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3527 /* We own a safe reference to the rdev */
3528 rdev = conf->disks[i].rdev;
3529 if (!rdev_set_badblocks(rdev, sh->sector,
3530 STRIPE_SECTORS, 0))
3531 md_error(conf->mddev, rdev);
3532 rdev_dec_pending(rdev, conf->mddev);
3533 }
NeilBrownb84db562011-07-28 11:39:23 +10003534 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3535 rdev = conf->disks[i].rdev;
3536 rdev_clear_badblocks(rdev, sh->sector,
3537 STRIPE_SECTORS);
3538 rdev_dec_pending(rdev, conf->mddev);
3539 }
NeilBrown977df362011-12-23 10:17:53 +11003540 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3541 rdev = conf->disks[i].replacement;
3542 rdev_clear_badblocks(rdev, sh->sector,
3543 STRIPE_SECTORS);
3544 rdev_dec_pending(rdev, conf->mddev);
3545 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003546 }
3547
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003548 if (s.ops_request)
3549 raid_run_ops(sh, s.ops_request);
3550
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003551 ops_run_io(sh, &s);
3552
NeilBrownc5709ef2011-07-26 11:35:20 +10003553 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003554 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003555 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003556 * have actually been submitted.
3557 */
3558 atomic_dec(&conf->preread_active_stripes);
3559 if (atomic_read(&conf->preread_active_stripes) <
3560 IO_THRESHOLD)
3561 md_wakeup_thread(conf->mddev->thread);
3562 }
3563
NeilBrownc5709ef2011-07-26 11:35:20 +10003564 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003565
Dan Williams257a4b42011-11-08 16:22:06 +11003566 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003567}
3568
NeilBrownd1688a62011-10-11 16:49:52 +11003569static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570{
3571 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3572 while (!list_empty(&conf->delayed_list)) {
3573 struct list_head *l = conf->delayed_list.next;
3574 struct stripe_head *sh;
3575 sh = list_entry(l, struct stripe_head, lru);
3576 list_del_init(l);
3577 clear_bit(STRIPE_DELAYED, &sh->state);
3578 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3579 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003580 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 }
NeilBrown482c0832011-04-18 18:25:42 +10003582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583}
3584
NeilBrownd1688a62011-10-11 16:49:52 +11003585static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003586{
3587 /* device_lock is held */
3588 struct list_head head;
3589 list_add(&head, &conf->bitmap_list);
3590 list_del_init(&conf->bitmap_list);
3591 while (!list_empty(&head)) {
3592 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3593 list_del_init(&sh->lru);
3594 atomic_inc(&sh->count);
3595 __release_stripe(conf, sh);
3596 }
3597}
3598
NeilBrownfd01b882011-10-11 16:47:53 +11003599int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003600{
NeilBrownd1688a62011-10-11 16:49:52 +11003601 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003602
3603 /* No difference between reads and writes. Just check
3604 * how busy the stripe_cache is
3605 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003606
NeilBrownf022b2f2006-10-03 01:15:56 -07003607 if (conf->inactive_blocked)
3608 return 1;
3609 if (conf->quiesce)
3610 return 1;
3611 if (list_empty_careful(&conf->inactive_list))
3612 return 1;
3613
3614 return 0;
3615}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003616EXPORT_SYMBOL_GPL(md_raid5_congested);
3617
3618static int raid5_congested(void *data, int bits)
3619{
NeilBrownfd01b882011-10-11 16:47:53 +11003620 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003621
3622 return mddev_congested(mddev, bits) ||
3623 md_raid5_congested(mddev, bits);
3624}
NeilBrownf022b2f2006-10-03 01:15:56 -07003625
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003626/* We want read requests to align with chunks where possible,
3627 * but write requests don't need to.
3628 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003629static int raid5_mergeable_bvec(struct request_queue *q,
3630 struct bvec_merge_data *bvm,
3631 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003632{
NeilBrownfd01b882011-10-11 16:47:53 +11003633 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003634 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003635 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003636 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003637 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003638
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003639 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003640 return biovec->bv_len; /* always allow writes to be mergeable */
3641
Andre Noll664e7c42009-06-18 08:45:27 +10003642 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3643 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003644 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3645 if (max < 0) max = 0;
3646 if (max <= biovec->bv_len && bio_sectors == 0)
3647 return biovec->bv_len;
3648 else
3649 return max;
3650}
3651
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003652
NeilBrownfd01b882011-10-11 16:47:53 +11003653static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003654{
3655 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003656 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003657 unsigned int bio_sectors = bio->bi_size >> 9;
3658
Andre Noll664e7c42009-06-18 08:45:27 +10003659 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3660 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003661 return chunk_sectors >=
3662 ((sector & (chunk_sectors - 1)) + bio_sectors);
3663}
3664
3665/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003666 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3667 * later sampled by raid5d.
3668 */
NeilBrownd1688a62011-10-11 16:49:52 +11003669static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003670{
3671 unsigned long flags;
3672
3673 spin_lock_irqsave(&conf->device_lock, flags);
3674
3675 bi->bi_next = conf->retry_read_aligned_list;
3676 conf->retry_read_aligned_list = bi;
3677
3678 spin_unlock_irqrestore(&conf->device_lock, flags);
3679 md_wakeup_thread(conf->mddev->thread);
3680}
3681
3682
NeilBrownd1688a62011-10-11 16:49:52 +11003683static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003684{
3685 struct bio *bi;
3686
3687 bi = conf->retry_read_aligned;
3688 if (bi) {
3689 conf->retry_read_aligned = NULL;
3690 return bi;
3691 }
3692 bi = conf->retry_read_aligned_list;
3693 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003694 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003695 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003696 /*
3697 * this sets the active strip count to 1 and the processed
3698 * strip count to zero (upper 8 bits)
3699 */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003700 bi->bi_phys_segments = 1; /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003701 }
3702
3703 return bi;
3704}
3705
3706
3707/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003708 * The "raid5_align_endio" should check if the read succeeded and if it
3709 * did, call bio_endio on the original bio (having bio_put the new bio
3710 * first).
3711 * If the read failed..
3712 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003713static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003714{
3715 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003716 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003717 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003718 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003719 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003720
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003721 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003722
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003723 rdev = (void*)raid_bi->bi_next;
3724 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003725 mddev = rdev->mddev;
3726 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003727
3728 rdev_dec_pending(rdev, conf->mddev);
3729
3730 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003731 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003732 if (atomic_dec_and_test(&conf->active_aligned_reads))
3733 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003734 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003735 }
3736
3737
Dan Williams45b42332007-07-09 11:56:43 -07003738 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003739
3740 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003741}
3742
Neil Brown387bb172007-02-08 14:20:29 -08003743static int bio_fits_rdev(struct bio *bi)
3744{
Jens Axboe165125e2007-07-24 09:28:11 +02003745 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003746
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003747 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003748 return 0;
3749 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003750 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003751 return 0;
3752
3753 if (q->merge_bvec_fn)
3754 /* it's too hard to apply the merge_bvec_fn at this stage,
3755 * just just give up
3756 */
3757 return 0;
3758
3759 return 1;
3760}
3761
3762
NeilBrownfd01b882011-10-11 16:47:53 +11003763static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003764{
NeilBrownd1688a62011-10-11 16:49:52 +11003765 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11003766 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003767 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11003768 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11003769 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003770
3771 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003772 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003773 return 0;
3774 }
3775 /*
NeilBrowna167f662010-10-26 18:31:13 +11003776 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003777 */
NeilBrowna167f662010-10-26 18:31:13 +11003778 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003779 if (!align_bi)
3780 return 0;
3781 /*
3782 * set bi_end_io to a new function, and set bi_private to the
3783 * original bio.
3784 */
3785 align_bi->bi_end_io = raid5_align_endio;
3786 align_bi->bi_private = raid_bio;
3787 /*
3788 * compute position
3789 */
NeilBrown112bf892009-03-31 14:39:38 +11003790 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3791 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003792 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003793
NeilBrown671488c2011-12-23 10:17:52 +11003794 end_sector = align_bi->bi_sector + (align_bi->bi_size >> 9);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003795 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11003796 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
3797 if (!rdev || test_bit(Faulty, &rdev->flags) ||
3798 rdev->recovery_offset < end_sector) {
3799 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3800 if (rdev &&
3801 (test_bit(Faulty, &rdev->flags) ||
3802 !(test_bit(In_sync, &rdev->flags) ||
3803 rdev->recovery_offset >= end_sector)))
3804 rdev = NULL;
3805 }
3806 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10003807 sector_t first_bad;
3808 int bad_sectors;
3809
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003810 atomic_inc(&rdev->nr_pending);
3811 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003812 raid_bio->bi_next = (void*)rdev;
3813 align_bi->bi_bdev = rdev->bdev;
3814 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
3815 align_bi->bi_sector += rdev->data_offset;
3816
NeilBrown31c176e2011-07-28 11:39:22 +10003817 if (!bio_fits_rdev(align_bi) ||
3818 is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
3819 &first_bad, &bad_sectors)) {
3820 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08003821 bio_put(align_bi);
3822 rdev_dec_pending(rdev, mddev);
3823 return 0;
3824 }
3825
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003826 spin_lock_irq(&conf->device_lock);
3827 wait_event_lock_irq(conf->wait_for_stripe,
3828 conf->quiesce == 0,
3829 conf->device_lock, /* nothing */);
3830 atomic_inc(&conf->active_aligned_reads);
3831 spin_unlock_irq(&conf->device_lock);
3832
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003833 generic_make_request(align_bi);
3834 return 1;
3835 } else {
3836 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003837 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003838 return 0;
3839 }
3840}
3841
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003842/* __get_priority_stripe - get the next stripe to process
3843 *
3844 * Full stripe writes are allowed to pass preread active stripes up until
3845 * the bypass_threshold is exceeded. In general the bypass_count
3846 * increments when the handle_list is handled before the hold_list; however, it
3847 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3848 * stripe with in flight i/o. The bypass_count will be reset when the
3849 * head of the hold_list has changed, i.e. the head was promoted to the
3850 * handle_list.
3851 */
NeilBrownd1688a62011-10-11 16:49:52 +11003852static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003853{
3854 struct stripe_head *sh;
3855
3856 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3857 __func__,
3858 list_empty(&conf->handle_list) ? "empty" : "busy",
3859 list_empty(&conf->hold_list) ? "empty" : "busy",
3860 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3861
3862 if (!list_empty(&conf->handle_list)) {
3863 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3864
3865 if (list_empty(&conf->hold_list))
3866 conf->bypass_count = 0;
3867 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3868 if (conf->hold_list.next == conf->last_hold)
3869 conf->bypass_count++;
3870 else {
3871 conf->last_hold = conf->hold_list.next;
3872 conf->bypass_count -= conf->bypass_threshold;
3873 if (conf->bypass_count < 0)
3874 conf->bypass_count = 0;
3875 }
3876 }
3877 } else if (!list_empty(&conf->hold_list) &&
3878 ((conf->bypass_threshold &&
3879 conf->bypass_count > conf->bypass_threshold) ||
3880 atomic_read(&conf->pending_full_writes) == 0)) {
3881 sh = list_entry(conf->hold_list.next,
3882 typeof(*sh), lru);
3883 conf->bypass_count -= conf->bypass_threshold;
3884 if (conf->bypass_count < 0)
3885 conf->bypass_count = 0;
3886 } else
3887 return NULL;
3888
3889 list_del_init(&sh->lru);
3890 atomic_inc(&sh->count);
3891 BUG_ON(atomic_read(&sh->count) != 1);
3892 return sh;
3893}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003894
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07003895static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896{
NeilBrownd1688a62011-10-11 16:49:52 +11003897 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11003898 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 sector_t new_sector;
3900 sector_t logical_sector, last_sector;
3901 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01003902 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11003903 int remaining;
NeilBrown7c13edc2011-04-18 18:25:43 +10003904 int plugged;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905
Tejun Heoe9c74692010-09-03 11:56:18 +02003906 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
3907 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02003908 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07003909 }
3910
NeilBrown3d310eb2005-06-21 17:17:26 -07003911 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07003912
NeilBrown802ba062006-12-13 00:34:13 -08003913 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003914 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11003915 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02003916 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003917
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
3919 last_sector = bi->bi_sector + (bi->bi_size>>9);
3920 bi->bi_next = NULL;
3921 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07003922
NeilBrown7c13edc2011-04-18 18:25:43 +10003923 plugged = mddev_check_plugged(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
3925 DEFINE_WAIT(w);
NeilBrown16a53ec2006-06-26 00:27:38 -07003926 int disks, data_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003927 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08003928
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003929 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11003930 previous = 0;
NeilBrownb0f9ec02009-03-31 15:27:18 +11003931 disks = conf->raid_disks;
NeilBrownb578d552006-03-27 01:18:12 -08003932 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11003933 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11003934 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f762006-03-27 01:18:15 -08003935 * 64bit on a 32bit platform, and so it might be
3936 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02003937 * Of course reshape_progress could change after
NeilBrowndf8e7f762006-03-27 01:18:15 -08003938 * the lock is dropped, so once we get a reference
3939 * to the stripe that we think it is, we will have
3940 * to check again.
3941 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003942 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11003943 if (mddev->delta_disks < 0
3944 ? logical_sector < conf->reshape_progress
3945 : logical_sector >= conf->reshape_progress) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003946 disks = conf->previous_raid_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003947 previous = 1;
3948 } else {
NeilBrownfef9c612009-03-31 15:16:46 +11003949 if (mddev->delta_disks < 0
3950 ? logical_sector < conf->reshape_safe
3951 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08003952 spin_unlock_irq(&conf->device_lock);
3953 schedule();
3954 goto retry;
3955 }
3956 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003957 spin_unlock_irq(&conf->device_lock);
3958 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003959 data_disks = disks - conf->max_degraded;
3960
NeilBrown112bf892009-03-31 14:39:38 +11003961 new_sector = raid5_compute_sector(conf, logical_sector,
3962 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003963 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10003964 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 (unsigned long long)new_sector,
3966 (unsigned long long)logical_sector);
3967
NeilBrownb5663ba2009-03-31 14:39:38 +11003968 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10003969 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11003971 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003972 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f762006-03-27 01:18:15 -08003973 * stripe, so we must do the range check again.
3974 * Expansion could still move past after this
3975 * test, but as we are holding a reference to
3976 * 'sh', we know that if that happens,
3977 * STRIPE_EXPANDING will get set and the expansion
3978 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003979 */
3980 int must_retry = 0;
3981 spin_lock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11003982 if (mddev->delta_disks < 0
3983 ? logical_sector >= conf->reshape_progress
3984 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003985 /* mismatch, need to try again */
3986 must_retry = 1;
3987 spin_unlock_irq(&conf->device_lock);
3988 if (must_retry) {
3989 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07003990 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003991 goto retry;
3992 }
3993 }
NeilBrowne62e58a2009-07-01 13:15:35 +10003994
Namhyung Kimffd96e32011-07-18 17:38:51 +10003995 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10003996 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08003997 logical_sector < mddev->suspend_hi) {
3998 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10003999 /* As the suspend_* range is controlled by
4000 * userspace, we want an interruptible
4001 * wait.
4002 */
4003 flush_signals(current);
4004 prepare_to_wait(&conf->wait_for_overlap,
4005 &w, TASK_INTERRUPTIBLE);
4006 if (logical_sector >= mddev->suspend_lo &&
4007 logical_sector < mddev->suspend_hi)
4008 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004009 goto retry;
4010 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004011
4012 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004013 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004014 /* Stripe is busy expanding or
4015 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 * and wait a while
4017 */
NeilBrown482c0832011-04-18 18:25:42 +10004018 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 release_stripe(sh);
4020 schedule();
4021 goto retry;
4022 }
4023 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004024 set_bit(STRIPE_HANDLE, &sh->state);
4025 clear_bit(STRIPE_DELAYED, &sh->state);
Tejun Heoe9c74692010-09-03 11:56:18 +02004026 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004027 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4028 atomic_inc(&conf->preread_active_stripes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 } else {
4031 /* cannot get stripe for read-ahead, just give-up */
4032 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4033 finish_wait(&conf->wait_for_overlap, &w);
4034 break;
4035 }
4036
4037 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004038 if (!plugged)
4039 md_wakeup_thread(mddev->thread);
4040
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004042 remaining = raid5_dec_bi_phys_segments(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004043 spin_unlock_irq(&conf->device_lock);
4044 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045
NeilBrown16a53ec2006-06-26 00:27:38 -07004046 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004048
Neil Brown0e13fe232008-06-28 08:31:20 +10004049 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051}
4052
NeilBrownfd01b882011-10-11 16:47:53 +11004053static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004054
NeilBrownfd01b882011-10-11 16:47:53 +11004055static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056{
NeilBrown52c03292006-06-26 00:27:43 -07004057 /* reshaping is quite different to recovery/resync so it is
4058 * handled quite separately ... here.
4059 *
4060 * On each call to sync_request, we gather one chunk worth of
4061 * destination stripes and flag them as expanding.
4062 * Then we find all the source stripes and request reads.
4063 * As the reads complete, handle_stripe will copy the data
4064 * into the destination stripe and release that stripe.
4065 */
NeilBrownd1688a62011-10-11 16:49:52 +11004066 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004068 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004069 int raid_disks = conf->previous_raid_disks;
4070 int data_disks = raid_disks - conf->max_degraded;
4071 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004072 int i;
4073 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004074 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004075 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004076 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004077 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004078
NeilBrownfef9c612009-03-31 15:16:46 +11004079 if (sector_nr == 0) {
4080 /* If restarting in the middle, skip the initial sectors */
4081 if (mddev->delta_disks < 0 &&
4082 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4083 sector_nr = raid5_size(mddev, 0, 0)
4084 - conf->reshape_progress;
NeilBrowna6397552009-08-13 10:13:00 +10004085 } else if (mddev->delta_disks >= 0 &&
NeilBrownfef9c612009-03-31 15:16:46 +11004086 conf->reshape_progress > 0)
4087 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004088 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004089 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004090 mddev->curr_resync_completed = sector_nr;
4091 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004092 *skipped = 1;
4093 return sector_nr;
4094 }
NeilBrown52c03292006-06-26 00:27:43 -07004095 }
4096
NeilBrown7a661382009-03-31 15:21:40 +11004097 /* We need to process a full chunk at a time.
4098 * If old and new chunk sizes differ, we need to process the
4099 * largest of these
4100 */
Andre Noll664e7c42009-06-18 08:45:27 +10004101 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4102 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004103 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004104 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004105
NeilBrown52c03292006-06-26 00:27:43 -07004106 /* we update the metadata when there is more than 3Meg
4107 * in the block range (that is rather arbitrary, should
4108 * probably be time based) or when the data about to be
4109 * copied would over-write the source of the data at
4110 * the front of the range.
NeilBrownfef9c612009-03-31 15:16:46 +11004111 * i.e. one new_stripe along from reshape_progress new_maps
4112 * to after where reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004113 */
NeilBrownfef9c612009-03-31 15:16:46 +11004114 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004115 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004116 readpos = conf->reshape_progress;
4117 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004118 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004119 sector_div(safepos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004120 if (mddev->delta_disks < 0) {
NeilBrowned37d832009-05-27 21:39:05 +10004121 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004122 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004123 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004124 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004125 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004126 readpos -= min_t(sector_t, reshape_sectors, readpos);
4127 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004128 }
NeilBrown52c03292006-06-26 00:27:43 -07004129
NeilBrownc8f517c2009-03-31 15:28:40 +11004130 /* 'writepos' is the most advanced device address we might write.
4131 * 'readpos' is the least advanced device address we might read.
4132 * 'safepos' is the least address recorded in the metadata as having
4133 * been reshaped.
4134 * If 'readpos' is behind 'writepos', then there is no way that we can
4135 * ensure safety in the face of a crash - that must be done by userspace
4136 * making a backup of the data. So in that case there is no particular
4137 * rush to update metadata.
4138 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4139 * update the metadata to advance 'safepos' to match 'readpos' so that
4140 * we can be safe in the event of a crash.
4141 * So we insist on updating metadata if safepos is behind writepos and
4142 * readpos is beyond writepos.
4143 * In any case, update the metadata every 10 seconds.
4144 * Maybe that number should be configurable, but I'm not sure it is
4145 * worth it.... maybe it could be a multiple of safemode_delay???
4146 */
NeilBrownfef9c612009-03-31 15:16:46 +11004147 if ((mddev->delta_disks < 0
NeilBrownc8f517c2009-03-31 15:28:40 +11004148 ? (safepos > writepos && readpos < writepos)
4149 : (safepos < writepos && readpos > writepos)) ||
4150 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004151 /* Cannot proceed until we've updated the superblock... */
4152 wait_event(conf->wait_for_overlap,
4153 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004154 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004155 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004156 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b422006-10-03 01:15:46 -07004157 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004158 md_wakeup_thread(mddev->thread);
NeilBrown850b2b422006-10-03 01:15:46 -07004159 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004160 kthread_should_stop());
4161 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004162 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004163 spin_unlock_irq(&conf->device_lock);
4164 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004165 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004166 }
4167
NeilBrownec32a2b2009-03-31 15:17:38 +11004168 if (mddev->delta_disks < 0) {
4169 BUG_ON(conf->reshape_progress == 0);
4170 stripe_addr = writepos;
4171 BUG_ON((mddev->dev_sectors &
NeilBrown7a661382009-03-31 15:21:40 +11004172 ~((sector_t)reshape_sectors - 1))
4173 - reshape_sectors - stripe_addr
NeilBrownec32a2b2009-03-31 15:17:38 +11004174 != sector_nr);
4175 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004176 BUG_ON(writepos != sector_nr + reshape_sectors);
NeilBrownec32a2b2009-03-31 15:17:38 +11004177 stripe_addr = sector_nr;
4178 }
NeilBrownab69ae12009-03-31 15:26:47 +11004179 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004180 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004181 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004182 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004183 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004184 set_bit(STRIPE_EXPANDING, &sh->state);
4185 atomic_inc(&conf->reshape_stripes);
4186 /* If any of this stripe is beyond the end of the old
4187 * array, then we need to zero those blocks
4188 */
4189 for (j=sh->disks; j--;) {
4190 sector_t s;
4191 if (j == sh->pd_idx)
4192 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004193 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004194 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004195 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004196 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004197 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004198 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004199 continue;
4200 }
4201 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4202 set_bit(R5_Expanded, &sh->dev[j].flags);
4203 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4204 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004205 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004206 set_bit(STRIPE_EXPAND_READY, &sh->state);
4207 set_bit(STRIPE_HANDLE, &sh->state);
4208 }
NeilBrownab69ae12009-03-31 15:26:47 +11004209 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004210 }
4211 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004212 if (mddev->delta_disks < 0)
NeilBrown7a661382009-03-31 15:21:40 +11004213 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004214 else
NeilBrown7a661382009-03-31 15:21:40 +11004215 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004216 spin_unlock_irq(&conf->device_lock);
4217 /* Ok, those stripe are ready. We can start scheduling
4218 * reads on the source stripes.
4219 * The source stripes are determined by mapping the first and last
4220 * block on the destination stripes.
4221 */
NeilBrown52c03292006-06-26 00:27:43 -07004222 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004223 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004224 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004225 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004226 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004227 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004228 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004229 if (last_sector >= mddev->dev_sectors)
4230 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004231 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004232 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004233 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4234 set_bit(STRIPE_HANDLE, &sh->state);
4235 release_stripe(sh);
4236 first_sector += STRIPE_SECTORS;
4237 }
NeilBrownab69ae12009-03-31 15:26:47 +11004238 /* Now that the sources are clearly marked, we can release
4239 * the destination stripes
4240 */
4241 while (!list_empty(&stripes)) {
4242 sh = list_entry(stripes.next, struct stripe_head, lru);
4243 list_del_init(&sh->lru);
4244 release_stripe(sh);
4245 }
NeilBrownc6207272008-02-06 01:39:52 -08004246 /* If this takes us to the resync_max point where we have to pause,
4247 * then we need to write out the superblock.
4248 */
NeilBrown7a661382009-03-31 15:21:40 +11004249 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004250 if ((sector_nr - mddev->curr_resync_completed) * 2
4251 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004252 /* Cannot proceed until we've updated the superblock... */
4253 wait_event(conf->wait_for_overlap,
4254 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004255 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004256 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004257 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004258 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4259 md_wakeup_thread(mddev->thread);
4260 wait_event(mddev->sb_wait,
4261 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4262 || kthread_should_stop());
4263 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004264 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004265 spin_unlock_irq(&conf->device_lock);
4266 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004267 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004268 }
NeilBrown7a661382009-03-31 15:21:40 +11004269 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004270}
4271
4272/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004273static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004274{
NeilBrownd1688a62011-10-11 16:49:52 +11004275 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004276 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004277 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004278 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004279 int still_degraded = 0;
4280 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281
NeilBrown72626682005-09-09 16:23:54 -07004282 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004284
NeilBrown29269552006-03-27 01:18:10 -08004285 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4286 end_reshape(conf);
4287 return 0;
4288 }
NeilBrown72626682005-09-09 16:23:54 -07004289
4290 if (mddev->curr_resync < max_sector) /* aborted */
4291 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4292 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004293 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004294 conf->fullsync = 0;
4295 bitmap_close_sync(mddev->bitmap);
4296
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 return 0;
4298 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004299
NeilBrown64bd6602009-08-03 10:59:58 +10004300 /* Allow raid5_quiesce to complete */
4301 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4302
NeilBrown52c03292006-06-26 00:27:43 -07004303 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4304 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004305
NeilBrownc6207272008-02-06 01:39:52 -08004306 /* No need to check resync_max as we never do more than one
4307 * stripe, and as resync_max will always be on a chunk boundary,
4308 * if the check in md_do_sync didn't fire, there is no chance
4309 * of overstepping resync_max here
4310 */
4311
NeilBrown16a53ec2006-06-26 00:27:38 -07004312 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 * to resync, then assert that we are finished, because there is
4314 * nothing we can do.
4315 */
NeilBrown3285edf2006-06-26 00:27:55 -07004316 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004317 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004318 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004319 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 return rv;
4321 }
NeilBrown72626682005-09-09 16:23:54 -07004322 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004323 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004324 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4325 /* we can skip this block, and probably more */
4326 sync_blocks /= STRIPE_SECTORS;
4327 *skipped = 1;
4328 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330
NeilBrownb47490c2008-02-06 01:39:50 -08004331 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4332
NeilBrowna8c906c2009-06-09 14:39:59 +10004333 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004335 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004337 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004339 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004341 /* Need to check if array will still be degraded after recovery/resync
4342 * We don't need to check the 'failed' flag as when that gets set,
4343 * recovery aborts.
4344 */
NeilBrownf001a702009-06-09 14:30:31 +10004345 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004346 if (conf->disks[i].rdev == NULL)
4347 still_degraded = 1;
4348
4349 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4350
NeilBrown83206d62011-07-26 11:19:49 +10004351 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352
NeilBrown14425772009-10-16 15:55:25 +11004353 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 release_stripe(sh);
4355
4356 return STRIPE_SECTORS;
4357}
4358
NeilBrownd1688a62011-10-11 16:49:52 +11004359static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004360{
4361 /* We may not be able to submit a whole bio at once as there
4362 * may not be enough stripe_heads available.
4363 * We cannot pre-allocate enough stripe_heads as we may need
4364 * more than exist in the cache (if we allow ever large chunks).
4365 * So we do one stripe head at a time and record in
4366 * ->bi_hw_segments how many have been done.
4367 *
4368 * We *know* that this entire raid_bio is in one chunk, so
4369 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4370 */
4371 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004372 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004373 sector_t sector, logical_sector, last_sector;
4374 int scnt = 0;
4375 int remaining;
4376 int handled = 0;
4377
4378 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004379 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004380 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004381 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4382
4383 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004384 logical_sector += STRIPE_SECTORS,
4385 sector += STRIPE_SECTORS,
4386 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004387
Jens Axboe960e7392008-08-15 10:41:18 +02004388 if (scnt < raid5_bi_hw_segments(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004389 /* already done this stripe */
4390 continue;
4391
NeilBrowna8c906c2009-06-09 14:39:59 +10004392 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004393
4394 if (!sh) {
4395 /* failed to get a stripe - must wait */
Jens Axboe960e7392008-08-15 10:41:18 +02004396 raid5_set_bi_hw_segments(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004397 conf->retry_read_aligned = raid_bio;
4398 return handled;
4399 }
4400
Neil Brown387bb172007-02-08 14:20:29 -08004401 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4402 release_stripe(sh);
Jens Axboe960e7392008-08-15 10:41:18 +02004403 raid5_set_bi_hw_segments(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004404 conf->retry_read_aligned = raid_bio;
4405 return handled;
4406 }
4407
Dan Williams36d1c642009-07-14 11:48:22 -07004408 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004409 release_stripe(sh);
4410 handled++;
4411 }
4412 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004413 remaining = raid5_dec_bi_phys_segments(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004414 spin_unlock_irq(&conf->device_lock);
Neil Brown0e13fe232008-06-28 08:31:20 +10004415 if (remaining == 0)
4416 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004417 if (atomic_dec_and_test(&conf->active_aligned_reads))
4418 wake_up(&conf->wait_for_stripe);
4419 return handled;
4420}
4421
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004422
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423/*
4424 * This is our raid5 kernel thread.
4425 *
4426 * We scan the hash table for stripes which can be handled now.
4427 * During the scan, completed stripes are saved for us by the interrupt
4428 * handler, so that they will not have to wait for our next wakeup.
4429 */
NeilBrownfd01b882011-10-11 16:47:53 +11004430static void raid5d(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431{
4432 struct stripe_head *sh;
NeilBrownd1688a62011-10-11 16:49:52 +11004433 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004435 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436
Dan Williams45b42332007-07-09 11:56:43 -07004437 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438
4439 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004441 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 handled = 0;
4443 spin_lock_irq(&conf->device_lock);
4444 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004445 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446
NeilBrown7c13edc2011-04-18 18:25:43 +10004447 if (atomic_read(&mddev->plug_cnt) == 0 &&
4448 !list_empty(&conf->bitmap_list)) {
4449 /* Now is a good time to flush some bitmap updates */
4450 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004451 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004452 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004453 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004454 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004455 activate_bit_delay(conf);
4456 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004457 if (atomic_read(&mddev->plug_cnt) == 0)
4458 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004459
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004460 while ((bio = remove_bio_from_retry(conf))) {
4461 int ok;
4462 spin_unlock_irq(&conf->device_lock);
4463 ok = retry_aligned_read(conf, bio);
4464 spin_lock_irq(&conf->device_lock);
4465 if (!ok)
4466 break;
4467 handled++;
4468 }
4469
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004470 sh = __get_priority_stripe(conf);
4471
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004472 if (!sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 spin_unlock_irq(&conf->device_lock);
4475
4476 handled++;
Dan Williams417b8d42009-10-16 16:25:22 +11004477 handle_stripe(sh);
4478 release_stripe(sh);
4479 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480
NeilBrownde393cd2011-07-28 11:31:48 +10004481 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
4482 md_check_recovery(mddev);
4483
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 spin_lock_irq(&conf->device_lock);
4485 }
Dan Williams45b42332007-07-09 11:56:43 -07004486 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487
4488 spin_unlock_irq(&conf->device_lock);
4489
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004490 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004491 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492
Dan Williams45b42332007-07-09 11:56:43 -07004493 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494}
4495
NeilBrown3f294f42005-11-08 21:39:25 -08004496static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004497raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004498{
NeilBrownd1688a62011-10-11 16:49:52 +11004499 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004500 if (conf)
4501 return sprintf(page, "%d\n", conf->max_nr_stripes);
4502 else
4503 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004504}
4505
NeilBrownc41d4ac2010-06-01 19:37:24 +10004506int
NeilBrownfd01b882011-10-11 16:47:53 +11004507raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004508{
NeilBrownd1688a62011-10-11 16:49:52 +11004509 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004510 int err;
4511
4512 if (size <= 16 || size > 32768)
4513 return -EINVAL;
4514 while (size < conf->max_nr_stripes) {
4515 if (drop_one_stripe(conf))
4516 conf->max_nr_stripes--;
4517 else
4518 break;
4519 }
4520 err = md_allow_write(mddev);
4521 if (err)
4522 return err;
4523 while (size > conf->max_nr_stripes) {
4524 if (grow_one_stripe(conf))
4525 conf->max_nr_stripes++;
4526 else break;
4527 }
4528 return 0;
4529}
4530EXPORT_SYMBOL(raid5_set_cache_size);
4531
NeilBrown3f294f42005-11-08 21:39:25 -08004532static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004533raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004534{
NeilBrownd1688a62011-10-11 16:49:52 +11004535 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004536 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004537 int err;
4538
NeilBrown3f294f42005-11-08 21:39:25 -08004539 if (len >= PAGE_SIZE)
4540 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004541 if (!conf)
4542 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004543
Dan Williams4ef197d82008-04-28 02:15:54 -07004544 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004545 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004546 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004547 if (err)
4548 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004549 return len;
4550}
NeilBrown007583c2005-11-08 21:39:30 -08004551
NeilBrown96de1e62005-11-08 21:39:39 -08004552static struct md_sysfs_entry
4553raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4554 raid5_show_stripe_cache_size,
4555 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004556
4557static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004558raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004559{
NeilBrownd1688a62011-10-11 16:49:52 +11004560 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004561 if (conf)
4562 return sprintf(page, "%d\n", conf->bypass_threshold);
4563 else
4564 return 0;
4565}
4566
4567static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004568raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004569{
NeilBrownd1688a62011-10-11 16:49:52 +11004570 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004571 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004572 if (len >= PAGE_SIZE)
4573 return -EINVAL;
4574 if (!conf)
4575 return -ENODEV;
4576
Dan Williams4ef197d82008-04-28 02:15:54 -07004577 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004578 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004579 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004580 return -EINVAL;
4581 conf->bypass_threshold = new;
4582 return len;
4583}
4584
4585static struct md_sysfs_entry
4586raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4587 S_IRUGO | S_IWUSR,
4588 raid5_show_preread_threshold,
4589 raid5_store_preread_threshold);
4590
4591static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004592stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004593{
NeilBrownd1688a62011-10-11 16:49:52 +11004594 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004595 if (conf)
4596 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4597 else
4598 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004599}
4600
NeilBrown96de1e62005-11-08 21:39:39 -08004601static struct md_sysfs_entry
4602raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004603
NeilBrown007583c2005-11-08 21:39:30 -08004604static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004605 &raid5_stripecache_size.attr,
4606 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004607 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004608 NULL,
4609};
NeilBrown007583c2005-11-08 21:39:30 -08004610static struct attribute_group raid5_attrs_group = {
4611 .name = NULL,
4612 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004613};
4614
Dan Williams80c3a6c2009-03-17 18:10:40 -07004615static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11004616raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07004617{
NeilBrownd1688a62011-10-11 16:49:52 +11004618 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004619
4620 if (!sectors)
4621 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004622 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11004623 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11004624 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004625
Andre Noll9d8f0362009-06-18 08:45:01 +10004626 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004627 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004628 return sectors * (raid_disks - conf->max_degraded);
4629}
4630
NeilBrownd1688a62011-10-11 16:49:52 +11004631static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004632{
4633 struct raid5_percpu *percpu;
4634 unsigned long cpu;
4635
4636 if (!conf->percpu)
4637 return;
4638
4639 get_online_cpus();
4640 for_each_possible_cpu(cpu) {
4641 percpu = per_cpu_ptr(conf->percpu, cpu);
4642 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004643 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004644 }
4645#ifdef CONFIG_HOTPLUG_CPU
4646 unregister_cpu_notifier(&conf->cpu_notify);
4647#endif
4648 put_online_cpus();
4649
4650 free_percpu(conf->percpu);
4651}
4652
NeilBrownd1688a62011-10-11 16:49:52 +11004653static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10004654{
4655 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004656 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10004657 kfree(conf->disks);
4658 kfree(conf->stripe_hashtbl);
4659 kfree(conf);
4660}
4661
Dan Williams36d1c642009-07-14 11:48:22 -07004662#ifdef CONFIG_HOTPLUG_CPU
4663static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4664 void *hcpu)
4665{
NeilBrownd1688a62011-10-11 16:49:52 +11004666 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07004667 long cpu = (long)hcpu;
4668 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4669
4670 switch (action) {
4671 case CPU_UP_PREPARE:
4672 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07004673 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07004674 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004675 if (!percpu->scribble)
4676 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4677
4678 if (!percpu->scribble ||
4679 (conf->level == 6 && !percpu->spare_page)) {
4680 safe_put_page(percpu->spare_page);
4681 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004682 pr_err("%s: failed memory allocation for cpu%ld\n",
4683 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07004684 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07004685 }
4686 break;
4687 case CPU_DEAD:
4688 case CPU_DEAD_FROZEN:
4689 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004690 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004691 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004692 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07004693 break;
4694 default:
4695 break;
4696 }
4697 return NOTIFY_OK;
4698}
4699#endif
4700
NeilBrownd1688a62011-10-11 16:49:52 +11004701static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004702{
4703 unsigned long cpu;
4704 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09004705 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004706 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004707 int err;
4708
Dan Williams36d1c642009-07-14 11:48:22 -07004709 allcpus = alloc_percpu(struct raid5_percpu);
4710 if (!allcpus)
4711 return -ENOMEM;
4712 conf->percpu = allcpus;
4713
4714 get_online_cpus();
4715 err = 0;
4716 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07004717 if (conf->level == 6) {
4718 spare_page = alloc_page(GFP_KERNEL);
4719 if (!spare_page) {
4720 err = -ENOMEM;
4721 break;
4722 }
4723 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
4724 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11004725 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004726 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07004727 err = -ENOMEM;
4728 break;
4729 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07004730 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004731 }
4732#ifdef CONFIG_HOTPLUG_CPU
4733 conf->cpu_notify.notifier_call = raid456_cpu_notify;
4734 conf->cpu_notify.priority = 0;
4735 if (err == 0)
4736 err = register_cpu_notifier(&conf->cpu_notify);
4737#endif
4738 put_online_cpus();
4739
4740 return err;
4741}
4742
NeilBrownd1688a62011-10-11 16:49:52 +11004743static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744{
NeilBrownd1688a62011-10-11 16:49:52 +11004745 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004746 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11004747 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 struct disk_info *disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749
NeilBrown91adb562009-03-31 14:39:39 +11004750 if (mddev->new_level != 5
4751 && mddev->new_level != 4
4752 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10004753 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004754 mdname(mddev), mddev->new_level);
4755 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 }
NeilBrown91adb562009-03-31 14:39:39 +11004757 if ((mddev->new_level == 5
4758 && !algorithm_valid_raid5(mddev->new_layout)) ||
4759 (mddev->new_level == 6
4760 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004761 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11004762 mdname(mddev), mddev->new_layout);
4763 return ERR_PTR(-EIO);
4764 }
4765 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10004766 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004767 mdname(mddev), mddev->raid_disks);
4768 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11004769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770
Andre Noll664e7c42009-06-18 08:45:27 +10004771 if (!mddev->new_chunk_sectors ||
4772 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
4773 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10004774 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
4775 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11004776 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11004777 }
4778
NeilBrownd1688a62011-10-11 16:49:52 +11004779 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11004780 if (conf == NULL)
4781 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11004782 spin_lock_init(&conf->device_lock);
4783 init_waitqueue_head(&conf->wait_for_stripe);
4784 init_waitqueue_head(&conf->wait_for_overlap);
4785 INIT_LIST_HEAD(&conf->handle_list);
4786 INIT_LIST_HEAD(&conf->hold_list);
4787 INIT_LIST_HEAD(&conf->delayed_list);
4788 INIT_LIST_HEAD(&conf->bitmap_list);
4789 INIT_LIST_HEAD(&conf->inactive_list);
4790 atomic_set(&conf->active_stripes, 0);
4791 atomic_set(&conf->preread_active_stripes, 0);
4792 atomic_set(&conf->active_aligned_reads, 0);
4793 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11004794 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11004795
4796 conf->raid_disks = mddev->raid_disks;
4797 if (mddev->reshape_position == MaxSector)
4798 conf->previous_raid_disks = mddev->raid_disks;
4799 else
4800 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004801 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
4802 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11004803
NeilBrown5e5e3e72009-10-16 16:35:30 +11004804 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11004805 GFP_KERNEL);
4806 if (!conf->disks)
4807 goto abort;
4808
4809 conf->mddev = mddev;
4810
4811 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
4812 goto abort;
4813
Dan Williams36d1c642009-07-14 11:48:22 -07004814 conf->level = mddev->new_level;
4815 if (raid5_alloc_percpu(conf) != 0)
4816 goto abort;
4817
NeilBrown0c55e022010-05-03 14:09:02 +10004818 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11004819
4820 list_for_each_entry(rdev, &mddev->disks, same_set) {
4821 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004822 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11004823 || raid_disk < 0)
4824 continue;
4825 disk = conf->disks + raid_disk;
4826
4827 disk->rdev = rdev;
4828
4829 if (test_bit(In_sync, &rdev->flags)) {
4830 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10004831 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
4832 " disk %d\n",
4833 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05004834 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11004835 /* Cannot rely on bitmap to complete recovery */
4836 conf->fullsync = 1;
4837 }
4838
Andre Noll09c9e5f2009-06-18 08:45:55 +10004839 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11004840 conf->level = mddev->new_level;
4841 if (conf->level == 6)
4842 conf->max_degraded = 2;
4843 else
4844 conf->max_degraded = 1;
4845 conf->algorithm = mddev->new_layout;
4846 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11004847 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11004848 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10004849 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11004850 conf->prev_algo = mddev->layout;
4851 }
NeilBrown91adb562009-03-31 14:39:39 +11004852
4853 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11004854 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11004855 if (grow_stripes(conf, conf->max_nr_stripes)) {
4856 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004857 "md/raid:%s: couldn't allocate %dkB for buffers\n",
4858 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004859 goto abort;
4860 } else
NeilBrown0c55e022010-05-03 14:09:02 +10004861 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
4862 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004863
NeilBrown0da3c612009-09-23 18:09:45 +10004864 conf->thread = md_register_thread(raid5d, mddev, NULL);
NeilBrown91adb562009-03-31 14:39:39 +11004865 if (!conf->thread) {
4866 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004867 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11004868 mdname(mddev));
4869 goto abort;
4870 }
4871
4872 return conf;
4873
4874 abort:
4875 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10004876 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11004877 return ERR_PTR(-EIO);
4878 } else
4879 return ERR_PTR(-ENOMEM);
4880}
4881
NeilBrownc148ffd2009-11-13 17:47:00 +11004882
4883static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
4884{
4885 switch (algo) {
4886 case ALGORITHM_PARITY_0:
4887 if (raid_disk < max_degraded)
4888 return 1;
4889 break;
4890 case ALGORITHM_PARITY_N:
4891 if (raid_disk >= raid_disks - max_degraded)
4892 return 1;
4893 break;
4894 case ALGORITHM_PARITY_0_6:
4895 if (raid_disk == 0 ||
4896 raid_disk == raid_disks - 1)
4897 return 1;
4898 break;
4899 case ALGORITHM_LEFT_ASYMMETRIC_6:
4900 case ALGORITHM_RIGHT_ASYMMETRIC_6:
4901 case ALGORITHM_LEFT_SYMMETRIC_6:
4902 case ALGORITHM_RIGHT_SYMMETRIC_6:
4903 if (raid_disk == raid_disks - 1)
4904 return 1;
4905 }
4906 return 0;
4907}
4908
NeilBrownfd01b882011-10-11 16:47:53 +11004909static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11004910{
NeilBrownd1688a62011-10-11 16:49:52 +11004911 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10004912 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11004913 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11004914 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11004915 sector_t reshape_offset = 0;
NeilBrown91adb562009-03-31 14:39:39 +11004916
Andre Noll8c6ac8682009-06-18 08:48:06 +10004917 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10004918 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac8682009-06-18 08:48:06 +10004919 " -- starting background reconstruction\n",
4920 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004921 if (mddev->reshape_position != MaxSector) {
4922 /* Check that we can continue the reshape.
4923 * Currently only disks can change, it must
4924 * increase, and we must be past the point where
4925 * a stripe over-writes itself
4926 */
4927 sector_t here_new, here_old;
4928 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11004929 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08004930
NeilBrown88ce4932009-03-31 15:24:23 +11004931 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10004932 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08004933 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08004934 mdname(mddev));
4935 return -EINVAL;
4936 }
NeilBrownf6705572006-03-27 01:18:11 -08004937 old_disks = mddev->raid_disks - mddev->delta_disks;
4938 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08004939 * further up in new geometry must map after here in old
4940 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08004941 */
4942 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10004943 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08004944 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004945 printk(KERN_ERR "md/raid:%s: reshape_position not "
4946 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004947 return -EINVAL;
4948 }
NeilBrownc148ffd2009-11-13 17:47:00 +11004949 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08004950 /* here_new is the stripe we will write to */
4951 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10004952 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08004953 (old_disks-max_degraded));
4954 /* here_old is the first stripe that we might need to read
4955 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10004956 if (mddev->delta_disks == 0) {
4957 /* We cannot be sure it is safe to start an in-place
4958 * reshape. It is only safe if user-space if monitoring
4959 * and taking constant backups.
4960 * mdadm always starts a situation like this in
4961 * readonly mode so it can take control before
4962 * allowing any writes. So just check for that.
4963 */
4964 if ((here_new * mddev->new_chunk_sectors !=
4965 here_old * mddev->chunk_sectors) ||
4966 mddev->ro == 0) {
NeilBrown0c55e022010-05-03 14:09:02 +10004967 printk(KERN_ERR "md/raid:%s: in-place reshape must be started"
4968 " in read-only mode - aborting\n",
4969 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10004970 return -EINVAL;
4971 }
4972 } else if (mddev->delta_disks < 0
4973 ? (here_new * mddev->new_chunk_sectors <=
4974 here_old * mddev->chunk_sectors)
4975 : (here_new * mddev->new_chunk_sectors >=
4976 here_old * mddev->chunk_sectors)) {
NeilBrownf6705572006-03-27 01:18:11 -08004977 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10004978 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
4979 "auto-recovery - aborting.\n",
4980 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004981 return -EINVAL;
4982 }
NeilBrown0c55e022010-05-03 14:09:02 +10004983 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
4984 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004985 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08004986 } else {
NeilBrown91adb562009-03-31 14:39:39 +11004987 BUG_ON(mddev->level != mddev->new_level);
4988 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10004989 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11004990 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08004991 }
4992
NeilBrown245f46c2009-03-31 14:39:39 +11004993 if (mddev->private == NULL)
4994 conf = setup_conf(mddev);
4995 else
4996 conf = mddev->private;
4997
NeilBrown91adb562009-03-31 14:39:39 +11004998 if (IS_ERR(conf))
4999 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005000
NeilBrown91adb562009-03-31 14:39:39 +11005001 mddev->thread = conf->thread;
5002 conf->thread = NULL;
5003 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005006 * 0 for a fully functional array, 1 or 2 for a degraded array.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 */
NeilBrownc148ffd2009-11-13 17:47:00 +11005008 list_for_each_entry(rdev, &mddev->disks, same_set) {
5009 if (rdev->raid_disk < 0)
5010 continue;
NeilBrown2f115882010-06-17 17:41:03 +10005011 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005012 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005013 continue;
5014 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005015 /* This disc is not fully in-sync. However if it
5016 * just stored parity (beyond the recovery_offset),
5017 * when we don't need to be concerned about the
5018 * array being dirty.
5019 * When reshape goes 'backwards', we never have
5020 * partially completed devices, so we only need
5021 * to worry about reshape going forwards.
5022 */
5023 /* Hack because v0.91 doesn't store recovery_offset properly. */
5024 if (mddev->major_version == 0 &&
5025 mddev->minor_version > 90)
5026 rdev->recovery_offset = reshape_offset;
5027
NeilBrownc148ffd2009-11-13 17:47:00 +11005028 if (rdev->recovery_offset < reshape_offset) {
5029 /* We need to check old and new layout */
5030 if (!only_parity(rdev->raid_disk,
5031 conf->algorithm,
5032 conf->raid_disks,
5033 conf->max_degraded))
5034 continue;
5035 }
5036 if (!only_parity(rdev->raid_disk,
5037 conf->prev_algo,
5038 conf->previous_raid_disks,
5039 conf->max_degraded))
5040 continue;
5041 dirty_parity_disks++;
5042 }
NeilBrown91adb562009-03-31 14:39:39 +11005043
NeilBrown908f4fb2011-12-23 10:17:50 +11005044 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045
NeilBrown674806d2010-06-16 17:17:53 +10005046 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005047 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07005049 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 goto abort;
5051 }
5052
NeilBrown91adb562009-03-31 14:39:39 +11005053 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10005054 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11005055 mddev->resync_max_sectors = mddev->dev_sectors;
5056
NeilBrownc148ffd2009-11-13 17:47:00 +11005057 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005059 if (mddev->ok_start_degraded)
5060 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10005061 "md/raid:%s: starting dirty degraded array"
5062 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005063 mdname(mddev));
5064 else {
5065 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005066 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005067 mdname(mddev));
5068 goto abort;
5069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 }
5071
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10005073 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5074 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11005075 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5076 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 else
NeilBrown0c55e022010-05-03 14:09:02 +10005078 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5079 " out of %d devices, algorithm %d\n",
5080 mdname(mddev), conf->level,
5081 mddev->raid_disks - mddev->degraded,
5082 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083
5084 print_raid5_conf(conf);
5085
NeilBrownfef9c612009-03-31 15:16:46 +11005086 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11005087 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005088 atomic_set(&conf->reshape_stripes, 0);
5089 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5090 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5091 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5092 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5093 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005094 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005095 }
5096
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097
5098 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005099 if (mddev->to_remove == &raid5_attrs_group)
5100 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005101 else if (mddev->kobj.sd &&
5102 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005103 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10005104 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08005105 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10005106 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5107
5108 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005109 int chunk_size;
NeilBrown4a5add42010-06-01 19:37:28 +10005110 /* read-ahead size must cover two whole stripes, which
5111 * is 2 * (datadisks) * chunksize where 'n' is the
5112 * number of raid devices
5113 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5115 int stripe = data_disks *
5116 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5117 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5118 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10005119
5120 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005121
5122 mddev->queue->backing_dev_info.congested_data = mddev;
5123 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10005124
5125 chunk_size = mddev->chunk_sectors << 9;
5126 blk_queue_io_min(mddev->queue, chunk_size);
5127 blk_queue_io_opt(mddev->queue, chunk_size *
5128 (conf->raid_disks - conf->max_degraded));
5129
5130 list_for_each_entry(rdev, &mddev->disks, same_set)
5131 disk_stack_limits(mddev->gendisk, rdev->bdev,
5132 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 }
5134
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 return 0;
5136abort:
NeilBrown01f96c02011-09-21 15:30:20 +10005137 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11005138 print_raid5_conf(conf);
5139 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005141 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 return -EIO;
5143}
5144
NeilBrownfd01b882011-10-11 16:47:53 +11005145static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146{
NeilBrownd1688a62011-10-11 16:49:52 +11005147 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148
NeilBrown01f96c02011-09-21 15:30:20 +10005149 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005150 if (mddev->queue)
5151 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10005152 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005153 mddev->private = NULL;
5154 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 return 0;
5156}
5157
NeilBrownfd01b882011-10-11 16:47:53 +11005158static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159{
NeilBrownd1688a62011-10-11 16:49:52 +11005160 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 int i;
5162
Andre Noll9d8f0362009-06-18 08:45:01 +10005163 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5164 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005165 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 for (i = 0; i < conf->raid_disks; i++)
5167 seq_printf (seq, "%s",
5168 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005169 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171}
5172
NeilBrownd1688a62011-10-11 16:49:52 +11005173static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174{
5175 int i;
5176 struct disk_info *tmp;
5177
NeilBrown0c55e022010-05-03 14:09:02 +10005178 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 if (!conf) {
5180 printk("(conf==NULL)\n");
5181 return;
5182 }
NeilBrown0c55e022010-05-03 14:09:02 +10005183 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5184 conf->raid_disks,
5185 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186
5187 for (i = 0; i < conf->raid_disks; i++) {
5188 char b[BDEVNAME_SIZE];
5189 tmp = conf->disks + i;
5190 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005191 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5192 i, !test_bit(Faulty, &tmp->rdev->flags),
5193 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 }
5195}
5196
NeilBrownfd01b882011-10-11 16:47:53 +11005197static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198{
5199 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005200 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005202 int count = 0;
5203 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204
5205 for (i = 0; i < conf->raid_disks; i++) {
5206 tmp = conf->disks + i;
5207 if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005208 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005209 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005210 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005211 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005212 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 }
5214 }
NeilBrown6b965622010-08-18 11:56:59 +10005215 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005216 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005217 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005219 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220}
5221
NeilBrownb8321b62011-12-23 10:17:51 +11005222static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223{
NeilBrownd1688a62011-10-11 16:49:52 +11005224 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11005226 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11005227 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 struct disk_info *p = conf->disks + number;
5229
5230 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11005231 if (rdev == p->rdev)
5232 rdevp = &p->rdev;
5233 else if (rdev == p->replacement)
5234 rdevp = &p->replacement;
5235 else
5236 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11005237
NeilBrown657e3e42011-12-23 10:17:52 +11005238 if (number >= conf->raid_disks &&
5239 conf->reshape_progress == MaxSector)
5240 clear_bit(In_sync, &rdev->flags);
5241
5242 if (test_bit(In_sync, &rdev->flags) ||
5243 atomic_read(&rdev->nr_pending)) {
5244 err = -EBUSY;
5245 goto abort;
5246 }
5247 /* Only remove non-faulty devices if recovery
5248 * isn't possible.
5249 */
5250 if (!test_bit(Faulty, &rdev->flags) &&
5251 mddev->recovery_disabled != conf->recovery_disabled &&
5252 !has_failed(conf) &&
5253 number < conf->raid_disks) {
5254 err = -EBUSY;
5255 goto abort;
5256 }
5257 *rdevp = NULL;
5258 synchronize_rcu();
5259 if (atomic_read(&rdev->nr_pending)) {
5260 /* lost the race, try later */
5261 err = -EBUSY;
5262 *rdevp = rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 }
5264abort:
5265
5266 print_raid5_conf(conf);
5267 return err;
5268}
5269
NeilBrownfd01b882011-10-11 16:47:53 +11005270static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271{
NeilBrownd1688a62011-10-11 16:49:52 +11005272 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005273 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 int disk;
5275 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005276 int first = 0;
5277 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278
NeilBrown7f0da592011-07-28 11:39:22 +10005279 if (mddev->recovery_disabled == conf->recovery_disabled)
5280 return -EBUSY;
5281
NeilBrown674806d2010-06-16 17:17:53 +10005282 if (has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005284 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285
Neil Brown6c2fce22008-06-28 08:31:31 +10005286 if (rdev->raid_disk >= 0)
5287 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288
5289 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005290 * find the disk ... but prefer rdev->saved_raid_disk
5291 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005293 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005294 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005295 conf->disks[rdev->saved_raid_disk].rdev == NULL)
5296 disk = rdev->saved_raid_disk;
5297 else
Neil Brown6c2fce22008-06-28 08:31:31 +10005298 disk = first;
5299 for ( ; disk <= last ; disk++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300 if ((p=conf->disks + disk)->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005301 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005303 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005304 if (rdev->saved_raid_disk != disk)
5305 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005306 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 break;
5308 }
5309 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005310 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311}
5312
NeilBrownfd01b882011-10-11 16:47:53 +11005313static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314{
5315 /* no resync is happening, and there is enough space
5316 * on all devices, so we can resize.
5317 * We need to make sure resync covers any new space.
5318 * If the array is shrinking we should possibly wait until
5319 * any io in the removed space completes, but it hardly seems
5320 * worth it.
5321 */
Andre Noll9d8f0362009-06-18 08:45:01 +10005322 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Dan Williams1f403622009-03-31 14:59:03 +11005323 md_set_array_sectors(mddev, raid5_size(mddev, sectors,
5324 mddev->raid_disks));
Dan Williamsb522adc2009-03-31 15:00:31 +11005325 if (mddev->array_sectors >
5326 raid5_size(mddev, sectors, mddev->raid_disks))
5327 return -EINVAL;
Andre Nollf233ea52008-07-21 17:05:22 +10005328 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005329 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005330 if (sectors > mddev->dev_sectors &&
5331 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005332 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5334 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005335 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005336 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 return 0;
5338}
5339
NeilBrownfd01b882011-10-11 16:47:53 +11005340static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005341{
5342 /* Can only proceed if there are plenty of stripe_heads.
5343 * We need a minimum of one full stripe,, and for sensible progress
5344 * it is best to have about 4 times that.
5345 * If we require 4 times, then the default 256 4K stripe_heads will
5346 * allow for chunk sizes up to 256K, which is probably OK.
5347 * If the chunk size is greater, user-space should request more
5348 * stripe_heads first.
5349 */
NeilBrownd1688a62011-10-11 16:49:52 +11005350 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005351 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5352 > conf->max_nr_stripes ||
5353 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5354 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005355 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5356 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005357 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5358 / STRIPE_SIZE)*4);
5359 return 0;
5360 }
5361 return 1;
5362}
5363
NeilBrownfd01b882011-10-11 16:47:53 +11005364static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005365{
NeilBrownd1688a62011-10-11 16:49:52 +11005366 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005367
NeilBrown88ce4932009-03-31 15:24:23 +11005368 if (mddev->delta_disks == 0 &&
5369 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005370 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005371 return 0; /* nothing to do */
NeilBrowndba034e2008-08-05 15:54:13 +10005372 if (mddev->bitmap)
5373 /* Cannot grow a bitmap yet */
5374 return -EBUSY;
NeilBrown674806d2010-06-16 17:17:53 +10005375 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005376 return -EINVAL;
5377 if (mddev->delta_disks < 0) {
5378 /* We might be able to shrink, but the devices must
5379 * be made bigger first.
5380 * For raid6, 4 is the minimum size.
5381 * Otherwise 2 is the minimum
5382 */
5383 int min = 2;
5384 if (mddev->level == 6)
5385 min = 4;
5386 if (mddev->raid_disks + mddev->delta_disks < min)
5387 return -EINVAL;
5388 }
NeilBrown29269552006-03-27 01:18:10 -08005389
NeilBrown01ee22b2009-06-18 08:47:20 +10005390 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005391 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005392
NeilBrownec32a2b2009-03-31 15:17:38 +11005393 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
NeilBrown63c70c42006-03-27 01:18:13 -08005394}
5395
NeilBrownfd01b882011-10-11 16:47:53 +11005396static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005397{
NeilBrownd1688a62011-10-11 16:49:52 +11005398 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005399 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005400 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005401 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005402
NeilBrownf4168852007-02-28 20:11:53 -08005403 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005404 return -EBUSY;
5405
NeilBrown01ee22b2009-06-18 08:47:20 +10005406 if (!check_stripe_cache(mddev))
5407 return -ENOSPC;
5408
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005409 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown469518a2011-01-31 11:57:43 +11005410 if (!test_bit(In_sync, &rdev->flags)
5411 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005412 spares++;
NeilBrown63c70c42006-03-27 01:18:13 -08005413
NeilBrownf4168852007-02-28 20:11:53 -08005414 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005415 /* Not enough devices even to make a degraded array
5416 * of that size
5417 */
5418 return -EINVAL;
5419
NeilBrownec32a2b2009-03-31 15:17:38 +11005420 /* Refuse to reduce size of the array. Any reductions in
5421 * array size must be through explicit setting of array_size
5422 * attribute.
5423 */
5424 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5425 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10005426 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11005427 "before number of disks\n", mdname(mddev));
5428 return -EINVAL;
5429 }
5430
NeilBrownf6705572006-03-27 01:18:11 -08005431 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005432 spin_lock_irq(&conf->device_lock);
5433 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005434 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005435 conf->prev_chunk_sectors = conf->chunk_sectors;
5436 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005437 conf->prev_algo = conf->algorithm;
5438 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11005439 if (mddev->delta_disks < 0)
5440 conf->reshape_progress = raid5_size(mddev, 0, 0);
5441 else
5442 conf->reshape_progress = 0;
5443 conf->reshape_safe = conf->reshape_progress;
NeilBrown86b42c72009-03-31 15:19:03 +11005444 conf->generation++;
NeilBrown29269552006-03-27 01:18:10 -08005445 spin_unlock_irq(&conf->device_lock);
5446
5447 /* Add some new drives, as many as will fit.
5448 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10005449 * Don't add devices if we are reducing the number of
5450 * devices in the array. This is because it is not possible
5451 * to correctly record the "partially reconstructed" state of
5452 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08005453 */
NeilBrown87a8dec2011-01-31 11:57:43 +11005454 if (mddev->delta_disks >= 0) {
5455 int added_devices = 0;
5456 list_for_each_entry(rdev, &mddev->disks, same_set)
5457 if (rdev->raid_disk < 0 &&
5458 !test_bit(Faulty, &rdev->flags)) {
5459 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11005460 if (rdev->raid_disk
5461 >= conf->previous_raid_disks) {
5462 set_bit(In_sync, &rdev->flags);
5463 added_devices++;
5464 } else
5465 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10005466
5467 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11005468 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11005469 }
NeilBrown87a8dec2011-01-31 11:57:43 +11005470 } else if (rdev->raid_disk >= conf->previous_raid_disks
5471 && !test_bit(Faulty, &rdev->flags)) {
5472 /* This is a spare that was manually added */
5473 set_bit(In_sync, &rdev->flags);
5474 added_devices++;
5475 }
NeilBrown29269552006-03-27 01:18:10 -08005476
NeilBrown87a8dec2011-01-31 11:57:43 +11005477 /* When a reshape changes the number of devices,
5478 * ->degraded is measured against the larger of the
5479 * pre and post number of devices.
5480 */
NeilBrownec32a2b2009-03-31 15:17:38 +11005481 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005482 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11005483 spin_unlock_irqrestore(&conf->device_lock, flags);
5484 }
NeilBrown63c70c42006-03-27 01:18:13 -08005485 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10005486 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b422006-10-03 01:15:46 -07005487 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005488
NeilBrown29269552006-03-27 01:18:10 -08005489 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5490 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5491 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5492 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5493 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005494 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08005495 if (!mddev->sync_thread) {
5496 mddev->recovery = 0;
5497 spin_lock_irq(&conf->device_lock);
5498 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005499 conf->reshape_progress = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005500 spin_unlock_irq(&conf->device_lock);
5501 return -EAGAIN;
5502 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005503 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005504 md_wakeup_thread(mddev->sync_thread);
5505 md_new_event(mddev);
5506 return 0;
5507}
NeilBrown29269552006-03-27 01:18:10 -08005508
NeilBrownec32a2b2009-03-31 15:17:38 +11005509/* This is called from the reshape thread and should make any
5510 * changes needed in 'conf'
5511 */
NeilBrownd1688a62011-10-11 16:49:52 +11005512static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08005513{
NeilBrown29269552006-03-27 01:18:10 -08005514
NeilBrownf6705572006-03-27 01:18:11 -08005515 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
Dan Williams80c3a6c2009-03-17 18:10:40 -07005516
NeilBrownf6705572006-03-27 01:18:11 -08005517 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11005518 conf->previous_raid_disks = conf->raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005519 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08005520 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005521 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07005522
5523 /* read-ahead size must cover two whole stripes, which is
5524 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5525 */
NeilBrown4a5add42010-06-01 19:37:28 +10005526 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11005527 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005528 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11005529 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07005530 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5531 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5532 }
NeilBrown29269552006-03-27 01:18:10 -08005533 }
NeilBrown29269552006-03-27 01:18:10 -08005534}
5535
NeilBrownec32a2b2009-03-31 15:17:38 +11005536/* This is called from the raid5d thread with mddev_lock held.
5537 * It makes config changes to the device.
5538 */
NeilBrownfd01b882011-10-11 16:47:53 +11005539static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11005540{
NeilBrownd1688a62011-10-11 16:49:52 +11005541 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11005542
5543 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5544
NeilBrownec32a2b2009-03-31 15:17:38 +11005545 if (mddev->delta_disks > 0) {
5546 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5547 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005548 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11005549 } else {
5550 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11005551 spin_lock_irq(&conf->device_lock);
5552 mddev->degraded = calc_degraded(conf);
5553 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11005554 for (d = conf->raid_disks ;
5555 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10005556 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11005557 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownb8321b62011-12-23 10:17:51 +11005558 if (rdev &&
5559 raid5_remove_disk(mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10005560 sysfs_unlink_rdev(mddev, rdev);
NeilBrown1a67dde2009-08-13 10:41:49 +10005561 rdev->raid_disk = -1;
5562 }
5563 }
NeilBrowncea9c222009-03-31 15:15:05 +11005564 }
NeilBrown88ce4932009-03-31 15:24:23 +11005565 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005566 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11005567 mddev->reshape_position = MaxSector;
5568 mddev->delta_disks = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11005569 }
5570}
5571
NeilBrownfd01b882011-10-11 16:47:53 +11005572static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07005573{
NeilBrownd1688a62011-10-11 16:49:52 +11005574 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07005575
5576 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08005577 case 2: /* resume for a suspend */
5578 wake_up(&conf->wait_for_overlap);
5579 break;
5580
NeilBrown72626682005-09-09 16:23:54 -07005581 case 1: /* stop all writes */
5582 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005583 /* '2' tells resync/reshape to pause so that all
5584 * active stripes can drain
5585 */
5586 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07005587 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005588 atomic_read(&conf->active_stripes) == 0 &&
5589 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07005590 conf->device_lock, /* nothing */);
NeilBrown64bd6602009-08-03 10:59:58 +10005591 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07005592 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005593 /* allow reshape to continue */
5594 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005595 break;
5596
5597 case 0: /* re-enable writes */
5598 spin_lock_irq(&conf->device_lock);
5599 conf->quiesce = 0;
5600 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08005601 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005602 spin_unlock_irq(&conf->device_lock);
5603 break;
5604 }
NeilBrown72626682005-09-09 16:23:54 -07005605}
NeilBrownb15c2e52006-01-06 00:20:16 -08005606
NeilBrownd562b0c2009-03-31 14:39:39 +11005607
NeilBrownfd01b882011-10-11 16:47:53 +11005608static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11005609{
NeilBrowne373ab12011-10-11 16:48:59 +11005610 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07005611 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11005612
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005613 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11005614 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10005615 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
5616 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005617 return ERR_PTR(-EINVAL);
5618 }
5619
NeilBrowne373ab12011-10-11 16:48:59 +11005620 sectors = raid0_conf->strip_zone[0].zone_end;
5621 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10005622 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005623 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11005624 mddev->new_layout = ALGORITHM_PARITY_N;
5625 mddev->new_chunk_sectors = mddev->chunk_sectors;
5626 mddev->raid_disks += 1;
5627 mddev->delta_disks = 1;
5628 /* make sure it will be not marked as dirty */
5629 mddev->recovery_cp = MaxSector;
5630
5631 return setup_conf(mddev);
5632}
5633
5634
NeilBrownfd01b882011-10-11 16:47:53 +11005635static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005636{
5637 int chunksect;
5638
5639 if (mddev->raid_disks != 2 ||
5640 mddev->degraded > 1)
5641 return ERR_PTR(-EINVAL);
5642
5643 /* Should check if there are write-behind devices? */
5644
5645 chunksect = 64*2; /* 64K by default */
5646
5647 /* The array must be an exact multiple of chunksize */
5648 while (chunksect && (mddev->array_sectors & (chunksect-1)))
5649 chunksect >>= 1;
5650
5651 if ((chunksect<<9) < STRIPE_SIZE)
5652 /* array size does not allow a suitable chunk size */
5653 return ERR_PTR(-EINVAL);
5654
5655 mddev->new_level = 5;
5656 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10005657 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11005658
5659 return setup_conf(mddev);
5660}
5661
NeilBrownfd01b882011-10-11 16:47:53 +11005662static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11005663{
5664 int new_layout;
5665
5666 switch (mddev->layout) {
5667 case ALGORITHM_LEFT_ASYMMETRIC_6:
5668 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
5669 break;
5670 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5671 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
5672 break;
5673 case ALGORITHM_LEFT_SYMMETRIC_6:
5674 new_layout = ALGORITHM_LEFT_SYMMETRIC;
5675 break;
5676 case ALGORITHM_RIGHT_SYMMETRIC_6:
5677 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
5678 break;
5679 case ALGORITHM_PARITY_0_6:
5680 new_layout = ALGORITHM_PARITY_0;
5681 break;
5682 case ALGORITHM_PARITY_N:
5683 new_layout = ALGORITHM_PARITY_N;
5684 break;
5685 default:
5686 return ERR_PTR(-EINVAL);
5687 }
5688 mddev->new_level = 5;
5689 mddev->new_layout = new_layout;
5690 mddev->delta_disks = -1;
5691 mddev->raid_disks -= 1;
5692 return setup_conf(mddev);
5693}
5694
NeilBrownd562b0c2009-03-31 14:39:39 +11005695
NeilBrownfd01b882011-10-11 16:47:53 +11005696static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11005697{
NeilBrown88ce4932009-03-31 15:24:23 +11005698 /* For a 2-drive array, the layout and chunk size can be changed
5699 * immediately as not restriping is needed.
5700 * For larger arrays we record the new value - after validation
5701 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11005702 */
NeilBrownd1688a62011-10-11 16:49:52 +11005703 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10005704 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11005705
NeilBrown597a7112009-06-18 08:47:42 +10005706 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11005707 return -EINVAL;
5708 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005709 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11005710 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005711 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11005712 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005713 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11005714 /* not factor of array size */
5715 return -EINVAL;
5716 }
5717
5718 /* They look valid */
5719
NeilBrown88ce4932009-03-31 15:24:23 +11005720 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10005721 /* can make the change immediately */
5722 if (mddev->new_layout >= 0) {
5723 conf->algorithm = mddev->new_layout;
5724 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11005725 }
5726 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10005727 conf->chunk_sectors = new_chunk ;
5728 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11005729 }
5730 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5731 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11005732 }
NeilBrown50ac1682009-06-18 08:47:55 +10005733 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11005734}
5735
NeilBrownfd01b882011-10-11 16:47:53 +11005736static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11005737{
NeilBrown597a7112009-06-18 08:47:42 +10005738 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10005739
NeilBrown597a7112009-06-18 08:47:42 +10005740 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11005741 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005742 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005743 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11005744 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005745 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11005746 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005747 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11005748 /* not factor of array size */
5749 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005750 }
NeilBrown88ce4932009-03-31 15:24:23 +11005751
5752 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10005753 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11005754}
5755
NeilBrownfd01b882011-10-11 16:47:53 +11005756static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005757{
5758 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005759 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005760 * raid1 - if there are two drives. We need to know the chunk size
5761 * raid4 - trivial - just use a raid4 layout.
5762 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005763 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005764 if (mddev->level == 0)
5765 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11005766 if (mddev->level == 1)
5767 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11005768 if (mddev->level == 4) {
5769 mddev->new_layout = ALGORITHM_PARITY_N;
5770 mddev->new_level = 5;
5771 return setup_conf(mddev);
5772 }
NeilBrownfc9739c2009-03-31 14:57:20 +11005773 if (mddev->level == 6)
5774 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11005775
5776 return ERR_PTR(-EINVAL);
5777}
5778
NeilBrownfd01b882011-10-11 16:47:53 +11005779static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11005780{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005781 /* raid4 can take over:
5782 * raid0 - if there is only one strip zone
5783 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11005784 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005785 if (mddev->level == 0)
5786 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11005787 if (mddev->level == 5 &&
5788 mddev->layout == ALGORITHM_PARITY_N) {
5789 mddev->new_layout = 0;
5790 mddev->new_level = 4;
5791 return setup_conf(mddev);
5792 }
5793 return ERR_PTR(-EINVAL);
5794}
NeilBrownd562b0c2009-03-31 14:39:39 +11005795
NeilBrown84fc4b52011-10-11 16:49:58 +11005796static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11005797
NeilBrownfd01b882011-10-11 16:47:53 +11005798static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11005799{
5800 /* Currently can only take over a raid5. We map the
5801 * personality to an equivalent raid6 personality
5802 * with the Q block at the end.
5803 */
5804 int new_layout;
5805
5806 if (mddev->pers != &raid5_personality)
5807 return ERR_PTR(-EINVAL);
5808 if (mddev->degraded > 1)
5809 return ERR_PTR(-EINVAL);
5810 if (mddev->raid_disks > 253)
5811 return ERR_PTR(-EINVAL);
5812 if (mddev->raid_disks < 3)
5813 return ERR_PTR(-EINVAL);
5814
5815 switch (mddev->layout) {
5816 case ALGORITHM_LEFT_ASYMMETRIC:
5817 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
5818 break;
5819 case ALGORITHM_RIGHT_ASYMMETRIC:
5820 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
5821 break;
5822 case ALGORITHM_LEFT_SYMMETRIC:
5823 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
5824 break;
5825 case ALGORITHM_RIGHT_SYMMETRIC:
5826 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
5827 break;
5828 case ALGORITHM_PARITY_0:
5829 new_layout = ALGORITHM_PARITY_0_6;
5830 break;
5831 case ALGORITHM_PARITY_N:
5832 new_layout = ALGORITHM_PARITY_N;
5833 break;
5834 default:
5835 return ERR_PTR(-EINVAL);
5836 }
5837 mddev->new_level = 6;
5838 mddev->new_layout = new_layout;
5839 mddev->delta_disks = 1;
5840 mddev->raid_disks += 1;
5841 return setup_conf(mddev);
5842}
5843
5844
NeilBrown84fc4b52011-10-11 16:49:58 +11005845static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07005846{
5847 .name = "raid6",
5848 .level = 6,
5849 .owner = THIS_MODULE,
5850 .make_request = make_request,
5851 .run = run,
5852 .stop = stop,
5853 .status = status,
5854 .error_handler = error,
5855 .hot_add_disk = raid5_add_disk,
5856 .hot_remove_disk= raid5_remove_disk,
5857 .spare_active = raid5_spare_active,
5858 .sync_request = sync_request,
5859 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005860 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10005861 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08005862 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005863 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07005864 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11005865 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07005866};
NeilBrown84fc4b52011-10-11 16:49:58 +11005867static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868{
5869 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08005870 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871 .owner = THIS_MODULE,
5872 .make_request = make_request,
5873 .run = run,
5874 .stop = stop,
5875 .status = status,
5876 .error_handler = error,
5877 .hot_add_disk = raid5_add_disk,
5878 .hot_remove_disk= raid5_remove_disk,
5879 .spare_active = raid5_spare_active,
5880 .sync_request = sync_request,
5881 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005882 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08005883 .check_reshape = raid5_check_reshape,
5884 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005885 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07005886 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11005887 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888};
5889
NeilBrown84fc4b52011-10-11 16:49:58 +11005890static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891{
NeilBrown2604b702006-01-06 00:20:36 -08005892 .name = "raid4",
5893 .level = 4,
5894 .owner = THIS_MODULE,
5895 .make_request = make_request,
5896 .run = run,
5897 .stop = stop,
5898 .status = status,
5899 .error_handler = error,
5900 .hot_add_disk = raid5_add_disk,
5901 .hot_remove_disk= raid5_remove_disk,
5902 .spare_active = raid5_spare_active,
5903 .sync_request = sync_request,
5904 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005905 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08005906 .check_reshape = raid5_check_reshape,
5907 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005908 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08005909 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11005910 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08005911};
5912
5913static int __init raid5_init(void)
5914{
NeilBrown16a53ec2006-06-26 00:27:38 -07005915 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08005916 register_md_personality(&raid5_personality);
5917 register_md_personality(&raid4_personality);
5918 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919}
5920
NeilBrown2604b702006-01-06 00:20:36 -08005921static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922{
NeilBrown16a53ec2006-06-26 00:27:38 -07005923 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08005924 unregister_md_personality(&raid5_personality);
5925 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926}
5927
5928module_init(raid5_init);
5929module_exit(raid5_exit);
5930MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11005931MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08005933MODULE_ALIAS("md-raid5");
5934MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08005935MODULE_ALIAS("md-level-5");
5936MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07005937MODULE_ALIAS("md-personality-8"); /* RAID6 */
5938MODULE_ALIAS("md-raid6");
5939MODULE_ALIAS("md-level-6");
5940
5941/* This used to be two separate modules, they were: */
5942MODULE_ALIAS("raid5");
5943MODULE_ALIAS("raid6");