blob: 23ac880bba9a5cbee0533e5144f135d3eb120ace [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));
majianpeng41fe75f2012-03-13 11:21:25 +1100211 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
212 if (atomic_dec_return(&conf->preread_active_stripes)
213 < IO_THRESHOLD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 md_wakeup_thread(conf->mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 atomic_dec(&conf->active_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800216 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
217 list_add_tail(&sh->lru, &conf->inactive_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 wake_up(&conf->wait_for_stripe);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -0800219 if (conf->retry_read_aligned)
220 md_wakeup_thread(conf->mddev->thread);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
223 }
224}
NeilBrownd0dabf72009-03-31 14:39:38 +1100225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226static void release_stripe(struct stripe_head *sh)
227{
NeilBrownd1688a62011-10-11 16:49:52 +1100228 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 unsigned long flags;
NeilBrown16a53ec2006-06-26 00:27:38 -0700230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 spin_lock_irqsave(&conf->device_lock, flags);
232 __release_stripe(conf, sh);
233 spin_unlock_irqrestore(&conf->device_lock, flags);
234}
235
NeilBrownfccddba2006-01-06 00:20:33 -0800236static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Dan Williams45b42332007-07-09 11:56:43 -0700238 pr_debug("remove_hash(), stripe %llu\n",
239 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
NeilBrownfccddba2006-01-06 00:20:33 -0800241 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
NeilBrownd1688a62011-10-11 16:49:52 +1100244static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
NeilBrownfccddba2006-01-06 00:20:33 -0800246 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Dan Williams45b42332007-07-09 11:56:43 -0700248 pr_debug("insert_hash(), stripe %llu\n",
249 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
NeilBrownfccddba2006-01-06 00:20:33 -0800251 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
254
255/* find an idle stripe, make sure it is unhashed, and return it. */
NeilBrownd1688a62011-10-11 16:49:52 +1100256static struct stripe_head *get_free_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 struct stripe_head *sh = NULL;
259 struct list_head *first;
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (list_empty(&conf->inactive_list))
262 goto out;
263 first = conf->inactive_list.next;
264 sh = list_entry(first, struct stripe_head, lru);
265 list_del_init(first);
266 remove_hash(sh);
267 atomic_inc(&conf->active_stripes);
268out:
269 return sh;
270}
271
NeilBrowne4e11e32010-06-16 16:45:16 +1000272static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 struct page *p;
275 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000276 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
NeilBrowne4e11e32010-06-16 16:45:16 +1000278 for (i = 0; i < num ; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 p = sh->dev[i].page;
280 if (!p)
281 continue;
282 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800283 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
285}
286
NeilBrowne4e11e32010-06-16 16:45:16 +1000287static int grow_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000290 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
NeilBrowne4e11e32010-06-16 16:45:16 +1000292 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 struct page *page;
294
295 if (!(page = alloc_page(GFP_KERNEL))) {
296 return 1;
297 }
298 sh->dev[i].page = page;
299 }
300 return 0;
301}
302
NeilBrown784052e2009-03-31 15:19:07 +1100303static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrownd1688a62011-10-11 16:49:52 +1100304static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100305 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
NeilBrownb5663ba2009-03-31 14:39:38 +1100307static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
NeilBrownd1688a62011-10-11 16:49:52 +1100309 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800310 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200312 BUG_ON(atomic_read(&sh->count) != 0);
313 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000314 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700315
Dan Williams45b42332007-07-09 11:56:43 -0700316 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 (unsigned long long)sh->sector);
318
319 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700320
NeilBrown86b42c72009-03-31 15:19:03 +1100321 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100322 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100324 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 sh->state = 0;
326
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800327
328 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 struct r5dev *dev = &sh->dev[i];
330
Dan Williamsd84e0f12007-01-02 13:52:30 -0700331 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700333 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700335 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000337 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100340 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342 insert_hash(conf, sh);
343}
344
NeilBrownd1688a62011-10-11 16:49:52 +1100345static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100346 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -0800349 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Dan Williams45b42332007-07-09 11:56:43 -0700351 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
NeilBrownfccddba2006-01-06 00:20:33 -0800352 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100353 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700355 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return NULL;
357}
358
NeilBrown674806d2010-06-16 17:17:53 +1000359/*
360 * Need to check if array has failed when deciding whether to:
361 * - start an array
362 * - remove non-faulty devices
363 * - add a spare
364 * - allow a reshape
365 * This determination is simple when no reshape is happening.
366 * However if there is a reshape, we need to carefully check
367 * both the before and after sections.
368 * This is because some failed devices may only affect one
369 * of the two sections, and some non-in_sync devices may
370 * be insync in the section most affected by failed devices.
371 */
NeilBrown908f4fb2011-12-23 10:17:50 +1100372static int calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000373{
NeilBrown908f4fb2011-12-23 10:17:50 +1100374 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000375 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000376
377 rcu_read_lock();
378 degraded = 0;
379 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100380 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown674806d2010-06-16 17:17:53 +1000381 if (!rdev || test_bit(Faulty, &rdev->flags))
382 degraded++;
383 else if (test_bit(In_sync, &rdev->flags))
384 ;
385 else
386 /* not in-sync or faulty.
387 * If the reshape increases the number of devices,
388 * this is being recovered by the reshape, so
389 * this 'previous' section is not in_sync.
390 * If the number of devices is being reduced however,
391 * the device can only be part of the array if
392 * we are reverting a reshape, so this section will
393 * be in-sync.
394 */
395 if (conf->raid_disks >= conf->previous_raid_disks)
396 degraded++;
397 }
398 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100399 if (conf->raid_disks == conf->previous_raid_disks)
400 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000401 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100402 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000403 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100404 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown674806d2010-06-16 17:17:53 +1000405 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100406 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000407 else if (test_bit(In_sync, &rdev->flags))
408 ;
409 else
410 /* not in-sync or faulty.
411 * If reshape increases the number of devices, this
412 * section has already been recovered, else it
413 * almost certainly hasn't.
414 */
415 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100416 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000417 }
418 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100419 if (degraded2 > degraded)
420 return degraded2;
421 return degraded;
422}
423
424static int has_failed(struct r5conf *conf)
425{
426 int degraded;
427
428 if (conf->mddev->reshape_position == MaxSector)
429 return conf->mddev->degraded > conf->max_degraded;
430
431 degraded = calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000432 if (degraded > conf->max_degraded)
433 return 1;
434 return 0;
435}
436
NeilBrownb5663ba2009-03-31 14:39:38 +1100437static struct stripe_head *
NeilBrownd1688a62011-10-11 16:49:52 +1100438get_active_stripe(struct r5conf *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000439 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
441 struct stripe_head *sh;
442
Dan Williams45b42332007-07-09 11:56:43 -0700443 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 spin_lock_irq(&conf->device_lock);
446
447 do {
NeilBrown72626682005-09-09 16:23:54 -0700448 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000449 conf->quiesce == 0 || noquiesce,
NeilBrown72626682005-09-09 16:23:54 -0700450 conf->device_lock, /* nothing */);
NeilBrown86b42c72009-03-31 15:19:03 +1100451 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (!sh) {
453 if (!conf->inactive_blocked)
454 sh = get_free_stripe(conf);
455 if (noblock && sh == NULL)
456 break;
457 if (!sh) {
458 conf->inactive_blocked = 1;
459 wait_event_lock_irq(conf->wait_for_stripe,
460 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800461 (atomic_read(&conf->active_stripes)
462 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 || !conf->inactive_blocked),
464 conf->device_lock,
NeilBrown7c13edc2011-04-18 18:25:43 +1000465 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 conf->inactive_blocked = 0;
467 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100468 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 } else {
470 if (atomic_read(&sh->count)) {
NeilBrownab69ae12009-03-31 15:26:47 +1100471 BUG_ON(!list_empty(&sh->lru)
472 && !test_bit(STRIPE_EXPANDING, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 } else {
474 if (!test_bit(STRIPE_HANDLE, &sh->state))
475 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700476 if (list_empty(&sh->lru) &&
477 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700478 BUG();
479 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
481 }
482 } while (sh == NULL);
483
484 if (sh)
485 atomic_inc(&sh->count);
486
487 spin_unlock_irq(&conf->device_lock);
488 return sh;
489}
490
NeilBrown6712ecf2007-09-27 12:47:43 +0200491static void
492raid5_end_read_request(struct bio *bi, int error);
493static void
494raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700495
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000496static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700497{
NeilBrownd1688a62011-10-11 16:49:52 +1100498 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700499 int i, disks = sh->disks;
500
501 might_sleep();
502
503 for (i = disks; i--; ) {
504 int rw;
NeilBrown9a3e1102011-12-23 10:17:53 +1100505 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100506 struct bio *bi, *rbi;
507 struct md_rdev *rdev, *rrdev = NULL;
Tejun Heoe9c74692010-09-03 11:56:18 +0200508 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
509 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
510 rw = WRITE_FUA;
511 else
512 rw = WRITE;
513 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700514 rw = READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100515 else if (test_and_clear_bit(R5_WantReplace,
516 &sh->dev[i].flags)) {
517 rw = WRITE;
518 replace_only = 1;
519 } else
Dan Williams91c00922007-01-02 13:52:30 -0700520 continue;
521
522 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100523 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700524
525 bi->bi_rw = rw;
NeilBrown977df362011-12-23 10:17:53 +1100526 rbi->bi_rw = rw;
527 if (rw & WRITE) {
Dan Williams91c00922007-01-02 13:52:30 -0700528 bi->bi_end_io = raid5_end_write_request;
NeilBrown977df362011-12-23 10:17:53 +1100529 rbi->bi_end_io = raid5_end_write_request;
530 } else
Dan Williams91c00922007-01-02 13:52:30 -0700531 bi->bi_end_io = raid5_end_read_request;
532
533 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100534 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100535 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
536 rdev = rcu_dereference(conf->disks[i].rdev);
537 if (!rdev) {
538 rdev = rrdev;
539 rrdev = NULL;
540 }
NeilBrown9a3e1102011-12-23 10:17:53 +1100541 if (rw & WRITE) {
542 if (replace_only)
543 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100544 if (rdev == rrdev)
545 /* We raced and saw duplicates */
546 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100547 } else {
NeilBrowndd054fc2011-12-23 10:17:53 +1100548 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100549 rdev = rrdev;
550 rrdev = NULL;
551 }
NeilBrown977df362011-12-23 10:17:53 +1100552
Dan Williams91c00922007-01-02 13:52:30 -0700553 if (rdev && test_bit(Faulty, &rdev->flags))
554 rdev = NULL;
555 if (rdev)
556 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100557 if (rrdev && test_bit(Faulty, &rrdev->flags))
558 rrdev = NULL;
559 if (rrdev)
560 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700561 rcu_read_unlock();
562
NeilBrown73e92e52011-07-28 11:39:22 +1000563 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100564 * need to check for writes. We never accept write errors
565 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000566 */
567 while ((rw & WRITE) && rdev &&
568 test_bit(WriteErrorSeen, &rdev->flags)) {
569 sector_t first_bad;
570 int bad_sectors;
571 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
572 &first_bad, &bad_sectors);
573 if (!bad)
574 break;
575
576 if (bad < 0) {
577 set_bit(BlockedBadBlocks, &rdev->flags);
578 if (!conf->mddev->external &&
579 conf->mddev->flags) {
580 /* It is very unlikely, but we might
581 * still need to write out the
582 * bad block log - better give it
583 * a chance*/
584 md_check_recovery(conf->mddev);
585 }
586 md_wait_for_blocked_rdev(rdev, conf->mddev);
587 } else {
588 /* Acknowledged bad block - skip the write */
589 rdev_dec_pending(rdev, conf->mddev);
590 rdev = NULL;
591 }
592 }
593
Dan Williams91c00922007-01-02 13:52:30 -0700594 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100595 if (s->syncing || s->expanding || s->expanded
596 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700597 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
598
Dan Williams2b7497f2008-06-28 08:31:52 +1000599 set_bit(STRIPE_IO_STARTED, &sh->state);
600
Dan Williams91c00922007-01-02 13:52:30 -0700601 bi->bi_bdev = rdev->bdev;
602 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700603 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700604 bi->bi_rw, i);
605 atomic_inc(&sh->count);
606 bi->bi_sector = sh->sector + rdev->data_offset;
607 bi->bi_flags = 1 << BIO_UPTODATE;
Dan Williams91c00922007-01-02 13:52:30 -0700608 bi->bi_idx = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700609 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
610 bi->bi_io_vec[0].bv_offset = 0;
611 bi->bi_size = STRIPE_SIZE;
612 bi->bi_next = NULL;
NeilBrown977df362011-12-23 10:17:53 +1100613 if (rrdev)
614 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Dan Williams91c00922007-01-02 13:52:30 -0700615 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100616 }
617 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100618 if (s->syncing || s->expanding || s->expanded
619 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +1100620 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
621
622 set_bit(STRIPE_IO_STARTED, &sh->state);
623
624 rbi->bi_bdev = rrdev->bdev;
625 pr_debug("%s: for %llu schedule op %ld on "
626 "replacement disc %d\n",
627 __func__, (unsigned long long)sh->sector,
628 rbi->bi_rw, i);
629 atomic_inc(&sh->count);
630 rbi->bi_sector = sh->sector + rrdev->data_offset;
631 rbi->bi_flags = 1 << BIO_UPTODATE;
632 rbi->bi_idx = 0;
633 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
634 rbi->bi_io_vec[0].bv_offset = 0;
635 rbi->bi_size = STRIPE_SIZE;
636 rbi->bi_next = NULL;
637 generic_make_request(rbi);
638 }
639 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000640 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700641 set_bit(STRIPE_DEGRADED, &sh->state);
642 pr_debug("skip op %ld on disc %d for sector %llu\n",
643 bi->bi_rw, i, (unsigned long long)sh->sector);
644 clear_bit(R5_LOCKED, &sh->dev[i].flags);
645 set_bit(STRIPE_HANDLE, &sh->state);
646 }
647 }
648}
649
650static struct dma_async_tx_descriptor *
651async_copy_data(int frombio, struct bio *bio, struct page *page,
652 sector_t sector, struct dma_async_tx_descriptor *tx)
653{
654 struct bio_vec *bvl;
655 struct page *bio_page;
656 int i;
657 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700658 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700659 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700660
661 if (bio->bi_sector >= sector)
662 page_offset = (signed)(bio->bi_sector - sector) * 512;
663 else
664 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700665
Dan Williams0403e382009-09-08 17:42:50 -0700666 if (frombio)
667 flags |= ASYNC_TX_FENCE;
668 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
669
Dan Williams91c00922007-01-02 13:52:30 -0700670 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000671 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700672 int clen;
673 int b_offset = 0;
674
675 if (page_offset < 0) {
676 b_offset = -page_offset;
677 page_offset += b_offset;
678 len -= b_offset;
679 }
680
681 if (len > 0 && page_offset + len > STRIPE_SIZE)
682 clen = STRIPE_SIZE - page_offset;
683 else
684 clen = len;
685
686 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000687 b_offset += bvl->bv_offset;
688 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700689 if (frombio)
690 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700691 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700692 else
693 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700694 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700695 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700696 /* chain the operations */
697 submit.depend_tx = tx;
698
Dan Williams91c00922007-01-02 13:52:30 -0700699 if (clen < len) /* hit end of page */
700 break;
701 page_offset += len;
702 }
703
704 return tx;
705}
706
707static void ops_complete_biofill(void *stripe_head_ref)
708{
709 struct stripe_head *sh = stripe_head_ref;
710 struct bio *return_bi = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +1100711 struct r5conf *conf = sh->raid_conf;
Dan Williamse4d84902007-09-24 10:06:13 -0700712 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700713
Harvey Harrisone46b2722008-04-28 02:15:50 -0700714 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700715 (unsigned long long)sh->sector);
716
717 /* clear completed biofills */
Dan Williams83de75c2008-06-28 08:31:58 +1000718 spin_lock_irq(&conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700719 for (i = sh->disks; i--; ) {
720 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700721
722 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700723 /* and check if we need to reply to a read request,
724 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000725 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700726 */
727 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700728 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700729
Dan Williams91c00922007-01-02 13:52:30 -0700730 BUG_ON(!dev->read);
731 rbi = dev->read;
732 dev->read = NULL;
733 while (rbi && rbi->bi_sector <
734 dev->sector + STRIPE_SECTORS) {
735 rbi2 = r5_next_bio(rbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +0200736 if (!raid5_dec_bi_phys_segments(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700737 rbi->bi_next = return_bi;
738 return_bi = rbi;
739 }
Dan Williams91c00922007-01-02 13:52:30 -0700740 rbi = rbi2;
741 }
742 }
743 }
Dan Williams83de75c2008-06-28 08:31:58 +1000744 spin_unlock_irq(&conf->device_lock);
745 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700746
747 return_io(return_bi);
748
Dan Williamse4d84902007-09-24 10:06:13 -0700749 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700750 release_stripe(sh);
751}
752
753static void ops_run_biofill(struct stripe_head *sh)
754{
755 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +1100756 struct r5conf *conf = sh->raid_conf;
Dan Williamsa08abd82009-06-03 11:43:59 -0700757 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700758 int i;
759
Harvey Harrisone46b2722008-04-28 02:15:50 -0700760 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700761 (unsigned long long)sh->sector);
762
763 for (i = sh->disks; i--; ) {
764 struct r5dev *dev = &sh->dev[i];
765 if (test_bit(R5_Wantfill, &dev->flags)) {
766 struct bio *rbi;
767 spin_lock_irq(&conf->device_lock);
768 dev->read = rbi = dev->toread;
769 dev->toread = NULL;
770 spin_unlock_irq(&conf->device_lock);
771 while (rbi && rbi->bi_sector <
772 dev->sector + STRIPE_SECTORS) {
773 tx = async_copy_data(0, rbi, dev->page,
774 dev->sector, tx);
775 rbi = r5_next_bio(rbi, dev->sector);
776 }
777 }
778 }
779
780 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700781 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
782 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700783}
784
Dan Williams4e7d2c02009-08-29 19:13:11 -0700785static void mark_target_uptodate(struct stripe_head *sh, int target)
786{
787 struct r5dev *tgt;
788
789 if (target < 0)
790 return;
791
792 tgt = &sh->dev[target];
793 set_bit(R5_UPTODATE, &tgt->flags);
794 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
795 clear_bit(R5_Wantcompute, &tgt->flags);
796}
797
Dan Williamsac6b53b2009-07-14 13:40:19 -0700798static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700799{
800 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700801
Harvey Harrisone46b2722008-04-28 02:15:50 -0700802 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700803 (unsigned long long)sh->sector);
804
Dan Williamsac6b53b2009-07-14 13:40:19 -0700805 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700806 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700807 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700808
Dan Williamsecc65c92008-06-28 08:31:57 +1000809 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
810 if (sh->check_state == check_state_compute_run)
811 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700812 set_bit(STRIPE_HANDLE, &sh->state);
813 release_stripe(sh);
814}
815
Dan Williamsd6f38f32009-07-14 11:50:52 -0700816/* return a pointer to the address conversion region of the scribble buffer */
817static addr_conv_t *to_addr_conv(struct stripe_head *sh,
818 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700819{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700820 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
821}
822
823static struct dma_async_tx_descriptor *
824ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
825{
Dan Williams91c00922007-01-02 13:52:30 -0700826 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700827 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700828 int target = sh->ops.target;
829 struct r5dev *tgt = &sh->dev[target];
830 struct page *xor_dest = tgt->page;
831 int count = 0;
832 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700833 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700834 int i;
835
836 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700837 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700838 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
839
840 for (i = disks; i--; )
841 if (i != target)
842 xor_srcs[count++] = sh->dev[i].page;
843
844 atomic_inc(&sh->count);
845
Dan Williams0403e382009-09-08 17:42:50 -0700846 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700847 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700848 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700849 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700850 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700851 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700852
Dan Williams91c00922007-01-02 13:52:30 -0700853 return tx;
854}
855
Dan Williamsac6b53b2009-07-14 13:40:19 -0700856/* set_syndrome_sources - populate source buffers for gen_syndrome
857 * @srcs - (struct page *) array of size sh->disks
858 * @sh - stripe_head to parse
859 *
860 * Populates srcs in proper layout order for the stripe and returns the
861 * 'count' of sources to be used in a call to async_gen_syndrome. The P
862 * destination buffer is recorded in srcs[count] and the Q destination
863 * is recorded in srcs[count+1]].
864 */
865static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
866{
867 int disks = sh->disks;
868 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
869 int d0_idx = raid6_d0(sh);
870 int count;
871 int i;
872
873 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100874 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700875
876 count = 0;
877 i = d0_idx;
878 do {
879 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
880
881 srcs[slot] = sh->dev[i].page;
882 i = raid6_next_disk(i, disks);
883 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700884
NeilBrowne4424fe2009-10-16 16:27:34 +1100885 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700886}
887
888static struct dma_async_tx_descriptor *
889ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
890{
891 int disks = sh->disks;
892 struct page **blocks = percpu->scribble;
893 int target;
894 int qd_idx = sh->qd_idx;
895 struct dma_async_tx_descriptor *tx;
896 struct async_submit_ctl submit;
897 struct r5dev *tgt;
898 struct page *dest;
899 int i;
900 int count;
901
902 if (sh->ops.target < 0)
903 target = sh->ops.target2;
904 else if (sh->ops.target2 < 0)
905 target = sh->ops.target;
906 else
907 /* we should only have one valid target */
908 BUG();
909 BUG_ON(target < 0);
910 pr_debug("%s: stripe %llu block: %d\n",
911 __func__, (unsigned long long)sh->sector, target);
912
913 tgt = &sh->dev[target];
914 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
915 dest = tgt->page;
916
917 atomic_inc(&sh->count);
918
919 if (target == qd_idx) {
920 count = set_syndrome_sources(blocks, sh);
921 blocks[count] = NULL; /* regenerating p is not necessary */
922 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700923 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
924 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700925 to_addr_conv(sh, percpu));
926 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
927 } else {
928 /* Compute any data- or p-drive using XOR */
929 count = 0;
930 for (i = disks; i-- ; ) {
931 if (i == target || i == qd_idx)
932 continue;
933 blocks[count++] = sh->dev[i].page;
934 }
935
Dan Williams0403e382009-09-08 17:42:50 -0700936 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
937 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700938 to_addr_conv(sh, percpu));
939 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
940 }
941
942 return tx;
943}
944
945static struct dma_async_tx_descriptor *
946ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
947{
948 int i, count, disks = sh->disks;
949 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
950 int d0_idx = raid6_d0(sh);
951 int faila = -1, failb = -1;
952 int target = sh->ops.target;
953 int target2 = sh->ops.target2;
954 struct r5dev *tgt = &sh->dev[target];
955 struct r5dev *tgt2 = &sh->dev[target2];
956 struct dma_async_tx_descriptor *tx;
957 struct page **blocks = percpu->scribble;
958 struct async_submit_ctl submit;
959
960 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
961 __func__, (unsigned long long)sh->sector, target, target2);
962 BUG_ON(target < 0 || target2 < 0);
963 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
964 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
965
Dan Williams6c910a72009-09-16 12:24:54 -0700966 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -0700967 * slot number conversion for 'faila' and 'failb'
968 */
969 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100970 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700971 count = 0;
972 i = d0_idx;
973 do {
974 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
975
976 blocks[slot] = sh->dev[i].page;
977
978 if (i == target)
979 faila = slot;
980 if (i == target2)
981 failb = slot;
982 i = raid6_next_disk(i, disks);
983 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700984
985 BUG_ON(faila == failb);
986 if (failb < faila)
987 swap(faila, failb);
988 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
989 __func__, (unsigned long long)sh->sector, faila, failb);
990
991 atomic_inc(&sh->count);
992
993 if (failb == syndrome_disks+1) {
994 /* Q disk is one of the missing disks */
995 if (faila == syndrome_disks) {
996 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -0700997 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
998 ops_complete_compute, sh,
999 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +11001000 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001001 STRIPE_SIZE, &submit);
1002 } else {
1003 struct page *dest;
1004 int data_target;
1005 int qd_idx = sh->qd_idx;
1006
1007 /* Missing D+Q: recompute D from P, then recompute Q */
1008 if (target == qd_idx)
1009 data_target = target2;
1010 else
1011 data_target = target;
1012
1013 count = 0;
1014 for (i = disks; i-- ; ) {
1015 if (i == data_target || i == qd_idx)
1016 continue;
1017 blocks[count++] = sh->dev[i].page;
1018 }
1019 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001020 init_async_submit(&submit,
1021 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1022 NULL, NULL, NULL,
1023 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001024 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1025 &submit);
1026
1027 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001028 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1029 ops_complete_compute, sh,
1030 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001031 return async_gen_syndrome(blocks, 0, count+2,
1032 STRIPE_SIZE, &submit);
1033 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001034 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001035 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1036 ops_complete_compute, sh,
1037 to_addr_conv(sh, percpu));
1038 if (failb == syndrome_disks) {
1039 /* We're missing D+P. */
1040 return async_raid6_datap_recov(syndrome_disks+2,
1041 STRIPE_SIZE, faila,
1042 blocks, &submit);
1043 } else {
1044 /* We're missing D+D. */
1045 return async_raid6_2data_recov(syndrome_disks+2,
1046 STRIPE_SIZE, faila, failb,
1047 blocks, &submit);
1048 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001049 }
1050}
1051
1052
Dan Williams91c00922007-01-02 13:52:30 -07001053static void ops_complete_prexor(void *stripe_head_ref)
1054{
1055 struct stripe_head *sh = stripe_head_ref;
1056
Harvey Harrisone46b2722008-04-28 02:15:50 -07001057 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001058 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001059}
1060
1061static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001062ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1063 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001064{
Dan Williams91c00922007-01-02 13:52:30 -07001065 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001066 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001067 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001068 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001069
1070 /* existing parity data subtracted */
1071 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1072
Harvey Harrisone46b2722008-04-28 02:15:50 -07001073 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001074 (unsigned long long)sh->sector);
1075
1076 for (i = disks; i--; ) {
1077 struct r5dev *dev = &sh->dev[i];
1078 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001079 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001080 xor_srcs[count++] = dev->page;
1081 }
1082
Dan Williams0403e382009-09-08 17:42:50 -07001083 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001084 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001085 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001086
1087 return tx;
1088}
1089
1090static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001091ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001092{
1093 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001094 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001095
Harvey Harrisone46b2722008-04-28 02:15:50 -07001096 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001097 (unsigned long long)sh->sector);
1098
1099 for (i = disks; i--; ) {
1100 struct r5dev *dev = &sh->dev[i];
1101 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001102
Dan Williamsd8ee0722008-06-28 08:32:06 +10001103 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001104 struct bio *wbi;
1105
NeilBrowncbe47ec2011-07-26 11:20:35 +10001106 spin_lock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001107 chosen = dev->towrite;
1108 dev->towrite = NULL;
1109 BUG_ON(dev->written);
1110 wbi = dev->written = chosen;
NeilBrowncbe47ec2011-07-26 11:20:35 +10001111 spin_unlock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001112
1113 while (wbi && wbi->bi_sector <
1114 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001115 if (wbi->bi_rw & REQ_FUA)
1116 set_bit(R5_WantFUA, &dev->flags);
Dan Williams91c00922007-01-02 13:52:30 -07001117 tx = async_copy_data(1, wbi, dev->page,
1118 dev->sector, tx);
1119 wbi = r5_next_bio(wbi, dev->sector);
1120 }
1121 }
1122 }
1123
1124 return tx;
1125}
1126
Dan Williamsac6b53b2009-07-14 13:40:19 -07001127static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001128{
1129 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001130 int disks = sh->disks;
1131 int pd_idx = sh->pd_idx;
1132 int qd_idx = sh->qd_idx;
1133 int i;
Tejun Heoe9c74692010-09-03 11:56:18 +02001134 bool fua = false;
Dan Williams91c00922007-01-02 13:52:30 -07001135
Harvey Harrisone46b2722008-04-28 02:15:50 -07001136 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001137 (unsigned long long)sh->sector);
1138
Tejun Heoe9c74692010-09-03 11:56:18 +02001139 for (i = disks; i--; )
1140 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
1141
Dan Williams91c00922007-01-02 13:52:30 -07001142 for (i = disks; i--; ) {
1143 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001144
Tejun Heoe9c74692010-09-03 11:56:18 +02001145 if (dev->written || i == pd_idx || i == qd_idx) {
Dan Williams91c00922007-01-02 13:52:30 -07001146 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001147 if (fua)
1148 set_bit(R5_WantFUA, &dev->flags);
1149 }
Dan Williams91c00922007-01-02 13:52:30 -07001150 }
1151
Dan Williamsd8ee0722008-06-28 08:32:06 +10001152 if (sh->reconstruct_state == reconstruct_state_drain_run)
1153 sh->reconstruct_state = reconstruct_state_drain_result;
1154 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1155 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1156 else {
1157 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1158 sh->reconstruct_state = reconstruct_state_result;
1159 }
Dan Williams91c00922007-01-02 13:52:30 -07001160
1161 set_bit(STRIPE_HANDLE, &sh->state);
1162 release_stripe(sh);
1163}
1164
1165static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001166ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1167 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001168{
Dan Williams91c00922007-01-02 13:52:30 -07001169 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001170 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001171 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001172 int count = 0, pd_idx = sh->pd_idx, i;
1173 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001174 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001175 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001176
Harvey Harrisone46b2722008-04-28 02:15:50 -07001177 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001178 (unsigned long long)sh->sector);
1179
1180 /* check if prexor is active which means only process blocks
1181 * that are part of a read-modify-write (written)
1182 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001183 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1184 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001185 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1186 for (i = disks; i--; ) {
1187 struct r5dev *dev = &sh->dev[i];
1188 if (dev->written)
1189 xor_srcs[count++] = dev->page;
1190 }
1191 } else {
1192 xor_dest = sh->dev[pd_idx].page;
1193 for (i = disks; i--; ) {
1194 struct r5dev *dev = &sh->dev[i];
1195 if (i != pd_idx)
1196 xor_srcs[count++] = dev->page;
1197 }
1198 }
1199
Dan Williams91c00922007-01-02 13:52:30 -07001200 /* 1/ if we prexor'd then the dest is reused as a source
1201 * 2/ if we did not prexor then we are redoing the parity
1202 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1203 * for the synchronous xor case
1204 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001205 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001206 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1207
1208 atomic_inc(&sh->count);
1209
Dan Williamsac6b53b2009-07-14 13:40:19 -07001210 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001211 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001212 if (unlikely(count == 1))
1213 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1214 else
1215 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001216}
1217
Dan Williamsac6b53b2009-07-14 13:40:19 -07001218static void
1219ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1220 struct dma_async_tx_descriptor *tx)
1221{
1222 struct async_submit_ctl submit;
1223 struct page **blocks = percpu->scribble;
1224 int count;
1225
1226 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1227
1228 count = set_syndrome_sources(blocks, sh);
1229
1230 atomic_inc(&sh->count);
1231
1232 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1233 sh, to_addr_conv(sh, percpu));
1234 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001235}
1236
1237static void ops_complete_check(void *stripe_head_ref)
1238{
1239 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001240
Harvey Harrisone46b2722008-04-28 02:15:50 -07001241 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001242 (unsigned long long)sh->sector);
1243
Dan Williamsecc65c92008-06-28 08:31:57 +10001244 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001245 set_bit(STRIPE_HANDLE, &sh->state);
1246 release_stripe(sh);
1247}
1248
Dan Williamsac6b53b2009-07-14 13:40:19 -07001249static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001250{
Dan Williams91c00922007-01-02 13:52:30 -07001251 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001252 int pd_idx = sh->pd_idx;
1253 int qd_idx = sh->qd_idx;
1254 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001255 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001256 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001257 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001258 int count;
1259 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001260
Harvey Harrisone46b2722008-04-28 02:15:50 -07001261 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001262 (unsigned long long)sh->sector);
1263
Dan Williamsac6b53b2009-07-14 13:40:19 -07001264 count = 0;
1265 xor_dest = sh->dev[pd_idx].page;
1266 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001267 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001268 if (i == pd_idx || i == qd_idx)
1269 continue;
1270 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001271 }
1272
Dan Williamsd6f38f32009-07-14 11:50:52 -07001273 init_async_submit(&submit, 0, NULL, NULL, NULL,
1274 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001275 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001276 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001277
Dan Williams91c00922007-01-02 13:52:30 -07001278 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001279 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1280 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001281}
1282
Dan Williamsac6b53b2009-07-14 13:40:19 -07001283static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1284{
1285 struct page **srcs = percpu->scribble;
1286 struct async_submit_ctl submit;
1287 int count;
1288
1289 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1290 (unsigned long long)sh->sector, checkp);
1291
1292 count = set_syndrome_sources(srcs, sh);
1293 if (!checkp)
1294 srcs[count] = NULL;
1295
1296 atomic_inc(&sh->count);
1297 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1298 sh, to_addr_conv(sh, percpu));
1299 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1300 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1301}
1302
Dan Williams417b8d42009-10-16 16:25:22 +11001303static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001304{
1305 int overlap_clear = 0, i, disks = sh->disks;
1306 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001307 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001308 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001309 struct raid5_percpu *percpu;
1310 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001311
Dan Williamsd6f38f32009-07-14 11:50:52 -07001312 cpu = get_cpu();
1313 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001314 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001315 ops_run_biofill(sh);
1316 overlap_clear++;
1317 }
1318
Dan Williams7b3a8712008-06-28 08:32:09 +10001319 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001320 if (level < 6)
1321 tx = ops_run_compute5(sh, percpu);
1322 else {
1323 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1324 tx = ops_run_compute6_1(sh, percpu);
1325 else
1326 tx = ops_run_compute6_2(sh, percpu);
1327 }
1328 /* terminate the chain if reconstruct is not set to be run */
1329 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001330 async_tx_ack(tx);
1331 }
Dan Williams91c00922007-01-02 13:52:30 -07001332
Dan Williams600aa102008-06-28 08:32:05 +10001333 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001334 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001335
Dan Williams600aa102008-06-28 08:32:05 +10001336 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001337 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001338 overlap_clear++;
1339 }
1340
Dan Williamsac6b53b2009-07-14 13:40:19 -07001341 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1342 if (level < 6)
1343 ops_run_reconstruct5(sh, percpu, tx);
1344 else
1345 ops_run_reconstruct6(sh, percpu, tx);
1346 }
Dan Williams91c00922007-01-02 13:52:30 -07001347
Dan Williamsac6b53b2009-07-14 13:40:19 -07001348 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1349 if (sh->check_state == check_state_run)
1350 ops_run_check_p(sh, percpu);
1351 else if (sh->check_state == check_state_run_q)
1352 ops_run_check_pq(sh, percpu, 0);
1353 else if (sh->check_state == check_state_run_pq)
1354 ops_run_check_pq(sh, percpu, 1);
1355 else
1356 BUG();
1357 }
Dan Williams91c00922007-01-02 13:52:30 -07001358
Dan Williams91c00922007-01-02 13:52:30 -07001359 if (overlap_clear)
1360 for (i = disks; i--; ) {
1361 struct r5dev *dev = &sh->dev[i];
1362 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1363 wake_up(&sh->raid_conf->wait_for_overlap);
1364 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001365 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001366}
1367
Dan Williams417b8d42009-10-16 16:25:22 +11001368#ifdef CONFIG_MULTICORE_RAID456
1369static void async_run_ops(void *param, async_cookie_t cookie)
1370{
1371 struct stripe_head *sh = param;
1372 unsigned long ops_request = sh->ops.request;
1373
1374 clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
1375 wake_up(&sh->ops.wait_for_ops);
1376
1377 __raid_run_ops(sh, ops_request);
1378 release_stripe(sh);
1379}
1380
1381static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1382{
1383 /* since handle_stripe can be called outside of raid5d context
1384 * we need to ensure sh->ops.request is de-staged before another
1385 * request arrives
1386 */
1387 wait_event(sh->ops.wait_for_ops,
1388 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
1389 sh->ops.request = ops_request;
1390
1391 atomic_inc(&sh->count);
1392 async_schedule(async_run_ops, sh);
1393}
1394#else
1395#define raid_run_ops __raid_run_ops
1396#endif
1397
NeilBrownd1688a62011-10-11 16:49:52 +11001398static int grow_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
1400 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001401 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001402 if (!sh)
1403 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001404
NeilBrown3f294f42005-11-08 21:39:25 -08001405 sh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001406 #ifdef CONFIG_MULTICORE_RAID456
1407 init_waitqueue_head(&sh->ops.wait_for_ops);
1408 #endif
NeilBrown3f294f42005-11-08 21:39:25 -08001409
NeilBrowne4e11e32010-06-16 16:45:16 +10001410 if (grow_buffers(sh)) {
1411 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001412 kmem_cache_free(conf->slab_cache, sh);
1413 return 0;
1414 }
1415 /* we just created an active stripe so... */
1416 atomic_set(&sh->count, 1);
1417 atomic_inc(&conf->active_stripes);
1418 INIT_LIST_HEAD(&sh->lru);
1419 release_stripe(sh);
1420 return 1;
1421}
1422
NeilBrownd1688a62011-10-11 16:49:52 +11001423static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001424{
Christoph Lametere18b8902006-12-06 20:33:20 -08001425 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001426 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
NeilBrownf4be6b42010-06-01 19:37:25 +10001428 if (conf->mddev->gendisk)
1429 sprintf(conf->cache_name[0],
1430 "raid%d-%s", conf->level, mdname(conf->mddev));
1431 else
1432 sprintf(conf->cache_name[0],
1433 "raid%d-%p", conf->level, conf->mddev);
1434 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1435
NeilBrownad01c9e2006-03-27 01:18:07 -08001436 conf->active_name = 0;
1437 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001439 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 if (!sc)
1441 return 1;
1442 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001443 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001444 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001445 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 return 0;
1448}
NeilBrown29269552006-03-27 01:18:10 -08001449
Dan Williamsd6f38f32009-07-14 11:50:52 -07001450/**
1451 * scribble_len - return the required size of the scribble region
1452 * @num - total number of disks in the array
1453 *
1454 * The size must be enough to contain:
1455 * 1/ a struct page pointer for each device in the array +2
1456 * 2/ room to convert each entry in (1) to its corresponding dma
1457 * (dma_map_page()) or page (page_address()) address.
1458 *
1459 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1460 * calculate over all devices (not just the data blocks), using zeros in place
1461 * of the P and Q blocks.
1462 */
1463static size_t scribble_len(int num)
1464{
1465 size_t len;
1466
1467 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1468
1469 return len;
1470}
1471
NeilBrownd1688a62011-10-11 16:49:52 +11001472static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001473{
1474 /* Make all the stripes able to hold 'newsize' devices.
1475 * New slots in each stripe get 'page' set to a new page.
1476 *
1477 * This happens in stages:
1478 * 1/ create a new kmem_cache and allocate the required number of
1479 * stripe_heads.
1480 * 2/ gather all the old stripe_heads and tranfer the pages across
1481 * to the new stripe_heads. This will have the side effect of
1482 * freezing the array as once all stripe_heads have been collected,
1483 * no IO will be possible. Old stripe heads are freed once their
1484 * pages have been transferred over, and the old kmem_cache is
1485 * freed when all stripes are done.
1486 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1487 * we simple return a failre status - no need to clean anything up.
1488 * 4/ allocate new pages for the new slots in the new stripe_heads.
1489 * If this fails, we don't bother trying the shrink the
1490 * stripe_heads down again, we just leave them as they are.
1491 * As each stripe_head is processed the new one is released into
1492 * active service.
1493 *
1494 * Once step2 is started, we cannot afford to wait for a write,
1495 * so we use GFP_NOIO allocations.
1496 */
1497 struct stripe_head *osh, *nsh;
1498 LIST_HEAD(newstripes);
1499 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001500 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001501 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001502 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001503 int i;
1504
1505 if (newsize <= conf->pool_size)
1506 return 0; /* never bother to shrink */
1507
Dan Williamsb5470dc2008-06-27 21:44:04 -07001508 err = md_allow_write(conf->mddev);
1509 if (err)
1510 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001511
NeilBrownad01c9e2006-03-27 01:18:07 -08001512 /* Step 1 */
1513 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1514 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001515 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001516 if (!sc)
1517 return -ENOMEM;
1518
1519 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001520 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001521 if (!nsh)
1522 break;
1523
NeilBrownad01c9e2006-03-27 01:18:07 -08001524 nsh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001525 #ifdef CONFIG_MULTICORE_RAID456
1526 init_waitqueue_head(&nsh->ops.wait_for_ops);
1527 #endif
NeilBrownad01c9e2006-03-27 01:18:07 -08001528
1529 list_add(&nsh->lru, &newstripes);
1530 }
1531 if (i) {
1532 /* didn't get enough, give up */
1533 while (!list_empty(&newstripes)) {
1534 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1535 list_del(&nsh->lru);
1536 kmem_cache_free(sc, nsh);
1537 }
1538 kmem_cache_destroy(sc);
1539 return -ENOMEM;
1540 }
1541 /* Step 2 - Must use GFP_NOIO now.
1542 * OK, we have enough stripes, start collecting inactive
1543 * stripes and copying them over
1544 */
1545 list_for_each_entry(nsh, &newstripes, lru) {
1546 spin_lock_irq(&conf->device_lock);
1547 wait_event_lock_irq(conf->wait_for_stripe,
1548 !list_empty(&conf->inactive_list),
1549 conf->device_lock,
NeilBrown482c0832011-04-18 18:25:42 +10001550 );
NeilBrownad01c9e2006-03-27 01:18:07 -08001551 osh = get_free_stripe(conf);
1552 spin_unlock_irq(&conf->device_lock);
1553 atomic_set(&nsh->count, 1);
1554 for(i=0; i<conf->pool_size; i++)
1555 nsh->dev[i].page = osh->dev[i].page;
1556 for( ; i<newsize; i++)
1557 nsh->dev[i].page = NULL;
1558 kmem_cache_free(conf->slab_cache, osh);
1559 }
1560 kmem_cache_destroy(conf->slab_cache);
1561
1562 /* Step 3.
1563 * At this point, we are holding all the stripes so the array
1564 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001565 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001566 */
1567 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1568 if (ndisks) {
1569 for (i=0; i<conf->raid_disks; i++)
1570 ndisks[i] = conf->disks[i];
1571 kfree(conf->disks);
1572 conf->disks = ndisks;
1573 } else
1574 err = -ENOMEM;
1575
Dan Williamsd6f38f32009-07-14 11:50:52 -07001576 get_online_cpus();
1577 conf->scribble_len = scribble_len(newsize);
1578 for_each_present_cpu(cpu) {
1579 struct raid5_percpu *percpu;
1580 void *scribble;
1581
1582 percpu = per_cpu_ptr(conf->percpu, cpu);
1583 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1584
1585 if (scribble) {
1586 kfree(percpu->scribble);
1587 percpu->scribble = scribble;
1588 } else {
1589 err = -ENOMEM;
1590 break;
1591 }
1592 }
1593 put_online_cpus();
1594
NeilBrownad01c9e2006-03-27 01:18:07 -08001595 /* Step 4, return new stripes to service */
1596 while(!list_empty(&newstripes)) {
1597 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1598 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001599
NeilBrownad01c9e2006-03-27 01:18:07 -08001600 for (i=conf->raid_disks; i < newsize; i++)
1601 if (nsh->dev[i].page == NULL) {
1602 struct page *p = alloc_page(GFP_NOIO);
1603 nsh->dev[i].page = p;
1604 if (!p)
1605 err = -ENOMEM;
1606 }
1607 release_stripe(nsh);
1608 }
1609 /* critical section pass, GFP_NOIO no longer needed */
1610
1611 conf->slab_cache = sc;
1612 conf->active_name = 1-conf->active_name;
1613 conf->pool_size = newsize;
1614 return err;
1615}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
NeilBrownd1688a62011-10-11 16:49:52 +11001617static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
1619 struct stripe_head *sh;
1620
NeilBrown3f294f42005-11-08 21:39:25 -08001621 spin_lock_irq(&conf->device_lock);
1622 sh = get_free_stripe(conf);
1623 spin_unlock_irq(&conf->device_lock);
1624 if (!sh)
1625 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001626 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001627 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001628 kmem_cache_free(conf->slab_cache, sh);
1629 atomic_dec(&conf->active_stripes);
1630 return 1;
1631}
1632
NeilBrownd1688a62011-10-11 16:49:52 +11001633static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001634{
1635 while (drop_one_stripe(conf))
1636 ;
1637
NeilBrown29fc7e32006-02-03 03:03:41 -08001638 if (conf->slab_cache)
1639 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 conf->slab_cache = NULL;
1641}
1642
NeilBrown6712ecf2007-09-27 12:47:43 +02001643static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
NeilBrown99c0fb52009-03-31 14:39:38 +11001645 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001646 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001647 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001649 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11001650 struct md_rdev *rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
1653 for (i=0 ; i<disks; i++)
1654 if (bi == &sh->dev[i].req)
1655 break;
1656
Dan Williams45b42332007-07-09 11:56:43 -07001657 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1658 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 uptodate);
1660 if (i == disks) {
1661 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001662 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 }
NeilBrown14a75d32011-12-23 10:17:52 +11001664 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11001665 /* If replacement finished while this request was outstanding,
1666 * 'replacement' might be NULL already.
1667 * In that case it moved down to 'rdev'.
1668 * rdev is not removed until all requests are finished.
1669 */
NeilBrown14a75d32011-12-23 10:17:52 +11001670 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001671 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11001672 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
1674 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001676 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11001677 /* Note that this cannot happen on a
1678 * replacement device. We just fail those on
1679 * any error
1680 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001681 printk_ratelimited(
1682 KERN_INFO
1683 "md/raid:%s: read error corrected"
1684 " (%lu sectors at %llu on %s)\n",
1685 mdname(conf->mddev), STRIPE_SECTORS,
1686 (unsigned long long)(sh->sector
1687 + rdev->data_offset),
1688 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001689 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001690 clear_bit(R5_ReadError, &sh->dev[i].flags);
1691 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1692 }
NeilBrown14a75d32011-12-23 10:17:52 +11001693 if (atomic_read(&rdev->read_errors))
1694 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11001696 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001697 int retry = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001698
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001700 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11001701 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1702 printk_ratelimited(
1703 KERN_WARNING
1704 "md/raid:%s: read error on replacement device "
1705 "(sector %llu on %s).\n",
1706 mdname(conf->mddev),
1707 (unsigned long long)(sh->sector
1708 + rdev->data_offset),
1709 bdn);
1710 else if (conf->mddev->degraded >= conf->max_degraded)
Christian Dietrich8bda4702011-07-27 11:00:36 +10001711 printk_ratelimited(
1712 KERN_WARNING
1713 "md/raid:%s: read error not correctable "
1714 "(sector %llu on %s).\n",
1715 mdname(conf->mddev),
1716 (unsigned long long)(sh->sector
1717 + rdev->data_offset),
1718 bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001719 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
NeilBrown4e5314b2005-11-08 21:39:22 -08001720 /* Oh, no!!! */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001721 printk_ratelimited(
1722 KERN_WARNING
1723 "md/raid:%s: read error NOT corrected!! "
1724 "(sector %llu on %s).\n",
1725 mdname(conf->mddev),
1726 (unsigned long long)(sh->sector
1727 + rdev->data_offset),
1728 bdn);
NeilBrownd6950432006-07-10 04:44:20 -07001729 else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001730 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001731 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001732 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001733 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001734 else
1735 retry = 1;
1736 if (retry)
1737 set_bit(R5_ReadError, &sh->dev[i].flags);
1738 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001739 clear_bit(R5_ReadError, &sh->dev[i].flags);
1740 clear_bit(R5_ReWrite, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001741 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 }
NeilBrown14a75d32011-12-23 10:17:52 +11001744 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1746 set_bit(STRIPE_HANDLE, &sh->state);
1747 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748}
1749
NeilBrownd710e132008-10-13 11:55:12 +11001750static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751{
NeilBrown99c0fb52009-03-31 14:39:38 +11001752 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001753 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001754 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11001755 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001757 sector_t first_bad;
1758 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11001759 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
NeilBrown977df362011-12-23 10:17:53 +11001761 for (i = 0 ; i < disks; i++) {
1762 if (bi == &sh->dev[i].req) {
1763 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 break;
NeilBrown977df362011-12-23 10:17:53 +11001765 }
1766 if (bi == &sh->dev[i].rreq) {
1767 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001768 if (rdev)
1769 replacement = 1;
1770 else
1771 /* rdev was removed and 'replacement'
1772 * replaced it. rdev is not removed
1773 * until all requests are finished.
1774 */
1775 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11001776 break;
1777 }
1778 }
Dan Williams45b42332007-07-09 11:56:43 -07001779 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1781 uptodate);
1782 if (i == disks) {
1783 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001784 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 }
1786
NeilBrown977df362011-12-23 10:17:53 +11001787 if (replacement) {
1788 if (!uptodate)
1789 md_error(conf->mddev, rdev);
1790 else if (is_badblock(rdev, sh->sector,
1791 STRIPE_SECTORS,
1792 &first_bad, &bad_sectors))
1793 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1794 } else {
1795 if (!uptodate) {
1796 set_bit(WriteErrorSeen, &rdev->flags);
1797 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11001798 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1799 set_bit(MD_RECOVERY_NEEDED,
1800 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11001801 } else if (is_badblock(rdev, sh->sector,
1802 STRIPE_SECTORS,
1803 &first_bad, &bad_sectors))
1804 set_bit(R5_MadeGood, &sh->dev[i].flags);
1805 }
1806 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
NeilBrown977df362011-12-23 10:17:53 +11001808 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1809 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001811 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812}
1813
NeilBrown784052e2009-03-31 15:19:07 +11001814static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
NeilBrown784052e2009-03-31 15:19:07 +11001816static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
1818 struct r5dev *dev = &sh->dev[i];
1819
1820 bio_init(&dev->req);
1821 dev->req.bi_io_vec = &dev->vec;
1822 dev->req.bi_vcnt++;
1823 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11001825 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
NeilBrown977df362011-12-23 10:17:53 +11001827 bio_init(&dev->rreq);
1828 dev->rreq.bi_io_vec = &dev->rvec;
1829 dev->rreq.bi_vcnt++;
1830 dev->rreq.bi_max_vecs++;
1831 dev->rreq.bi_private = sh;
1832 dev->rvec.bv_page = dev->page;
1833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001835 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836}
1837
NeilBrownfd01b882011-10-11 16:47:53 +11001838static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839{
1840 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11001841 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11001842 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10001843 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
NeilBrown908f4fb2011-12-23 10:17:50 +11001845 spin_lock_irqsave(&conf->device_lock, flags);
1846 clear_bit(In_sync, &rdev->flags);
1847 mddev->degraded = calc_degraded(conf);
1848 spin_unlock_irqrestore(&conf->device_lock, flags);
1849 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1850
NeilBrownde393cd2011-07-28 11:31:48 +10001851 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001852 set_bit(Faulty, &rdev->flags);
1853 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1854 printk(KERN_ALERT
1855 "md/raid:%s: Disk failure on %s, disabling device.\n"
1856 "md/raid:%s: Operation continuing on %d devices.\n",
1857 mdname(mddev),
1858 bdevname(rdev->bdev, b),
1859 mdname(mddev),
1860 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001861}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863/*
1864 * Input: a 'big' sector number,
1865 * Output: index of the data and parity disk, and the sector # in them.
1866 */
NeilBrownd1688a62011-10-11 16:49:52 +11001867static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001868 int previous, int *dd_idx,
1869 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001871 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001872 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001874 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001875 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001877 int algorithm = previous ? conf->prev_algo
1878 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001879 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1880 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001881 int raid_disks = previous ? conf->previous_raid_disks
1882 : conf->raid_disks;
1883 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
1885 /* First compute the information on this sector */
1886
1887 /*
1888 * Compute the chunk number and the sector offset inside the chunk
1889 */
1890 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1891 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
1893 /*
1894 * Compute the stripe number
1895 */
NeilBrown35f2a592010-04-20 14:13:34 +10001896 stripe = chunk_number;
1897 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10001898 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 /*
1900 * Select the parity disk based on the user selected algorithm.
1901 */
NeilBrown84789552011-07-27 11:00:36 +10001902 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07001903 switch(conf->level) {
1904 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001905 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001906 break;
1907 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001908 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001910 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001911 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 (*dd_idx)++;
1913 break;
1914 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001915 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001916 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 (*dd_idx)++;
1918 break;
1919 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001920 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001921 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 break;
1923 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001924 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001925 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001927 case ALGORITHM_PARITY_0:
1928 pd_idx = 0;
1929 (*dd_idx)++;
1930 break;
1931 case ALGORITHM_PARITY_N:
1932 pd_idx = data_disks;
1933 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11001935 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001936 }
1937 break;
1938 case 6:
1939
NeilBrowne183eae2009-03-31 15:20:22 +11001940 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001941 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001942 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001943 qd_idx = pd_idx + 1;
1944 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001945 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001946 qd_idx = 0;
1947 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001948 (*dd_idx) += 2; /* D D P Q D */
1949 break;
1950 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001951 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001952 qd_idx = pd_idx + 1;
1953 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001954 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001955 qd_idx = 0;
1956 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001957 (*dd_idx) += 2; /* D D P Q D */
1958 break;
1959 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001960 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001961 qd_idx = (pd_idx + 1) % raid_disks;
1962 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001963 break;
1964 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001965 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001966 qd_idx = (pd_idx + 1) % raid_disks;
1967 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001968 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001969
1970 case ALGORITHM_PARITY_0:
1971 pd_idx = 0;
1972 qd_idx = 1;
1973 (*dd_idx) += 2;
1974 break;
1975 case ALGORITHM_PARITY_N:
1976 pd_idx = data_disks;
1977 qd_idx = data_disks + 1;
1978 break;
1979
1980 case ALGORITHM_ROTATING_ZERO_RESTART:
1981 /* Exactly the same as RIGHT_ASYMMETRIC, but or
1982 * of blocks for computing Q is different.
1983 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001984 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11001985 qd_idx = pd_idx + 1;
1986 if (pd_idx == raid_disks-1) {
1987 (*dd_idx)++; /* Q D D D P */
1988 qd_idx = 0;
1989 } else if (*dd_idx >= pd_idx)
1990 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001991 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001992 break;
1993
1994 case ALGORITHM_ROTATING_N_RESTART:
1995 /* Same a left_asymmetric, by first stripe is
1996 * D D D P Q rather than
1997 * Q D D D P
1998 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001999 stripe2 += 1;
2000 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002001 qd_idx = pd_idx + 1;
2002 if (pd_idx == raid_disks-1) {
2003 (*dd_idx)++; /* Q D D D P */
2004 qd_idx = 0;
2005 } else if (*dd_idx >= pd_idx)
2006 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002007 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002008 break;
2009
2010 case ALGORITHM_ROTATING_N_CONTINUE:
2011 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002012 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002013 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2014 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002015 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002016 break;
2017
2018 case ALGORITHM_LEFT_ASYMMETRIC_6:
2019 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002020 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002021 if (*dd_idx >= pd_idx)
2022 (*dd_idx)++;
2023 qd_idx = raid_disks - 1;
2024 break;
2025
2026 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002027 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002028 if (*dd_idx >= pd_idx)
2029 (*dd_idx)++;
2030 qd_idx = raid_disks - 1;
2031 break;
2032
2033 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002034 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002035 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2036 qd_idx = raid_disks - 1;
2037 break;
2038
2039 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002040 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002041 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2042 qd_idx = raid_disks - 1;
2043 break;
2044
2045 case ALGORITHM_PARITY_0_6:
2046 pd_idx = 0;
2047 (*dd_idx)++;
2048 qd_idx = raid_disks - 1;
2049 break;
2050
NeilBrown16a53ec2006-06-26 00:27:38 -07002051 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002052 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002053 }
2054 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 }
2056
NeilBrown911d4ee2009-03-31 14:39:38 +11002057 if (sh) {
2058 sh->pd_idx = pd_idx;
2059 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002060 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 /*
2063 * Finally, compute the new sector number
2064 */
2065 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2066 return new_sector;
2067}
2068
2069
NeilBrown784052e2009-03-31 15:19:07 +11002070static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071{
NeilBrownd1688a62011-10-11 16:49:52 +11002072 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002073 int raid_disks = sh->disks;
2074 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002076 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2077 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002078 int algorithm = previous ? conf->prev_algo
2079 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 sector_t stripe;
2081 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002082 sector_t chunk_number;
2083 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002085 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
NeilBrown16a53ec2006-06-26 00:27:38 -07002087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2089 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
NeilBrown16a53ec2006-06-26 00:27:38 -07002091 if (i == sh->pd_idx)
2092 return 0;
2093 switch(conf->level) {
2094 case 4: break;
2095 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002096 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 case ALGORITHM_LEFT_ASYMMETRIC:
2098 case ALGORITHM_RIGHT_ASYMMETRIC:
2099 if (i > sh->pd_idx)
2100 i--;
2101 break;
2102 case ALGORITHM_LEFT_SYMMETRIC:
2103 case ALGORITHM_RIGHT_SYMMETRIC:
2104 if (i < sh->pd_idx)
2105 i += raid_disks;
2106 i -= (sh->pd_idx + 1);
2107 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002108 case ALGORITHM_PARITY_0:
2109 i -= 1;
2110 break;
2111 case ALGORITHM_PARITY_N:
2112 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002114 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002115 }
2116 break;
2117 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002118 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002119 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002120 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002121 case ALGORITHM_LEFT_ASYMMETRIC:
2122 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002123 case ALGORITHM_ROTATING_ZERO_RESTART:
2124 case ALGORITHM_ROTATING_N_RESTART:
2125 if (sh->pd_idx == raid_disks-1)
2126 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002127 else if (i > sh->pd_idx)
2128 i -= 2; /* D D P Q D */
2129 break;
2130 case ALGORITHM_LEFT_SYMMETRIC:
2131 case ALGORITHM_RIGHT_SYMMETRIC:
2132 if (sh->pd_idx == raid_disks-1)
2133 i--; /* Q D D D P */
2134 else {
2135 /* D D P Q D */
2136 if (i < sh->pd_idx)
2137 i += raid_disks;
2138 i -= (sh->pd_idx + 2);
2139 }
2140 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002141 case ALGORITHM_PARITY_0:
2142 i -= 2;
2143 break;
2144 case ALGORITHM_PARITY_N:
2145 break;
2146 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002147 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002148 if (sh->pd_idx == 0)
2149 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002150 else {
2151 /* D D Q P D */
2152 if (i < sh->pd_idx)
2153 i += raid_disks;
2154 i -= (sh->pd_idx + 1);
2155 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002156 break;
2157 case ALGORITHM_LEFT_ASYMMETRIC_6:
2158 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2159 if (i > sh->pd_idx)
2160 i--;
2161 break;
2162 case ALGORITHM_LEFT_SYMMETRIC_6:
2163 case ALGORITHM_RIGHT_SYMMETRIC_6:
2164 if (i < sh->pd_idx)
2165 i += data_disks + 1;
2166 i -= (sh->pd_idx + 1);
2167 break;
2168 case ALGORITHM_PARITY_0_6:
2169 i -= 1;
2170 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002171 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002172 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002173 }
2174 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 }
2176
2177 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002178 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
NeilBrown112bf892009-03-31 14:39:38 +11002180 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002181 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002182 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2183 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002184 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2185 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 return 0;
2187 }
2188 return r_sector;
2189}
2190
2191
Dan Williams600aa102008-06-28 08:32:05 +10002192static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002193schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002194 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002195{
2196 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002197 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002198 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002199
Dan Williamse33129d2007-01-02 13:52:30 -07002200 if (rcw) {
2201 /* if we are not expanding this is a proper write request, and
2202 * there will be bios with new data to be drained into the
2203 * stripe cache
2204 */
2205 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10002206 sh->reconstruct_state = reconstruct_state_drain_run;
2207 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2208 } else
2209 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07002210
Dan Williamsac6b53b2009-07-14 13:40:19 -07002211 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002212
2213 for (i = disks; i--; ) {
2214 struct r5dev *dev = &sh->dev[i];
2215
2216 if (dev->towrite) {
2217 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002218 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002219 if (!expand)
2220 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002221 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002222 }
2223 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002224 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002225 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002226 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002227 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002228 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002229 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2230 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2231
Dan Williamsd8ee0722008-06-28 08:32:06 +10002232 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002233 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2234 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002235 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002236
2237 for (i = disks; i--; ) {
2238 struct r5dev *dev = &sh->dev[i];
2239 if (i == pd_idx)
2240 continue;
2241
Dan Williamse33129d2007-01-02 13:52:30 -07002242 if (dev->towrite &&
2243 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002244 test_bit(R5_Wantcompute, &dev->flags))) {
2245 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002246 set_bit(R5_LOCKED, &dev->flags);
2247 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002248 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002249 }
2250 }
2251 }
2252
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002253 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002254 * are in flight
2255 */
2256 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2257 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002258 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002259
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002260 if (level == 6) {
2261 int qd_idx = sh->qd_idx;
2262 struct r5dev *dev = &sh->dev[qd_idx];
2263
2264 set_bit(R5_LOCKED, &dev->flags);
2265 clear_bit(R5_UPTODATE, &dev->flags);
2266 s->locked++;
2267 }
2268
Dan Williams600aa102008-06-28 08:32:05 +10002269 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002270 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002271 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002272}
NeilBrown16a53ec2006-06-26 00:27:38 -07002273
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274/*
2275 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002276 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 * The bi_next chain must be in order.
2278 */
2279static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2280{
2281 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002282 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002283 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
NeilBrowncbe47ec2011-07-26 11:20:35 +10002285 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 (unsigned long long)bi->bi_sector,
2287 (unsigned long long)sh->sector);
2288
2289
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07002291 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -07002293 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
2294 firstwrite = 1;
2295 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 bip = &sh->dev[dd_idx].toread;
2297 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2298 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2299 goto overlap;
2300 bip = & (*bip)->bi_next;
2301 }
2302 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2303 goto overlap;
2304
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002305 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 if (*bip)
2307 bi->bi_next = *bip;
2308 *bip = bi;
Jens Axboe960e7392008-08-15 10:41:18 +02002309 bi->bi_phys_segments++;
NeilBrown72626682005-09-09 16:23:54 -07002310
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 if (forwrite) {
2312 /* check if page is covered */
2313 sector_t sector = sh->dev[dd_idx].sector;
2314 for (bi=sh->dev[dd_idx].towrite;
2315 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2316 bi && bi->bi_sector <= sector;
2317 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2318 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2319 sector = bi->bi_sector + (bi->bi_size>>9);
2320 }
2321 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2322 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2323 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002324 spin_unlock_irq(&conf->device_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002325
2326 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2327 (unsigned long long)(*bip)->bi_sector,
2328 (unsigned long long)sh->sector, dd_idx);
2329
2330 if (conf->mddev->bitmap && firstwrite) {
2331 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2332 STRIPE_SECTORS, 0);
2333 sh->bm_seq = conf->seq_flush+1;
2334 set_bit(STRIPE_BIT_DELAY, &sh->state);
2335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 return 1;
2337
2338 overlap:
2339 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
2340 spin_unlock_irq(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 return 0;
2342}
2343
NeilBrownd1688a62011-10-11 16:49:52 +11002344static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002345
NeilBrownd1688a62011-10-11 16:49:52 +11002346static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002347 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002348{
NeilBrown784052e2009-03-31 15:19:07 +11002349 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002350 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002351 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002352 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002353 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002354
NeilBrown112bf892009-03-31 14:39:38 +11002355 raid5_compute_sector(conf,
2356 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002357 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002358 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002359 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002360}
2361
Dan Williamsa4456852007-07-09 11:56:43 -07002362static void
NeilBrownd1688a62011-10-11 16:49:52 +11002363handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002364 struct stripe_head_state *s, int disks,
2365 struct bio **return_bi)
2366{
2367 int i;
2368 for (i = disks; i--; ) {
2369 struct bio *bi;
2370 int bitmap_end = 0;
2371
2372 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002373 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002374 rcu_read_lock();
2375 rdev = rcu_dereference(conf->disks[i].rdev);
2376 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002377 atomic_inc(&rdev->nr_pending);
2378 else
2379 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002380 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002381 if (rdev) {
2382 if (!rdev_set_badblocks(
2383 rdev,
2384 sh->sector,
2385 STRIPE_SECTORS, 0))
2386 md_error(conf->mddev, rdev);
2387 rdev_dec_pending(rdev, conf->mddev);
2388 }
Dan Williamsa4456852007-07-09 11:56:43 -07002389 }
2390 spin_lock_irq(&conf->device_lock);
2391 /* fail all writes first */
2392 bi = sh->dev[i].towrite;
2393 sh->dev[i].towrite = NULL;
2394 if (bi) {
2395 s->to_write--;
2396 bitmap_end = 1;
2397 }
2398
2399 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2400 wake_up(&conf->wait_for_overlap);
2401
2402 while (bi && bi->bi_sector <
2403 sh->dev[i].sector + STRIPE_SECTORS) {
2404 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2405 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002406 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002407 md_write_end(conf->mddev);
2408 bi->bi_next = *return_bi;
2409 *return_bi = bi;
2410 }
2411 bi = nextbi;
2412 }
2413 /* and fail all 'written' */
2414 bi = sh->dev[i].written;
2415 sh->dev[i].written = NULL;
2416 if (bi) bitmap_end = 1;
2417 while (bi && bi->bi_sector <
2418 sh->dev[i].sector + STRIPE_SECTORS) {
2419 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2420 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002421 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002422 md_write_end(conf->mddev);
2423 bi->bi_next = *return_bi;
2424 *return_bi = bi;
2425 }
2426 bi = bi2;
2427 }
2428
Dan Williamsb5e98d62007-01-02 13:52:31 -07002429 /* fail any reads if this device is non-operational and
2430 * the data has not reached the cache yet.
2431 */
2432 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2433 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2434 test_bit(R5_ReadError, &sh->dev[i].flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002435 bi = sh->dev[i].toread;
2436 sh->dev[i].toread = NULL;
2437 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2438 wake_up(&conf->wait_for_overlap);
2439 if (bi) s->to_read--;
2440 while (bi && bi->bi_sector <
2441 sh->dev[i].sector + STRIPE_SECTORS) {
2442 struct bio *nextbi =
2443 r5_next_bio(bi, sh->dev[i].sector);
2444 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002445 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002446 bi->bi_next = *return_bi;
2447 *return_bi = bi;
2448 }
2449 bi = nextbi;
2450 }
2451 }
2452 spin_unlock_irq(&conf->device_lock);
2453 if (bitmap_end)
2454 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2455 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002456 /* If we were in the middle of a write the parity block might
2457 * still be locked - so just clear all R5_LOCKED flags
2458 */
2459 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002460 }
2461
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002462 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2463 if (atomic_dec_and_test(&conf->pending_full_writes))
2464 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002465}
2466
NeilBrown7f0da592011-07-28 11:39:22 +10002467static void
NeilBrownd1688a62011-10-11 16:49:52 +11002468handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002469 struct stripe_head_state *s)
2470{
2471 int abort = 0;
2472 int i;
2473
2474 md_done_sync(conf->mddev, STRIPE_SECTORS, 0);
2475 clear_bit(STRIPE_SYNCING, &sh->state);
2476 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002477 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002478 /* There is nothing more to do for sync/check/repair.
NeilBrown9a3e1102011-12-23 10:17:53 +11002479 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002480 * non-sync devices, or abort the recovery
2481 */
2482 if (!test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery))
2483 return;
2484 /* During recovery devices cannot be removed, so locking and
2485 * refcounting of rdevs is not needed
2486 */
2487 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +11002488 struct md_rdev *rdev = conf->disks[i].rdev;
NeilBrown9a3e1102011-12-23 10:17:53 +11002489 if (rdev
2490 && !test_bit(Faulty, &rdev->flags)
2491 && !test_bit(In_sync, &rdev->flags)
2492 && !rdev_set_badblocks(rdev, sh->sector,
2493 STRIPE_SECTORS, 0))
2494 abort = 1;
2495 rdev = conf->disks[i].replacement;
2496 if (rdev
2497 && !test_bit(Faulty, &rdev->flags)
2498 && !test_bit(In_sync, &rdev->flags)
2499 && !rdev_set_badblocks(rdev, sh->sector,
2500 STRIPE_SECTORS, 0))
NeilBrown7f0da592011-07-28 11:39:22 +10002501 abort = 1;
2502 }
2503 if (abort) {
2504 conf->recovery_disabled = conf->mddev->recovery_disabled;
2505 set_bit(MD_RECOVERY_INTR, &conf->mddev->recovery);
2506 }
2507}
2508
NeilBrown9a3e1102011-12-23 10:17:53 +11002509static int want_replace(struct stripe_head *sh, int disk_idx)
2510{
2511 struct md_rdev *rdev;
2512 int rv = 0;
2513 /* Doing recovery so rcu locking not required */
2514 rdev = sh->raid_conf->disks[disk_idx].replacement;
2515 if (rdev
2516 && !test_bit(Faulty, &rdev->flags)
2517 && !test_bit(In_sync, &rdev->flags)
2518 && (rdev->recovery_offset <= sh->sector
2519 || rdev->mddev->recovery_cp <= sh->sector))
2520 rv = 1;
2521
2522 return rv;
2523}
2524
NeilBrown93b3dbc2011-07-27 11:00:36 +10002525/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002526 * to be read or computed to satisfy a request.
2527 *
2528 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002529 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002530 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002531static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2532 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002533{
2534 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002535 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2536 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002537
Dan Williamsf38e1212007-01-02 13:52:30 -07002538 /* is the data in this block needed, and can we get it? */
2539 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002540 !test_bit(R5_UPTODATE, &dev->flags) &&
2541 (dev->toread ||
2542 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2543 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002544 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002545 (s->failed >= 1 && fdev[0]->toread) ||
2546 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002547 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2548 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2549 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002550 /* we would like to get this block, possibly by computing it,
2551 * otherwise read it if the backing disk is insync
2552 */
2553 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2554 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2555 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002556 (s->failed && (disk_idx == s->failed_num[0] ||
2557 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002558 /* have disk failed, and we're requested to fetch it;
2559 * do compute it
2560 */
2561 pr_debug("Computing stripe %llu block %d\n",
2562 (unsigned long long)sh->sector, disk_idx);
2563 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2564 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2565 set_bit(R5_Wantcompute, &dev->flags);
2566 sh->ops.target = disk_idx;
2567 sh->ops.target2 = -1; /* no 2nd target */
2568 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002569 /* Careful: from this point on 'uptodate' is in the eye
2570 * of raid_run_ops which services 'compute' operations
2571 * before writes. R5_Wantcompute flags a block that will
2572 * be R5_UPTODATE by the time it is needed for a
2573 * subsequent operation.
2574 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002575 s->uptodate++;
2576 return 1;
2577 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2578 /* Computing 2-failure is *very* expensive; only
2579 * do it if failed >= 2
2580 */
2581 int other;
2582 for (other = disks; other--; ) {
2583 if (other == disk_idx)
2584 continue;
2585 if (!test_bit(R5_UPTODATE,
2586 &sh->dev[other].flags))
2587 break;
2588 }
2589 BUG_ON(other < 0);
2590 pr_debug("Computing stripe %llu blocks %d,%d\n",
2591 (unsigned long long)sh->sector,
2592 disk_idx, other);
2593 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2594 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2595 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2596 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2597 sh->ops.target = disk_idx;
2598 sh->ops.target2 = other;
2599 s->uptodate += 2;
2600 s->req_compute = 1;
2601 return 1;
2602 } else if (test_bit(R5_Insync, &dev->flags)) {
2603 set_bit(R5_LOCKED, &dev->flags);
2604 set_bit(R5_Wantread, &dev->flags);
2605 s->locked++;
2606 pr_debug("Reading block %d (sync=%d)\n",
2607 disk_idx, s->syncing);
2608 }
2609 }
2610
2611 return 0;
2612}
2613
2614/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002615 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002616 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002617static void handle_stripe_fill(struct stripe_head *sh,
2618 struct stripe_head_state *s,
2619 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002620{
2621 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002622
2623 /* look for blocks to read/compute, skip this if a compute
2624 * is already in flight, or if the stripe contents are in the
2625 * midst of changing due to a write
2626 */
2627 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2628 !sh->reconstruct_state)
2629 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002630 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002631 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002632 set_bit(STRIPE_HANDLE, &sh->state);
2633}
2634
2635
Dan Williams1fe797e2008-06-28 09:16:30 +10002636/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002637 * any written block on an uptodate or failed drive can be returned.
2638 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2639 * never LOCKED, so we don't need to test 'failed' directly.
2640 */
NeilBrownd1688a62011-10-11 16:49:52 +11002641static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002642 struct stripe_head *sh, int disks, struct bio **return_bi)
2643{
2644 int i;
2645 struct r5dev *dev;
2646
2647 for (i = disks; i--; )
2648 if (sh->dev[i].written) {
2649 dev = &sh->dev[i];
2650 if (!test_bit(R5_LOCKED, &dev->flags) &&
2651 test_bit(R5_UPTODATE, &dev->flags)) {
2652 /* We can return any write requests */
2653 struct bio *wbi, *wbi2;
2654 int bitmap_end = 0;
Dan Williams45b42332007-07-09 11:56:43 -07002655 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002656 spin_lock_irq(&conf->device_lock);
2657 wbi = dev->written;
2658 dev->written = NULL;
2659 while (wbi && wbi->bi_sector <
2660 dev->sector + STRIPE_SECTORS) {
2661 wbi2 = r5_next_bio(wbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +02002662 if (!raid5_dec_bi_phys_segments(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002663 md_write_end(conf->mddev);
2664 wbi->bi_next = *return_bi;
2665 *return_bi = wbi;
2666 }
2667 wbi = wbi2;
2668 }
2669 if (dev->towrite == NULL)
2670 bitmap_end = 1;
2671 spin_unlock_irq(&conf->device_lock);
2672 if (bitmap_end)
2673 bitmap_endwrite(conf->mddev->bitmap,
2674 sh->sector,
2675 STRIPE_SECTORS,
2676 !test_bit(STRIPE_DEGRADED, &sh->state),
2677 0);
2678 }
2679 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002680
2681 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2682 if (atomic_dec_and_test(&conf->pending_full_writes))
2683 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002684}
2685
NeilBrownd1688a62011-10-11 16:49:52 +11002686static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002687 struct stripe_head *sh,
2688 struct stripe_head_state *s,
2689 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002690{
2691 int rmw = 0, rcw = 0, i;
NeilBrownc8ac1802011-07-27 11:00:36 +10002692 if (conf->max_degraded == 2) {
2693 /* RAID6 requires 'rcw' in current implementation
2694 * Calculate the real rcw later - for now fake it
2695 * look like rcw is cheaper
2696 */
2697 rcw = 1; rmw = 2;
2698 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002699 /* would I have to read this buffer for read_modify_write */
2700 struct r5dev *dev = &sh->dev[i];
2701 if ((dev->towrite || i == sh->pd_idx) &&
2702 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002703 !(test_bit(R5_UPTODATE, &dev->flags) ||
2704 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002705 if (test_bit(R5_Insync, &dev->flags))
2706 rmw++;
2707 else
2708 rmw += 2*disks; /* cannot read it */
2709 }
2710 /* Would I have to read this buffer for reconstruct_write */
2711 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2712 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002713 !(test_bit(R5_UPTODATE, &dev->flags) ||
2714 test_bit(R5_Wantcompute, &dev->flags))) {
2715 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002716 else
2717 rcw += 2*disks;
2718 }
2719 }
Dan Williams45b42332007-07-09 11:56:43 -07002720 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002721 (unsigned long long)sh->sector, rmw, rcw);
2722 set_bit(STRIPE_HANDLE, &sh->state);
2723 if (rmw < rcw && rmw > 0)
2724 /* prefer read-modify-write, but need to get some data */
2725 for (i = disks; i--; ) {
2726 struct r5dev *dev = &sh->dev[i];
2727 if ((dev->towrite || i == sh->pd_idx) &&
2728 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002729 !(test_bit(R5_UPTODATE, &dev->flags) ||
2730 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002731 test_bit(R5_Insync, &dev->flags)) {
2732 if (
2733 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002734 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002735 "%d for r-m-w\n", i);
2736 set_bit(R5_LOCKED, &dev->flags);
2737 set_bit(R5_Wantread, &dev->flags);
2738 s->locked++;
2739 } else {
2740 set_bit(STRIPE_DELAYED, &sh->state);
2741 set_bit(STRIPE_HANDLE, &sh->state);
2742 }
2743 }
2744 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002745 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002746 /* want reconstruct write, but need to get some data */
NeilBrownc8ac1802011-07-27 11:00:36 +10002747 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002748 for (i = disks; i--; ) {
2749 struct r5dev *dev = &sh->dev[i];
2750 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002751 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002752 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002753 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002754 test_bit(R5_Wantcompute, &dev->flags))) {
2755 rcw++;
2756 if (!test_bit(R5_Insync, &dev->flags))
2757 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002758 if (
2759 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002760 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002761 "%d for Reconstruct\n", i);
2762 set_bit(R5_LOCKED, &dev->flags);
2763 set_bit(R5_Wantread, &dev->flags);
2764 s->locked++;
2765 } else {
2766 set_bit(STRIPE_DELAYED, &sh->state);
2767 set_bit(STRIPE_HANDLE, &sh->state);
2768 }
2769 }
2770 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002771 }
Dan Williamsa4456852007-07-09 11:56:43 -07002772 /* now if nothing is locked, and if we have enough data,
2773 * we can start a write request
2774 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002775 /* since handle_stripe can be called at any time we need to handle the
2776 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002777 * subsequent call wants to start a write request. raid_run_ops only
2778 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002779 * simultaneously. If this is not the case then new writes need to be
2780 * held off until the compute completes.
2781 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002782 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2783 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2784 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002785 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002786}
2787
NeilBrownd1688a62011-10-11 16:49:52 +11002788static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002789 struct stripe_head_state *s, int disks)
2790{
Dan Williamsecc65c92008-06-28 08:31:57 +10002791 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002792
Dan Williamsbd2ab672008-04-10 21:29:27 -07002793 set_bit(STRIPE_HANDLE, &sh->state);
2794
Dan Williamsecc65c92008-06-28 08:31:57 +10002795 switch (sh->check_state) {
2796 case check_state_idle:
2797 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002798 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002799 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002800 sh->check_state = check_state_run;
2801 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002802 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002803 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002804 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002805 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002806 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002807 /* fall through */
2808 case check_state_compute_result:
2809 sh->check_state = check_state_idle;
2810 if (!dev)
2811 dev = &sh->dev[sh->pd_idx];
2812
2813 /* check that a write has not made the stripe insync */
2814 if (test_bit(STRIPE_INSYNC, &sh->state))
2815 break;
2816
2817 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002818 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2819 BUG_ON(s->uptodate != disks);
2820
2821 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002822 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002823 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002824
Dan Williamsa4456852007-07-09 11:56:43 -07002825 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002826 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002827 break;
2828 case check_state_run:
2829 break; /* we will be called again upon completion */
2830 case check_state_check_result:
2831 sh->check_state = check_state_idle;
2832
2833 /* if a failure occurred during the check operation, leave
2834 * STRIPE_INSYNC not set and let the stripe be handled again
2835 */
2836 if (s->failed)
2837 break;
2838
2839 /* handle a successful check operation, if parity is correct
2840 * we are done. Otherwise update the mismatch count and repair
2841 * parity if !MD_RECOVERY_CHECK
2842 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002843 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002844 /* parity is correct (on disc,
2845 * not in buffer any more)
2846 */
2847 set_bit(STRIPE_INSYNC, &sh->state);
2848 else {
2849 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2850 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2851 /* don't try to repair!! */
2852 set_bit(STRIPE_INSYNC, &sh->state);
2853 else {
2854 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002855 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002856 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2857 set_bit(R5_Wantcompute,
2858 &sh->dev[sh->pd_idx].flags);
2859 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002860 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10002861 s->uptodate++;
2862 }
2863 }
2864 break;
2865 case check_state_compute_run:
2866 break;
2867 default:
2868 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2869 __func__, sh->check_state,
2870 (unsigned long long) sh->sector);
2871 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002872 }
2873}
2874
2875
NeilBrownd1688a62011-10-11 16:49:52 +11002876static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07002877 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10002878 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002879{
Dan Williamsa4456852007-07-09 11:56:43 -07002880 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11002881 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07002882 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07002883
2884 set_bit(STRIPE_HANDLE, &sh->state);
2885
2886 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002887
Dan Williamsa4456852007-07-09 11:56:43 -07002888 /* Want to check and possibly repair P and Q.
2889 * However there could be one 'failed' device, in which
2890 * case we can only check one of them, possibly using the
2891 * other to generate missing data
2892 */
2893
Dan Williamsd82dfee2009-07-14 13:40:57 -07002894 switch (sh->check_state) {
2895 case check_state_idle:
2896 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10002897 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002898 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07002899 * makes sense to check P (If anything else were failed,
2900 * we would have used P to recreate it).
2901 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002902 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07002903 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002904 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002905 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07002906 * anything, so it makes sense to check it
2907 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002908 if (sh->check_state == check_state_run)
2909 sh->check_state = check_state_run_pq;
2910 else
2911 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07002912 }
Dan Williams36d1c642009-07-14 11:48:22 -07002913
Dan Williamsd82dfee2009-07-14 13:40:57 -07002914 /* discard potentially stale zero_sum_result */
2915 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07002916
Dan Williamsd82dfee2009-07-14 13:40:57 -07002917 if (sh->check_state == check_state_run) {
2918 /* async_xor_zero_sum destroys the contents of P */
2919 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
2920 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07002921 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002922 if (sh->check_state >= check_state_run &&
2923 sh->check_state <= check_state_run_pq) {
2924 /* async_syndrome_zero_sum preserves P and Q, so
2925 * no need to mark them !uptodate here
2926 */
2927 set_bit(STRIPE_OP_CHECK, &s->ops_request);
2928 break;
2929 }
Dan Williams36d1c642009-07-14 11:48:22 -07002930
Dan Williamsd82dfee2009-07-14 13:40:57 -07002931 /* we have 2-disk failure */
2932 BUG_ON(s->failed != 2);
2933 /* fall through */
2934 case check_state_compute_result:
2935 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07002936
Dan Williamsd82dfee2009-07-14 13:40:57 -07002937 /* check that a write has not made the stripe insync */
2938 if (test_bit(STRIPE_INSYNC, &sh->state))
2939 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002940
2941 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07002942 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07002943 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002944 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07002945 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10002946 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07002947 s->locked++;
2948 set_bit(R5_LOCKED, &dev->flags);
2949 set_bit(R5_Wantwrite, &dev->flags);
2950 }
2951 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10002952 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07002953 s->locked++;
2954 set_bit(R5_LOCKED, &dev->flags);
2955 set_bit(R5_Wantwrite, &dev->flags);
2956 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002957 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002958 dev = &sh->dev[pd_idx];
2959 s->locked++;
2960 set_bit(R5_LOCKED, &dev->flags);
2961 set_bit(R5_Wantwrite, &dev->flags);
2962 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002963 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002964 dev = &sh->dev[qd_idx];
2965 s->locked++;
2966 set_bit(R5_LOCKED, &dev->flags);
2967 set_bit(R5_Wantwrite, &dev->flags);
2968 }
2969 clear_bit(STRIPE_DEGRADED, &sh->state);
2970
2971 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002972 break;
2973 case check_state_run:
2974 case check_state_run_q:
2975 case check_state_run_pq:
2976 break; /* we will be called again upon completion */
2977 case check_state_check_result:
2978 sh->check_state = check_state_idle;
2979
2980 /* handle a successful check operation, if parity is correct
2981 * we are done. Otherwise update the mismatch count and repair
2982 * parity if !MD_RECOVERY_CHECK
2983 */
2984 if (sh->ops.zero_sum_result == 0) {
2985 /* both parities are correct */
2986 if (!s->failed)
2987 set_bit(STRIPE_INSYNC, &sh->state);
2988 else {
2989 /* in contrast to the raid5 case we can validate
2990 * parity, but still have a failure to write
2991 * back
2992 */
2993 sh->check_state = check_state_compute_result;
2994 /* Returning at this point means that we may go
2995 * off and bring p and/or q uptodate again so
2996 * we make sure to check zero_sum_result again
2997 * to verify if p or q need writeback
2998 */
2999 }
3000 } else {
3001 conf->mddev->resync_mismatches += STRIPE_SECTORS;
3002 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3003 /* don't try to repair!! */
3004 set_bit(STRIPE_INSYNC, &sh->state);
3005 else {
3006 int *target = &sh->ops.target;
3007
3008 sh->ops.target = -1;
3009 sh->ops.target2 = -1;
3010 sh->check_state = check_state_compute_run;
3011 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3012 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3013 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3014 set_bit(R5_Wantcompute,
3015 &sh->dev[pd_idx].flags);
3016 *target = pd_idx;
3017 target = &sh->ops.target2;
3018 s->uptodate++;
3019 }
3020 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3021 set_bit(R5_Wantcompute,
3022 &sh->dev[qd_idx].flags);
3023 *target = qd_idx;
3024 s->uptodate++;
3025 }
3026 }
3027 }
3028 break;
3029 case check_state_compute_run:
3030 break;
3031 default:
3032 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3033 __func__, sh->check_state,
3034 (unsigned long long) sh->sector);
3035 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003036 }
3037}
3038
NeilBrownd1688a62011-10-11 16:49:52 +11003039static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003040{
3041 int i;
3042
3043 /* We have read all the blocks in this stripe and now we need to
3044 * copy some of them into a target stripe for expand.
3045 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003046 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003047 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3048 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003049 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003050 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003051 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003052 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003053
NeilBrown784052e2009-03-31 15:19:07 +11003054 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003055 sector_t s = raid5_compute_sector(conf, bn, 0,
3056 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003057 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003058 if (sh2 == NULL)
3059 /* so far only the early blocks of this stripe
3060 * have been requested. When later blocks
3061 * get requested, we will try again
3062 */
3063 continue;
3064 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3065 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3066 /* must have already done this block */
3067 release_stripe(sh2);
3068 continue;
3069 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003070
3071 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003072 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003073 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003074 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003075 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003076
Dan Williamsa4456852007-07-09 11:56:43 -07003077 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3078 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3079 for (j = 0; j < conf->raid_disks; j++)
3080 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003081 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003082 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3083 break;
3084 if (j == conf->raid_disks) {
3085 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3086 set_bit(STRIPE_HANDLE, &sh2->state);
3087 }
3088 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003089
Dan Williamsa4456852007-07-09 11:56:43 -07003090 }
NeilBrowna2e08552007-09-11 15:23:36 -07003091 /* done submitting copies, wait for them to complete */
3092 if (tx) {
3093 async_tx_ack(tx);
3094 dma_wait_for_async_tx(tx);
3095 }
Dan Williamsa4456852007-07-09 11:56:43 -07003096}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097
3098/*
3099 * handle_stripe - do things to a stripe.
3100 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003101 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3102 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003104 * return some read requests which now have data
3105 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 * schedule a read on some buffers
3107 * schedule a write of some buffers
3108 * return confirmation of parity correctness
3109 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 */
Dan Williamsa4456852007-07-09 11:56:43 -07003111
NeilBrownacfe7262011-07-27 11:00:36 +10003112static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003113{
NeilBrownd1688a62011-10-11 16:49:52 +11003114 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003115 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003116 struct r5dev *dev;
3117 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003118 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003119
NeilBrownacfe7262011-07-27 11:00:36 +10003120 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003121
NeilBrownacfe7262011-07-27 11:00:36 +10003122 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3123 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3124 s->failed_num[0] = -1;
3125 s->failed_num[1] = -1;
3126
3127 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003128 rcu_read_lock();
NeilBrownc4c16632011-07-26 11:34:20 +10003129 spin_lock_irq(&conf->device_lock);
NeilBrown16a53ec2006-06-26 00:27:38 -07003130 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003131 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003132 sector_t first_bad;
3133 int bad_sectors;
3134 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003135
NeilBrown16a53ec2006-06-26 00:27:38 -07003136 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003137
Dan Williams45b42332007-07-09 11:56:43 -07003138 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003139 i, dev->flags,
3140 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003141 /* maybe we can reply to a read
3142 *
3143 * new wantfill requests are only permitted while
3144 * ops_complete_biofill is guaranteed to be inactive
3145 */
3146 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3147 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3148 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003149
3150 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003151 if (test_bit(R5_LOCKED, &dev->flags))
3152 s->locked++;
3153 if (test_bit(R5_UPTODATE, &dev->flags))
3154 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003155 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003156 s->compute++;
3157 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003158 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003159
NeilBrownacfe7262011-07-27 11:00:36 +10003160 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003161 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003162 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003163 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003164 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003165 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003166 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003167 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003168 }
Dan Williamsa4456852007-07-09 11:56:43 -07003169 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003170 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003171 /* Prefer to use the replacement for reads, but only
3172 * if it is recovered enough and has no bad blocks.
3173 */
3174 rdev = rcu_dereference(conf->disks[i].replacement);
3175 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3176 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3177 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3178 &first_bad, &bad_sectors))
3179 set_bit(R5_ReadRepl, &dev->flags);
3180 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003181 if (rdev)
3182 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003183 rdev = rcu_dereference(conf->disks[i].rdev);
3184 clear_bit(R5_ReadRepl, &dev->flags);
3185 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003186 if (rdev && test_bit(Faulty, &rdev->flags))
3187 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003188 if (rdev) {
3189 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3190 &first_bad, &bad_sectors);
3191 if (s->blocked_rdev == NULL
3192 && (test_bit(Blocked, &rdev->flags)
3193 || is_bad < 0)) {
3194 if (is_bad < 0)
3195 set_bit(BlockedBadBlocks,
3196 &rdev->flags);
3197 s->blocked_rdev = rdev;
3198 atomic_inc(&rdev->nr_pending);
3199 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003200 }
NeilBrown415e72d2010-06-17 17:25:21 +10003201 clear_bit(R5_Insync, &dev->flags);
3202 if (!rdev)
3203 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003204 else if (is_bad) {
3205 /* also not in-sync */
3206 if (!test_bit(WriteErrorSeen, &rdev->flags)) {
3207 /* treat as in-sync, but with a read error
3208 * which we can now try to correct
3209 */
3210 set_bit(R5_Insync, &dev->flags);
3211 set_bit(R5_ReadError, &dev->flags);
3212 }
3213 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003214 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003215 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003216 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003217 set_bit(R5_Insync, &dev->flags);
3218 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3219 test_bit(R5_Expanded, &dev->flags))
3220 /* If we've reshaped into here, we assume it is Insync.
3221 * We will shortly update recovery_offset to make
3222 * it official.
3223 */
3224 set_bit(R5_Insync, &dev->flags);
3225
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003226 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003227 /* This flag does not apply to '.replacement'
3228 * only to .rdev, so make sure to check that*/
3229 struct md_rdev *rdev2 = rcu_dereference(
3230 conf->disks[i].rdev);
3231 if (rdev2 == rdev)
3232 clear_bit(R5_Insync, &dev->flags);
3233 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003234 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003235 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003236 } else
3237 clear_bit(R5_WriteError, &dev->flags);
3238 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003239 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003240 /* This flag does not apply to '.replacement'
3241 * only to .rdev, so make sure to check that*/
3242 struct md_rdev *rdev2 = rcu_dereference(
3243 conf->disks[i].rdev);
3244 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003245 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003246 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003247 } else
3248 clear_bit(R5_MadeGood, &dev->flags);
3249 }
NeilBrown977df362011-12-23 10:17:53 +11003250 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3251 struct md_rdev *rdev2 = rcu_dereference(
3252 conf->disks[i].replacement);
3253 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3254 s->handle_bad_blocks = 1;
3255 atomic_inc(&rdev2->nr_pending);
3256 } else
3257 clear_bit(R5_MadeGoodRepl, &dev->flags);
3258 }
NeilBrown415e72d2010-06-17 17:25:21 +10003259 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003260 /* The ReadError flag will just be confusing now */
3261 clear_bit(R5_ReadError, &dev->flags);
3262 clear_bit(R5_ReWrite, &dev->flags);
3263 }
NeilBrown415e72d2010-06-17 17:25:21 +10003264 if (test_bit(R5_ReadError, &dev->flags))
3265 clear_bit(R5_Insync, &dev->flags);
3266 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003267 if (s->failed < 2)
3268 s->failed_num[s->failed] = i;
3269 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003270 if (rdev && !test_bit(Faulty, &rdev->flags))
3271 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003272 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003273 }
NeilBrownc4c16632011-07-26 11:34:20 +10003274 spin_unlock_irq(&conf->device_lock);
NeilBrown9a3e1102011-12-23 10:17:53 +11003275 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3276 /* If there is a failed device being replaced,
3277 * we must be recovering.
3278 * else if we are after recovery_cp, we must be syncing
3279 * else we can only be replacing
3280 * sync and recovery both need to read all devices, and so
3281 * use the same flag.
3282 */
3283 if (do_recovery ||
3284 sh->sector >= conf->mddev->recovery_cp)
3285 s->syncing = 1;
3286 else
3287 s->replacing = 1;
3288 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003289 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003290}
NeilBrownf4168852007-02-28 20:11:53 -08003291
NeilBrowncc940152011-07-26 11:35:35 +10003292static void handle_stripe(struct stripe_head *sh)
3293{
3294 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003295 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003296 int i;
NeilBrown84789552011-07-27 11:00:36 +10003297 int prexor;
3298 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003299 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003300
3301 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003302 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003303 /* already being handled, ensure it gets handled
3304 * again when current action finishes */
3305 set_bit(STRIPE_HANDLE, &sh->state);
3306 return;
3307 }
3308
3309 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3310 set_bit(STRIPE_SYNCING, &sh->state);
3311 clear_bit(STRIPE_INSYNC, &sh->state);
3312 }
3313 clear_bit(STRIPE_DELAYED, &sh->state);
3314
3315 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3316 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3317 (unsigned long long)sh->sector, sh->state,
3318 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3319 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003320
NeilBrownacfe7262011-07-27 11:00:36 +10003321 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003322
NeilBrownbc2607f2011-07-28 11:39:22 +10003323 if (s.handle_bad_blocks) {
3324 set_bit(STRIPE_HANDLE, &sh->state);
3325 goto finish;
3326 }
3327
NeilBrown474af965fe2011-07-27 11:00:36 +10003328 if (unlikely(s.blocked_rdev)) {
3329 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003330 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003331 set_bit(STRIPE_HANDLE, &sh->state);
3332 goto finish;
3333 }
3334 /* There is nothing for the blocked_rdev to block */
3335 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3336 s.blocked_rdev = NULL;
3337 }
3338
3339 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3340 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3341 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3342 }
3343
3344 pr_debug("locked=%d uptodate=%d to_read=%d"
3345 " to_write=%d failed=%d failed_num=%d,%d\n",
3346 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3347 s.failed_num[0], s.failed_num[1]);
3348 /* check if the array has lost more than max_degraded devices and,
3349 * if so, some requests might need to be failed.
3350 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003351 if (s.failed > conf->max_degraded) {
3352 sh->check_state = 0;
3353 sh->reconstruct_state = 0;
3354 if (s.to_read+s.to_write+s.written)
3355 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003356 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003357 handle_failed_sync(conf, sh, &s);
3358 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003359
3360 /*
3361 * might be able to return some write requests if the parity blocks
3362 * are safe, or on a failed drive
3363 */
3364 pdev = &sh->dev[sh->pd_idx];
3365 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3366 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3367 qdev = &sh->dev[sh->qd_idx];
3368 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3369 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3370 || conf->level < 6;
3371
3372 if (s.written &&
3373 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3374 && !test_bit(R5_LOCKED, &pdev->flags)
3375 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3376 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3377 && !test_bit(R5_LOCKED, &qdev->flags)
3378 && test_bit(R5_UPTODATE, &qdev->flags)))))
3379 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3380
3381 /* Now we might consider reading some blocks, either to check/generate
3382 * parity, or to satisfy requests
3383 * or to load a block that is being partially written.
3384 */
3385 if (s.to_read || s.non_overwrite
3386 || (conf->level == 6 && s.to_write && s.failed)
NeilBrown9a3e1102011-12-23 10:17:53 +11003387 || (s.syncing && (s.uptodate + s.compute < disks))
3388 || s.replacing
3389 || s.expanding)
NeilBrown474af965fe2011-07-27 11:00:36 +10003390 handle_stripe_fill(sh, &s, disks);
3391
NeilBrown84789552011-07-27 11:00:36 +10003392 /* Now we check to see if any write operations have recently
3393 * completed
3394 */
3395 prexor = 0;
3396 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3397 prexor = 1;
3398 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3399 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3400 sh->reconstruct_state = reconstruct_state_idle;
3401
3402 /* All the 'written' buffers and the parity block are ready to
3403 * be written back to disk
3404 */
3405 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3406 BUG_ON(sh->qd_idx >= 0 &&
3407 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags));
3408 for (i = disks; i--; ) {
3409 struct r5dev *dev = &sh->dev[i];
3410 if (test_bit(R5_LOCKED, &dev->flags) &&
3411 (i == sh->pd_idx || i == sh->qd_idx ||
3412 dev->written)) {
3413 pr_debug("Writing block %d\n", i);
3414 set_bit(R5_Wantwrite, &dev->flags);
3415 if (prexor)
3416 continue;
3417 if (!test_bit(R5_Insync, &dev->flags) ||
3418 ((i == sh->pd_idx || i == sh->qd_idx) &&
3419 s.failed == 0))
3420 set_bit(STRIPE_INSYNC, &sh->state);
3421 }
3422 }
3423 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3424 s.dec_preread_active = 1;
3425 }
3426
3427 /* Now to consider new write requests and what else, if anything
3428 * should be read. We do not handle new writes when:
3429 * 1/ A 'write' operation (copy+xor) is already in flight.
3430 * 2/ A 'check' operation is in flight, as it may clobber the parity
3431 * block.
3432 */
3433 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3434 handle_stripe_dirtying(conf, sh, &s, disks);
3435
3436 /* maybe we need to check and possibly fix the parity for this stripe
3437 * Any reads will already have been scheduled, so we just see if enough
3438 * data is available. The parity check is held off while parity
3439 * dependent operations are in flight.
3440 */
3441 if (sh->check_state ||
3442 (s.syncing && s.locked == 0 &&
3443 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3444 !test_bit(STRIPE_INSYNC, &sh->state))) {
3445 if (conf->level == 6)
3446 handle_parity_checks6(conf, sh, &s, disks);
3447 else
3448 handle_parity_checks5(conf, sh, &s, disks);
3449 }
NeilBrownc5a31002011-07-27 11:00:36 +10003450
NeilBrown9a3e1102011-12-23 10:17:53 +11003451 if (s.replacing && s.locked == 0
3452 && !test_bit(STRIPE_INSYNC, &sh->state)) {
3453 /* Write out to replacement devices where possible */
3454 for (i = 0; i < conf->raid_disks; i++)
3455 if (test_bit(R5_UPTODATE, &sh->dev[i].flags) &&
3456 test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3457 set_bit(R5_WantReplace, &sh->dev[i].flags);
3458 set_bit(R5_LOCKED, &sh->dev[i].flags);
3459 s.locked++;
3460 }
3461 set_bit(STRIPE_INSYNC, &sh->state);
3462 }
3463 if ((s.syncing || s.replacing) && s.locked == 0 &&
3464 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003465 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3466 clear_bit(STRIPE_SYNCING, &sh->state);
3467 }
3468
3469 /* If the failed drives are just a ReadError, then we might need
3470 * to progress the repair/check process
3471 */
3472 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3473 for (i = 0; i < s.failed; i++) {
3474 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3475 if (test_bit(R5_ReadError, &dev->flags)
3476 && !test_bit(R5_LOCKED, &dev->flags)
3477 && test_bit(R5_UPTODATE, &dev->flags)
3478 ) {
3479 if (!test_bit(R5_ReWrite, &dev->flags)) {
3480 set_bit(R5_Wantwrite, &dev->flags);
3481 set_bit(R5_ReWrite, &dev->flags);
3482 set_bit(R5_LOCKED, &dev->flags);
3483 s.locked++;
3484 } else {
3485 /* let's read it back */
3486 set_bit(R5_Wantread, &dev->flags);
3487 set_bit(R5_LOCKED, &dev->flags);
3488 s.locked++;
3489 }
3490 }
3491 }
3492
3493
NeilBrown3687c062011-07-27 11:00:36 +10003494 /* Finish reconstruct operations initiated by the expansion process */
3495 if (sh->reconstruct_state == reconstruct_state_result) {
3496 struct stripe_head *sh_src
3497 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3498 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3499 /* sh cannot be written until sh_src has been read.
3500 * so arrange for sh to be delayed a little
3501 */
3502 set_bit(STRIPE_DELAYED, &sh->state);
3503 set_bit(STRIPE_HANDLE, &sh->state);
3504 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3505 &sh_src->state))
3506 atomic_inc(&conf->preread_active_stripes);
3507 release_stripe(sh_src);
3508 goto finish;
3509 }
3510 if (sh_src)
3511 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003512
NeilBrown3687c062011-07-27 11:00:36 +10003513 sh->reconstruct_state = reconstruct_state_idle;
3514 clear_bit(STRIPE_EXPANDING, &sh->state);
3515 for (i = conf->raid_disks; i--; ) {
3516 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3517 set_bit(R5_LOCKED, &sh->dev[i].flags);
3518 s.locked++;
3519 }
3520 }
3521
3522 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3523 !sh->reconstruct_state) {
3524 /* Need to write out all blocks after computing parity */
3525 sh->disks = conf->raid_disks;
3526 stripe_set_idx(sh->sector, conf, 0, sh);
3527 schedule_reconstruction(sh, &s, 1, 1);
3528 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3529 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3530 atomic_dec(&conf->reshape_stripes);
3531 wake_up(&conf->wait_for_overlap);
3532 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3533 }
3534
3535 if (s.expanding && s.locked == 0 &&
3536 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3537 handle_stripe_expansion(conf, sh);
3538
3539finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003540 /* wait for this device to become unblocked */
NeilBrown43220aa2011-08-31 12:49:14 +10003541 if (conf->mddev->external && unlikely(s.blocked_rdev))
NeilBrownc5709ef2011-07-26 11:35:20 +10003542 md_wait_for_blocked_rdev(s.blocked_rdev, conf->mddev);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003543
NeilBrownbc2607f2011-07-28 11:39:22 +10003544 if (s.handle_bad_blocks)
3545 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003546 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003547 struct r5dev *dev = &sh->dev[i];
3548 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3549 /* We own a safe reference to the rdev */
3550 rdev = conf->disks[i].rdev;
3551 if (!rdev_set_badblocks(rdev, sh->sector,
3552 STRIPE_SECTORS, 0))
3553 md_error(conf->mddev, rdev);
3554 rdev_dec_pending(rdev, conf->mddev);
3555 }
NeilBrownb84db562011-07-28 11:39:23 +10003556 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3557 rdev = conf->disks[i].rdev;
3558 rdev_clear_badblocks(rdev, sh->sector,
3559 STRIPE_SECTORS);
3560 rdev_dec_pending(rdev, conf->mddev);
3561 }
NeilBrown977df362011-12-23 10:17:53 +11003562 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3563 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11003564 if (!rdev)
3565 /* rdev have been moved down */
3566 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11003567 rdev_clear_badblocks(rdev, sh->sector,
3568 STRIPE_SECTORS);
3569 rdev_dec_pending(rdev, conf->mddev);
3570 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003571 }
3572
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003573 if (s.ops_request)
3574 raid_run_ops(sh, s.ops_request);
3575
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003576 ops_run_io(sh, &s);
3577
NeilBrownc5709ef2011-07-26 11:35:20 +10003578 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003579 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003580 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003581 * have actually been submitted.
3582 */
3583 atomic_dec(&conf->preread_active_stripes);
3584 if (atomic_read(&conf->preread_active_stripes) <
3585 IO_THRESHOLD)
3586 md_wakeup_thread(conf->mddev->thread);
3587 }
3588
NeilBrownc5709ef2011-07-26 11:35:20 +10003589 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003590
Dan Williams257a4b42011-11-08 16:22:06 +11003591 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003592}
3593
NeilBrownd1688a62011-10-11 16:49:52 +11003594static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595{
3596 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3597 while (!list_empty(&conf->delayed_list)) {
3598 struct list_head *l = conf->delayed_list.next;
3599 struct stripe_head *sh;
3600 sh = list_entry(l, struct stripe_head, lru);
3601 list_del_init(l);
3602 clear_bit(STRIPE_DELAYED, &sh->state);
3603 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3604 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003605 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 }
NeilBrown482c0832011-04-18 18:25:42 +10003607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608}
3609
NeilBrownd1688a62011-10-11 16:49:52 +11003610static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003611{
3612 /* device_lock is held */
3613 struct list_head head;
3614 list_add(&head, &conf->bitmap_list);
3615 list_del_init(&conf->bitmap_list);
3616 while (!list_empty(&head)) {
3617 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3618 list_del_init(&sh->lru);
3619 atomic_inc(&sh->count);
3620 __release_stripe(conf, sh);
3621 }
3622}
3623
NeilBrownfd01b882011-10-11 16:47:53 +11003624int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003625{
NeilBrownd1688a62011-10-11 16:49:52 +11003626 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003627
3628 /* No difference between reads and writes. Just check
3629 * how busy the stripe_cache is
3630 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003631
NeilBrownf022b2f2006-10-03 01:15:56 -07003632 if (conf->inactive_blocked)
3633 return 1;
3634 if (conf->quiesce)
3635 return 1;
3636 if (list_empty_careful(&conf->inactive_list))
3637 return 1;
3638
3639 return 0;
3640}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003641EXPORT_SYMBOL_GPL(md_raid5_congested);
3642
3643static int raid5_congested(void *data, int bits)
3644{
NeilBrownfd01b882011-10-11 16:47:53 +11003645 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003646
3647 return mddev_congested(mddev, bits) ||
3648 md_raid5_congested(mddev, bits);
3649}
NeilBrownf022b2f2006-10-03 01:15:56 -07003650
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003651/* We want read requests to align with chunks where possible,
3652 * but write requests don't need to.
3653 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003654static int raid5_mergeable_bvec(struct request_queue *q,
3655 struct bvec_merge_data *bvm,
3656 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003657{
NeilBrownfd01b882011-10-11 16:47:53 +11003658 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003659 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003660 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003661 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003662 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003663
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003664 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003665 return biovec->bv_len; /* always allow writes to be mergeable */
3666
Andre Noll664e7c42009-06-18 08:45:27 +10003667 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3668 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003669 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3670 if (max < 0) max = 0;
3671 if (max <= biovec->bv_len && bio_sectors == 0)
3672 return biovec->bv_len;
3673 else
3674 return max;
3675}
3676
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003677
NeilBrownfd01b882011-10-11 16:47:53 +11003678static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003679{
3680 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003681 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003682 unsigned int bio_sectors = bio->bi_size >> 9;
3683
Andre Noll664e7c42009-06-18 08:45:27 +10003684 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3685 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003686 return chunk_sectors >=
3687 ((sector & (chunk_sectors - 1)) + bio_sectors);
3688}
3689
3690/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003691 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3692 * later sampled by raid5d.
3693 */
NeilBrownd1688a62011-10-11 16:49:52 +11003694static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003695{
3696 unsigned long flags;
3697
3698 spin_lock_irqsave(&conf->device_lock, flags);
3699
3700 bi->bi_next = conf->retry_read_aligned_list;
3701 conf->retry_read_aligned_list = bi;
3702
3703 spin_unlock_irqrestore(&conf->device_lock, flags);
3704 md_wakeup_thread(conf->mddev->thread);
3705}
3706
3707
NeilBrownd1688a62011-10-11 16:49:52 +11003708static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003709{
3710 struct bio *bi;
3711
3712 bi = conf->retry_read_aligned;
3713 if (bi) {
3714 conf->retry_read_aligned = NULL;
3715 return bi;
3716 }
3717 bi = conf->retry_read_aligned_list;
3718 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003719 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003720 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003721 /*
3722 * this sets the active strip count to 1 and the processed
3723 * strip count to zero (upper 8 bits)
3724 */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003725 bi->bi_phys_segments = 1; /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003726 }
3727
3728 return bi;
3729}
3730
3731
3732/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003733 * The "raid5_align_endio" should check if the read succeeded and if it
3734 * did, call bio_endio on the original bio (having bio_put the new bio
3735 * first).
3736 * If the read failed..
3737 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003738static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003739{
3740 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003741 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003742 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003743 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003744 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003745
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003746 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003747
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003748 rdev = (void*)raid_bi->bi_next;
3749 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003750 mddev = rdev->mddev;
3751 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003752
3753 rdev_dec_pending(rdev, conf->mddev);
3754
3755 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003756 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003757 if (atomic_dec_and_test(&conf->active_aligned_reads))
3758 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003759 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003760 }
3761
3762
Dan Williams45b42332007-07-09 11:56:43 -07003763 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003764
3765 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003766}
3767
Neil Brown387bb172007-02-08 14:20:29 -08003768static int bio_fits_rdev(struct bio *bi)
3769{
Jens Axboe165125e2007-07-24 09:28:11 +02003770 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003771
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003772 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003773 return 0;
3774 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003775 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003776 return 0;
3777
3778 if (q->merge_bvec_fn)
3779 /* it's too hard to apply the merge_bvec_fn at this stage,
3780 * just just give up
3781 */
3782 return 0;
3783
3784 return 1;
3785}
3786
3787
NeilBrownfd01b882011-10-11 16:47:53 +11003788static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003789{
NeilBrownd1688a62011-10-11 16:49:52 +11003790 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11003791 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003792 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11003793 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11003794 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003795
3796 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003797 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003798 return 0;
3799 }
3800 /*
NeilBrowna167f662010-10-26 18:31:13 +11003801 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003802 */
NeilBrowna167f662010-10-26 18:31:13 +11003803 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003804 if (!align_bi)
3805 return 0;
3806 /*
3807 * set bi_end_io to a new function, and set bi_private to the
3808 * original bio.
3809 */
3810 align_bi->bi_end_io = raid5_align_endio;
3811 align_bi->bi_private = raid_bio;
3812 /*
3813 * compute position
3814 */
NeilBrown112bf892009-03-31 14:39:38 +11003815 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3816 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003817 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003818
NeilBrown671488c2011-12-23 10:17:52 +11003819 end_sector = align_bi->bi_sector + (align_bi->bi_size >> 9);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003820 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11003821 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
3822 if (!rdev || test_bit(Faulty, &rdev->flags) ||
3823 rdev->recovery_offset < end_sector) {
3824 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3825 if (rdev &&
3826 (test_bit(Faulty, &rdev->flags) ||
3827 !(test_bit(In_sync, &rdev->flags) ||
3828 rdev->recovery_offset >= end_sector)))
3829 rdev = NULL;
3830 }
3831 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10003832 sector_t first_bad;
3833 int bad_sectors;
3834
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003835 atomic_inc(&rdev->nr_pending);
3836 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003837 raid_bio->bi_next = (void*)rdev;
3838 align_bi->bi_bdev = rdev->bdev;
3839 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
3840 align_bi->bi_sector += rdev->data_offset;
3841
NeilBrown31c176e2011-07-28 11:39:22 +10003842 if (!bio_fits_rdev(align_bi) ||
3843 is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
3844 &first_bad, &bad_sectors)) {
3845 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08003846 bio_put(align_bi);
3847 rdev_dec_pending(rdev, mddev);
3848 return 0;
3849 }
3850
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003851 spin_lock_irq(&conf->device_lock);
3852 wait_event_lock_irq(conf->wait_for_stripe,
3853 conf->quiesce == 0,
3854 conf->device_lock, /* nothing */);
3855 atomic_inc(&conf->active_aligned_reads);
3856 spin_unlock_irq(&conf->device_lock);
3857
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003858 generic_make_request(align_bi);
3859 return 1;
3860 } else {
3861 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003862 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003863 return 0;
3864 }
3865}
3866
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003867/* __get_priority_stripe - get the next stripe to process
3868 *
3869 * Full stripe writes are allowed to pass preread active stripes up until
3870 * the bypass_threshold is exceeded. In general the bypass_count
3871 * increments when the handle_list is handled before the hold_list; however, it
3872 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3873 * stripe with in flight i/o. The bypass_count will be reset when the
3874 * head of the hold_list has changed, i.e. the head was promoted to the
3875 * handle_list.
3876 */
NeilBrownd1688a62011-10-11 16:49:52 +11003877static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003878{
3879 struct stripe_head *sh;
3880
3881 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3882 __func__,
3883 list_empty(&conf->handle_list) ? "empty" : "busy",
3884 list_empty(&conf->hold_list) ? "empty" : "busy",
3885 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3886
3887 if (!list_empty(&conf->handle_list)) {
3888 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3889
3890 if (list_empty(&conf->hold_list))
3891 conf->bypass_count = 0;
3892 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3893 if (conf->hold_list.next == conf->last_hold)
3894 conf->bypass_count++;
3895 else {
3896 conf->last_hold = conf->hold_list.next;
3897 conf->bypass_count -= conf->bypass_threshold;
3898 if (conf->bypass_count < 0)
3899 conf->bypass_count = 0;
3900 }
3901 }
3902 } else if (!list_empty(&conf->hold_list) &&
3903 ((conf->bypass_threshold &&
3904 conf->bypass_count > conf->bypass_threshold) ||
3905 atomic_read(&conf->pending_full_writes) == 0)) {
3906 sh = list_entry(conf->hold_list.next,
3907 typeof(*sh), lru);
3908 conf->bypass_count -= conf->bypass_threshold;
3909 if (conf->bypass_count < 0)
3910 conf->bypass_count = 0;
3911 } else
3912 return NULL;
3913
3914 list_del_init(&sh->lru);
3915 atomic_inc(&sh->count);
3916 BUG_ON(atomic_read(&sh->count) != 1);
3917 return sh;
3918}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003919
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07003920static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921{
NeilBrownd1688a62011-10-11 16:49:52 +11003922 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11003923 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 sector_t new_sector;
3925 sector_t logical_sector, last_sector;
3926 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01003927 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11003928 int remaining;
NeilBrown7c13edc2011-04-18 18:25:43 +10003929 int plugged;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930
Tejun Heoe9c74692010-09-03 11:56:18 +02003931 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
3932 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02003933 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07003934 }
3935
NeilBrown3d310eb2005-06-21 17:17:26 -07003936 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07003937
NeilBrown802ba062006-12-13 00:34:13 -08003938 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003939 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11003940 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02003941 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003942
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
3944 last_sector = bi->bi_sector + (bi->bi_size>>9);
3945 bi->bi_next = NULL;
3946 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07003947
NeilBrown7c13edc2011-04-18 18:25:43 +10003948 plugged = mddev_check_plugged(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
3950 DEFINE_WAIT(w);
NeilBrown16a53ec2006-06-26 00:27:38 -07003951 int disks, data_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003952 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08003953
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003954 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11003955 previous = 0;
NeilBrownb0f9ec02009-03-31 15:27:18 +11003956 disks = conf->raid_disks;
NeilBrownb578d552006-03-27 01:18:12 -08003957 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11003958 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11003959 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08003960 * 64bit on a 32bit platform, and so it might be
3961 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02003962 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08003963 * the lock is dropped, so once we get a reference
3964 * to the stripe that we think it is, we will have
3965 * to check again.
3966 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003967 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11003968 if (mddev->delta_disks < 0
3969 ? logical_sector < conf->reshape_progress
3970 : logical_sector >= conf->reshape_progress) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003971 disks = conf->previous_raid_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003972 previous = 1;
3973 } else {
NeilBrownfef9c612009-03-31 15:16:46 +11003974 if (mddev->delta_disks < 0
3975 ? logical_sector < conf->reshape_safe
3976 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08003977 spin_unlock_irq(&conf->device_lock);
3978 schedule();
3979 goto retry;
3980 }
3981 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003982 spin_unlock_irq(&conf->device_lock);
3983 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003984 data_disks = disks - conf->max_degraded;
3985
NeilBrown112bf892009-03-31 14:39:38 +11003986 new_sector = raid5_compute_sector(conf, logical_sector,
3987 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003988 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10003989 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 (unsigned long long)new_sector,
3991 (unsigned long long)logical_sector);
3992
NeilBrownb5663ba2009-03-31 14:39:38 +11003993 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10003994 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11003996 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003997 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08003998 * stripe, so we must do the range check again.
3999 * Expansion could still move past after this
4000 * test, but as we are holding a reference to
4001 * 'sh', we know that if that happens,
4002 * STRIPE_EXPANDING will get set and the expansion
4003 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004004 */
4005 int must_retry = 0;
4006 spin_lock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004007 if (mddev->delta_disks < 0
4008 ? logical_sector >= conf->reshape_progress
4009 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004010 /* mismatch, need to try again */
4011 must_retry = 1;
4012 spin_unlock_irq(&conf->device_lock);
4013 if (must_retry) {
4014 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004015 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004016 goto retry;
4017 }
4018 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004019
Namhyung Kimffd96e32011-07-18 17:38:51 +10004020 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004021 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004022 logical_sector < mddev->suspend_hi) {
4023 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004024 /* As the suspend_* range is controlled by
4025 * userspace, we want an interruptible
4026 * wait.
4027 */
4028 flush_signals(current);
4029 prepare_to_wait(&conf->wait_for_overlap,
4030 &w, TASK_INTERRUPTIBLE);
4031 if (logical_sector >= mddev->suspend_lo &&
4032 logical_sector < mddev->suspend_hi)
4033 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004034 goto retry;
4035 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004036
4037 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004038 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004039 /* Stripe is busy expanding or
4040 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 * and wait a while
4042 */
NeilBrown482c0832011-04-18 18:25:42 +10004043 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 release_stripe(sh);
4045 schedule();
4046 goto retry;
4047 }
4048 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004049 set_bit(STRIPE_HANDLE, &sh->state);
4050 clear_bit(STRIPE_DELAYED, &sh->state);
Tejun Heoe9c74692010-09-03 11:56:18 +02004051 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004052 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4053 atomic_inc(&conf->preread_active_stripes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 } else {
4056 /* cannot get stripe for read-ahead, just give-up */
4057 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4058 finish_wait(&conf->wait_for_overlap, &w);
4059 break;
4060 }
4061
4062 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004063 if (!plugged)
4064 md_wakeup_thread(mddev->thread);
4065
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004067 remaining = raid5_dec_bi_phys_segments(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004068 spin_unlock_irq(&conf->device_lock);
4069 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070
NeilBrown16a53ec2006-06-26 00:27:38 -07004071 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004073
Neil Brown0e13fe232008-06-28 08:31:20 +10004074 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076}
4077
NeilBrownfd01b882011-10-11 16:47:53 +11004078static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004079
NeilBrownfd01b882011-10-11 16:47:53 +11004080static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081{
NeilBrown52c03292006-06-26 00:27:43 -07004082 /* reshaping is quite different to recovery/resync so it is
4083 * handled quite separately ... here.
4084 *
4085 * On each call to sync_request, we gather one chunk worth of
4086 * destination stripes and flag them as expanding.
4087 * Then we find all the source stripes and request reads.
4088 * As the reads complete, handle_stripe will copy the data
4089 * into the destination stripe and release that stripe.
4090 */
NeilBrownd1688a62011-10-11 16:49:52 +11004091 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004093 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004094 int raid_disks = conf->previous_raid_disks;
4095 int data_disks = raid_disks - conf->max_degraded;
4096 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004097 int i;
4098 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004099 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004100 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004101 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004102 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004103
NeilBrownfef9c612009-03-31 15:16:46 +11004104 if (sector_nr == 0) {
4105 /* If restarting in the middle, skip the initial sectors */
4106 if (mddev->delta_disks < 0 &&
4107 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4108 sector_nr = raid5_size(mddev, 0, 0)
4109 - conf->reshape_progress;
NeilBrowna6397552009-08-13 10:13:00 +10004110 } else if (mddev->delta_disks >= 0 &&
NeilBrownfef9c612009-03-31 15:16:46 +11004111 conf->reshape_progress > 0)
4112 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004113 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004114 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004115 mddev->curr_resync_completed = sector_nr;
4116 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004117 *skipped = 1;
4118 return sector_nr;
4119 }
NeilBrown52c03292006-06-26 00:27:43 -07004120 }
4121
NeilBrown7a661382009-03-31 15:21:40 +11004122 /* We need to process a full chunk at a time.
4123 * If old and new chunk sizes differ, we need to process the
4124 * largest of these
4125 */
Andre Noll664e7c42009-06-18 08:45:27 +10004126 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4127 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004128 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004129 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004130
NeilBrown52c03292006-06-26 00:27:43 -07004131 /* we update the metadata when there is more than 3Meg
4132 * in the block range (that is rather arbitrary, should
4133 * probably be time based) or when the data about to be
4134 * copied would over-write the source of the data at
4135 * the front of the range.
NeilBrownfef9c612009-03-31 15:16:46 +11004136 * i.e. one new_stripe along from reshape_progress new_maps
4137 * to after where reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004138 */
NeilBrownfef9c612009-03-31 15:16:46 +11004139 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004140 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004141 readpos = conf->reshape_progress;
4142 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004143 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004144 sector_div(safepos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004145 if (mddev->delta_disks < 0) {
NeilBrowned37d832009-05-27 21:39:05 +10004146 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004147 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004148 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004149 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004150 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004151 readpos -= min_t(sector_t, reshape_sectors, readpos);
4152 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004153 }
NeilBrown52c03292006-06-26 00:27:43 -07004154
NeilBrownc8f517c2009-03-31 15:28:40 +11004155 /* 'writepos' is the most advanced device address we might write.
4156 * 'readpos' is the least advanced device address we might read.
4157 * 'safepos' is the least address recorded in the metadata as having
4158 * been reshaped.
4159 * If 'readpos' is behind 'writepos', then there is no way that we can
4160 * ensure safety in the face of a crash - that must be done by userspace
4161 * making a backup of the data. So in that case there is no particular
4162 * rush to update metadata.
4163 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4164 * update the metadata to advance 'safepos' to match 'readpos' so that
4165 * we can be safe in the event of a crash.
4166 * So we insist on updating metadata if safepos is behind writepos and
4167 * readpos is beyond writepos.
4168 * In any case, update the metadata every 10 seconds.
4169 * Maybe that number should be configurable, but I'm not sure it is
4170 * worth it.... maybe it could be a multiple of safemode_delay???
4171 */
NeilBrownfef9c612009-03-31 15:16:46 +11004172 if ((mddev->delta_disks < 0
NeilBrownc8f517c2009-03-31 15:28:40 +11004173 ? (safepos > writepos && readpos < writepos)
4174 : (safepos < writepos && readpos > writepos)) ||
4175 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004176 /* Cannot proceed until we've updated the superblock... */
4177 wait_event(conf->wait_for_overlap,
4178 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004179 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004180 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004181 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07004182 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004183 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07004184 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004185 kthread_should_stop());
4186 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004187 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004188 spin_unlock_irq(&conf->device_lock);
4189 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004190 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004191 }
4192
NeilBrownec32a2b2009-03-31 15:17:38 +11004193 if (mddev->delta_disks < 0) {
4194 BUG_ON(conf->reshape_progress == 0);
4195 stripe_addr = writepos;
4196 BUG_ON((mddev->dev_sectors &
NeilBrown7a661382009-03-31 15:21:40 +11004197 ~((sector_t)reshape_sectors - 1))
4198 - reshape_sectors - stripe_addr
NeilBrownec32a2b2009-03-31 15:17:38 +11004199 != sector_nr);
4200 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004201 BUG_ON(writepos != sector_nr + reshape_sectors);
NeilBrownec32a2b2009-03-31 15:17:38 +11004202 stripe_addr = sector_nr;
4203 }
NeilBrownab69ae12009-03-31 15:26:47 +11004204 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004205 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004206 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004207 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004208 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004209 set_bit(STRIPE_EXPANDING, &sh->state);
4210 atomic_inc(&conf->reshape_stripes);
4211 /* If any of this stripe is beyond the end of the old
4212 * array, then we need to zero those blocks
4213 */
4214 for (j=sh->disks; j--;) {
4215 sector_t s;
4216 if (j == sh->pd_idx)
4217 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004218 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004219 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004220 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004221 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004222 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004223 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004224 continue;
4225 }
4226 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4227 set_bit(R5_Expanded, &sh->dev[j].flags);
4228 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4229 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004230 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004231 set_bit(STRIPE_EXPAND_READY, &sh->state);
4232 set_bit(STRIPE_HANDLE, &sh->state);
4233 }
NeilBrownab69ae12009-03-31 15:26:47 +11004234 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004235 }
4236 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004237 if (mddev->delta_disks < 0)
NeilBrown7a661382009-03-31 15:21:40 +11004238 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004239 else
NeilBrown7a661382009-03-31 15:21:40 +11004240 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004241 spin_unlock_irq(&conf->device_lock);
4242 /* Ok, those stripe are ready. We can start scheduling
4243 * reads on the source stripes.
4244 * The source stripes are determined by mapping the first and last
4245 * block on the destination stripes.
4246 */
NeilBrown52c03292006-06-26 00:27:43 -07004247 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004248 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004249 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004250 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004251 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004252 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004253 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004254 if (last_sector >= mddev->dev_sectors)
4255 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004256 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004257 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004258 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4259 set_bit(STRIPE_HANDLE, &sh->state);
4260 release_stripe(sh);
4261 first_sector += STRIPE_SECTORS;
4262 }
NeilBrownab69ae12009-03-31 15:26:47 +11004263 /* Now that the sources are clearly marked, we can release
4264 * the destination stripes
4265 */
4266 while (!list_empty(&stripes)) {
4267 sh = list_entry(stripes.next, struct stripe_head, lru);
4268 list_del_init(&sh->lru);
4269 release_stripe(sh);
4270 }
NeilBrownc6207272008-02-06 01:39:52 -08004271 /* If this takes us to the resync_max point where we have to pause,
4272 * then we need to write out the superblock.
4273 */
NeilBrown7a661382009-03-31 15:21:40 +11004274 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004275 if ((sector_nr - mddev->curr_resync_completed) * 2
4276 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004277 /* Cannot proceed until we've updated the superblock... */
4278 wait_event(conf->wait_for_overlap,
4279 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004280 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004281 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004282 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004283 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4284 md_wakeup_thread(mddev->thread);
4285 wait_event(mddev->sb_wait,
4286 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4287 || kthread_should_stop());
4288 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004289 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004290 spin_unlock_irq(&conf->device_lock);
4291 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004292 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004293 }
NeilBrown7a661382009-03-31 15:21:40 +11004294 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004295}
4296
4297/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004298static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004299{
NeilBrownd1688a62011-10-11 16:49:52 +11004300 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004301 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004302 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004303 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004304 int still_degraded = 0;
4305 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306
NeilBrown72626682005-09-09 16:23:54 -07004307 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004309
NeilBrown29269552006-03-27 01:18:10 -08004310 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4311 end_reshape(conf);
4312 return 0;
4313 }
NeilBrown72626682005-09-09 16:23:54 -07004314
4315 if (mddev->curr_resync < max_sector) /* aborted */
4316 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4317 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004318 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004319 conf->fullsync = 0;
4320 bitmap_close_sync(mddev->bitmap);
4321
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 return 0;
4323 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004324
NeilBrown64bd6602009-08-03 10:59:58 +10004325 /* Allow raid5_quiesce to complete */
4326 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4327
NeilBrown52c03292006-06-26 00:27:43 -07004328 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4329 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004330
NeilBrownc6207272008-02-06 01:39:52 -08004331 /* No need to check resync_max as we never do more than one
4332 * stripe, and as resync_max will always be on a chunk boundary,
4333 * if the check in md_do_sync didn't fire, there is no chance
4334 * of overstepping resync_max here
4335 */
4336
NeilBrown16a53ec2006-06-26 00:27:38 -07004337 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 * to resync, then assert that we are finished, because there is
4339 * nothing we can do.
4340 */
NeilBrown3285edf2006-06-26 00:27:55 -07004341 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004342 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004343 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004344 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 return rv;
4346 }
NeilBrown72626682005-09-09 16:23:54 -07004347 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004348 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004349 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4350 /* we can skip this block, and probably more */
4351 sync_blocks /= STRIPE_SECTORS;
4352 *skipped = 1;
4353 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355
NeilBrownb47490c2008-02-06 01:39:50 -08004356 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4357
NeilBrowna8c906c2009-06-09 14:39:59 +10004358 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004360 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004362 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004364 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004366 /* Need to check if array will still be degraded after recovery/resync
4367 * We don't need to check the 'failed' flag as when that gets set,
4368 * recovery aborts.
4369 */
NeilBrownf001a702009-06-09 14:30:31 +10004370 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004371 if (conf->disks[i].rdev == NULL)
4372 still_degraded = 1;
4373
4374 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4375
NeilBrown83206d62011-07-26 11:19:49 +10004376 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377
NeilBrown14425772009-10-16 15:55:25 +11004378 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 release_stripe(sh);
4380
4381 return STRIPE_SECTORS;
4382}
4383
NeilBrownd1688a62011-10-11 16:49:52 +11004384static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004385{
4386 /* We may not be able to submit a whole bio at once as there
4387 * may not be enough stripe_heads available.
4388 * We cannot pre-allocate enough stripe_heads as we may need
4389 * more than exist in the cache (if we allow ever large chunks).
4390 * So we do one stripe head at a time and record in
4391 * ->bi_hw_segments how many have been done.
4392 *
4393 * We *know* that this entire raid_bio is in one chunk, so
4394 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4395 */
4396 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004397 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004398 sector_t sector, logical_sector, last_sector;
4399 int scnt = 0;
4400 int remaining;
4401 int handled = 0;
4402
4403 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004404 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004405 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004406 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4407
4408 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004409 logical_sector += STRIPE_SECTORS,
4410 sector += STRIPE_SECTORS,
4411 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004412
Jens Axboe960e7392008-08-15 10:41:18 +02004413 if (scnt < raid5_bi_hw_segments(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004414 /* already done this stripe */
4415 continue;
4416
NeilBrowna8c906c2009-06-09 14:39:59 +10004417 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004418
4419 if (!sh) {
4420 /* failed to get a stripe - must wait */
Jens Axboe960e7392008-08-15 10:41:18 +02004421 raid5_set_bi_hw_segments(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004422 conf->retry_read_aligned = raid_bio;
4423 return handled;
4424 }
4425
Neil Brown387bb172007-02-08 14:20:29 -08004426 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4427 release_stripe(sh);
Jens Axboe960e7392008-08-15 10:41:18 +02004428 raid5_set_bi_hw_segments(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004429 conf->retry_read_aligned = raid_bio;
4430 return handled;
4431 }
4432
Dan Williams36d1c642009-07-14 11:48:22 -07004433 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004434 release_stripe(sh);
4435 handled++;
4436 }
4437 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004438 remaining = raid5_dec_bi_phys_segments(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004439 spin_unlock_irq(&conf->device_lock);
Neil Brown0e13fe232008-06-28 08:31:20 +10004440 if (remaining == 0)
4441 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004442 if (atomic_dec_and_test(&conf->active_aligned_reads))
4443 wake_up(&conf->wait_for_stripe);
4444 return handled;
4445}
4446
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004447
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448/*
4449 * This is our raid5 kernel thread.
4450 *
4451 * We scan the hash table for stripes which can be handled now.
4452 * During the scan, completed stripes are saved for us by the interrupt
4453 * handler, so that they will not have to wait for our next wakeup.
4454 */
NeilBrownfd01b882011-10-11 16:47:53 +11004455static void raid5d(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456{
4457 struct stripe_head *sh;
NeilBrownd1688a62011-10-11 16:49:52 +11004458 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004460 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461
Dan Williams45b42332007-07-09 11:56:43 -07004462 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463
4464 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004466 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 handled = 0;
4468 spin_lock_irq(&conf->device_lock);
4469 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004470 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471
NeilBrown7c13edc2011-04-18 18:25:43 +10004472 if (atomic_read(&mddev->plug_cnt) == 0 &&
4473 !list_empty(&conf->bitmap_list)) {
4474 /* Now is a good time to flush some bitmap updates */
4475 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004476 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004477 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004478 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004479 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004480 activate_bit_delay(conf);
4481 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004482 if (atomic_read(&mddev->plug_cnt) == 0)
4483 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004484
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004485 while ((bio = remove_bio_from_retry(conf))) {
4486 int ok;
4487 spin_unlock_irq(&conf->device_lock);
4488 ok = retry_aligned_read(conf, bio);
4489 spin_lock_irq(&conf->device_lock);
4490 if (!ok)
4491 break;
4492 handled++;
4493 }
4494
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004495 sh = __get_priority_stripe(conf);
4496
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004497 if (!sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 spin_unlock_irq(&conf->device_lock);
4500
4501 handled++;
Dan Williams417b8d42009-10-16 16:25:22 +11004502 handle_stripe(sh);
4503 release_stripe(sh);
4504 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505
NeilBrownde393cd2011-07-28 11:31:48 +10004506 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
4507 md_check_recovery(mddev);
4508
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 spin_lock_irq(&conf->device_lock);
4510 }
Dan Williams45b42332007-07-09 11:56:43 -07004511 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512
4513 spin_unlock_irq(&conf->device_lock);
4514
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004515 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004516 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517
Dan Williams45b42332007-07-09 11:56:43 -07004518 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519}
4520
NeilBrown3f294f42005-11-08 21:39:25 -08004521static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004522raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004523{
NeilBrownd1688a62011-10-11 16:49:52 +11004524 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004525 if (conf)
4526 return sprintf(page, "%d\n", conf->max_nr_stripes);
4527 else
4528 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004529}
4530
NeilBrownc41d4ac2010-06-01 19:37:24 +10004531int
NeilBrownfd01b882011-10-11 16:47:53 +11004532raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004533{
NeilBrownd1688a62011-10-11 16:49:52 +11004534 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004535 int err;
4536
4537 if (size <= 16 || size > 32768)
4538 return -EINVAL;
4539 while (size < conf->max_nr_stripes) {
4540 if (drop_one_stripe(conf))
4541 conf->max_nr_stripes--;
4542 else
4543 break;
4544 }
4545 err = md_allow_write(mddev);
4546 if (err)
4547 return err;
4548 while (size > conf->max_nr_stripes) {
4549 if (grow_one_stripe(conf))
4550 conf->max_nr_stripes++;
4551 else break;
4552 }
4553 return 0;
4554}
4555EXPORT_SYMBOL(raid5_set_cache_size);
4556
NeilBrown3f294f42005-11-08 21:39:25 -08004557static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004558raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004559{
NeilBrownd1688a62011-10-11 16:49:52 +11004560 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004561 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004562 int err;
4563
NeilBrown3f294f42005-11-08 21:39:25 -08004564 if (len >= PAGE_SIZE)
4565 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004566 if (!conf)
4567 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004568
Dan Williams4ef197d82008-04-28 02:15:54 -07004569 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004570 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004571 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004572 if (err)
4573 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004574 return len;
4575}
NeilBrown007583c2005-11-08 21:39:30 -08004576
NeilBrown96de1e62005-11-08 21:39:39 -08004577static struct md_sysfs_entry
4578raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4579 raid5_show_stripe_cache_size,
4580 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004581
4582static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004583raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004584{
NeilBrownd1688a62011-10-11 16:49:52 +11004585 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004586 if (conf)
4587 return sprintf(page, "%d\n", conf->bypass_threshold);
4588 else
4589 return 0;
4590}
4591
4592static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004593raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004594{
NeilBrownd1688a62011-10-11 16:49:52 +11004595 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004596 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004597 if (len >= PAGE_SIZE)
4598 return -EINVAL;
4599 if (!conf)
4600 return -ENODEV;
4601
Dan Williams4ef197d82008-04-28 02:15:54 -07004602 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004603 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004604 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004605 return -EINVAL;
4606 conf->bypass_threshold = new;
4607 return len;
4608}
4609
4610static struct md_sysfs_entry
4611raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4612 S_IRUGO | S_IWUSR,
4613 raid5_show_preread_threshold,
4614 raid5_store_preread_threshold);
4615
4616static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004617stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004618{
NeilBrownd1688a62011-10-11 16:49:52 +11004619 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004620 if (conf)
4621 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4622 else
4623 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004624}
4625
NeilBrown96de1e62005-11-08 21:39:39 -08004626static struct md_sysfs_entry
4627raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004628
NeilBrown007583c2005-11-08 21:39:30 -08004629static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004630 &raid5_stripecache_size.attr,
4631 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004632 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004633 NULL,
4634};
NeilBrown007583c2005-11-08 21:39:30 -08004635static struct attribute_group raid5_attrs_group = {
4636 .name = NULL,
4637 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004638};
4639
Dan Williams80c3a6c2009-03-17 18:10:40 -07004640static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11004641raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07004642{
NeilBrownd1688a62011-10-11 16:49:52 +11004643 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004644
4645 if (!sectors)
4646 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004647 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11004648 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11004649 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004650
Andre Noll9d8f0362009-06-18 08:45:01 +10004651 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004652 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004653 return sectors * (raid_disks - conf->max_degraded);
4654}
4655
NeilBrownd1688a62011-10-11 16:49:52 +11004656static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004657{
4658 struct raid5_percpu *percpu;
4659 unsigned long cpu;
4660
4661 if (!conf->percpu)
4662 return;
4663
4664 get_online_cpus();
4665 for_each_possible_cpu(cpu) {
4666 percpu = per_cpu_ptr(conf->percpu, cpu);
4667 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004668 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004669 }
4670#ifdef CONFIG_HOTPLUG_CPU
4671 unregister_cpu_notifier(&conf->cpu_notify);
4672#endif
4673 put_online_cpus();
4674
4675 free_percpu(conf->percpu);
4676}
4677
NeilBrownd1688a62011-10-11 16:49:52 +11004678static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10004679{
4680 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004681 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10004682 kfree(conf->disks);
4683 kfree(conf->stripe_hashtbl);
4684 kfree(conf);
4685}
4686
Dan Williams36d1c642009-07-14 11:48:22 -07004687#ifdef CONFIG_HOTPLUG_CPU
4688static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4689 void *hcpu)
4690{
NeilBrownd1688a62011-10-11 16:49:52 +11004691 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07004692 long cpu = (long)hcpu;
4693 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4694
4695 switch (action) {
4696 case CPU_UP_PREPARE:
4697 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07004698 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07004699 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004700 if (!percpu->scribble)
4701 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4702
4703 if (!percpu->scribble ||
4704 (conf->level == 6 && !percpu->spare_page)) {
4705 safe_put_page(percpu->spare_page);
4706 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004707 pr_err("%s: failed memory allocation for cpu%ld\n",
4708 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07004709 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07004710 }
4711 break;
4712 case CPU_DEAD:
4713 case CPU_DEAD_FROZEN:
4714 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004715 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004716 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004717 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07004718 break;
4719 default:
4720 break;
4721 }
4722 return NOTIFY_OK;
4723}
4724#endif
4725
NeilBrownd1688a62011-10-11 16:49:52 +11004726static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004727{
4728 unsigned long cpu;
4729 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09004730 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004731 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004732 int err;
4733
Dan Williams36d1c642009-07-14 11:48:22 -07004734 allcpus = alloc_percpu(struct raid5_percpu);
4735 if (!allcpus)
4736 return -ENOMEM;
4737 conf->percpu = allcpus;
4738
4739 get_online_cpus();
4740 err = 0;
4741 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07004742 if (conf->level == 6) {
4743 spare_page = alloc_page(GFP_KERNEL);
4744 if (!spare_page) {
4745 err = -ENOMEM;
4746 break;
4747 }
4748 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
4749 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11004750 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004751 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07004752 err = -ENOMEM;
4753 break;
4754 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07004755 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004756 }
4757#ifdef CONFIG_HOTPLUG_CPU
4758 conf->cpu_notify.notifier_call = raid456_cpu_notify;
4759 conf->cpu_notify.priority = 0;
4760 if (err == 0)
4761 err = register_cpu_notifier(&conf->cpu_notify);
4762#endif
4763 put_online_cpus();
4764
4765 return err;
4766}
4767
NeilBrownd1688a62011-10-11 16:49:52 +11004768static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769{
NeilBrownd1688a62011-10-11 16:49:52 +11004770 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004771 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11004772 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 struct disk_info *disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774
NeilBrown91adb562009-03-31 14:39:39 +11004775 if (mddev->new_level != 5
4776 && mddev->new_level != 4
4777 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10004778 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004779 mdname(mddev), mddev->new_level);
4780 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 }
NeilBrown91adb562009-03-31 14:39:39 +11004782 if ((mddev->new_level == 5
4783 && !algorithm_valid_raid5(mddev->new_layout)) ||
4784 (mddev->new_level == 6
4785 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004786 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11004787 mdname(mddev), mddev->new_layout);
4788 return ERR_PTR(-EIO);
4789 }
4790 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10004791 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004792 mdname(mddev), mddev->raid_disks);
4793 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11004794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795
Andre Noll664e7c42009-06-18 08:45:27 +10004796 if (!mddev->new_chunk_sectors ||
4797 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
4798 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10004799 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
4800 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11004801 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11004802 }
4803
NeilBrownd1688a62011-10-11 16:49:52 +11004804 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11004805 if (conf == NULL)
4806 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11004807 spin_lock_init(&conf->device_lock);
4808 init_waitqueue_head(&conf->wait_for_stripe);
4809 init_waitqueue_head(&conf->wait_for_overlap);
4810 INIT_LIST_HEAD(&conf->handle_list);
4811 INIT_LIST_HEAD(&conf->hold_list);
4812 INIT_LIST_HEAD(&conf->delayed_list);
4813 INIT_LIST_HEAD(&conf->bitmap_list);
4814 INIT_LIST_HEAD(&conf->inactive_list);
4815 atomic_set(&conf->active_stripes, 0);
4816 atomic_set(&conf->preread_active_stripes, 0);
4817 atomic_set(&conf->active_aligned_reads, 0);
4818 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11004819 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11004820
4821 conf->raid_disks = mddev->raid_disks;
4822 if (mddev->reshape_position == MaxSector)
4823 conf->previous_raid_disks = mddev->raid_disks;
4824 else
4825 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004826 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
4827 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11004828
NeilBrown5e5e3e72009-10-16 16:35:30 +11004829 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11004830 GFP_KERNEL);
4831 if (!conf->disks)
4832 goto abort;
4833
4834 conf->mddev = mddev;
4835
4836 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
4837 goto abort;
4838
Dan Williams36d1c642009-07-14 11:48:22 -07004839 conf->level = mddev->new_level;
4840 if (raid5_alloc_percpu(conf) != 0)
4841 goto abort;
4842
NeilBrown0c55e022010-05-03 14:09:02 +10004843 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11004844
NeilBrowndafb20f2012-03-19 12:46:39 +11004845 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11004846 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004847 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11004848 || raid_disk < 0)
4849 continue;
4850 disk = conf->disks + raid_disk;
4851
NeilBrown17045f52011-12-23 10:17:53 +11004852 if (test_bit(Replacement, &rdev->flags)) {
4853 if (disk->replacement)
4854 goto abort;
4855 disk->replacement = rdev;
4856 } else {
4857 if (disk->rdev)
4858 goto abort;
4859 disk->rdev = rdev;
4860 }
NeilBrown91adb562009-03-31 14:39:39 +11004861
4862 if (test_bit(In_sync, &rdev->flags)) {
4863 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10004864 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
4865 " disk %d\n",
4866 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05004867 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11004868 /* Cannot rely on bitmap to complete recovery */
4869 conf->fullsync = 1;
4870 }
4871
Andre Noll09c9e5f2009-06-18 08:45:55 +10004872 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11004873 conf->level = mddev->new_level;
4874 if (conf->level == 6)
4875 conf->max_degraded = 2;
4876 else
4877 conf->max_degraded = 1;
4878 conf->algorithm = mddev->new_layout;
4879 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11004880 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11004881 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10004882 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11004883 conf->prev_algo = mddev->layout;
4884 }
NeilBrown91adb562009-03-31 14:39:39 +11004885
4886 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11004887 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11004888 if (grow_stripes(conf, conf->max_nr_stripes)) {
4889 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004890 "md/raid:%s: couldn't allocate %dkB for buffers\n",
4891 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004892 goto abort;
4893 } else
NeilBrown0c55e022010-05-03 14:09:02 +10004894 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
4895 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004896
NeilBrown0da3c612009-09-23 18:09:45 +10004897 conf->thread = md_register_thread(raid5d, mddev, NULL);
NeilBrown91adb562009-03-31 14:39:39 +11004898 if (!conf->thread) {
4899 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004900 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11004901 mdname(mddev));
4902 goto abort;
4903 }
4904
4905 return conf;
4906
4907 abort:
4908 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10004909 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11004910 return ERR_PTR(-EIO);
4911 } else
4912 return ERR_PTR(-ENOMEM);
4913}
4914
NeilBrownc148ffd2009-11-13 17:47:00 +11004915
4916static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
4917{
4918 switch (algo) {
4919 case ALGORITHM_PARITY_0:
4920 if (raid_disk < max_degraded)
4921 return 1;
4922 break;
4923 case ALGORITHM_PARITY_N:
4924 if (raid_disk >= raid_disks - max_degraded)
4925 return 1;
4926 break;
4927 case ALGORITHM_PARITY_0_6:
4928 if (raid_disk == 0 ||
4929 raid_disk == raid_disks - 1)
4930 return 1;
4931 break;
4932 case ALGORITHM_LEFT_ASYMMETRIC_6:
4933 case ALGORITHM_RIGHT_ASYMMETRIC_6:
4934 case ALGORITHM_LEFT_SYMMETRIC_6:
4935 case ALGORITHM_RIGHT_SYMMETRIC_6:
4936 if (raid_disk == raid_disks - 1)
4937 return 1;
4938 }
4939 return 0;
4940}
4941
NeilBrownfd01b882011-10-11 16:47:53 +11004942static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11004943{
NeilBrownd1688a62011-10-11 16:49:52 +11004944 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10004945 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11004946 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11004947 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11004948 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11004949 int i;
NeilBrown91adb562009-03-31 14:39:39 +11004950
Andre Noll8c6ac862009-06-18 08:48:06 +10004951 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10004952 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10004953 " -- starting background reconstruction\n",
4954 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004955 if (mddev->reshape_position != MaxSector) {
4956 /* Check that we can continue the reshape.
4957 * Currently only disks can change, it must
4958 * increase, and we must be past the point where
4959 * a stripe over-writes itself
4960 */
4961 sector_t here_new, here_old;
4962 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11004963 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08004964
NeilBrown88ce4932009-03-31 15:24:23 +11004965 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10004966 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08004967 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08004968 mdname(mddev));
4969 return -EINVAL;
4970 }
NeilBrownf6705572006-03-27 01:18:11 -08004971 old_disks = mddev->raid_disks - mddev->delta_disks;
4972 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08004973 * further up in new geometry must map after here in old
4974 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08004975 */
4976 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10004977 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08004978 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004979 printk(KERN_ERR "md/raid:%s: reshape_position not "
4980 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004981 return -EINVAL;
4982 }
NeilBrownc148ffd2009-11-13 17:47:00 +11004983 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08004984 /* here_new is the stripe we will write to */
4985 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10004986 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08004987 (old_disks-max_degraded));
4988 /* here_old is the first stripe that we might need to read
4989 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10004990 if (mddev->delta_disks == 0) {
4991 /* We cannot be sure it is safe to start an in-place
4992 * reshape. It is only safe if user-space if monitoring
4993 * and taking constant backups.
4994 * mdadm always starts a situation like this in
4995 * readonly mode so it can take control before
4996 * allowing any writes. So just check for that.
4997 */
4998 if ((here_new * mddev->new_chunk_sectors !=
4999 here_old * mddev->chunk_sectors) ||
5000 mddev->ro == 0) {
NeilBrown0c55e022010-05-03 14:09:02 +10005001 printk(KERN_ERR "md/raid:%s: in-place reshape must be started"
5002 " in read-only mode - aborting\n",
5003 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005004 return -EINVAL;
5005 }
5006 } else if (mddev->delta_disks < 0
5007 ? (here_new * mddev->new_chunk_sectors <=
5008 here_old * mddev->chunk_sectors)
5009 : (here_new * mddev->new_chunk_sectors >=
5010 here_old * mddev->chunk_sectors)) {
NeilBrownf6705572006-03-27 01:18:11 -08005011 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005012 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5013 "auto-recovery - aborting.\n",
5014 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005015 return -EINVAL;
5016 }
NeilBrown0c55e022010-05-03 14:09:02 +10005017 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5018 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005019 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005020 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005021 BUG_ON(mddev->level != mddev->new_level);
5022 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005023 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005024 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005025 }
5026
NeilBrown245f46c2009-03-31 14:39:39 +11005027 if (mddev->private == NULL)
5028 conf = setup_conf(mddev);
5029 else
5030 conf = mddev->private;
5031
NeilBrown91adb562009-03-31 14:39:39 +11005032 if (IS_ERR(conf))
5033 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005034
NeilBrown91adb562009-03-31 14:39:39 +11005035 mddev->thread = conf->thread;
5036 conf->thread = NULL;
5037 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038
NeilBrown17045f52011-12-23 10:17:53 +11005039 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5040 i++) {
5041 rdev = conf->disks[i].rdev;
5042 if (!rdev && conf->disks[i].replacement) {
5043 /* The replacement is all we have yet */
5044 rdev = conf->disks[i].replacement;
5045 conf->disks[i].replacement = NULL;
5046 clear_bit(Replacement, &rdev->flags);
5047 conf->disks[i].rdev = rdev;
5048 }
5049 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11005050 continue;
NeilBrown17045f52011-12-23 10:17:53 +11005051 if (conf->disks[i].replacement &&
5052 conf->reshape_progress != MaxSector) {
5053 /* replacements and reshape simply do not mix. */
5054 printk(KERN_ERR "md: cannot handle concurrent "
5055 "replacement and reshape.\n");
5056 goto abort;
5057 }
NeilBrown2f115882010-06-17 17:41:03 +10005058 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005059 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005060 continue;
5061 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005062 /* This disc is not fully in-sync. However if it
5063 * just stored parity (beyond the recovery_offset),
5064 * when we don't need to be concerned about the
5065 * array being dirty.
5066 * When reshape goes 'backwards', we never have
5067 * partially completed devices, so we only need
5068 * to worry about reshape going forwards.
5069 */
5070 /* Hack because v0.91 doesn't store recovery_offset properly. */
5071 if (mddev->major_version == 0 &&
5072 mddev->minor_version > 90)
5073 rdev->recovery_offset = reshape_offset;
5074
NeilBrownc148ffd2009-11-13 17:47:00 +11005075 if (rdev->recovery_offset < reshape_offset) {
5076 /* We need to check old and new layout */
5077 if (!only_parity(rdev->raid_disk,
5078 conf->algorithm,
5079 conf->raid_disks,
5080 conf->max_degraded))
5081 continue;
5082 }
5083 if (!only_parity(rdev->raid_disk,
5084 conf->prev_algo,
5085 conf->previous_raid_disks,
5086 conf->max_degraded))
5087 continue;
5088 dirty_parity_disks++;
5089 }
NeilBrown91adb562009-03-31 14:39:39 +11005090
NeilBrown17045f52011-12-23 10:17:53 +11005091 /*
5092 * 0 for a fully functional array, 1 or 2 for a degraded array.
5093 */
NeilBrown908f4fb2011-12-23 10:17:50 +11005094 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095
NeilBrown674806d2010-06-16 17:17:53 +10005096 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005097 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07005099 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 goto abort;
5101 }
5102
NeilBrown91adb562009-03-31 14:39:39 +11005103 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10005104 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11005105 mddev->resync_max_sectors = mddev->dev_sectors;
5106
NeilBrownc148ffd2009-11-13 17:47:00 +11005107 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005109 if (mddev->ok_start_degraded)
5110 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10005111 "md/raid:%s: starting dirty degraded array"
5112 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005113 mdname(mddev));
5114 else {
5115 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005116 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005117 mdname(mddev));
5118 goto abort;
5119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 }
5121
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10005123 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5124 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11005125 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5126 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 else
NeilBrown0c55e022010-05-03 14:09:02 +10005128 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5129 " out of %d devices, algorithm %d\n",
5130 mdname(mddev), conf->level,
5131 mddev->raid_disks - mddev->degraded,
5132 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133
5134 print_raid5_conf(conf);
5135
NeilBrownfef9c612009-03-31 15:16:46 +11005136 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11005137 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005138 atomic_set(&conf->reshape_stripes, 0);
5139 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5140 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5141 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5142 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5143 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005144 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005145 }
5146
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147
5148 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005149 if (mddev->to_remove == &raid5_attrs_group)
5150 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005151 else if (mddev->kobj.sd &&
5152 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005153 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10005154 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08005155 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10005156 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5157
5158 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005159 int chunk_size;
NeilBrown4a5add42010-06-01 19:37:28 +10005160 /* read-ahead size must cover two whole stripes, which
5161 * is 2 * (datadisks) * chunksize where 'n' is the
5162 * number of raid devices
5163 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5165 int stripe = data_disks *
5166 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5167 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5168 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10005169
5170 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005171
5172 mddev->queue->backing_dev_info.congested_data = mddev;
5173 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10005174
5175 chunk_size = mddev->chunk_sectors << 9;
5176 blk_queue_io_min(mddev->queue, chunk_size);
5177 blk_queue_io_opt(mddev->queue, chunk_size *
5178 (conf->raid_disks - conf->max_degraded));
5179
NeilBrowndafb20f2012-03-19 12:46:39 +11005180 rdev_for_each(rdev, mddev)
NeilBrown9f7c2222010-07-26 12:04:13 +10005181 disk_stack_limits(mddev->gendisk, rdev->bdev,
5182 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 }
5184
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 return 0;
5186abort:
NeilBrown01f96c02011-09-21 15:30:20 +10005187 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11005188 print_raid5_conf(conf);
5189 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005191 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192 return -EIO;
5193}
5194
NeilBrownfd01b882011-10-11 16:47:53 +11005195static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196{
NeilBrownd1688a62011-10-11 16:49:52 +11005197 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198
NeilBrown01f96c02011-09-21 15:30:20 +10005199 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005200 if (mddev->queue)
5201 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10005202 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005203 mddev->private = NULL;
5204 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 return 0;
5206}
5207
NeilBrownfd01b882011-10-11 16:47:53 +11005208static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209{
NeilBrownd1688a62011-10-11 16:49:52 +11005210 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 int i;
5212
Andre Noll9d8f0362009-06-18 08:45:01 +10005213 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5214 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005215 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 for (i = 0; i < conf->raid_disks; i++)
5217 seq_printf (seq, "%s",
5218 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005219 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221}
5222
NeilBrownd1688a62011-10-11 16:49:52 +11005223static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224{
5225 int i;
5226 struct disk_info *tmp;
5227
NeilBrown0c55e022010-05-03 14:09:02 +10005228 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 if (!conf) {
5230 printk("(conf==NULL)\n");
5231 return;
5232 }
NeilBrown0c55e022010-05-03 14:09:02 +10005233 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5234 conf->raid_disks,
5235 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236
5237 for (i = 0; i < conf->raid_disks; i++) {
5238 char b[BDEVNAME_SIZE];
5239 tmp = conf->disks + i;
5240 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005241 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5242 i, !test_bit(Faulty, &tmp->rdev->flags),
5243 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 }
5245}
5246
NeilBrownfd01b882011-10-11 16:47:53 +11005247static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248{
5249 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005250 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005252 int count = 0;
5253 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254
5255 for (i = 0; i < conf->raid_disks; i++) {
5256 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11005257 if (tmp->replacement
5258 && tmp->replacement->recovery_offset == MaxSector
5259 && !test_bit(Faulty, &tmp->replacement->flags)
5260 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
5261 /* Replacement has just become active. */
5262 if (!tmp->rdev
5263 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
5264 count++;
5265 if (tmp->rdev) {
5266 /* Replaced device not technically faulty,
5267 * but we need to be sure it gets removed
5268 * and never re-added.
5269 */
5270 set_bit(Faulty, &tmp->rdev->flags);
5271 sysfs_notify_dirent_safe(
5272 tmp->rdev->sysfs_state);
5273 }
5274 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
5275 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005276 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005277 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005278 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005279 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005280 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281 }
5282 }
NeilBrown6b965622010-08-18 11:56:59 +10005283 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005284 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005285 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005287 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288}
5289
NeilBrownb8321b62011-12-23 10:17:51 +11005290static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291{
NeilBrownd1688a62011-10-11 16:49:52 +11005292 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11005294 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11005295 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 struct disk_info *p = conf->disks + number;
5297
5298 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11005299 if (rdev == p->rdev)
5300 rdevp = &p->rdev;
5301 else if (rdev == p->replacement)
5302 rdevp = &p->replacement;
5303 else
5304 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11005305
NeilBrown657e3e42011-12-23 10:17:52 +11005306 if (number >= conf->raid_disks &&
5307 conf->reshape_progress == MaxSector)
5308 clear_bit(In_sync, &rdev->flags);
5309
5310 if (test_bit(In_sync, &rdev->flags) ||
5311 atomic_read(&rdev->nr_pending)) {
5312 err = -EBUSY;
5313 goto abort;
5314 }
5315 /* Only remove non-faulty devices if recovery
5316 * isn't possible.
5317 */
5318 if (!test_bit(Faulty, &rdev->flags) &&
5319 mddev->recovery_disabled != conf->recovery_disabled &&
5320 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11005321 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11005322 number < conf->raid_disks) {
5323 err = -EBUSY;
5324 goto abort;
5325 }
5326 *rdevp = NULL;
5327 synchronize_rcu();
5328 if (atomic_read(&rdev->nr_pending)) {
5329 /* lost the race, try later */
5330 err = -EBUSY;
5331 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11005332 } else if (p->replacement) {
5333 /* We must have just cleared 'rdev' */
5334 p->rdev = p->replacement;
5335 clear_bit(Replacement, &p->replacement->flags);
5336 smp_mb(); /* Make sure other CPUs may see both as identical
5337 * but will never see neither - if they are careful
5338 */
5339 p->replacement = NULL;
5340 clear_bit(WantReplacement, &rdev->flags);
5341 } else
5342 /* We might have just removed the Replacement as faulty-
5343 * clear the bit just in case
5344 */
5345 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346abort:
5347
5348 print_raid5_conf(conf);
5349 return err;
5350}
5351
NeilBrownfd01b882011-10-11 16:47:53 +11005352static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353{
NeilBrownd1688a62011-10-11 16:49:52 +11005354 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005355 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356 int disk;
5357 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005358 int first = 0;
5359 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360
NeilBrown7f0da592011-07-28 11:39:22 +10005361 if (mddev->recovery_disabled == conf->recovery_disabled)
5362 return -EBUSY;
5363
NeilBrowndc10c642012-03-19 12:46:37 +11005364 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005366 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367
Neil Brown6c2fce22008-06-28 08:31:31 +10005368 if (rdev->raid_disk >= 0)
5369 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370
5371 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005372 * find the disk ... but prefer rdev->saved_raid_disk
5373 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005375 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005376 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005377 conf->disks[rdev->saved_raid_disk].rdev == NULL)
5378 disk = rdev->saved_raid_disk;
5379 else
Neil Brown6c2fce22008-06-28 08:31:31 +10005380 disk = first;
NeilBrown7bfec5f2011-12-23 10:17:53 +11005381 for ( ; disk <= last ; disk++) {
5382 p = conf->disks + disk;
5383 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005384 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005386 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005387 if (rdev->saved_raid_disk != disk)
5388 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005389 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 break;
5391 }
NeilBrown7bfec5f2011-12-23 10:17:53 +11005392 if (test_bit(WantReplacement, &p->rdev->flags) &&
5393 p->replacement == NULL) {
5394 clear_bit(In_sync, &rdev->flags);
5395 set_bit(Replacement, &rdev->flags);
5396 rdev->raid_disk = disk;
5397 err = 0;
5398 conf->fullsync = 1;
5399 rcu_assign_pointer(p->replacement, rdev);
5400 break;
5401 }
5402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005404 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405}
5406
NeilBrownfd01b882011-10-11 16:47:53 +11005407static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408{
5409 /* no resync is happening, and there is enough space
5410 * on all devices, so we can resize.
5411 * We need to make sure resync covers any new space.
5412 * If the array is shrinking we should possibly wait until
5413 * any io in the removed space completes, but it hardly seems
5414 * worth it.
5415 */
Andre Noll9d8f0362009-06-18 08:45:01 +10005416 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Dan Williams1f403622009-03-31 14:59:03 +11005417 md_set_array_sectors(mddev, raid5_size(mddev, sectors,
5418 mddev->raid_disks));
Dan Williamsb522adc2009-03-31 15:00:31 +11005419 if (mddev->array_sectors >
5420 raid5_size(mddev, sectors, mddev->raid_disks))
5421 return -EINVAL;
Andre Nollf233ea52008-07-21 17:05:22 +10005422 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005423 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005424 if (sectors > mddev->dev_sectors &&
5425 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005426 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5428 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005429 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005430 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 return 0;
5432}
5433
NeilBrownfd01b882011-10-11 16:47:53 +11005434static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005435{
5436 /* Can only proceed if there are plenty of stripe_heads.
5437 * We need a minimum of one full stripe,, and for sensible progress
5438 * it is best to have about 4 times that.
5439 * If we require 4 times, then the default 256 4K stripe_heads will
5440 * allow for chunk sizes up to 256K, which is probably OK.
5441 * If the chunk size is greater, user-space should request more
5442 * stripe_heads first.
5443 */
NeilBrownd1688a62011-10-11 16:49:52 +11005444 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005445 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5446 > conf->max_nr_stripes ||
5447 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5448 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005449 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5450 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005451 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5452 / STRIPE_SIZE)*4);
5453 return 0;
5454 }
5455 return 1;
5456}
5457
NeilBrownfd01b882011-10-11 16:47:53 +11005458static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005459{
NeilBrownd1688a62011-10-11 16:49:52 +11005460 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005461
NeilBrown88ce4932009-03-31 15:24:23 +11005462 if (mddev->delta_disks == 0 &&
5463 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005464 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005465 return 0; /* nothing to do */
NeilBrowndba034e2008-08-05 15:54:13 +10005466 if (mddev->bitmap)
5467 /* Cannot grow a bitmap yet */
5468 return -EBUSY;
NeilBrown674806d2010-06-16 17:17:53 +10005469 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005470 return -EINVAL;
5471 if (mddev->delta_disks < 0) {
5472 /* We might be able to shrink, but the devices must
5473 * be made bigger first.
5474 * For raid6, 4 is the minimum size.
5475 * Otherwise 2 is the minimum
5476 */
5477 int min = 2;
5478 if (mddev->level == 6)
5479 min = 4;
5480 if (mddev->raid_disks + mddev->delta_disks < min)
5481 return -EINVAL;
5482 }
NeilBrown29269552006-03-27 01:18:10 -08005483
NeilBrown01ee22b2009-06-18 08:47:20 +10005484 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005485 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005486
NeilBrownec32a2b2009-03-31 15:17:38 +11005487 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
NeilBrown63c70c42006-03-27 01:18:13 -08005488}
5489
NeilBrownfd01b882011-10-11 16:47:53 +11005490static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005491{
NeilBrownd1688a62011-10-11 16:49:52 +11005492 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005493 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005494 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005495 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005496
NeilBrownf4168852007-02-28 20:11:53 -08005497 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005498 return -EBUSY;
5499
NeilBrown01ee22b2009-06-18 08:47:20 +10005500 if (!check_stripe_cache(mddev))
5501 return -ENOSPC;
5502
NeilBrowndafb20f2012-03-19 12:46:39 +11005503 rdev_for_each(rdev, mddev)
NeilBrown469518a2011-01-31 11:57:43 +11005504 if (!test_bit(In_sync, &rdev->flags)
5505 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005506 spares++;
NeilBrown63c70c42006-03-27 01:18:13 -08005507
NeilBrownf4168852007-02-28 20:11:53 -08005508 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005509 /* Not enough devices even to make a degraded array
5510 * of that size
5511 */
5512 return -EINVAL;
5513
NeilBrownec32a2b2009-03-31 15:17:38 +11005514 /* Refuse to reduce size of the array. Any reductions in
5515 * array size must be through explicit setting of array_size
5516 * attribute.
5517 */
5518 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5519 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10005520 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11005521 "before number of disks\n", mdname(mddev));
5522 return -EINVAL;
5523 }
5524
NeilBrownf6705572006-03-27 01:18:11 -08005525 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005526 spin_lock_irq(&conf->device_lock);
5527 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005528 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005529 conf->prev_chunk_sectors = conf->chunk_sectors;
5530 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005531 conf->prev_algo = conf->algorithm;
5532 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11005533 if (mddev->delta_disks < 0)
5534 conf->reshape_progress = raid5_size(mddev, 0, 0);
5535 else
5536 conf->reshape_progress = 0;
5537 conf->reshape_safe = conf->reshape_progress;
NeilBrown86b42c72009-03-31 15:19:03 +11005538 conf->generation++;
NeilBrown29269552006-03-27 01:18:10 -08005539 spin_unlock_irq(&conf->device_lock);
5540
5541 /* Add some new drives, as many as will fit.
5542 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10005543 * Don't add devices if we are reducing the number of
5544 * devices in the array. This is because it is not possible
5545 * to correctly record the "partially reconstructed" state of
5546 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08005547 */
NeilBrown87a8dec2011-01-31 11:57:43 +11005548 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11005549 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11005550 if (rdev->raid_disk < 0 &&
5551 !test_bit(Faulty, &rdev->flags)) {
5552 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11005553 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11005554 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11005555 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11005556 else
NeilBrown87a8dec2011-01-31 11:57:43 +11005557 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10005558
5559 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11005560 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11005561 }
NeilBrown87a8dec2011-01-31 11:57:43 +11005562 } else if (rdev->raid_disk >= conf->previous_raid_disks
5563 && !test_bit(Faulty, &rdev->flags)) {
5564 /* This is a spare that was manually added */
5565 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11005566 }
NeilBrown29269552006-03-27 01:18:10 -08005567
NeilBrown87a8dec2011-01-31 11:57:43 +11005568 /* When a reshape changes the number of devices,
5569 * ->degraded is measured against the larger of the
5570 * pre and post number of devices.
5571 */
NeilBrownec32a2b2009-03-31 15:17:38 +11005572 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005573 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11005574 spin_unlock_irqrestore(&conf->device_lock, flags);
5575 }
NeilBrown63c70c42006-03-27 01:18:13 -08005576 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10005577 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07005578 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005579
NeilBrown29269552006-03-27 01:18:10 -08005580 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5581 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5582 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5583 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5584 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005585 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08005586 if (!mddev->sync_thread) {
5587 mddev->recovery = 0;
5588 spin_lock_irq(&conf->device_lock);
5589 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005590 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11005591 mddev->reshape_position = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005592 spin_unlock_irq(&conf->device_lock);
5593 return -EAGAIN;
5594 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005595 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005596 md_wakeup_thread(mddev->sync_thread);
5597 md_new_event(mddev);
5598 return 0;
5599}
NeilBrown29269552006-03-27 01:18:10 -08005600
NeilBrownec32a2b2009-03-31 15:17:38 +11005601/* This is called from the reshape thread and should make any
5602 * changes needed in 'conf'
5603 */
NeilBrownd1688a62011-10-11 16:49:52 +11005604static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08005605{
NeilBrown29269552006-03-27 01:18:10 -08005606
NeilBrownf6705572006-03-27 01:18:11 -08005607 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
Dan Williams80c3a6c2009-03-17 18:10:40 -07005608
NeilBrownf6705572006-03-27 01:18:11 -08005609 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11005610 conf->previous_raid_disks = conf->raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005611 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08005612 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005613 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07005614
5615 /* read-ahead size must cover two whole stripes, which is
5616 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5617 */
NeilBrown4a5add42010-06-01 19:37:28 +10005618 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11005619 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005620 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11005621 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07005622 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5623 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5624 }
NeilBrown29269552006-03-27 01:18:10 -08005625 }
NeilBrown29269552006-03-27 01:18:10 -08005626}
5627
NeilBrownec32a2b2009-03-31 15:17:38 +11005628/* This is called from the raid5d thread with mddev_lock held.
5629 * It makes config changes to the device.
5630 */
NeilBrownfd01b882011-10-11 16:47:53 +11005631static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11005632{
NeilBrownd1688a62011-10-11 16:49:52 +11005633 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11005634
5635 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5636
NeilBrownec32a2b2009-03-31 15:17:38 +11005637 if (mddev->delta_disks > 0) {
5638 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5639 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005640 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11005641 } else {
5642 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11005643 spin_lock_irq(&conf->device_lock);
5644 mddev->degraded = calc_degraded(conf);
5645 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11005646 for (d = conf->raid_disks ;
5647 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10005648 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11005649 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownb8321b62011-12-23 10:17:51 +11005650 if (rdev &&
5651 raid5_remove_disk(mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10005652 sysfs_unlink_rdev(mddev, rdev);
NeilBrown1a67dde2009-08-13 10:41:49 +10005653 rdev->raid_disk = -1;
5654 }
5655 }
NeilBrowncea9c222009-03-31 15:15:05 +11005656 }
NeilBrown88ce4932009-03-31 15:24:23 +11005657 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005658 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11005659 mddev->reshape_position = MaxSector;
5660 mddev->delta_disks = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11005661 }
5662}
5663
NeilBrownfd01b882011-10-11 16:47:53 +11005664static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07005665{
NeilBrownd1688a62011-10-11 16:49:52 +11005666 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07005667
5668 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08005669 case 2: /* resume for a suspend */
5670 wake_up(&conf->wait_for_overlap);
5671 break;
5672
NeilBrown72626682005-09-09 16:23:54 -07005673 case 1: /* stop all writes */
5674 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005675 /* '2' tells resync/reshape to pause so that all
5676 * active stripes can drain
5677 */
5678 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07005679 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005680 atomic_read(&conf->active_stripes) == 0 &&
5681 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07005682 conf->device_lock, /* nothing */);
NeilBrown64bd6602009-08-03 10:59:58 +10005683 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07005684 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005685 /* allow reshape to continue */
5686 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005687 break;
5688
5689 case 0: /* re-enable writes */
5690 spin_lock_irq(&conf->device_lock);
5691 conf->quiesce = 0;
5692 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08005693 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005694 spin_unlock_irq(&conf->device_lock);
5695 break;
5696 }
NeilBrown72626682005-09-09 16:23:54 -07005697}
NeilBrownb15c2e52006-01-06 00:20:16 -08005698
NeilBrownd562b0c2009-03-31 14:39:39 +11005699
NeilBrownfd01b882011-10-11 16:47:53 +11005700static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11005701{
NeilBrowne373ab12011-10-11 16:48:59 +11005702 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07005703 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11005704
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005705 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11005706 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10005707 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
5708 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005709 return ERR_PTR(-EINVAL);
5710 }
5711
NeilBrowne373ab12011-10-11 16:48:59 +11005712 sectors = raid0_conf->strip_zone[0].zone_end;
5713 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10005714 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005715 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11005716 mddev->new_layout = ALGORITHM_PARITY_N;
5717 mddev->new_chunk_sectors = mddev->chunk_sectors;
5718 mddev->raid_disks += 1;
5719 mddev->delta_disks = 1;
5720 /* make sure it will be not marked as dirty */
5721 mddev->recovery_cp = MaxSector;
5722
5723 return setup_conf(mddev);
5724}
5725
5726
NeilBrownfd01b882011-10-11 16:47:53 +11005727static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005728{
5729 int chunksect;
5730
5731 if (mddev->raid_disks != 2 ||
5732 mddev->degraded > 1)
5733 return ERR_PTR(-EINVAL);
5734
5735 /* Should check if there are write-behind devices? */
5736
5737 chunksect = 64*2; /* 64K by default */
5738
5739 /* The array must be an exact multiple of chunksize */
5740 while (chunksect && (mddev->array_sectors & (chunksect-1)))
5741 chunksect >>= 1;
5742
5743 if ((chunksect<<9) < STRIPE_SIZE)
5744 /* array size does not allow a suitable chunk size */
5745 return ERR_PTR(-EINVAL);
5746
5747 mddev->new_level = 5;
5748 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10005749 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11005750
5751 return setup_conf(mddev);
5752}
5753
NeilBrownfd01b882011-10-11 16:47:53 +11005754static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11005755{
5756 int new_layout;
5757
5758 switch (mddev->layout) {
5759 case ALGORITHM_LEFT_ASYMMETRIC_6:
5760 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
5761 break;
5762 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5763 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
5764 break;
5765 case ALGORITHM_LEFT_SYMMETRIC_6:
5766 new_layout = ALGORITHM_LEFT_SYMMETRIC;
5767 break;
5768 case ALGORITHM_RIGHT_SYMMETRIC_6:
5769 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
5770 break;
5771 case ALGORITHM_PARITY_0_6:
5772 new_layout = ALGORITHM_PARITY_0;
5773 break;
5774 case ALGORITHM_PARITY_N:
5775 new_layout = ALGORITHM_PARITY_N;
5776 break;
5777 default:
5778 return ERR_PTR(-EINVAL);
5779 }
5780 mddev->new_level = 5;
5781 mddev->new_layout = new_layout;
5782 mddev->delta_disks = -1;
5783 mddev->raid_disks -= 1;
5784 return setup_conf(mddev);
5785}
5786
NeilBrownd562b0c2009-03-31 14:39:39 +11005787
NeilBrownfd01b882011-10-11 16:47:53 +11005788static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11005789{
NeilBrown88ce4932009-03-31 15:24:23 +11005790 /* For a 2-drive array, the layout and chunk size can be changed
5791 * immediately as not restriping is needed.
5792 * For larger arrays we record the new value - after validation
5793 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11005794 */
NeilBrownd1688a62011-10-11 16:49:52 +11005795 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10005796 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11005797
NeilBrown597a7112009-06-18 08:47:42 +10005798 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11005799 return -EINVAL;
5800 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005801 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11005802 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005803 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11005804 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005805 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11005806 /* not factor of array size */
5807 return -EINVAL;
5808 }
5809
5810 /* They look valid */
5811
NeilBrown88ce4932009-03-31 15:24:23 +11005812 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10005813 /* can make the change immediately */
5814 if (mddev->new_layout >= 0) {
5815 conf->algorithm = mddev->new_layout;
5816 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11005817 }
5818 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10005819 conf->chunk_sectors = new_chunk ;
5820 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11005821 }
5822 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5823 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11005824 }
NeilBrown50ac1682009-06-18 08:47:55 +10005825 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11005826}
5827
NeilBrownfd01b882011-10-11 16:47:53 +11005828static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11005829{
NeilBrown597a7112009-06-18 08:47:42 +10005830 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10005831
NeilBrown597a7112009-06-18 08:47:42 +10005832 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11005833 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005834 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005835 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11005836 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005837 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11005838 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005839 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11005840 /* not factor of array size */
5841 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005842 }
NeilBrown88ce4932009-03-31 15:24:23 +11005843
5844 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10005845 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11005846}
5847
NeilBrownfd01b882011-10-11 16:47:53 +11005848static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005849{
5850 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005851 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005852 * raid1 - if there are two drives. We need to know the chunk size
5853 * raid4 - trivial - just use a raid4 layout.
5854 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005855 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005856 if (mddev->level == 0)
5857 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11005858 if (mddev->level == 1)
5859 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11005860 if (mddev->level == 4) {
5861 mddev->new_layout = ALGORITHM_PARITY_N;
5862 mddev->new_level = 5;
5863 return setup_conf(mddev);
5864 }
NeilBrownfc9739c2009-03-31 14:57:20 +11005865 if (mddev->level == 6)
5866 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11005867
5868 return ERR_PTR(-EINVAL);
5869}
5870
NeilBrownfd01b882011-10-11 16:47:53 +11005871static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11005872{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005873 /* raid4 can take over:
5874 * raid0 - if there is only one strip zone
5875 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11005876 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005877 if (mddev->level == 0)
5878 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11005879 if (mddev->level == 5 &&
5880 mddev->layout == ALGORITHM_PARITY_N) {
5881 mddev->new_layout = 0;
5882 mddev->new_level = 4;
5883 return setup_conf(mddev);
5884 }
5885 return ERR_PTR(-EINVAL);
5886}
NeilBrownd562b0c2009-03-31 14:39:39 +11005887
NeilBrown84fc4b52011-10-11 16:49:58 +11005888static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11005889
NeilBrownfd01b882011-10-11 16:47:53 +11005890static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11005891{
5892 /* Currently can only take over a raid5. We map the
5893 * personality to an equivalent raid6 personality
5894 * with the Q block at the end.
5895 */
5896 int new_layout;
5897
5898 if (mddev->pers != &raid5_personality)
5899 return ERR_PTR(-EINVAL);
5900 if (mddev->degraded > 1)
5901 return ERR_PTR(-EINVAL);
5902 if (mddev->raid_disks > 253)
5903 return ERR_PTR(-EINVAL);
5904 if (mddev->raid_disks < 3)
5905 return ERR_PTR(-EINVAL);
5906
5907 switch (mddev->layout) {
5908 case ALGORITHM_LEFT_ASYMMETRIC:
5909 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
5910 break;
5911 case ALGORITHM_RIGHT_ASYMMETRIC:
5912 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
5913 break;
5914 case ALGORITHM_LEFT_SYMMETRIC:
5915 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
5916 break;
5917 case ALGORITHM_RIGHT_SYMMETRIC:
5918 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
5919 break;
5920 case ALGORITHM_PARITY_0:
5921 new_layout = ALGORITHM_PARITY_0_6;
5922 break;
5923 case ALGORITHM_PARITY_N:
5924 new_layout = ALGORITHM_PARITY_N;
5925 break;
5926 default:
5927 return ERR_PTR(-EINVAL);
5928 }
5929 mddev->new_level = 6;
5930 mddev->new_layout = new_layout;
5931 mddev->delta_disks = 1;
5932 mddev->raid_disks += 1;
5933 return setup_conf(mddev);
5934}
5935
5936
NeilBrown84fc4b52011-10-11 16:49:58 +11005937static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07005938{
5939 .name = "raid6",
5940 .level = 6,
5941 .owner = THIS_MODULE,
5942 .make_request = make_request,
5943 .run = run,
5944 .stop = stop,
5945 .status = status,
5946 .error_handler = error,
5947 .hot_add_disk = raid5_add_disk,
5948 .hot_remove_disk= raid5_remove_disk,
5949 .spare_active = raid5_spare_active,
5950 .sync_request = sync_request,
5951 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005952 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10005953 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08005954 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005955 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07005956 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11005957 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07005958};
NeilBrown84fc4b52011-10-11 16:49:58 +11005959static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960{
5961 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08005962 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963 .owner = THIS_MODULE,
5964 .make_request = make_request,
5965 .run = run,
5966 .stop = stop,
5967 .status = status,
5968 .error_handler = error,
5969 .hot_add_disk = raid5_add_disk,
5970 .hot_remove_disk= raid5_remove_disk,
5971 .spare_active = raid5_spare_active,
5972 .sync_request = sync_request,
5973 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005974 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08005975 .check_reshape = raid5_check_reshape,
5976 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005977 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07005978 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11005979 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980};
5981
NeilBrown84fc4b52011-10-11 16:49:58 +11005982static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983{
NeilBrown2604b702006-01-06 00:20:36 -08005984 .name = "raid4",
5985 .level = 4,
5986 .owner = THIS_MODULE,
5987 .make_request = make_request,
5988 .run = run,
5989 .stop = stop,
5990 .status = status,
5991 .error_handler = error,
5992 .hot_add_disk = raid5_add_disk,
5993 .hot_remove_disk= raid5_remove_disk,
5994 .spare_active = raid5_spare_active,
5995 .sync_request = sync_request,
5996 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005997 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08005998 .check_reshape = raid5_check_reshape,
5999 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006000 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08006001 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11006002 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08006003};
6004
6005static int __init raid5_init(void)
6006{
NeilBrown16a53ec2006-06-26 00:27:38 -07006007 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006008 register_md_personality(&raid5_personality);
6009 register_md_personality(&raid4_personality);
6010 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011}
6012
NeilBrown2604b702006-01-06 00:20:36 -08006013static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014{
NeilBrown16a53ec2006-06-26 00:27:38 -07006015 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006016 unregister_md_personality(&raid5_personality);
6017 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018}
6019
6020module_init(raid5_init);
6021module_exit(raid5_exit);
6022MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11006023MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08006025MODULE_ALIAS("md-raid5");
6026MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08006027MODULE_ALIAS("md-level-5");
6028MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07006029MODULE_ALIAS("md-personality-8"); /* RAID6 */
6030MODULE_ALIAS("md-raid6");
6031MODULE_ALIAS("md-level-6");
6032
6033/* This used to be two separate modules, they were: */
6034MODULE_ALIAS("raid5");
6035MODULE_ALIAS("raid6");