blob: 62b6b3a83abf7f7bfab954b11f1a3087a3ec3f8b [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
NeilBrown05616be2012-05-21 09:27:00 +1000491/* Determine if 'data_offset' or 'new_data_offset' should be used
492 * in this stripe_head.
493 */
494static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
495{
496 sector_t progress = conf->reshape_progress;
497 /* Need a memory barrier to make sure we see the value
498 * of conf->generation, or ->data_offset that was set before
499 * reshape_progress was updated.
500 */
501 smp_rmb();
502 if (progress == MaxSector)
503 return 0;
504 if (sh->generation == conf->generation - 1)
505 return 0;
506 /* We are in a reshape, and this is a new-generation stripe,
507 * so use new_data_offset.
508 */
509 return 1;
510}
511
NeilBrown6712ecf2007-09-27 12:47:43 +0200512static void
513raid5_end_read_request(struct bio *bi, int error);
514static void
515raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700516
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000517static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700518{
NeilBrownd1688a62011-10-11 16:49:52 +1100519 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700520 int i, disks = sh->disks;
521
522 might_sleep();
523
524 for (i = disks; i--; ) {
525 int rw;
NeilBrown9a3e1102011-12-23 10:17:53 +1100526 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100527 struct bio *bi, *rbi;
528 struct md_rdev *rdev, *rrdev = NULL;
Tejun Heoe9c74692010-09-03 11:56:18 +0200529 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
530 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
531 rw = WRITE_FUA;
532 else
533 rw = WRITE;
534 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700535 rw = READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100536 else if (test_and_clear_bit(R5_WantReplace,
537 &sh->dev[i].flags)) {
538 rw = WRITE;
539 replace_only = 1;
540 } else
Dan Williams91c00922007-01-02 13:52:30 -0700541 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +1000542 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
543 rw |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -0700544
545 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100546 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700547
548 bi->bi_rw = rw;
NeilBrown977df362011-12-23 10:17:53 +1100549 rbi->bi_rw = rw;
550 if (rw & WRITE) {
Dan Williams91c00922007-01-02 13:52:30 -0700551 bi->bi_end_io = raid5_end_write_request;
NeilBrown977df362011-12-23 10:17:53 +1100552 rbi->bi_end_io = raid5_end_write_request;
553 } else
Dan Williams91c00922007-01-02 13:52:30 -0700554 bi->bi_end_io = raid5_end_read_request;
555
556 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100557 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100558 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
559 rdev = rcu_dereference(conf->disks[i].rdev);
560 if (!rdev) {
561 rdev = rrdev;
562 rrdev = NULL;
563 }
NeilBrown9a3e1102011-12-23 10:17:53 +1100564 if (rw & WRITE) {
565 if (replace_only)
566 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100567 if (rdev == rrdev)
568 /* We raced and saw duplicates */
569 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100570 } else {
NeilBrowndd054fc2011-12-23 10:17:53 +1100571 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100572 rdev = rrdev;
573 rrdev = NULL;
574 }
NeilBrown977df362011-12-23 10:17:53 +1100575
Dan Williams91c00922007-01-02 13:52:30 -0700576 if (rdev && test_bit(Faulty, &rdev->flags))
577 rdev = NULL;
578 if (rdev)
579 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100580 if (rrdev && test_bit(Faulty, &rrdev->flags))
581 rrdev = NULL;
582 if (rrdev)
583 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700584 rcu_read_unlock();
585
NeilBrown73e92e52011-07-28 11:39:22 +1000586 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100587 * need to check for writes. We never accept write errors
588 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000589 */
590 while ((rw & WRITE) && rdev &&
591 test_bit(WriteErrorSeen, &rdev->flags)) {
592 sector_t first_bad;
593 int bad_sectors;
594 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
595 &first_bad, &bad_sectors);
596 if (!bad)
597 break;
598
599 if (bad < 0) {
600 set_bit(BlockedBadBlocks, &rdev->flags);
601 if (!conf->mddev->external &&
602 conf->mddev->flags) {
603 /* It is very unlikely, but we might
604 * still need to write out the
605 * bad block log - better give it
606 * a chance*/
607 md_check_recovery(conf->mddev);
608 }
majianpeng18507532012-07-03 12:11:54 +1000609 /*
610 * Because md_wait_for_blocked_rdev
611 * will dec nr_pending, we must
612 * increment it first.
613 */
614 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +1000615 md_wait_for_blocked_rdev(rdev, conf->mddev);
616 } else {
617 /* Acknowledged bad block - skip the write */
618 rdev_dec_pending(rdev, conf->mddev);
619 rdev = NULL;
620 }
621 }
622
Dan Williams91c00922007-01-02 13:52:30 -0700623 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100624 if (s->syncing || s->expanding || s->expanded
625 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700626 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
627
Dan Williams2b7497f2008-06-28 08:31:52 +1000628 set_bit(STRIPE_IO_STARTED, &sh->state);
629
Dan Williams91c00922007-01-02 13:52:30 -0700630 bi->bi_bdev = rdev->bdev;
631 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700632 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700633 bi->bi_rw, i);
634 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000635 if (use_new_offset(conf, sh))
636 bi->bi_sector = (sh->sector
637 + rdev->new_data_offset);
638 else
639 bi->bi_sector = (sh->sector
640 + rdev->data_offset);
Dan Williams91c00922007-01-02 13:52:30 -0700641 bi->bi_flags = 1 << BIO_UPTODATE;
Dan Williams91c00922007-01-02 13:52:30 -0700642 bi->bi_idx = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700643 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
644 bi->bi_io_vec[0].bv_offset = 0;
645 bi->bi_size = STRIPE_SIZE;
646 bi->bi_next = NULL;
NeilBrown977df362011-12-23 10:17:53 +1100647 if (rrdev)
648 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Dan Williams91c00922007-01-02 13:52:30 -0700649 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100650 }
651 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100652 if (s->syncing || s->expanding || s->expanded
653 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +1100654 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
655
656 set_bit(STRIPE_IO_STARTED, &sh->state);
657
658 rbi->bi_bdev = rrdev->bdev;
659 pr_debug("%s: for %llu schedule op %ld on "
660 "replacement disc %d\n",
661 __func__, (unsigned long long)sh->sector,
662 rbi->bi_rw, i);
663 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000664 if (use_new_offset(conf, sh))
665 rbi->bi_sector = (sh->sector
666 + rrdev->new_data_offset);
667 else
668 rbi->bi_sector = (sh->sector
669 + rrdev->data_offset);
NeilBrown977df362011-12-23 10:17:53 +1100670 rbi->bi_flags = 1 << BIO_UPTODATE;
671 rbi->bi_idx = 0;
672 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
673 rbi->bi_io_vec[0].bv_offset = 0;
674 rbi->bi_size = STRIPE_SIZE;
675 rbi->bi_next = NULL;
676 generic_make_request(rbi);
677 }
678 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000679 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700680 set_bit(STRIPE_DEGRADED, &sh->state);
681 pr_debug("skip op %ld on disc %d for sector %llu\n",
682 bi->bi_rw, i, (unsigned long long)sh->sector);
683 clear_bit(R5_LOCKED, &sh->dev[i].flags);
684 set_bit(STRIPE_HANDLE, &sh->state);
685 }
686 }
687}
688
689static struct dma_async_tx_descriptor *
690async_copy_data(int frombio, struct bio *bio, struct page *page,
691 sector_t sector, struct dma_async_tx_descriptor *tx)
692{
693 struct bio_vec *bvl;
694 struct page *bio_page;
695 int i;
696 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700697 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700698 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700699
700 if (bio->bi_sector >= sector)
701 page_offset = (signed)(bio->bi_sector - sector) * 512;
702 else
703 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700704
Dan Williams0403e382009-09-08 17:42:50 -0700705 if (frombio)
706 flags |= ASYNC_TX_FENCE;
707 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
708
Dan Williams91c00922007-01-02 13:52:30 -0700709 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000710 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700711 int clen;
712 int b_offset = 0;
713
714 if (page_offset < 0) {
715 b_offset = -page_offset;
716 page_offset += b_offset;
717 len -= b_offset;
718 }
719
720 if (len > 0 && page_offset + len > STRIPE_SIZE)
721 clen = STRIPE_SIZE - page_offset;
722 else
723 clen = len;
724
725 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000726 b_offset += bvl->bv_offset;
727 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700728 if (frombio)
729 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700730 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700731 else
732 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700733 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700734 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700735 /* chain the operations */
736 submit.depend_tx = tx;
737
Dan Williams91c00922007-01-02 13:52:30 -0700738 if (clen < len) /* hit end of page */
739 break;
740 page_offset += len;
741 }
742
743 return tx;
744}
745
746static void ops_complete_biofill(void *stripe_head_ref)
747{
748 struct stripe_head *sh = stripe_head_ref;
749 struct bio *return_bi = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +1100750 struct r5conf *conf = sh->raid_conf;
Dan Williamse4d84902007-09-24 10:06:13 -0700751 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700752
Harvey Harrisone46b2722008-04-28 02:15:50 -0700753 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700754 (unsigned long long)sh->sector);
755
756 /* clear completed biofills */
Dan Williams83de75c2008-06-28 08:31:58 +1000757 spin_lock_irq(&conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700758 for (i = sh->disks; i--; ) {
759 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700760
761 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700762 /* and check if we need to reply to a read request,
763 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000764 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700765 */
766 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700767 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700768
Dan Williams91c00922007-01-02 13:52:30 -0700769 BUG_ON(!dev->read);
770 rbi = dev->read;
771 dev->read = NULL;
772 while (rbi && rbi->bi_sector <
773 dev->sector + STRIPE_SECTORS) {
774 rbi2 = r5_next_bio(rbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +0200775 if (!raid5_dec_bi_phys_segments(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700776 rbi->bi_next = return_bi;
777 return_bi = rbi;
778 }
Dan Williams91c00922007-01-02 13:52:30 -0700779 rbi = rbi2;
780 }
781 }
782 }
Dan Williams83de75c2008-06-28 08:31:58 +1000783 spin_unlock_irq(&conf->device_lock);
784 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700785
786 return_io(return_bi);
787
Dan Williamse4d84902007-09-24 10:06:13 -0700788 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700789 release_stripe(sh);
790}
791
792static void ops_run_biofill(struct stripe_head *sh)
793{
794 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +1100795 struct r5conf *conf = sh->raid_conf;
Dan Williamsa08abd82009-06-03 11:43:59 -0700796 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700797 int i;
798
Harvey Harrisone46b2722008-04-28 02:15:50 -0700799 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700800 (unsigned long long)sh->sector);
801
802 for (i = sh->disks; i--; ) {
803 struct r5dev *dev = &sh->dev[i];
804 if (test_bit(R5_Wantfill, &dev->flags)) {
805 struct bio *rbi;
806 spin_lock_irq(&conf->device_lock);
807 dev->read = rbi = dev->toread;
808 dev->toread = NULL;
809 spin_unlock_irq(&conf->device_lock);
810 while (rbi && rbi->bi_sector <
811 dev->sector + STRIPE_SECTORS) {
812 tx = async_copy_data(0, rbi, dev->page,
813 dev->sector, tx);
814 rbi = r5_next_bio(rbi, dev->sector);
815 }
816 }
817 }
818
819 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700820 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
821 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700822}
823
Dan Williams4e7d2c02009-08-29 19:13:11 -0700824static void mark_target_uptodate(struct stripe_head *sh, int target)
825{
826 struct r5dev *tgt;
827
828 if (target < 0)
829 return;
830
831 tgt = &sh->dev[target];
832 set_bit(R5_UPTODATE, &tgt->flags);
833 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
834 clear_bit(R5_Wantcompute, &tgt->flags);
835}
836
Dan Williamsac6b53b2009-07-14 13:40:19 -0700837static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700838{
839 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700840
Harvey Harrisone46b2722008-04-28 02:15:50 -0700841 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700842 (unsigned long long)sh->sector);
843
Dan Williamsac6b53b2009-07-14 13:40:19 -0700844 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700845 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700846 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700847
Dan Williamsecc65c92008-06-28 08:31:57 +1000848 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
849 if (sh->check_state == check_state_compute_run)
850 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700851 set_bit(STRIPE_HANDLE, &sh->state);
852 release_stripe(sh);
853}
854
Dan Williamsd6f38f32009-07-14 11:50:52 -0700855/* return a pointer to the address conversion region of the scribble buffer */
856static addr_conv_t *to_addr_conv(struct stripe_head *sh,
857 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700858{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700859 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
860}
861
862static struct dma_async_tx_descriptor *
863ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
864{
Dan Williams91c00922007-01-02 13:52:30 -0700865 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700866 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700867 int target = sh->ops.target;
868 struct r5dev *tgt = &sh->dev[target];
869 struct page *xor_dest = tgt->page;
870 int count = 0;
871 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700872 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700873 int i;
874
875 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700876 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700877 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
878
879 for (i = disks; i--; )
880 if (i != target)
881 xor_srcs[count++] = sh->dev[i].page;
882
883 atomic_inc(&sh->count);
884
Dan Williams0403e382009-09-08 17:42:50 -0700885 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700886 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700887 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700888 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700889 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700890 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700891
Dan Williams91c00922007-01-02 13:52:30 -0700892 return tx;
893}
894
Dan Williamsac6b53b2009-07-14 13:40:19 -0700895/* set_syndrome_sources - populate source buffers for gen_syndrome
896 * @srcs - (struct page *) array of size sh->disks
897 * @sh - stripe_head to parse
898 *
899 * Populates srcs in proper layout order for the stripe and returns the
900 * 'count' of sources to be used in a call to async_gen_syndrome. The P
901 * destination buffer is recorded in srcs[count] and the Q destination
902 * is recorded in srcs[count+1]].
903 */
904static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
905{
906 int disks = sh->disks;
907 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
908 int d0_idx = raid6_d0(sh);
909 int count;
910 int i;
911
912 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100913 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700914
915 count = 0;
916 i = d0_idx;
917 do {
918 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
919
920 srcs[slot] = sh->dev[i].page;
921 i = raid6_next_disk(i, disks);
922 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700923
NeilBrowne4424fe2009-10-16 16:27:34 +1100924 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700925}
926
927static struct dma_async_tx_descriptor *
928ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
929{
930 int disks = sh->disks;
931 struct page **blocks = percpu->scribble;
932 int target;
933 int qd_idx = sh->qd_idx;
934 struct dma_async_tx_descriptor *tx;
935 struct async_submit_ctl submit;
936 struct r5dev *tgt;
937 struct page *dest;
938 int i;
939 int count;
940
941 if (sh->ops.target < 0)
942 target = sh->ops.target2;
943 else if (sh->ops.target2 < 0)
944 target = sh->ops.target;
945 else
946 /* we should only have one valid target */
947 BUG();
948 BUG_ON(target < 0);
949 pr_debug("%s: stripe %llu block: %d\n",
950 __func__, (unsigned long long)sh->sector, target);
951
952 tgt = &sh->dev[target];
953 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
954 dest = tgt->page;
955
956 atomic_inc(&sh->count);
957
958 if (target == qd_idx) {
959 count = set_syndrome_sources(blocks, sh);
960 blocks[count] = NULL; /* regenerating p is not necessary */
961 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700962 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
963 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700964 to_addr_conv(sh, percpu));
965 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
966 } else {
967 /* Compute any data- or p-drive using XOR */
968 count = 0;
969 for (i = disks; i-- ; ) {
970 if (i == target || i == qd_idx)
971 continue;
972 blocks[count++] = sh->dev[i].page;
973 }
974
Dan Williams0403e382009-09-08 17:42:50 -0700975 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
976 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700977 to_addr_conv(sh, percpu));
978 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
979 }
980
981 return tx;
982}
983
984static struct dma_async_tx_descriptor *
985ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
986{
987 int i, count, disks = sh->disks;
988 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
989 int d0_idx = raid6_d0(sh);
990 int faila = -1, failb = -1;
991 int target = sh->ops.target;
992 int target2 = sh->ops.target2;
993 struct r5dev *tgt = &sh->dev[target];
994 struct r5dev *tgt2 = &sh->dev[target2];
995 struct dma_async_tx_descriptor *tx;
996 struct page **blocks = percpu->scribble;
997 struct async_submit_ctl submit;
998
999 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1000 __func__, (unsigned long long)sh->sector, target, target2);
1001 BUG_ON(target < 0 || target2 < 0);
1002 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1003 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1004
Dan Williams6c910a72009-09-16 12:24:54 -07001005 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001006 * slot number conversion for 'faila' and 'failb'
1007 */
1008 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001009 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001010 count = 0;
1011 i = d0_idx;
1012 do {
1013 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1014
1015 blocks[slot] = sh->dev[i].page;
1016
1017 if (i == target)
1018 faila = slot;
1019 if (i == target2)
1020 failb = slot;
1021 i = raid6_next_disk(i, disks);
1022 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001023
1024 BUG_ON(faila == failb);
1025 if (failb < faila)
1026 swap(faila, failb);
1027 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1028 __func__, (unsigned long long)sh->sector, faila, failb);
1029
1030 atomic_inc(&sh->count);
1031
1032 if (failb == syndrome_disks+1) {
1033 /* Q disk is one of the missing disks */
1034 if (faila == syndrome_disks) {
1035 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001036 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1037 ops_complete_compute, sh,
1038 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +11001039 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001040 STRIPE_SIZE, &submit);
1041 } else {
1042 struct page *dest;
1043 int data_target;
1044 int qd_idx = sh->qd_idx;
1045
1046 /* Missing D+Q: recompute D from P, then recompute Q */
1047 if (target == qd_idx)
1048 data_target = target2;
1049 else
1050 data_target = target;
1051
1052 count = 0;
1053 for (i = disks; i-- ; ) {
1054 if (i == data_target || i == qd_idx)
1055 continue;
1056 blocks[count++] = sh->dev[i].page;
1057 }
1058 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001059 init_async_submit(&submit,
1060 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1061 NULL, NULL, NULL,
1062 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001063 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1064 &submit);
1065
1066 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001067 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1068 ops_complete_compute, sh,
1069 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001070 return async_gen_syndrome(blocks, 0, count+2,
1071 STRIPE_SIZE, &submit);
1072 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001073 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001074 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1075 ops_complete_compute, sh,
1076 to_addr_conv(sh, percpu));
1077 if (failb == syndrome_disks) {
1078 /* We're missing D+P. */
1079 return async_raid6_datap_recov(syndrome_disks+2,
1080 STRIPE_SIZE, faila,
1081 blocks, &submit);
1082 } else {
1083 /* We're missing D+D. */
1084 return async_raid6_2data_recov(syndrome_disks+2,
1085 STRIPE_SIZE, faila, failb,
1086 blocks, &submit);
1087 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001088 }
1089}
1090
1091
Dan Williams91c00922007-01-02 13:52:30 -07001092static void ops_complete_prexor(void *stripe_head_ref)
1093{
1094 struct stripe_head *sh = stripe_head_ref;
1095
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);
Dan Williams91c00922007-01-02 13:52:30 -07001098}
1099
1100static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001101ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1102 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001103{
Dan Williams91c00922007-01-02 13:52:30 -07001104 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001105 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001106 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001107 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001108
1109 /* existing parity data subtracted */
1110 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1111
Harvey Harrisone46b2722008-04-28 02:15:50 -07001112 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001113 (unsigned long long)sh->sector);
1114
1115 for (i = disks; i--; ) {
1116 struct r5dev *dev = &sh->dev[i];
1117 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001118 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001119 xor_srcs[count++] = dev->page;
1120 }
1121
Dan Williams0403e382009-09-08 17:42:50 -07001122 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001123 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001124 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001125
1126 return tx;
1127}
1128
1129static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001130ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001131{
1132 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001133 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001134
Harvey Harrisone46b2722008-04-28 02:15:50 -07001135 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001136 (unsigned long long)sh->sector);
1137
1138 for (i = disks; i--; ) {
1139 struct r5dev *dev = &sh->dev[i];
1140 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001141
Dan Williamsd8ee0722008-06-28 08:32:06 +10001142 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001143 struct bio *wbi;
1144
NeilBrowncbe47ec2011-07-26 11:20:35 +10001145 spin_lock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001146 chosen = dev->towrite;
1147 dev->towrite = NULL;
1148 BUG_ON(dev->written);
1149 wbi = dev->written = chosen;
NeilBrowncbe47ec2011-07-26 11:20:35 +10001150 spin_unlock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001151
1152 while (wbi && wbi->bi_sector <
1153 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001154 if (wbi->bi_rw & REQ_FUA)
1155 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001156 if (wbi->bi_rw & REQ_SYNC)
1157 set_bit(R5_SyncIO, &dev->flags);
Dan Williams91c00922007-01-02 13:52:30 -07001158 tx = async_copy_data(1, wbi, dev->page,
1159 dev->sector, tx);
1160 wbi = r5_next_bio(wbi, dev->sector);
1161 }
1162 }
1163 }
1164
1165 return tx;
1166}
1167
Dan Williamsac6b53b2009-07-14 13:40:19 -07001168static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001169{
1170 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001171 int disks = sh->disks;
1172 int pd_idx = sh->pd_idx;
1173 int qd_idx = sh->qd_idx;
1174 int i;
Shaohua Libc0934f2012-05-22 13:55:05 +10001175 bool fua = false, sync = false;
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
Shaohua Libc0934f2012-05-22 13:55:05 +10001180 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001181 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001182 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
1183 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001184
Dan Williams91c00922007-01-02 13:52:30 -07001185 for (i = disks; i--; ) {
1186 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001187
Tejun Heoe9c74692010-09-03 11:56:18 +02001188 if (dev->written || i == pd_idx || i == qd_idx) {
Dan Williams91c00922007-01-02 13:52:30 -07001189 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001190 if (fua)
1191 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001192 if (sync)
1193 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001194 }
Dan Williams91c00922007-01-02 13:52:30 -07001195 }
1196
Dan Williamsd8ee0722008-06-28 08:32:06 +10001197 if (sh->reconstruct_state == reconstruct_state_drain_run)
1198 sh->reconstruct_state = reconstruct_state_drain_result;
1199 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1200 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1201 else {
1202 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1203 sh->reconstruct_state = reconstruct_state_result;
1204 }
Dan Williams91c00922007-01-02 13:52:30 -07001205
1206 set_bit(STRIPE_HANDLE, &sh->state);
1207 release_stripe(sh);
1208}
1209
1210static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001211ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1212 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001213{
Dan Williams91c00922007-01-02 13:52:30 -07001214 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001215 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001216 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001217 int count = 0, pd_idx = sh->pd_idx, i;
1218 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001219 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001220 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001221
Harvey Harrisone46b2722008-04-28 02:15:50 -07001222 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001223 (unsigned long long)sh->sector);
1224
1225 /* check if prexor is active which means only process blocks
1226 * that are part of a read-modify-write (written)
1227 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001228 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1229 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001230 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1231 for (i = disks; i--; ) {
1232 struct r5dev *dev = &sh->dev[i];
1233 if (dev->written)
1234 xor_srcs[count++] = dev->page;
1235 }
1236 } else {
1237 xor_dest = sh->dev[pd_idx].page;
1238 for (i = disks; i--; ) {
1239 struct r5dev *dev = &sh->dev[i];
1240 if (i != pd_idx)
1241 xor_srcs[count++] = dev->page;
1242 }
1243 }
1244
Dan Williams91c00922007-01-02 13:52:30 -07001245 /* 1/ if we prexor'd then the dest is reused as a source
1246 * 2/ if we did not prexor then we are redoing the parity
1247 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1248 * for the synchronous xor case
1249 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001250 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001251 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1252
1253 atomic_inc(&sh->count);
1254
Dan Williamsac6b53b2009-07-14 13:40:19 -07001255 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001256 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001257 if (unlikely(count == 1))
1258 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1259 else
1260 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001261}
1262
Dan Williamsac6b53b2009-07-14 13:40:19 -07001263static void
1264ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1265 struct dma_async_tx_descriptor *tx)
1266{
1267 struct async_submit_ctl submit;
1268 struct page **blocks = percpu->scribble;
1269 int count;
1270
1271 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1272
1273 count = set_syndrome_sources(blocks, sh);
1274
1275 atomic_inc(&sh->count);
1276
1277 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1278 sh, to_addr_conv(sh, percpu));
1279 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001280}
1281
1282static void ops_complete_check(void *stripe_head_ref)
1283{
1284 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001285
Harvey Harrisone46b2722008-04-28 02:15:50 -07001286 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001287 (unsigned long long)sh->sector);
1288
Dan Williamsecc65c92008-06-28 08:31:57 +10001289 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001290 set_bit(STRIPE_HANDLE, &sh->state);
1291 release_stripe(sh);
1292}
1293
Dan Williamsac6b53b2009-07-14 13:40:19 -07001294static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001295{
Dan Williams91c00922007-01-02 13:52:30 -07001296 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001297 int pd_idx = sh->pd_idx;
1298 int qd_idx = sh->qd_idx;
1299 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001300 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001301 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001302 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001303 int count;
1304 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001305
Harvey Harrisone46b2722008-04-28 02:15:50 -07001306 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001307 (unsigned long long)sh->sector);
1308
Dan Williamsac6b53b2009-07-14 13:40:19 -07001309 count = 0;
1310 xor_dest = sh->dev[pd_idx].page;
1311 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001312 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001313 if (i == pd_idx || i == qd_idx)
1314 continue;
1315 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001316 }
1317
Dan Williamsd6f38f32009-07-14 11:50:52 -07001318 init_async_submit(&submit, 0, NULL, NULL, NULL,
1319 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001320 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001321 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001322
Dan Williams91c00922007-01-02 13:52:30 -07001323 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001324 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1325 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001326}
1327
Dan Williamsac6b53b2009-07-14 13:40:19 -07001328static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1329{
1330 struct page **srcs = percpu->scribble;
1331 struct async_submit_ctl submit;
1332 int count;
1333
1334 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1335 (unsigned long long)sh->sector, checkp);
1336
1337 count = set_syndrome_sources(srcs, sh);
1338 if (!checkp)
1339 srcs[count] = NULL;
1340
1341 atomic_inc(&sh->count);
1342 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1343 sh, to_addr_conv(sh, percpu));
1344 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1345 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1346}
1347
Dan Williams417b8d42009-10-16 16:25:22 +11001348static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001349{
1350 int overlap_clear = 0, i, disks = sh->disks;
1351 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001352 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001353 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001354 struct raid5_percpu *percpu;
1355 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001356
Dan Williamsd6f38f32009-07-14 11:50:52 -07001357 cpu = get_cpu();
1358 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001359 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001360 ops_run_biofill(sh);
1361 overlap_clear++;
1362 }
1363
Dan Williams7b3a8712008-06-28 08:32:09 +10001364 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001365 if (level < 6)
1366 tx = ops_run_compute5(sh, percpu);
1367 else {
1368 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1369 tx = ops_run_compute6_1(sh, percpu);
1370 else
1371 tx = ops_run_compute6_2(sh, percpu);
1372 }
1373 /* terminate the chain if reconstruct is not set to be run */
1374 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001375 async_tx_ack(tx);
1376 }
Dan Williams91c00922007-01-02 13:52:30 -07001377
Dan Williams600aa102008-06-28 08:32:05 +10001378 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001379 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001380
Dan Williams600aa102008-06-28 08:32:05 +10001381 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001382 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001383 overlap_clear++;
1384 }
1385
Dan Williamsac6b53b2009-07-14 13:40:19 -07001386 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1387 if (level < 6)
1388 ops_run_reconstruct5(sh, percpu, tx);
1389 else
1390 ops_run_reconstruct6(sh, percpu, tx);
1391 }
Dan Williams91c00922007-01-02 13:52:30 -07001392
Dan Williamsac6b53b2009-07-14 13:40:19 -07001393 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1394 if (sh->check_state == check_state_run)
1395 ops_run_check_p(sh, percpu);
1396 else if (sh->check_state == check_state_run_q)
1397 ops_run_check_pq(sh, percpu, 0);
1398 else if (sh->check_state == check_state_run_pq)
1399 ops_run_check_pq(sh, percpu, 1);
1400 else
1401 BUG();
1402 }
Dan Williams91c00922007-01-02 13:52:30 -07001403
Dan Williams91c00922007-01-02 13:52:30 -07001404 if (overlap_clear)
1405 for (i = disks; i--; ) {
1406 struct r5dev *dev = &sh->dev[i];
1407 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1408 wake_up(&sh->raid_conf->wait_for_overlap);
1409 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001410 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001411}
1412
Dan Williams417b8d42009-10-16 16:25:22 +11001413#ifdef CONFIG_MULTICORE_RAID456
1414static void async_run_ops(void *param, async_cookie_t cookie)
1415{
1416 struct stripe_head *sh = param;
1417 unsigned long ops_request = sh->ops.request;
1418
1419 clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
1420 wake_up(&sh->ops.wait_for_ops);
1421
1422 __raid_run_ops(sh, ops_request);
1423 release_stripe(sh);
1424}
1425
1426static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1427{
1428 /* since handle_stripe can be called outside of raid5d context
1429 * we need to ensure sh->ops.request is de-staged before another
1430 * request arrives
1431 */
1432 wait_event(sh->ops.wait_for_ops,
1433 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
1434 sh->ops.request = ops_request;
1435
1436 atomic_inc(&sh->count);
1437 async_schedule(async_run_ops, sh);
1438}
1439#else
1440#define raid_run_ops __raid_run_ops
1441#endif
1442
NeilBrownd1688a62011-10-11 16:49:52 +11001443static int grow_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
1445 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001446 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001447 if (!sh)
1448 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001449
NeilBrown3f294f42005-11-08 21:39:25 -08001450 sh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001451 #ifdef CONFIG_MULTICORE_RAID456
1452 init_waitqueue_head(&sh->ops.wait_for_ops);
1453 #endif
NeilBrown3f294f42005-11-08 21:39:25 -08001454
NeilBrowne4e11e32010-06-16 16:45:16 +10001455 if (grow_buffers(sh)) {
1456 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001457 kmem_cache_free(conf->slab_cache, sh);
1458 return 0;
1459 }
1460 /* we just created an active stripe so... */
1461 atomic_set(&sh->count, 1);
1462 atomic_inc(&conf->active_stripes);
1463 INIT_LIST_HEAD(&sh->lru);
1464 release_stripe(sh);
1465 return 1;
1466}
1467
NeilBrownd1688a62011-10-11 16:49:52 +11001468static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001469{
Christoph Lametere18b8902006-12-06 20:33:20 -08001470 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001471 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
NeilBrownf4be6b42010-06-01 19:37:25 +10001473 if (conf->mddev->gendisk)
1474 sprintf(conf->cache_name[0],
1475 "raid%d-%s", conf->level, mdname(conf->mddev));
1476 else
1477 sprintf(conf->cache_name[0],
1478 "raid%d-%p", conf->level, conf->mddev);
1479 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1480
NeilBrownad01c9e2006-03-27 01:18:07 -08001481 conf->active_name = 0;
1482 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001484 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 if (!sc)
1486 return 1;
1487 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001488 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001489 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001490 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 return 0;
1493}
NeilBrown29269552006-03-27 01:18:10 -08001494
Dan Williamsd6f38f32009-07-14 11:50:52 -07001495/**
1496 * scribble_len - return the required size of the scribble region
1497 * @num - total number of disks in the array
1498 *
1499 * The size must be enough to contain:
1500 * 1/ a struct page pointer for each device in the array +2
1501 * 2/ room to convert each entry in (1) to its corresponding dma
1502 * (dma_map_page()) or page (page_address()) address.
1503 *
1504 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1505 * calculate over all devices (not just the data blocks), using zeros in place
1506 * of the P and Q blocks.
1507 */
1508static size_t scribble_len(int num)
1509{
1510 size_t len;
1511
1512 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1513
1514 return len;
1515}
1516
NeilBrownd1688a62011-10-11 16:49:52 +11001517static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001518{
1519 /* Make all the stripes able to hold 'newsize' devices.
1520 * New slots in each stripe get 'page' set to a new page.
1521 *
1522 * This happens in stages:
1523 * 1/ create a new kmem_cache and allocate the required number of
1524 * stripe_heads.
1525 * 2/ gather all the old stripe_heads and tranfer the pages across
1526 * to the new stripe_heads. This will have the side effect of
1527 * freezing the array as once all stripe_heads have been collected,
1528 * no IO will be possible. Old stripe heads are freed once their
1529 * pages have been transferred over, and the old kmem_cache is
1530 * freed when all stripes are done.
1531 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1532 * we simple return a failre status - no need to clean anything up.
1533 * 4/ allocate new pages for the new slots in the new stripe_heads.
1534 * If this fails, we don't bother trying the shrink the
1535 * stripe_heads down again, we just leave them as they are.
1536 * As each stripe_head is processed the new one is released into
1537 * active service.
1538 *
1539 * Once step2 is started, we cannot afford to wait for a write,
1540 * so we use GFP_NOIO allocations.
1541 */
1542 struct stripe_head *osh, *nsh;
1543 LIST_HEAD(newstripes);
1544 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001545 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001546 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001547 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001548 int i;
1549
1550 if (newsize <= conf->pool_size)
1551 return 0; /* never bother to shrink */
1552
Dan Williamsb5470dc2008-06-27 21:44:04 -07001553 err = md_allow_write(conf->mddev);
1554 if (err)
1555 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001556
NeilBrownad01c9e2006-03-27 01:18:07 -08001557 /* Step 1 */
1558 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1559 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001560 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001561 if (!sc)
1562 return -ENOMEM;
1563
1564 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001565 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001566 if (!nsh)
1567 break;
1568
NeilBrownad01c9e2006-03-27 01:18:07 -08001569 nsh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001570 #ifdef CONFIG_MULTICORE_RAID456
1571 init_waitqueue_head(&nsh->ops.wait_for_ops);
1572 #endif
NeilBrownad01c9e2006-03-27 01:18:07 -08001573
1574 list_add(&nsh->lru, &newstripes);
1575 }
1576 if (i) {
1577 /* didn't get enough, give up */
1578 while (!list_empty(&newstripes)) {
1579 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1580 list_del(&nsh->lru);
1581 kmem_cache_free(sc, nsh);
1582 }
1583 kmem_cache_destroy(sc);
1584 return -ENOMEM;
1585 }
1586 /* Step 2 - Must use GFP_NOIO now.
1587 * OK, we have enough stripes, start collecting inactive
1588 * stripes and copying them over
1589 */
1590 list_for_each_entry(nsh, &newstripes, lru) {
1591 spin_lock_irq(&conf->device_lock);
1592 wait_event_lock_irq(conf->wait_for_stripe,
1593 !list_empty(&conf->inactive_list),
1594 conf->device_lock,
NeilBrown482c0832011-04-18 18:25:42 +10001595 );
NeilBrownad01c9e2006-03-27 01:18:07 -08001596 osh = get_free_stripe(conf);
1597 spin_unlock_irq(&conf->device_lock);
1598 atomic_set(&nsh->count, 1);
1599 for(i=0; i<conf->pool_size; i++)
1600 nsh->dev[i].page = osh->dev[i].page;
1601 for( ; i<newsize; i++)
1602 nsh->dev[i].page = NULL;
1603 kmem_cache_free(conf->slab_cache, osh);
1604 }
1605 kmem_cache_destroy(conf->slab_cache);
1606
1607 /* Step 3.
1608 * At this point, we are holding all the stripes so the array
1609 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001610 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001611 */
1612 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1613 if (ndisks) {
1614 for (i=0; i<conf->raid_disks; i++)
1615 ndisks[i] = conf->disks[i];
1616 kfree(conf->disks);
1617 conf->disks = ndisks;
1618 } else
1619 err = -ENOMEM;
1620
Dan Williamsd6f38f32009-07-14 11:50:52 -07001621 get_online_cpus();
1622 conf->scribble_len = scribble_len(newsize);
1623 for_each_present_cpu(cpu) {
1624 struct raid5_percpu *percpu;
1625 void *scribble;
1626
1627 percpu = per_cpu_ptr(conf->percpu, cpu);
1628 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1629
1630 if (scribble) {
1631 kfree(percpu->scribble);
1632 percpu->scribble = scribble;
1633 } else {
1634 err = -ENOMEM;
1635 break;
1636 }
1637 }
1638 put_online_cpus();
1639
NeilBrownad01c9e2006-03-27 01:18:07 -08001640 /* Step 4, return new stripes to service */
1641 while(!list_empty(&newstripes)) {
1642 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1643 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001644
NeilBrownad01c9e2006-03-27 01:18:07 -08001645 for (i=conf->raid_disks; i < newsize; i++)
1646 if (nsh->dev[i].page == NULL) {
1647 struct page *p = alloc_page(GFP_NOIO);
1648 nsh->dev[i].page = p;
1649 if (!p)
1650 err = -ENOMEM;
1651 }
1652 release_stripe(nsh);
1653 }
1654 /* critical section pass, GFP_NOIO no longer needed */
1655
1656 conf->slab_cache = sc;
1657 conf->active_name = 1-conf->active_name;
1658 conf->pool_size = newsize;
1659 return err;
1660}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
NeilBrownd1688a62011-10-11 16:49:52 +11001662static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663{
1664 struct stripe_head *sh;
1665
NeilBrown3f294f42005-11-08 21:39:25 -08001666 spin_lock_irq(&conf->device_lock);
1667 sh = get_free_stripe(conf);
1668 spin_unlock_irq(&conf->device_lock);
1669 if (!sh)
1670 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001671 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001672 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001673 kmem_cache_free(conf->slab_cache, sh);
1674 atomic_dec(&conf->active_stripes);
1675 return 1;
1676}
1677
NeilBrownd1688a62011-10-11 16:49:52 +11001678static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001679{
1680 while (drop_one_stripe(conf))
1681 ;
1682
NeilBrown29fc7e32006-02-03 03:03:41 -08001683 if (conf->slab_cache)
1684 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 conf->slab_cache = NULL;
1686}
1687
NeilBrown6712ecf2007-09-27 12:47:43 +02001688static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
NeilBrown99c0fb52009-03-31 14:39:38 +11001690 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001691 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001692 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001694 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11001695 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10001696 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
1698 for (i=0 ; i<disks; i++)
1699 if (bi == &sh->dev[i].req)
1700 break;
1701
Dan Williams45b42332007-07-09 11:56:43 -07001702 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1703 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 uptodate);
1705 if (i == disks) {
1706 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001707 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 }
NeilBrown14a75d32011-12-23 10:17:52 +11001709 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11001710 /* If replacement finished while this request was outstanding,
1711 * 'replacement' might be NULL already.
1712 * In that case it moved down to 'rdev'.
1713 * rdev is not removed until all requests are finished.
1714 */
NeilBrown14a75d32011-12-23 10:17:52 +11001715 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001716 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11001717 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
NeilBrown05616be2012-05-21 09:27:00 +10001719 if (use_new_offset(conf, sh))
1720 s = sh->sector + rdev->new_data_offset;
1721 else
1722 s = sh->sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001725 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11001726 /* Note that this cannot happen on a
1727 * replacement device. We just fail those on
1728 * any error
1729 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001730 printk_ratelimited(
1731 KERN_INFO
1732 "md/raid:%s: read error corrected"
1733 " (%lu sectors at %llu on %s)\n",
1734 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10001735 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001736 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001737 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001738 clear_bit(R5_ReadError, &sh->dev[i].flags);
1739 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1740 }
NeilBrown14a75d32011-12-23 10:17:52 +11001741 if (atomic_read(&rdev->read_errors))
1742 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11001744 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001745 int retry = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001748 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11001749 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1750 printk_ratelimited(
1751 KERN_WARNING
1752 "md/raid:%s: read error on replacement device "
1753 "(sector %llu on %s).\n",
1754 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001755 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11001756 bdn);
1757 else if (conf->mddev->degraded >= conf->max_degraded)
Christian Dietrich8bda4702011-07-27 11:00:36 +10001758 printk_ratelimited(
1759 KERN_WARNING
1760 "md/raid:%s: read error not correctable "
1761 "(sector %llu on %s).\n",
1762 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001763 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001764 bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001765 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
NeilBrown4e5314b2005-11-08 21:39:22 -08001766 /* Oh, no!!! */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001767 printk_ratelimited(
1768 KERN_WARNING
1769 "md/raid:%s: read error NOT corrected!! "
1770 "(sector %llu on %s).\n",
1771 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001772 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001773 bdn);
NeilBrownd6950432006-07-10 04:44:20 -07001774 else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001775 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001776 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001777 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001778 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001779 else
1780 retry = 1;
1781 if (retry)
1782 set_bit(R5_ReadError, &sh->dev[i].flags);
1783 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001784 clear_bit(R5_ReadError, &sh->dev[i].flags);
1785 clear_bit(R5_ReWrite, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001786 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 }
NeilBrown14a75d32011-12-23 10:17:52 +11001789 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1791 set_bit(STRIPE_HANDLE, &sh->state);
1792 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793}
1794
NeilBrownd710e132008-10-13 11:55:12 +11001795static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
NeilBrown99c0fb52009-03-31 14:39:38 +11001797 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001798 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001799 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11001800 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001802 sector_t first_bad;
1803 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11001804 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
NeilBrown977df362011-12-23 10:17:53 +11001806 for (i = 0 ; i < disks; i++) {
1807 if (bi == &sh->dev[i].req) {
1808 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 break;
NeilBrown977df362011-12-23 10:17:53 +11001810 }
1811 if (bi == &sh->dev[i].rreq) {
1812 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001813 if (rdev)
1814 replacement = 1;
1815 else
1816 /* rdev was removed and 'replacement'
1817 * replaced it. rdev is not removed
1818 * until all requests are finished.
1819 */
1820 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11001821 break;
1822 }
1823 }
Dan Williams45b42332007-07-09 11:56:43 -07001824 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1826 uptodate);
1827 if (i == disks) {
1828 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001829 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 }
1831
NeilBrown977df362011-12-23 10:17:53 +11001832 if (replacement) {
1833 if (!uptodate)
1834 md_error(conf->mddev, rdev);
1835 else if (is_badblock(rdev, sh->sector,
1836 STRIPE_SECTORS,
1837 &first_bad, &bad_sectors))
1838 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1839 } else {
1840 if (!uptodate) {
1841 set_bit(WriteErrorSeen, &rdev->flags);
1842 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11001843 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1844 set_bit(MD_RECOVERY_NEEDED,
1845 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11001846 } else if (is_badblock(rdev, sh->sector,
1847 STRIPE_SECTORS,
1848 &first_bad, &bad_sectors))
1849 set_bit(R5_MadeGood, &sh->dev[i].flags);
1850 }
1851 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
NeilBrown977df362011-12-23 10:17:53 +11001853 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1854 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001856 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857}
1858
NeilBrown784052e2009-03-31 15:19:07 +11001859static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
NeilBrown784052e2009-03-31 15:19:07 +11001861static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
1863 struct r5dev *dev = &sh->dev[i];
1864
1865 bio_init(&dev->req);
1866 dev->req.bi_io_vec = &dev->vec;
1867 dev->req.bi_vcnt++;
1868 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11001870 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
NeilBrown977df362011-12-23 10:17:53 +11001872 bio_init(&dev->rreq);
1873 dev->rreq.bi_io_vec = &dev->rvec;
1874 dev->rreq.bi_vcnt++;
1875 dev->rreq.bi_max_vecs++;
1876 dev->rreq.bi_private = sh;
1877 dev->rvec.bv_page = dev->page;
1878
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001880 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881}
1882
NeilBrownfd01b882011-10-11 16:47:53 +11001883static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884{
1885 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11001886 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11001887 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10001888 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
NeilBrown908f4fb2011-12-23 10:17:50 +11001890 spin_lock_irqsave(&conf->device_lock, flags);
1891 clear_bit(In_sync, &rdev->flags);
1892 mddev->degraded = calc_degraded(conf);
1893 spin_unlock_irqrestore(&conf->device_lock, flags);
1894 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1895
NeilBrownde393cd2011-07-28 11:31:48 +10001896 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001897 set_bit(Faulty, &rdev->flags);
1898 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1899 printk(KERN_ALERT
1900 "md/raid:%s: Disk failure on %s, disabling device.\n"
1901 "md/raid:%s: Operation continuing on %d devices.\n",
1902 mdname(mddev),
1903 bdevname(rdev->bdev, b),
1904 mdname(mddev),
1905 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001906}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
1908/*
1909 * Input: a 'big' sector number,
1910 * Output: index of the data and parity disk, and the sector # in them.
1911 */
NeilBrownd1688a62011-10-11 16:49:52 +11001912static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001913 int previous, int *dd_idx,
1914 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001916 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001917 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001919 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001920 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001922 int algorithm = previous ? conf->prev_algo
1923 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001924 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1925 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001926 int raid_disks = previous ? conf->previous_raid_disks
1927 : conf->raid_disks;
1928 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
1930 /* First compute the information on this sector */
1931
1932 /*
1933 * Compute the chunk number and the sector offset inside the chunk
1934 */
1935 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1936 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
1938 /*
1939 * Compute the stripe number
1940 */
NeilBrown35f2a592010-04-20 14:13:34 +10001941 stripe = chunk_number;
1942 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10001943 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 /*
1945 * Select the parity disk based on the user selected algorithm.
1946 */
NeilBrown84789552011-07-27 11:00:36 +10001947 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07001948 switch(conf->level) {
1949 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001950 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001951 break;
1952 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001953 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001955 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001956 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 (*dd_idx)++;
1958 break;
1959 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001960 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001961 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 (*dd_idx)++;
1963 break;
1964 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001965 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001966 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 break;
1968 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001969 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001970 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001972 case ALGORITHM_PARITY_0:
1973 pd_idx = 0;
1974 (*dd_idx)++;
1975 break;
1976 case ALGORITHM_PARITY_N:
1977 pd_idx = data_disks;
1978 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11001980 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001981 }
1982 break;
1983 case 6:
1984
NeilBrowne183eae2009-03-31 15:20:22 +11001985 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001986 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001987 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001988 qd_idx = pd_idx + 1;
1989 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001990 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001991 qd_idx = 0;
1992 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001993 (*dd_idx) += 2; /* D D P Q D */
1994 break;
1995 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001996 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001997 qd_idx = pd_idx + 1;
1998 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001999 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002000 qd_idx = 0;
2001 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002002 (*dd_idx) += 2; /* D D P Q D */
2003 break;
2004 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002005 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002006 qd_idx = (pd_idx + 1) % raid_disks;
2007 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002008 break;
2009 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002010 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002011 qd_idx = (pd_idx + 1) % raid_disks;
2012 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002013 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002014
2015 case ALGORITHM_PARITY_0:
2016 pd_idx = 0;
2017 qd_idx = 1;
2018 (*dd_idx) += 2;
2019 break;
2020 case ALGORITHM_PARITY_N:
2021 pd_idx = data_disks;
2022 qd_idx = data_disks + 1;
2023 break;
2024
2025 case ALGORITHM_ROTATING_ZERO_RESTART:
2026 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2027 * of blocks for computing Q is different.
2028 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002029 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002030 qd_idx = pd_idx + 1;
2031 if (pd_idx == raid_disks-1) {
2032 (*dd_idx)++; /* Q D D D P */
2033 qd_idx = 0;
2034 } else if (*dd_idx >= pd_idx)
2035 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002036 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002037 break;
2038
2039 case ALGORITHM_ROTATING_N_RESTART:
2040 /* Same a left_asymmetric, by first stripe is
2041 * D D D P Q rather than
2042 * Q D D D P
2043 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002044 stripe2 += 1;
2045 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002046 qd_idx = pd_idx + 1;
2047 if (pd_idx == raid_disks-1) {
2048 (*dd_idx)++; /* Q D D D P */
2049 qd_idx = 0;
2050 } else if (*dd_idx >= pd_idx)
2051 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002052 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002053 break;
2054
2055 case ALGORITHM_ROTATING_N_CONTINUE:
2056 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002057 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002058 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2059 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002060 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002061 break;
2062
2063 case ALGORITHM_LEFT_ASYMMETRIC_6:
2064 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002065 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002066 if (*dd_idx >= pd_idx)
2067 (*dd_idx)++;
2068 qd_idx = raid_disks - 1;
2069 break;
2070
2071 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002072 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002073 if (*dd_idx >= pd_idx)
2074 (*dd_idx)++;
2075 qd_idx = raid_disks - 1;
2076 break;
2077
2078 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002079 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002080 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2081 qd_idx = raid_disks - 1;
2082 break;
2083
2084 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002085 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002086 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2087 qd_idx = raid_disks - 1;
2088 break;
2089
2090 case ALGORITHM_PARITY_0_6:
2091 pd_idx = 0;
2092 (*dd_idx)++;
2093 qd_idx = raid_disks - 1;
2094 break;
2095
NeilBrown16a53ec2006-06-26 00:27:38 -07002096 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002097 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002098 }
2099 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 }
2101
NeilBrown911d4ee2009-03-31 14:39:38 +11002102 if (sh) {
2103 sh->pd_idx = pd_idx;
2104 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002105 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 /*
2108 * Finally, compute the new sector number
2109 */
2110 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2111 return new_sector;
2112}
2113
2114
NeilBrown784052e2009-03-31 15:19:07 +11002115static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116{
NeilBrownd1688a62011-10-11 16:49:52 +11002117 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002118 int raid_disks = sh->disks;
2119 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002121 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2122 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002123 int algorithm = previous ? conf->prev_algo
2124 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 sector_t stripe;
2126 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002127 sector_t chunk_number;
2128 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002130 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
NeilBrown16a53ec2006-06-26 00:27:38 -07002132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2134 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
NeilBrown16a53ec2006-06-26 00:27:38 -07002136 if (i == sh->pd_idx)
2137 return 0;
2138 switch(conf->level) {
2139 case 4: break;
2140 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002141 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 case ALGORITHM_LEFT_ASYMMETRIC:
2143 case ALGORITHM_RIGHT_ASYMMETRIC:
2144 if (i > sh->pd_idx)
2145 i--;
2146 break;
2147 case ALGORITHM_LEFT_SYMMETRIC:
2148 case ALGORITHM_RIGHT_SYMMETRIC:
2149 if (i < sh->pd_idx)
2150 i += raid_disks;
2151 i -= (sh->pd_idx + 1);
2152 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002153 case ALGORITHM_PARITY_0:
2154 i -= 1;
2155 break;
2156 case ALGORITHM_PARITY_N:
2157 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002159 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002160 }
2161 break;
2162 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002163 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002164 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002165 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002166 case ALGORITHM_LEFT_ASYMMETRIC:
2167 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002168 case ALGORITHM_ROTATING_ZERO_RESTART:
2169 case ALGORITHM_ROTATING_N_RESTART:
2170 if (sh->pd_idx == raid_disks-1)
2171 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002172 else if (i > sh->pd_idx)
2173 i -= 2; /* D D P Q D */
2174 break;
2175 case ALGORITHM_LEFT_SYMMETRIC:
2176 case ALGORITHM_RIGHT_SYMMETRIC:
2177 if (sh->pd_idx == raid_disks-1)
2178 i--; /* Q D D D P */
2179 else {
2180 /* D D P Q D */
2181 if (i < sh->pd_idx)
2182 i += raid_disks;
2183 i -= (sh->pd_idx + 2);
2184 }
2185 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002186 case ALGORITHM_PARITY_0:
2187 i -= 2;
2188 break;
2189 case ALGORITHM_PARITY_N:
2190 break;
2191 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002192 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002193 if (sh->pd_idx == 0)
2194 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002195 else {
2196 /* D D Q P D */
2197 if (i < sh->pd_idx)
2198 i += raid_disks;
2199 i -= (sh->pd_idx + 1);
2200 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002201 break;
2202 case ALGORITHM_LEFT_ASYMMETRIC_6:
2203 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2204 if (i > sh->pd_idx)
2205 i--;
2206 break;
2207 case ALGORITHM_LEFT_SYMMETRIC_6:
2208 case ALGORITHM_RIGHT_SYMMETRIC_6:
2209 if (i < sh->pd_idx)
2210 i += data_disks + 1;
2211 i -= (sh->pd_idx + 1);
2212 break;
2213 case ALGORITHM_PARITY_0_6:
2214 i -= 1;
2215 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002216 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002217 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002218 }
2219 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 }
2221
2222 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002223 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
NeilBrown112bf892009-03-31 14:39:38 +11002225 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002226 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002227 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2228 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002229 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2230 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 return 0;
2232 }
2233 return r_sector;
2234}
2235
2236
Dan Williams600aa102008-06-28 08:32:05 +10002237static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002238schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002239 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002240{
2241 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002242 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002243 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002244
Dan Williamse33129d2007-01-02 13:52:30 -07002245 if (rcw) {
2246 /* if we are not expanding this is a proper write request, and
2247 * there will be bios with new data to be drained into the
2248 * stripe cache
2249 */
2250 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10002251 sh->reconstruct_state = reconstruct_state_drain_run;
2252 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2253 } else
2254 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07002255
Dan Williamsac6b53b2009-07-14 13:40:19 -07002256 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002257
2258 for (i = disks; i--; ) {
2259 struct r5dev *dev = &sh->dev[i];
2260
2261 if (dev->towrite) {
2262 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002263 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002264 if (!expand)
2265 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002266 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002267 }
2268 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002269 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002270 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002271 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002272 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002273 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002274 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2275 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2276
Dan Williamsd8ee0722008-06-28 08:32:06 +10002277 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002278 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2279 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002280 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002281
2282 for (i = disks; i--; ) {
2283 struct r5dev *dev = &sh->dev[i];
2284 if (i == pd_idx)
2285 continue;
2286
Dan Williamse33129d2007-01-02 13:52:30 -07002287 if (dev->towrite &&
2288 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002289 test_bit(R5_Wantcompute, &dev->flags))) {
2290 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002291 set_bit(R5_LOCKED, &dev->flags);
2292 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002293 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002294 }
2295 }
2296 }
2297
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002298 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002299 * are in flight
2300 */
2301 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2302 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002303 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002304
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002305 if (level == 6) {
2306 int qd_idx = sh->qd_idx;
2307 struct r5dev *dev = &sh->dev[qd_idx];
2308
2309 set_bit(R5_LOCKED, &dev->flags);
2310 clear_bit(R5_UPTODATE, &dev->flags);
2311 s->locked++;
2312 }
2313
Dan Williams600aa102008-06-28 08:32:05 +10002314 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002315 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002316 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002317}
NeilBrown16a53ec2006-06-26 00:27:38 -07002318
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319/*
2320 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002321 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 * The bi_next chain must be in order.
2323 */
2324static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2325{
2326 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002327 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002328 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
NeilBrowncbe47ec2011-07-26 11:20:35 +10002330 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 (unsigned long long)bi->bi_sector,
2332 (unsigned long long)sh->sector);
2333
2334
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07002336 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -07002338 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
2339 firstwrite = 1;
2340 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 bip = &sh->dev[dd_idx].toread;
2342 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2343 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2344 goto overlap;
2345 bip = & (*bip)->bi_next;
2346 }
2347 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2348 goto overlap;
2349
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002350 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 if (*bip)
2352 bi->bi_next = *bip;
2353 *bip = bi;
Jens Axboe960e7392008-08-15 10:41:18 +02002354 bi->bi_phys_segments++;
NeilBrown72626682005-09-09 16:23:54 -07002355
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 if (forwrite) {
2357 /* check if page is covered */
2358 sector_t sector = sh->dev[dd_idx].sector;
2359 for (bi=sh->dev[dd_idx].towrite;
2360 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2361 bi && bi->bi_sector <= sector;
2362 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2363 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2364 sector = bi->bi_sector + (bi->bi_size>>9);
2365 }
2366 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2367 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2368 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002369 spin_unlock_irq(&conf->device_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002370
2371 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2372 (unsigned long long)(*bip)->bi_sector,
2373 (unsigned long long)sh->sector, dd_idx);
2374
2375 if (conf->mddev->bitmap && firstwrite) {
2376 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2377 STRIPE_SECTORS, 0);
2378 sh->bm_seq = conf->seq_flush+1;
2379 set_bit(STRIPE_BIT_DELAY, &sh->state);
2380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 return 1;
2382
2383 overlap:
2384 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
2385 spin_unlock_irq(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 return 0;
2387}
2388
NeilBrownd1688a62011-10-11 16:49:52 +11002389static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002390
NeilBrownd1688a62011-10-11 16:49:52 +11002391static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002392 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002393{
NeilBrown784052e2009-03-31 15:19:07 +11002394 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002395 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002396 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002397 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002398 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002399
NeilBrown112bf892009-03-31 14:39:38 +11002400 raid5_compute_sector(conf,
2401 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002402 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002403 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002404 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002405}
2406
Dan Williamsa4456852007-07-09 11:56:43 -07002407static void
NeilBrownd1688a62011-10-11 16:49:52 +11002408handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002409 struct stripe_head_state *s, int disks,
2410 struct bio **return_bi)
2411{
2412 int i;
2413 for (i = disks; i--; ) {
2414 struct bio *bi;
2415 int bitmap_end = 0;
2416
2417 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002418 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002419 rcu_read_lock();
2420 rdev = rcu_dereference(conf->disks[i].rdev);
2421 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002422 atomic_inc(&rdev->nr_pending);
2423 else
2424 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002425 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002426 if (rdev) {
2427 if (!rdev_set_badblocks(
2428 rdev,
2429 sh->sector,
2430 STRIPE_SECTORS, 0))
2431 md_error(conf->mddev, rdev);
2432 rdev_dec_pending(rdev, conf->mddev);
2433 }
Dan Williamsa4456852007-07-09 11:56:43 -07002434 }
2435 spin_lock_irq(&conf->device_lock);
2436 /* fail all writes first */
2437 bi = sh->dev[i].towrite;
2438 sh->dev[i].towrite = NULL;
2439 if (bi) {
2440 s->to_write--;
2441 bitmap_end = 1;
2442 }
2443
2444 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2445 wake_up(&conf->wait_for_overlap);
2446
2447 while (bi && bi->bi_sector <
2448 sh->dev[i].sector + STRIPE_SECTORS) {
2449 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2450 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002451 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002452 md_write_end(conf->mddev);
2453 bi->bi_next = *return_bi;
2454 *return_bi = bi;
2455 }
2456 bi = nextbi;
2457 }
2458 /* and fail all 'written' */
2459 bi = sh->dev[i].written;
2460 sh->dev[i].written = NULL;
2461 if (bi) bitmap_end = 1;
2462 while (bi && bi->bi_sector <
2463 sh->dev[i].sector + STRIPE_SECTORS) {
2464 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2465 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002466 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002467 md_write_end(conf->mddev);
2468 bi->bi_next = *return_bi;
2469 *return_bi = bi;
2470 }
2471 bi = bi2;
2472 }
2473
Dan Williamsb5e98d62007-01-02 13:52:31 -07002474 /* fail any reads if this device is non-operational and
2475 * the data has not reached the cache yet.
2476 */
2477 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2478 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2479 test_bit(R5_ReadError, &sh->dev[i].flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002480 bi = sh->dev[i].toread;
2481 sh->dev[i].toread = NULL;
2482 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2483 wake_up(&conf->wait_for_overlap);
2484 if (bi) s->to_read--;
2485 while (bi && bi->bi_sector <
2486 sh->dev[i].sector + STRIPE_SECTORS) {
2487 struct bio *nextbi =
2488 r5_next_bio(bi, sh->dev[i].sector);
2489 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002490 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002491 bi->bi_next = *return_bi;
2492 *return_bi = bi;
2493 }
2494 bi = nextbi;
2495 }
2496 }
2497 spin_unlock_irq(&conf->device_lock);
2498 if (bitmap_end)
2499 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2500 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002501 /* If we were in the middle of a write the parity block might
2502 * still be locked - so just clear all R5_LOCKED flags
2503 */
2504 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002505 }
2506
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002507 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2508 if (atomic_dec_and_test(&conf->pending_full_writes))
2509 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002510}
2511
NeilBrown7f0da592011-07-28 11:39:22 +10002512static void
NeilBrownd1688a62011-10-11 16:49:52 +11002513handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002514 struct stripe_head_state *s)
2515{
2516 int abort = 0;
2517 int i;
2518
NeilBrown7f0da592011-07-28 11:39:22 +10002519 clear_bit(STRIPE_SYNCING, &sh->state);
2520 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002521 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002522 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10002523 * Don't even need to abort as that is handled elsewhere
2524 * if needed, and not always wanted e.g. if there is a known
2525 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11002526 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002527 * non-sync devices, or abort the recovery
2528 */
NeilBrown18b98372012-04-01 23:48:38 +10002529 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2530 /* During recovery devices cannot be removed, so
2531 * locking and refcounting of rdevs is not needed
2532 */
2533 for (i = 0; i < conf->raid_disks; i++) {
2534 struct md_rdev *rdev = conf->disks[i].rdev;
2535 if (rdev
2536 && !test_bit(Faulty, &rdev->flags)
2537 && !test_bit(In_sync, &rdev->flags)
2538 && !rdev_set_badblocks(rdev, sh->sector,
2539 STRIPE_SECTORS, 0))
2540 abort = 1;
2541 rdev = conf->disks[i].replacement;
2542 if (rdev
2543 && !test_bit(Faulty, &rdev->flags)
2544 && !test_bit(In_sync, &rdev->flags)
2545 && !rdev_set_badblocks(rdev, sh->sector,
2546 STRIPE_SECTORS, 0))
2547 abort = 1;
2548 }
2549 if (abort)
2550 conf->recovery_disabled =
2551 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10002552 }
NeilBrown18b98372012-04-01 23:48:38 +10002553 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10002554}
2555
NeilBrown9a3e1102011-12-23 10:17:53 +11002556static int want_replace(struct stripe_head *sh, int disk_idx)
2557{
2558 struct md_rdev *rdev;
2559 int rv = 0;
2560 /* Doing recovery so rcu locking not required */
2561 rdev = sh->raid_conf->disks[disk_idx].replacement;
2562 if (rdev
2563 && !test_bit(Faulty, &rdev->flags)
2564 && !test_bit(In_sync, &rdev->flags)
2565 && (rdev->recovery_offset <= sh->sector
2566 || rdev->mddev->recovery_cp <= sh->sector))
2567 rv = 1;
2568
2569 return rv;
2570}
2571
NeilBrown93b3dbc2011-07-27 11:00:36 +10002572/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002573 * to be read or computed to satisfy a request.
2574 *
2575 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002576 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002577 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002578static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2579 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002580{
2581 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002582 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2583 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002584
Dan Williamsf38e1212007-01-02 13:52:30 -07002585 /* is the data in this block needed, and can we get it? */
2586 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002587 !test_bit(R5_UPTODATE, &dev->flags) &&
2588 (dev->toread ||
2589 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2590 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002591 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002592 (s->failed >= 1 && fdev[0]->toread) ||
2593 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002594 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2595 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2596 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002597 /* we would like to get this block, possibly by computing it,
2598 * otherwise read it if the backing disk is insync
2599 */
2600 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2601 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2602 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002603 (s->failed && (disk_idx == s->failed_num[0] ||
2604 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002605 /* have disk failed, and we're requested to fetch it;
2606 * do compute it
2607 */
2608 pr_debug("Computing stripe %llu block %d\n",
2609 (unsigned long long)sh->sector, disk_idx);
2610 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2611 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2612 set_bit(R5_Wantcompute, &dev->flags);
2613 sh->ops.target = disk_idx;
2614 sh->ops.target2 = -1; /* no 2nd target */
2615 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002616 /* Careful: from this point on 'uptodate' is in the eye
2617 * of raid_run_ops which services 'compute' operations
2618 * before writes. R5_Wantcompute flags a block that will
2619 * be R5_UPTODATE by the time it is needed for a
2620 * subsequent operation.
2621 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002622 s->uptodate++;
2623 return 1;
2624 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2625 /* Computing 2-failure is *very* expensive; only
2626 * do it if failed >= 2
2627 */
2628 int other;
2629 for (other = disks; other--; ) {
2630 if (other == disk_idx)
2631 continue;
2632 if (!test_bit(R5_UPTODATE,
2633 &sh->dev[other].flags))
2634 break;
2635 }
2636 BUG_ON(other < 0);
2637 pr_debug("Computing stripe %llu blocks %d,%d\n",
2638 (unsigned long long)sh->sector,
2639 disk_idx, other);
2640 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2641 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2642 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2643 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2644 sh->ops.target = disk_idx;
2645 sh->ops.target2 = other;
2646 s->uptodate += 2;
2647 s->req_compute = 1;
2648 return 1;
2649 } else if (test_bit(R5_Insync, &dev->flags)) {
2650 set_bit(R5_LOCKED, &dev->flags);
2651 set_bit(R5_Wantread, &dev->flags);
2652 s->locked++;
2653 pr_debug("Reading block %d (sync=%d)\n",
2654 disk_idx, s->syncing);
2655 }
2656 }
2657
2658 return 0;
2659}
2660
2661/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002662 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002663 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002664static void handle_stripe_fill(struct stripe_head *sh,
2665 struct stripe_head_state *s,
2666 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002667{
2668 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002669
2670 /* look for blocks to read/compute, skip this if a compute
2671 * is already in flight, or if the stripe contents are in the
2672 * midst of changing due to a write
2673 */
2674 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2675 !sh->reconstruct_state)
2676 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002677 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002678 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002679 set_bit(STRIPE_HANDLE, &sh->state);
2680}
2681
2682
Dan Williams1fe797e2008-06-28 09:16:30 +10002683/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002684 * any written block on an uptodate or failed drive can be returned.
2685 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2686 * never LOCKED, so we don't need to test 'failed' directly.
2687 */
NeilBrownd1688a62011-10-11 16:49:52 +11002688static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002689 struct stripe_head *sh, int disks, struct bio **return_bi)
2690{
2691 int i;
2692 struct r5dev *dev;
2693
2694 for (i = disks; i--; )
2695 if (sh->dev[i].written) {
2696 dev = &sh->dev[i];
2697 if (!test_bit(R5_LOCKED, &dev->flags) &&
2698 test_bit(R5_UPTODATE, &dev->flags)) {
2699 /* We can return any write requests */
2700 struct bio *wbi, *wbi2;
2701 int bitmap_end = 0;
Dan Williams45b42332007-07-09 11:56:43 -07002702 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002703 spin_lock_irq(&conf->device_lock);
2704 wbi = dev->written;
2705 dev->written = NULL;
2706 while (wbi && wbi->bi_sector <
2707 dev->sector + STRIPE_SECTORS) {
2708 wbi2 = r5_next_bio(wbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +02002709 if (!raid5_dec_bi_phys_segments(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002710 md_write_end(conf->mddev);
2711 wbi->bi_next = *return_bi;
2712 *return_bi = wbi;
2713 }
2714 wbi = wbi2;
2715 }
2716 if (dev->towrite == NULL)
2717 bitmap_end = 1;
2718 spin_unlock_irq(&conf->device_lock);
2719 if (bitmap_end)
2720 bitmap_endwrite(conf->mddev->bitmap,
2721 sh->sector,
2722 STRIPE_SECTORS,
2723 !test_bit(STRIPE_DEGRADED, &sh->state),
2724 0);
2725 }
2726 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002727
2728 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2729 if (atomic_dec_and_test(&conf->pending_full_writes))
2730 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002731}
2732
NeilBrownd1688a62011-10-11 16:49:52 +11002733static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002734 struct stripe_head *sh,
2735 struct stripe_head_state *s,
2736 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002737{
2738 int rmw = 0, rcw = 0, i;
NeilBrownc8ac1802011-07-27 11:00:36 +10002739 if (conf->max_degraded == 2) {
2740 /* RAID6 requires 'rcw' in current implementation
2741 * Calculate the real rcw later - for now fake it
2742 * look like rcw is cheaper
2743 */
2744 rcw = 1; rmw = 2;
2745 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002746 /* would I have to read this buffer for read_modify_write */
2747 struct r5dev *dev = &sh->dev[i];
2748 if ((dev->towrite || i == sh->pd_idx) &&
2749 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002750 !(test_bit(R5_UPTODATE, &dev->flags) ||
2751 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002752 if (test_bit(R5_Insync, &dev->flags))
2753 rmw++;
2754 else
2755 rmw += 2*disks; /* cannot read it */
2756 }
2757 /* Would I have to read this buffer for reconstruct_write */
2758 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2759 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002760 !(test_bit(R5_UPTODATE, &dev->flags) ||
2761 test_bit(R5_Wantcompute, &dev->flags))) {
2762 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002763 else
2764 rcw += 2*disks;
2765 }
2766 }
Dan Williams45b42332007-07-09 11:56:43 -07002767 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002768 (unsigned long long)sh->sector, rmw, rcw);
2769 set_bit(STRIPE_HANDLE, &sh->state);
2770 if (rmw < rcw && rmw > 0)
2771 /* prefer read-modify-write, but need to get some data */
2772 for (i = disks; i--; ) {
2773 struct r5dev *dev = &sh->dev[i];
2774 if ((dev->towrite || i == sh->pd_idx) &&
2775 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002776 !(test_bit(R5_UPTODATE, &dev->flags) ||
2777 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002778 test_bit(R5_Insync, &dev->flags)) {
2779 if (
2780 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002781 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002782 "%d for r-m-w\n", i);
2783 set_bit(R5_LOCKED, &dev->flags);
2784 set_bit(R5_Wantread, &dev->flags);
2785 s->locked++;
2786 } else {
2787 set_bit(STRIPE_DELAYED, &sh->state);
2788 set_bit(STRIPE_HANDLE, &sh->state);
2789 }
2790 }
2791 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002792 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002793 /* want reconstruct write, but need to get some data */
NeilBrownc8ac1802011-07-27 11:00:36 +10002794 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002795 for (i = disks; i--; ) {
2796 struct r5dev *dev = &sh->dev[i];
2797 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002798 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002799 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002800 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002801 test_bit(R5_Wantcompute, &dev->flags))) {
2802 rcw++;
2803 if (!test_bit(R5_Insync, &dev->flags))
2804 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002805 if (
2806 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002807 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002808 "%d for Reconstruct\n", i);
2809 set_bit(R5_LOCKED, &dev->flags);
2810 set_bit(R5_Wantread, &dev->flags);
2811 s->locked++;
2812 } else {
2813 set_bit(STRIPE_DELAYED, &sh->state);
2814 set_bit(STRIPE_HANDLE, &sh->state);
2815 }
2816 }
2817 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002818 }
Dan Williamsa4456852007-07-09 11:56:43 -07002819 /* now if nothing is locked, and if we have enough data,
2820 * we can start a write request
2821 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002822 /* since handle_stripe can be called at any time we need to handle the
2823 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002824 * subsequent call wants to start a write request. raid_run_ops only
2825 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002826 * simultaneously. If this is not the case then new writes need to be
2827 * held off until the compute completes.
2828 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002829 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2830 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2831 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002832 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002833}
2834
NeilBrownd1688a62011-10-11 16:49:52 +11002835static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002836 struct stripe_head_state *s, int disks)
2837{
Dan Williamsecc65c92008-06-28 08:31:57 +10002838 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002839
Dan Williamsbd2ab672008-04-10 21:29:27 -07002840 set_bit(STRIPE_HANDLE, &sh->state);
2841
Dan Williamsecc65c92008-06-28 08:31:57 +10002842 switch (sh->check_state) {
2843 case check_state_idle:
2844 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002845 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002846 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002847 sh->check_state = check_state_run;
2848 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002849 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002850 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002851 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002852 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002853 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002854 /* fall through */
2855 case check_state_compute_result:
2856 sh->check_state = check_state_idle;
2857 if (!dev)
2858 dev = &sh->dev[sh->pd_idx];
2859
2860 /* check that a write has not made the stripe insync */
2861 if (test_bit(STRIPE_INSYNC, &sh->state))
2862 break;
2863
2864 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002865 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2866 BUG_ON(s->uptodate != disks);
2867
2868 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002869 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002870 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002871
Dan Williamsa4456852007-07-09 11:56:43 -07002872 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002873 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002874 break;
2875 case check_state_run:
2876 break; /* we will be called again upon completion */
2877 case check_state_check_result:
2878 sh->check_state = check_state_idle;
2879
2880 /* if a failure occurred during the check operation, leave
2881 * STRIPE_INSYNC not set and let the stripe be handled again
2882 */
2883 if (s->failed)
2884 break;
2885
2886 /* handle a successful check operation, if parity is correct
2887 * we are done. Otherwise update the mismatch count and repair
2888 * parity if !MD_RECOVERY_CHECK
2889 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002890 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002891 /* parity is correct (on disc,
2892 * not in buffer any more)
2893 */
2894 set_bit(STRIPE_INSYNC, &sh->state);
2895 else {
2896 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2897 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2898 /* don't try to repair!! */
2899 set_bit(STRIPE_INSYNC, &sh->state);
2900 else {
2901 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002902 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002903 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2904 set_bit(R5_Wantcompute,
2905 &sh->dev[sh->pd_idx].flags);
2906 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002907 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10002908 s->uptodate++;
2909 }
2910 }
2911 break;
2912 case check_state_compute_run:
2913 break;
2914 default:
2915 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2916 __func__, sh->check_state,
2917 (unsigned long long) sh->sector);
2918 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002919 }
2920}
2921
2922
NeilBrownd1688a62011-10-11 16:49:52 +11002923static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07002924 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10002925 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002926{
Dan Williamsa4456852007-07-09 11:56:43 -07002927 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11002928 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07002929 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07002930
2931 set_bit(STRIPE_HANDLE, &sh->state);
2932
2933 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002934
Dan Williamsa4456852007-07-09 11:56:43 -07002935 /* Want to check and possibly repair P and Q.
2936 * However there could be one 'failed' device, in which
2937 * case we can only check one of them, possibly using the
2938 * other to generate missing data
2939 */
2940
Dan Williamsd82dfee2009-07-14 13:40:57 -07002941 switch (sh->check_state) {
2942 case check_state_idle:
2943 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10002944 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002945 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07002946 * makes sense to check P (If anything else were failed,
2947 * we would have used P to recreate it).
2948 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002949 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07002950 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002951 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002952 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07002953 * anything, so it makes sense to check it
2954 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002955 if (sh->check_state == check_state_run)
2956 sh->check_state = check_state_run_pq;
2957 else
2958 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07002959 }
Dan Williams36d1c642009-07-14 11:48:22 -07002960
Dan Williamsd82dfee2009-07-14 13:40:57 -07002961 /* discard potentially stale zero_sum_result */
2962 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07002963
Dan Williamsd82dfee2009-07-14 13:40:57 -07002964 if (sh->check_state == check_state_run) {
2965 /* async_xor_zero_sum destroys the contents of P */
2966 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
2967 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07002968 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002969 if (sh->check_state >= check_state_run &&
2970 sh->check_state <= check_state_run_pq) {
2971 /* async_syndrome_zero_sum preserves P and Q, so
2972 * no need to mark them !uptodate here
2973 */
2974 set_bit(STRIPE_OP_CHECK, &s->ops_request);
2975 break;
2976 }
Dan Williams36d1c642009-07-14 11:48:22 -07002977
Dan Williamsd82dfee2009-07-14 13:40:57 -07002978 /* we have 2-disk failure */
2979 BUG_ON(s->failed != 2);
2980 /* fall through */
2981 case check_state_compute_result:
2982 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07002983
Dan Williamsd82dfee2009-07-14 13:40:57 -07002984 /* check that a write has not made the stripe insync */
2985 if (test_bit(STRIPE_INSYNC, &sh->state))
2986 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002987
2988 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07002989 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07002990 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002991 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07002992 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10002993 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07002994 s->locked++;
2995 set_bit(R5_LOCKED, &dev->flags);
2996 set_bit(R5_Wantwrite, &dev->flags);
2997 }
2998 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10002999 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07003000 s->locked++;
3001 set_bit(R5_LOCKED, &dev->flags);
3002 set_bit(R5_Wantwrite, &dev->flags);
3003 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003004 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003005 dev = &sh->dev[pd_idx];
3006 s->locked++;
3007 set_bit(R5_LOCKED, &dev->flags);
3008 set_bit(R5_Wantwrite, &dev->flags);
3009 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003010 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003011 dev = &sh->dev[qd_idx];
3012 s->locked++;
3013 set_bit(R5_LOCKED, &dev->flags);
3014 set_bit(R5_Wantwrite, &dev->flags);
3015 }
3016 clear_bit(STRIPE_DEGRADED, &sh->state);
3017
3018 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003019 break;
3020 case check_state_run:
3021 case check_state_run_q:
3022 case check_state_run_pq:
3023 break; /* we will be called again upon completion */
3024 case check_state_check_result:
3025 sh->check_state = check_state_idle;
3026
3027 /* handle a successful check operation, if parity is correct
3028 * we are done. Otherwise update the mismatch count and repair
3029 * parity if !MD_RECOVERY_CHECK
3030 */
3031 if (sh->ops.zero_sum_result == 0) {
3032 /* both parities are correct */
3033 if (!s->failed)
3034 set_bit(STRIPE_INSYNC, &sh->state);
3035 else {
3036 /* in contrast to the raid5 case we can validate
3037 * parity, but still have a failure to write
3038 * back
3039 */
3040 sh->check_state = check_state_compute_result;
3041 /* Returning at this point means that we may go
3042 * off and bring p and/or q uptodate again so
3043 * we make sure to check zero_sum_result again
3044 * to verify if p or q need writeback
3045 */
3046 }
3047 } else {
3048 conf->mddev->resync_mismatches += STRIPE_SECTORS;
3049 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3050 /* don't try to repair!! */
3051 set_bit(STRIPE_INSYNC, &sh->state);
3052 else {
3053 int *target = &sh->ops.target;
3054
3055 sh->ops.target = -1;
3056 sh->ops.target2 = -1;
3057 sh->check_state = check_state_compute_run;
3058 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3059 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3060 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3061 set_bit(R5_Wantcompute,
3062 &sh->dev[pd_idx].flags);
3063 *target = pd_idx;
3064 target = &sh->ops.target2;
3065 s->uptodate++;
3066 }
3067 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3068 set_bit(R5_Wantcompute,
3069 &sh->dev[qd_idx].flags);
3070 *target = qd_idx;
3071 s->uptodate++;
3072 }
3073 }
3074 }
3075 break;
3076 case check_state_compute_run:
3077 break;
3078 default:
3079 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3080 __func__, sh->check_state,
3081 (unsigned long long) sh->sector);
3082 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003083 }
3084}
3085
NeilBrownd1688a62011-10-11 16:49:52 +11003086static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003087{
3088 int i;
3089
3090 /* We have read all the blocks in this stripe and now we need to
3091 * copy some of them into a target stripe for expand.
3092 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003093 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003094 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3095 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003096 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003097 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003098 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003099 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003100
NeilBrown784052e2009-03-31 15:19:07 +11003101 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003102 sector_t s = raid5_compute_sector(conf, bn, 0,
3103 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003104 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003105 if (sh2 == NULL)
3106 /* so far only the early blocks of this stripe
3107 * have been requested. When later blocks
3108 * get requested, we will try again
3109 */
3110 continue;
3111 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3112 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3113 /* must have already done this block */
3114 release_stripe(sh2);
3115 continue;
3116 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003117
3118 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003119 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003120 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003121 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003122 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003123
Dan Williamsa4456852007-07-09 11:56:43 -07003124 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3125 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3126 for (j = 0; j < conf->raid_disks; j++)
3127 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003128 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003129 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3130 break;
3131 if (j == conf->raid_disks) {
3132 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3133 set_bit(STRIPE_HANDLE, &sh2->state);
3134 }
3135 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003136
Dan Williamsa4456852007-07-09 11:56:43 -07003137 }
NeilBrowna2e08552007-09-11 15:23:36 -07003138 /* done submitting copies, wait for them to complete */
3139 if (tx) {
3140 async_tx_ack(tx);
3141 dma_wait_for_async_tx(tx);
3142 }
Dan Williamsa4456852007-07-09 11:56:43 -07003143}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144
3145/*
3146 * handle_stripe - do things to a stripe.
3147 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003148 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3149 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003151 * return some read requests which now have data
3152 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 * schedule a read on some buffers
3154 * schedule a write of some buffers
3155 * return confirmation of parity correctness
3156 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 */
Dan Williamsa4456852007-07-09 11:56:43 -07003158
NeilBrownacfe7262011-07-27 11:00:36 +10003159static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003160{
NeilBrownd1688a62011-10-11 16:49:52 +11003161 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003162 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003163 struct r5dev *dev;
3164 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003165 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003166
NeilBrownacfe7262011-07-27 11:00:36 +10003167 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003168
NeilBrownacfe7262011-07-27 11:00:36 +10003169 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3170 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3171 s->failed_num[0] = -1;
3172 s->failed_num[1] = -1;
3173
3174 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003175 rcu_read_lock();
NeilBrownc4c16632011-07-26 11:34:20 +10003176 spin_lock_irq(&conf->device_lock);
NeilBrown16a53ec2006-06-26 00:27:38 -07003177 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003178 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003179 sector_t first_bad;
3180 int bad_sectors;
3181 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003182
NeilBrown16a53ec2006-06-26 00:27:38 -07003183 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003184
Dan Williams45b42332007-07-09 11:56:43 -07003185 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003186 i, dev->flags,
3187 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003188 /* maybe we can reply to a read
3189 *
3190 * new wantfill requests are only permitted while
3191 * ops_complete_biofill is guaranteed to be inactive
3192 */
3193 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3194 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3195 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003196
3197 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003198 if (test_bit(R5_LOCKED, &dev->flags))
3199 s->locked++;
3200 if (test_bit(R5_UPTODATE, &dev->flags))
3201 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003202 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003203 s->compute++;
3204 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003205 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003206
NeilBrownacfe7262011-07-27 11:00:36 +10003207 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003208 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003209 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003210 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003211 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003212 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003213 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003214 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003215 }
Dan Williamsa4456852007-07-09 11:56:43 -07003216 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003217 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003218 /* Prefer to use the replacement for reads, but only
3219 * if it is recovered enough and has no bad blocks.
3220 */
3221 rdev = rcu_dereference(conf->disks[i].replacement);
3222 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3223 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3224 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3225 &first_bad, &bad_sectors))
3226 set_bit(R5_ReadRepl, &dev->flags);
3227 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003228 if (rdev)
3229 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003230 rdev = rcu_dereference(conf->disks[i].rdev);
3231 clear_bit(R5_ReadRepl, &dev->flags);
3232 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003233 if (rdev && test_bit(Faulty, &rdev->flags))
3234 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003235 if (rdev) {
3236 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3237 &first_bad, &bad_sectors);
3238 if (s->blocked_rdev == NULL
3239 && (test_bit(Blocked, &rdev->flags)
3240 || is_bad < 0)) {
3241 if (is_bad < 0)
3242 set_bit(BlockedBadBlocks,
3243 &rdev->flags);
3244 s->blocked_rdev = rdev;
3245 atomic_inc(&rdev->nr_pending);
3246 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003247 }
NeilBrown415e72d2010-06-17 17:25:21 +10003248 clear_bit(R5_Insync, &dev->flags);
3249 if (!rdev)
3250 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003251 else if (is_bad) {
3252 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10003253 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3254 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003255 /* treat as in-sync, but with a read error
3256 * which we can now try to correct
3257 */
3258 set_bit(R5_Insync, &dev->flags);
3259 set_bit(R5_ReadError, &dev->flags);
3260 }
3261 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003262 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003263 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003264 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003265 set_bit(R5_Insync, &dev->flags);
3266 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3267 test_bit(R5_Expanded, &dev->flags))
3268 /* If we've reshaped into here, we assume it is Insync.
3269 * We will shortly update recovery_offset to make
3270 * it official.
3271 */
3272 set_bit(R5_Insync, &dev->flags);
3273
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003274 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003275 /* This flag does not apply to '.replacement'
3276 * only to .rdev, so make sure to check that*/
3277 struct md_rdev *rdev2 = rcu_dereference(
3278 conf->disks[i].rdev);
3279 if (rdev2 == rdev)
3280 clear_bit(R5_Insync, &dev->flags);
3281 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003282 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003283 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003284 } else
3285 clear_bit(R5_WriteError, &dev->flags);
3286 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003287 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003288 /* This flag does not apply to '.replacement'
3289 * only to .rdev, so make sure to check that*/
3290 struct md_rdev *rdev2 = rcu_dereference(
3291 conf->disks[i].rdev);
3292 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003293 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003294 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003295 } else
3296 clear_bit(R5_MadeGood, &dev->flags);
3297 }
NeilBrown977df362011-12-23 10:17:53 +11003298 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3299 struct md_rdev *rdev2 = rcu_dereference(
3300 conf->disks[i].replacement);
3301 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3302 s->handle_bad_blocks = 1;
3303 atomic_inc(&rdev2->nr_pending);
3304 } else
3305 clear_bit(R5_MadeGoodRepl, &dev->flags);
3306 }
NeilBrown415e72d2010-06-17 17:25:21 +10003307 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003308 /* The ReadError flag will just be confusing now */
3309 clear_bit(R5_ReadError, &dev->flags);
3310 clear_bit(R5_ReWrite, &dev->flags);
3311 }
NeilBrown415e72d2010-06-17 17:25:21 +10003312 if (test_bit(R5_ReadError, &dev->flags))
3313 clear_bit(R5_Insync, &dev->flags);
3314 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003315 if (s->failed < 2)
3316 s->failed_num[s->failed] = i;
3317 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003318 if (rdev && !test_bit(Faulty, &rdev->flags))
3319 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003320 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003321 }
NeilBrownc4c16632011-07-26 11:34:20 +10003322 spin_unlock_irq(&conf->device_lock);
NeilBrown9a3e1102011-12-23 10:17:53 +11003323 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3324 /* If there is a failed device being replaced,
3325 * we must be recovering.
3326 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10003327 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11003328 * else we can only be replacing
3329 * sync and recovery both need to read all devices, and so
3330 * use the same flag.
3331 */
3332 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10003333 sh->sector >= conf->mddev->recovery_cp ||
3334 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11003335 s->syncing = 1;
3336 else
3337 s->replacing = 1;
3338 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003339 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003340}
NeilBrownf4168852007-02-28 20:11:53 -08003341
NeilBrowncc940152011-07-26 11:35:35 +10003342static void handle_stripe(struct stripe_head *sh)
3343{
3344 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003345 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003346 int i;
NeilBrown84789552011-07-27 11:00:36 +10003347 int prexor;
3348 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003349 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003350
3351 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003352 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003353 /* already being handled, ensure it gets handled
3354 * again when current action finishes */
3355 set_bit(STRIPE_HANDLE, &sh->state);
3356 return;
3357 }
3358
3359 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3360 set_bit(STRIPE_SYNCING, &sh->state);
3361 clear_bit(STRIPE_INSYNC, &sh->state);
3362 }
3363 clear_bit(STRIPE_DELAYED, &sh->state);
3364
3365 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3366 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3367 (unsigned long long)sh->sector, sh->state,
3368 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3369 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003370
NeilBrownacfe7262011-07-27 11:00:36 +10003371 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003372
NeilBrownbc2607f2011-07-28 11:39:22 +10003373 if (s.handle_bad_blocks) {
3374 set_bit(STRIPE_HANDLE, &sh->state);
3375 goto finish;
3376 }
3377
NeilBrown474af965fe2011-07-27 11:00:36 +10003378 if (unlikely(s.blocked_rdev)) {
3379 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003380 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003381 set_bit(STRIPE_HANDLE, &sh->state);
3382 goto finish;
3383 }
3384 /* There is nothing for the blocked_rdev to block */
3385 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3386 s.blocked_rdev = NULL;
3387 }
3388
3389 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3390 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3391 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3392 }
3393
3394 pr_debug("locked=%d uptodate=%d to_read=%d"
3395 " to_write=%d failed=%d failed_num=%d,%d\n",
3396 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3397 s.failed_num[0], s.failed_num[1]);
3398 /* check if the array has lost more than max_degraded devices and,
3399 * if so, some requests might need to be failed.
3400 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003401 if (s.failed > conf->max_degraded) {
3402 sh->check_state = 0;
3403 sh->reconstruct_state = 0;
3404 if (s.to_read+s.to_write+s.written)
3405 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003406 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003407 handle_failed_sync(conf, sh, &s);
3408 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003409
3410 /*
3411 * might be able to return some write requests if the parity blocks
3412 * are safe, or on a failed drive
3413 */
3414 pdev = &sh->dev[sh->pd_idx];
3415 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3416 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3417 qdev = &sh->dev[sh->qd_idx];
3418 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3419 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3420 || conf->level < 6;
3421
3422 if (s.written &&
3423 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3424 && !test_bit(R5_LOCKED, &pdev->flags)
3425 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3426 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3427 && !test_bit(R5_LOCKED, &qdev->flags)
3428 && test_bit(R5_UPTODATE, &qdev->flags)))))
3429 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3430
3431 /* Now we might consider reading some blocks, either to check/generate
3432 * parity, or to satisfy requests
3433 * or to load a block that is being partially written.
3434 */
3435 if (s.to_read || s.non_overwrite
3436 || (conf->level == 6 && s.to_write && s.failed)
NeilBrown9a3e1102011-12-23 10:17:53 +11003437 || (s.syncing && (s.uptodate + s.compute < disks))
3438 || s.replacing
3439 || s.expanding)
NeilBrown474af965fe2011-07-27 11:00:36 +10003440 handle_stripe_fill(sh, &s, disks);
3441
NeilBrown84789552011-07-27 11:00:36 +10003442 /* Now we check to see if any write operations have recently
3443 * completed
3444 */
3445 prexor = 0;
3446 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3447 prexor = 1;
3448 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3449 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3450 sh->reconstruct_state = reconstruct_state_idle;
3451
3452 /* All the 'written' buffers and the parity block are ready to
3453 * be written back to disk
3454 */
3455 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3456 BUG_ON(sh->qd_idx >= 0 &&
3457 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags));
3458 for (i = disks; i--; ) {
3459 struct r5dev *dev = &sh->dev[i];
3460 if (test_bit(R5_LOCKED, &dev->flags) &&
3461 (i == sh->pd_idx || i == sh->qd_idx ||
3462 dev->written)) {
3463 pr_debug("Writing block %d\n", i);
3464 set_bit(R5_Wantwrite, &dev->flags);
3465 if (prexor)
3466 continue;
3467 if (!test_bit(R5_Insync, &dev->flags) ||
3468 ((i == sh->pd_idx || i == sh->qd_idx) &&
3469 s.failed == 0))
3470 set_bit(STRIPE_INSYNC, &sh->state);
3471 }
3472 }
3473 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3474 s.dec_preread_active = 1;
3475 }
3476
3477 /* Now to consider new write requests and what else, if anything
3478 * should be read. We do not handle new writes when:
3479 * 1/ A 'write' operation (copy+xor) is already in flight.
3480 * 2/ A 'check' operation is in flight, as it may clobber the parity
3481 * block.
3482 */
3483 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3484 handle_stripe_dirtying(conf, sh, &s, disks);
3485
3486 /* maybe we need to check and possibly fix the parity for this stripe
3487 * Any reads will already have been scheduled, so we just see if enough
3488 * data is available. The parity check is held off while parity
3489 * dependent operations are in flight.
3490 */
3491 if (sh->check_state ||
3492 (s.syncing && s.locked == 0 &&
3493 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3494 !test_bit(STRIPE_INSYNC, &sh->state))) {
3495 if (conf->level == 6)
3496 handle_parity_checks6(conf, sh, &s, disks);
3497 else
3498 handle_parity_checks5(conf, sh, &s, disks);
3499 }
NeilBrownc5a31002011-07-27 11:00:36 +10003500
NeilBrown9a3e1102011-12-23 10:17:53 +11003501 if (s.replacing && s.locked == 0
3502 && !test_bit(STRIPE_INSYNC, &sh->state)) {
3503 /* Write out to replacement devices where possible */
3504 for (i = 0; i < conf->raid_disks; i++)
3505 if (test_bit(R5_UPTODATE, &sh->dev[i].flags) &&
3506 test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3507 set_bit(R5_WantReplace, &sh->dev[i].flags);
3508 set_bit(R5_LOCKED, &sh->dev[i].flags);
3509 s.locked++;
3510 }
3511 set_bit(STRIPE_INSYNC, &sh->state);
3512 }
3513 if ((s.syncing || s.replacing) && s.locked == 0 &&
3514 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003515 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3516 clear_bit(STRIPE_SYNCING, &sh->state);
3517 }
3518
3519 /* If the failed drives are just a ReadError, then we might need
3520 * to progress the repair/check process
3521 */
3522 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3523 for (i = 0; i < s.failed; i++) {
3524 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3525 if (test_bit(R5_ReadError, &dev->flags)
3526 && !test_bit(R5_LOCKED, &dev->flags)
3527 && test_bit(R5_UPTODATE, &dev->flags)
3528 ) {
3529 if (!test_bit(R5_ReWrite, &dev->flags)) {
3530 set_bit(R5_Wantwrite, &dev->flags);
3531 set_bit(R5_ReWrite, &dev->flags);
3532 set_bit(R5_LOCKED, &dev->flags);
3533 s.locked++;
3534 } else {
3535 /* let's read it back */
3536 set_bit(R5_Wantread, &dev->flags);
3537 set_bit(R5_LOCKED, &dev->flags);
3538 s.locked++;
3539 }
3540 }
3541 }
3542
3543
NeilBrown3687c062011-07-27 11:00:36 +10003544 /* Finish reconstruct operations initiated by the expansion process */
3545 if (sh->reconstruct_state == reconstruct_state_result) {
3546 struct stripe_head *sh_src
3547 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3548 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3549 /* sh cannot be written until sh_src has been read.
3550 * so arrange for sh to be delayed a little
3551 */
3552 set_bit(STRIPE_DELAYED, &sh->state);
3553 set_bit(STRIPE_HANDLE, &sh->state);
3554 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3555 &sh_src->state))
3556 atomic_inc(&conf->preread_active_stripes);
3557 release_stripe(sh_src);
3558 goto finish;
3559 }
3560 if (sh_src)
3561 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003562
NeilBrown3687c062011-07-27 11:00:36 +10003563 sh->reconstruct_state = reconstruct_state_idle;
3564 clear_bit(STRIPE_EXPANDING, &sh->state);
3565 for (i = conf->raid_disks; i--; ) {
3566 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3567 set_bit(R5_LOCKED, &sh->dev[i].flags);
3568 s.locked++;
3569 }
3570 }
3571
3572 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3573 !sh->reconstruct_state) {
3574 /* Need to write out all blocks after computing parity */
3575 sh->disks = conf->raid_disks;
3576 stripe_set_idx(sh->sector, conf, 0, sh);
3577 schedule_reconstruction(sh, &s, 1, 1);
3578 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3579 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3580 atomic_dec(&conf->reshape_stripes);
3581 wake_up(&conf->wait_for_overlap);
3582 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3583 }
3584
3585 if (s.expanding && s.locked == 0 &&
3586 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3587 handle_stripe_expansion(conf, sh);
3588
3589finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003590 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10003591 if (unlikely(s.blocked_rdev)) {
3592 if (conf->mddev->external)
3593 md_wait_for_blocked_rdev(s.blocked_rdev,
3594 conf->mddev);
3595 else
3596 /* Internal metadata will immediately
3597 * be written by raid5d, so we don't
3598 * need to wait here.
3599 */
3600 rdev_dec_pending(s.blocked_rdev,
3601 conf->mddev);
3602 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003603
NeilBrownbc2607f2011-07-28 11:39:22 +10003604 if (s.handle_bad_blocks)
3605 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003606 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003607 struct r5dev *dev = &sh->dev[i];
3608 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3609 /* We own a safe reference to the rdev */
3610 rdev = conf->disks[i].rdev;
3611 if (!rdev_set_badblocks(rdev, sh->sector,
3612 STRIPE_SECTORS, 0))
3613 md_error(conf->mddev, rdev);
3614 rdev_dec_pending(rdev, conf->mddev);
3615 }
NeilBrownb84db562011-07-28 11:39:23 +10003616 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3617 rdev = conf->disks[i].rdev;
3618 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003619 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10003620 rdev_dec_pending(rdev, conf->mddev);
3621 }
NeilBrown977df362011-12-23 10:17:53 +11003622 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3623 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11003624 if (!rdev)
3625 /* rdev have been moved down */
3626 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11003627 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003628 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11003629 rdev_dec_pending(rdev, conf->mddev);
3630 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003631 }
3632
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003633 if (s.ops_request)
3634 raid_run_ops(sh, s.ops_request);
3635
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003636 ops_run_io(sh, &s);
3637
NeilBrownc5709ef2011-07-26 11:35:20 +10003638 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003639 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003640 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003641 * have actually been submitted.
3642 */
3643 atomic_dec(&conf->preread_active_stripes);
3644 if (atomic_read(&conf->preread_active_stripes) <
3645 IO_THRESHOLD)
3646 md_wakeup_thread(conf->mddev->thread);
3647 }
3648
NeilBrownc5709ef2011-07-26 11:35:20 +10003649 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003650
Dan Williams257a4b42011-11-08 16:22:06 +11003651 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003652}
3653
NeilBrownd1688a62011-10-11 16:49:52 +11003654static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655{
3656 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3657 while (!list_empty(&conf->delayed_list)) {
3658 struct list_head *l = conf->delayed_list.next;
3659 struct stripe_head *sh;
3660 sh = list_entry(l, struct stripe_head, lru);
3661 list_del_init(l);
3662 clear_bit(STRIPE_DELAYED, &sh->state);
3663 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3664 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003665 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 }
NeilBrown482c0832011-04-18 18:25:42 +10003667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668}
3669
NeilBrownd1688a62011-10-11 16:49:52 +11003670static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003671{
3672 /* device_lock is held */
3673 struct list_head head;
3674 list_add(&head, &conf->bitmap_list);
3675 list_del_init(&conf->bitmap_list);
3676 while (!list_empty(&head)) {
3677 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3678 list_del_init(&sh->lru);
3679 atomic_inc(&sh->count);
3680 __release_stripe(conf, sh);
3681 }
3682}
3683
NeilBrownfd01b882011-10-11 16:47:53 +11003684int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003685{
NeilBrownd1688a62011-10-11 16:49:52 +11003686 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003687
3688 /* No difference between reads and writes. Just check
3689 * how busy the stripe_cache is
3690 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003691
NeilBrownf022b2f2006-10-03 01:15:56 -07003692 if (conf->inactive_blocked)
3693 return 1;
3694 if (conf->quiesce)
3695 return 1;
3696 if (list_empty_careful(&conf->inactive_list))
3697 return 1;
3698
3699 return 0;
3700}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003701EXPORT_SYMBOL_GPL(md_raid5_congested);
3702
3703static int raid5_congested(void *data, int bits)
3704{
NeilBrownfd01b882011-10-11 16:47:53 +11003705 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003706
3707 return mddev_congested(mddev, bits) ||
3708 md_raid5_congested(mddev, bits);
3709}
NeilBrownf022b2f2006-10-03 01:15:56 -07003710
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003711/* We want read requests to align with chunks where possible,
3712 * but write requests don't need to.
3713 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003714static int raid5_mergeable_bvec(struct request_queue *q,
3715 struct bvec_merge_data *bvm,
3716 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003717{
NeilBrownfd01b882011-10-11 16:47:53 +11003718 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003719 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003720 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003721 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003722 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003723
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003724 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003725 return biovec->bv_len; /* always allow writes to be mergeable */
3726
Andre Noll664e7c42009-06-18 08:45:27 +10003727 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3728 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003729 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3730 if (max < 0) max = 0;
3731 if (max <= biovec->bv_len && bio_sectors == 0)
3732 return biovec->bv_len;
3733 else
3734 return max;
3735}
3736
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003737
NeilBrownfd01b882011-10-11 16:47:53 +11003738static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003739{
3740 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003741 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003742 unsigned int bio_sectors = bio->bi_size >> 9;
3743
Andre Noll664e7c42009-06-18 08:45:27 +10003744 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3745 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003746 return chunk_sectors >=
3747 ((sector & (chunk_sectors - 1)) + bio_sectors);
3748}
3749
3750/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003751 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3752 * later sampled by raid5d.
3753 */
NeilBrownd1688a62011-10-11 16:49:52 +11003754static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003755{
3756 unsigned long flags;
3757
3758 spin_lock_irqsave(&conf->device_lock, flags);
3759
3760 bi->bi_next = conf->retry_read_aligned_list;
3761 conf->retry_read_aligned_list = bi;
3762
3763 spin_unlock_irqrestore(&conf->device_lock, flags);
3764 md_wakeup_thread(conf->mddev->thread);
3765}
3766
3767
NeilBrownd1688a62011-10-11 16:49:52 +11003768static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003769{
3770 struct bio *bi;
3771
3772 bi = conf->retry_read_aligned;
3773 if (bi) {
3774 conf->retry_read_aligned = NULL;
3775 return bi;
3776 }
3777 bi = conf->retry_read_aligned_list;
3778 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003779 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003780 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003781 /*
3782 * this sets the active strip count to 1 and the processed
3783 * strip count to zero (upper 8 bits)
3784 */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003785 bi->bi_phys_segments = 1; /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003786 }
3787
3788 return bi;
3789}
3790
3791
3792/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003793 * The "raid5_align_endio" should check if the read succeeded and if it
3794 * did, call bio_endio on the original bio (having bio_put the new bio
3795 * first).
3796 * If the read failed..
3797 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003798static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003799{
3800 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003801 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003802 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003803 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003804 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003805
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003806 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003807
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003808 rdev = (void*)raid_bi->bi_next;
3809 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003810 mddev = rdev->mddev;
3811 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003812
3813 rdev_dec_pending(rdev, conf->mddev);
3814
3815 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003816 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003817 if (atomic_dec_and_test(&conf->active_aligned_reads))
3818 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003819 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003820 }
3821
3822
Dan Williams45b42332007-07-09 11:56:43 -07003823 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003824
3825 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003826}
3827
Neil Brown387bb172007-02-08 14:20:29 -08003828static int bio_fits_rdev(struct bio *bi)
3829{
Jens Axboe165125e2007-07-24 09:28:11 +02003830 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003831
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003832 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003833 return 0;
3834 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003835 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003836 return 0;
3837
3838 if (q->merge_bvec_fn)
3839 /* it's too hard to apply the merge_bvec_fn at this stage,
3840 * just just give up
3841 */
3842 return 0;
3843
3844 return 1;
3845}
3846
3847
NeilBrownfd01b882011-10-11 16:47:53 +11003848static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003849{
NeilBrownd1688a62011-10-11 16:49:52 +11003850 struct r5conf *conf = mddev->private;
NeilBrown8553fe72009-12-14 12:49:47 +11003851 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003852 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11003853 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11003854 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003855
3856 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003857 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003858 return 0;
3859 }
3860 /*
NeilBrowna167f662010-10-26 18:31:13 +11003861 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003862 */
NeilBrowna167f662010-10-26 18:31:13 +11003863 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003864 if (!align_bi)
3865 return 0;
3866 /*
3867 * set bi_end_io to a new function, and set bi_private to the
3868 * original bio.
3869 */
3870 align_bi->bi_end_io = raid5_align_endio;
3871 align_bi->bi_private = raid_bio;
3872 /*
3873 * compute position
3874 */
NeilBrown112bf892009-03-31 14:39:38 +11003875 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3876 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003877 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003878
NeilBrown671488c2011-12-23 10:17:52 +11003879 end_sector = align_bi->bi_sector + (align_bi->bi_size >> 9);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003880 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11003881 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
3882 if (!rdev || test_bit(Faulty, &rdev->flags) ||
3883 rdev->recovery_offset < end_sector) {
3884 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3885 if (rdev &&
3886 (test_bit(Faulty, &rdev->flags) ||
3887 !(test_bit(In_sync, &rdev->flags) ||
3888 rdev->recovery_offset >= end_sector)))
3889 rdev = NULL;
3890 }
3891 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10003892 sector_t first_bad;
3893 int bad_sectors;
3894
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003895 atomic_inc(&rdev->nr_pending);
3896 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003897 raid_bio->bi_next = (void*)rdev;
3898 align_bi->bi_bdev = rdev->bdev;
3899 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003900
NeilBrown31c176e2011-07-28 11:39:22 +10003901 if (!bio_fits_rdev(align_bi) ||
3902 is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
3903 &first_bad, &bad_sectors)) {
3904 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08003905 bio_put(align_bi);
3906 rdev_dec_pending(rdev, mddev);
3907 return 0;
3908 }
3909
majianpeng6c0544e2012-06-12 08:31:10 +08003910 /* No reshape active, so we can trust rdev->data_offset */
3911 align_bi->bi_sector += rdev->data_offset;
3912
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003913 spin_lock_irq(&conf->device_lock);
3914 wait_event_lock_irq(conf->wait_for_stripe,
3915 conf->quiesce == 0,
3916 conf->device_lock, /* nothing */);
3917 atomic_inc(&conf->active_aligned_reads);
3918 spin_unlock_irq(&conf->device_lock);
3919
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003920 generic_make_request(align_bi);
3921 return 1;
3922 } else {
3923 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003924 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003925 return 0;
3926 }
3927}
3928
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003929/* __get_priority_stripe - get the next stripe to process
3930 *
3931 * Full stripe writes are allowed to pass preread active stripes up until
3932 * the bypass_threshold is exceeded. In general the bypass_count
3933 * increments when the handle_list is handled before the hold_list; however, it
3934 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3935 * stripe with in flight i/o. The bypass_count will be reset when the
3936 * head of the hold_list has changed, i.e. the head was promoted to the
3937 * handle_list.
3938 */
NeilBrownd1688a62011-10-11 16:49:52 +11003939static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003940{
3941 struct stripe_head *sh;
3942
3943 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3944 __func__,
3945 list_empty(&conf->handle_list) ? "empty" : "busy",
3946 list_empty(&conf->hold_list) ? "empty" : "busy",
3947 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3948
3949 if (!list_empty(&conf->handle_list)) {
3950 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3951
3952 if (list_empty(&conf->hold_list))
3953 conf->bypass_count = 0;
3954 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3955 if (conf->hold_list.next == conf->last_hold)
3956 conf->bypass_count++;
3957 else {
3958 conf->last_hold = conf->hold_list.next;
3959 conf->bypass_count -= conf->bypass_threshold;
3960 if (conf->bypass_count < 0)
3961 conf->bypass_count = 0;
3962 }
3963 }
3964 } else if (!list_empty(&conf->hold_list) &&
3965 ((conf->bypass_threshold &&
3966 conf->bypass_count > conf->bypass_threshold) ||
3967 atomic_read(&conf->pending_full_writes) == 0)) {
3968 sh = list_entry(conf->hold_list.next,
3969 typeof(*sh), lru);
3970 conf->bypass_count -= conf->bypass_threshold;
3971 if (conf->bypass_count < 0)
3972 conf->bypass_count = 0;
3973 } else
3974 return NULL;
3975
3976 list_del_init(&sh->lru);
3977 atomic_inc(&sh->count);
3978 BUG_ON(atomic_read(&sh->count) != 1);
3979 return sh;
3980}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003981
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07003982static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983{
NeilBrownd1688a62011-10-11 16:49:52 +11003984 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11003985 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 sector_t new_sector;
3987 sector_t logical_sector, last_sector;
3988 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01003989 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11003990 int remaining;
NeilBrown7c13edc2011-04-18 18:25:43 +10003991 int plugged;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992
Tejun Heoe9c74692010-09-03 11:56:18 +02003993 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
3994 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02003995 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07003996 }
3997
NeilBrown3d310eb2005-06-21 17:17:26 -07003998 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07003999
NeilBrown802ba062006-12-13 00:34:13 -08004000 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004001 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11004002 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004003 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004004
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4006 last_sector = bi->bi_sector + (bi->bi_size>>9);
4007 bi->bi_next = NULL;
4008 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07004009
NeilBrown7c13edc2011-04-18 18:25:43 +10004010 plugged = mddev_check_plugged(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
4012 DEFINE_WAIT(w);
NeilBrownb5663ba2009-03-31 14:39:38 +11004013 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08004014
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004015 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11004016 previous = 0;
NeilBrownb578d552006-03-27 01:18:12 -08004017 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004018 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11004019 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08004020 * 64bit on a 32bit platform, and so it might be
4021 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02004022 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08004023 * the lock is dropped, so once we get a reference
4024 * to the stripe that we think it is, we will have
4025 * to check again.
4026 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004027 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004028 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004029 ? logical_sector < conf->reshape_progress
4030 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004031 previous = 1;
4032 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10004033 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004034 ? logical_sector < conf->reshape_safe
4035 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08004036 spin_unlock_irq(&conf->device_lock);
4037 schedule();
4038 goto retry;
4039 }
4040 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004041 spin_unlock_irq(&conf->device_lock);
4042 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004043
NeilBrown112bf892009-03-31 14:39:38 +11004044 new_sector = raid5_compute_sector(conf, logical_sector,
4045 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11004046 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10004047 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 (unsigned long long)new_sector,
4049 (unsigned long long)logical_sector);
4050
NeilBrownb5663ba2009-03-31 14:39:38 +11004051 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10004052 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004054 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004055 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08004056 * stripe, so we must do the range check again.
4057 * Expansion could still move past after this
4058 * test, but as we are holding a reference to
4059 * 'sh', we know that if that happens,
4060 * STRIPE_EXPANDING will get set and the expansion
4061 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004062 */
4063 int must_retry = 0;
4064 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004065 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11004066 ? logical_sector >= conf->reshape_progress
4067 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004068 /* mismatch, need to try again */
4069 must_retry = 1;
4070 spin_unlock_irq(&conf->device_lock);
4071 if (must_retry) {
4072 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004073 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004074 goto retry;
4075 }
4076 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004077
Namhyung Kimffd96e32011-07-18 17:38:51 +10004078 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004079 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004080 logical_sector < mddev->suspend_hi) {
4081 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004082 /* As the suspend_* range is controlled by
4083 * userspace, we want an interruptible
4084 * wait.
4085 */
4086 flush_signals(current);
4087 prepare_to_wait(&conf->wait_for_overlap,
4088 &w, TASK_INTERRUPTIBLE);
4089 if (logical_sector >= mddev->suspend_lo &&
4090 logical_sector < mddev->suspend_hi)
4091 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004092 goto retry;
4093 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004094
4095 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004096 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004097 /* Stripe is busy expanding or
4098 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 * and wait a while
4100 */
NeilBrown482c0832011-04-18 18:25:42 +10004101 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 release_stripe(sh);
4103 schedule();
4104 goto retry;
4105 }
4106 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004107 set_bit(STRIPE_HANDLE, &sh->state);
4108 clear_bit(STRIPE_DELAYED, &sh->state);
Tejun Heoe9c74692010-09-03 11:56:18 +02004109 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004110 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4111 atomic_inc(&conf->preread_active_stripes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 } else {
4114 /* cannot get stripe for read-ahead, just give-up */
4115 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4116 finish_wait(&conf->wait_for_overlap, &w);
4117 break;
4118 }
4119
4120 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004121 if (!plugged)
4122 md_wakeup_thread(mddev->thread);
4123
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004125 remaining = raid5_dec_bi_phys_segments(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004126 spin_unlock_irq(&conf->device_lock);
4127 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128
NeilBrown16a53ec2006-06-26 00:27:38 -07004129 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004131
Neil Brown0e13fe232008-06-28 08:31:20 +10004132 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134}
4135
NeilBrownfd01b882011-10-11 16:47:53 +11004136static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004137
NeilBrownfd01b882011-10-11 16:47:53 +11004138static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139{
NeilBrown52c03292006-06-26 00:27:43 -07004140 /* reshaping is quite different to recovery/resync so it is
4141 * handled quite separately ... here.
4142 *
4143 * On each call to sync_request, we gather one chunk worth of
4144 * destination stripes and flag them as expanding.
4145 * Then we find all the source stripes and request reads.
4146 * As the reads complete, handle_stripe will copy the data
4147 * into the destination stripe and release that stripe.
4148 */
NeilBrownd1688a62011-10-11 16:49:52 +11004149 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004151 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004152 int raid_disks = conf->previous_raid_disks;
4153 int data_disks = raid_disks - conf->max_degraded;
4154 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004155 int i;
4156 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004157 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004158 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004159 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004160 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004161
NeilBrownfef9c612009-03-31 15:16:46 +11004162 if (sector_nr == 0) {
4163 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10004164 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004165 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4166 sector_nr = raid5_size(mddev, 0, 0)
4167 - conf->reshape_progress;
NeilBrown2c810cd2012-05-21 09:27:00 +10004168 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004169 conf->reshape_progress > 0)
4170 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004171 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004172 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004173 mddev->curr_resync_completed = sector_nr;
4174 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004175 *skipped = 1;
4176 return sector_nr;
4177 }
NeilBrown52c03292006-06-26 00:27:43 -07004178 }
4179
NeilBrown7a661382009-03-31 15:21:40 +11004180 /* We need to process a full chunk at a time.
4181 * If old and new chunk sizes differ, we need to process the
4182 * largest of these
4183 */
Andre Noll664e7c42009-06-18 08:45:27 +10004184 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4185 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004186 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004187 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004188
NeilBrownb5254dd2012-05-21 09:27:01 +10004189 /* We update the metadata at least every 10 seconds, or when
4190 * the data about to be copied would over-write the source of
4191 * the data at the front of the range. i.e. one new_stripe
4192 * along from reshape_progress new_maps to after where
4193 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004194 */
NeilBrownfef9c612009-03-31 15:16:46 +11004195 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004196 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004197 readpos = conf->reshape_progress;
4198 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004199 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004200 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10004201 if (mddev->reshape_backwards) {
NeilBrowned37d832009-05-27 21:39:05 +10004202 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004203 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004204 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004205 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004206 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004207 readpos -= min_t(sector_t, reshape_sectors, readpos);
4208 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004209 }
NeilBrown52c03292006-06-26 00:27:43 -07004210
NeilBrownb5254dd2012-05-21 09:27:01 +10004211 /* Having calculated the 'writepos' possibly use it
4212 * to set 'stripe_addr' which is where we will write to.
4213 */
4214 if (mddev->reshape_backwards) {
4215 BUG_ON(conf->reshape_progress == 0);
4216 stripe_addr = writepos;
4217 BUG_ON((mddev->dev_sectors &
4218 ~((sector_t)reshape_sectors - 1))
4219 - reshape_sectors - stripe_addr
4220 != sector_nr);
4221 } else {
4222 BUG_ON(writepos != sector_nr + reshape_sectors);
4223 stripe_addr = sector_nr;
4224 }
4225
NeilBrownc8f517c2009-03-31 15:28:40 +11004226 /* 'writepos' is the most advanced device address we might write.
4227 * 'readpos' is the least advanced device address we might read.
4228 * 'safepos' is the least address recorded in the metadata as having
4229 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10004230 * If there is a min_offset_diff, these are adjusted either by
4231 * increasing the safepos/readpos if diff is negative, or
4232 * increasing writepos if diff is positive.
4233 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11004234 * ensure safety in the face of a crash - that must be done by userspace
4235 * making a backup of the data. So in that case there is no particular
4236 * rush to update metadata.
4237 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4238 * update the metadata to advance 'safepos' to match 'readpos' so that
4239 * we can be safe in the event of a crash.
4240 * So we insist on updating metadata if safepos is behind writepos and
4241 * readpos is beyond writepos.
4242 * In any case, update the metadata every 10 seconds.
4243 * Maybe that number should be configurable, but I'm not sure it is
4244 * worth it.... maybe it could be a multiple of safemode_delay???
4245 */
NeilBrownb5254dd2012-05-21 09:27:01 +10004246 if (conf->min_offset_diff < 0) {
4247 safepos += -conf->min_offset_diff;
4248 readpos += -conf->min_offset_diff;
4249 } else
4250 writepos += conf->min_offset_diff;
4251
NeilBrown2c810cd2012-05-21 09:27:00 +10004252 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11004253 ? (safepos > writepos && readpos < writepos)
4254 : (safepos < writepos && readpos > writepos)) ||
4255 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004256 /* Cannot proceed until we've updated the superblock... */
4257 wait_event(conf->wait_for_overlap,
4258 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004259 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004260 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004261 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07004262 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004263 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07004264 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004265 kthread_should_stop());
4266 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004267 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004268 spin_unlock_irq(&conf->device_lock);
4269 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004270 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004271 }
4272
NeilBrownab69ae12009-03-31 15:26:47 +11004273 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004274 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004275 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004276 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004277 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004278 set_bit(STRIPE_EXPANDING, &sh->state);
4279 atomic_inc(&conf->reshape_stripes);
4280 /* If any of this stripe is beyond the end of the old
4281 * array, then we need to zero those blocks
4282 */
4283 for (j=sh->disks; j--;) {
4284 sector_t s;
4285 if (j == sh->pd_idx)
4286 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004287 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004288 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004289 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004290 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004291 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004292 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004293 continue;
4294 }
4295 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4296 set_bit(R5_Expanded, &sh->dev[j].flags);
4297 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4298 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004299 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004300 set_bit(STRIPE_EXPAND_READY, &sh->state);
4301 set_bit(STRIPE_HANDLE, &sh->state);
4302 }
NeilBrownab69ae12009-03-31 15:26:47 +11004303 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004304 }
4305 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004306 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11004307 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004308 else
NeilBrown7a661382009-03-31 15:21:40 +11004309 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004310 spin_unlock_irq(&conf->device_lock);
4311 /* Ok, those stripe are ready. We can start scheduling
4312 * reads on the source stripes.
4313 * The source stripes are determined by mapping the first and last
4314 * block on the destination stripes.
4315 */
NeilBrown52c03292006-06-26 00:27:43 -07004316 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004317 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004318 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004319 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004320 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004321 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004322 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004323 if (last_sector >= mddev->dev_sectors)
4324 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004325 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004326 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004327 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4328 set_bit(STRIPE_HANDLE, &sh->state);
4329 release_stripe(sh);
4330 first_sector += STRIPE_SECTORS;
4331 }
NeilBrownab69ae12009-03-31 15:26:47 +11004332 /* Now that the sources are clearly marked, we can release
4333 * the destination stripes
4334 */
4335 while (!list_empty(&stripes)) {
4336 sh = list_entry(stripes.next, struct stripe_head, lru);
4337 list_del_init(&sh->lru);
4338 release_stripe(sh);
4339 }
NeilBrownc6207272008-02-06 01:39:52 -08004340 /* If this takes us to the resync_max point where we have to pause,
4341 * then we need to write out the superblock.
4342 */
NeilBrown7a661382009-03-31 15:21:40 +11004343 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004344 if ((sector_nr - mddev->curr_resync_completed) * 2
4345 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004346 /* Cannot proceed until we've updated the superblock... */
4347 wait_event(conf->wait_for_overlap,
4348 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004349 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004350 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004351 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004352 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4353 md_wakeup_thread(mddev->thread);
4354 wait_event(mddev->sb_wait,
4355 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4356 || kthread_should_stop());
4357 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004358 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004359 spin_unlock_irq(&conf->device_lock);
4360 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004361 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004362 }
NeilBrown7a661382009-03-31 15:21:40 +11004363 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004364}
4365
4366/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004367static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004368{
NeilBrownd1688a62011-10-11 16:49:52 +11004369 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004370 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004371 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004372 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004373 int still_degraded = 0;
4374 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375
NeilBrown72626682005-09-09 16:23:54 -07004376 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004378
NeilBrown29269552006-03-27 01:18:10 -08004379 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4380 end_reshape(conf);
4381 return 0;
4382 }
NeilBrown72626682005-09-09 16:23:54 -07004383
4384 if (mddev->curr_resync < max_sector) /* aborted */
4385 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4386 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004387 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004388 conf->fullsync = 0;
4389 bitmap_close_sync(mddev->bitmap);
4390
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 return 0;
4392 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004393
NeilBrown64bd6602009-08-03 10:59:58 +10004394 /* Allow raid5_quiesce to complete */
4395 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4396
NeilBrown52c03292006-06-26 00:27:43 -07004397 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4398 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004399
NeilBrownc6207272008-02-06 01:39:52 -08004400 /* No need to check resync_max as we never do more than one
4401 * stripe, and as resync_max will always be on a chunk boundary,
4402 * if the check in md_do_sync didn't fire, there is no chance
4403 * of overstepping resync_max here
4404 */
4405
NeilBrown16a53ec2006-06-26 00:27:38 -07004406 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 * to resync, then assert that we are finished, because there is
4408 * nothing we can do.
4409 */
NeilBrown3285edf2006-06-26 00:27:55 -07004410 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004411 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004412 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004413 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 return rv;
4415 }
NeilBrown72626682005-09-09 16:23:54 -07004416 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004417 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004418 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4419 /* we can skip this block, and probably more */
4420 sync_blocks /= STRIPE_SECTORS;
4421 *skipped = 1;
4422 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424
NeilBrownb47490c2008-02-06 01:39:50 -08004425 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4426
NeilBrowna8c906c2009-06-09 14:39:59 +10004427 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004429 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004431 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004433 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004435 /* Need to check if array will still be degraded after recovery/resync
4436 * We don't need to check the 'failed' flag as when that gets set,
4437 * recovery aborts.
4438 */
NeilBrownf001a702009-06-09 14:30:31 +10004439 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004440 if (conf->disks[i].rdev == NULL)
4441 still_degraded = 1;
4442
4443 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4444
NeilBrown83206d62011-07-26 11:19:49 +10004445 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446
NeilBrown14425772009-10-16 15:55:25 +11004447 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 release_stripe(sh);
4449
4450 return STRIPE_SECTORS;
4451}
4452
NeilBrownd1688a62011-10-11 16:49:52 +11004453static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004454{
4455 /* We may not be able to submit a whole bio at once as there
4456 * may not be enough stripe_heads available.
4457 * We cannot pre-allocate enough stripe_heads as we may need
4458 * more than exist in the cache (if we allow ever large chunks).
4459 * So we do one stripe head at a time and record in
4460 * ->bi_hw_segments how many have been done.
4461 *
4462 * We *know* that this entire raid_bio is in one chunk, so
4463 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4464 */
4465 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004466 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004467 sector_t sector, logical_sector, last_sector;
4468 int scnt = 0;
4469 int remaining;
4470 int handled = 0;
4471
4472 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004473 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004474 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004475 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4476
4477 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004478 logical_sector += STRIPE_SECTORS,
4479 sector += STRIPE_SECTORS,
4480 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004481
Jens Axboe960e7392008-08-15 10:41:18 +02004482 if (scnt < raid5_bi_hw_segments(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004483 /* already done this stripe */
4484 continue;
4485
NeilBrowna8c906c2009-06-09 14:39:59 +10004486 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004487
4488 if (!sh) {
4489 /* failed to get a stripe - must wait */
Jens Axboe960e7392008-08-15 10:41:18 +02004490 raid5_set_bi_hw_segments(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004491 conf->retry_read_aligned = raid_bio;
4492 return handled;
4493 }
4494
Neil Brown387bb172007-02-08 14:20:29 -08004495 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4496 release_stripe(sh);
Jens Axboe960e7392008-08-15 10:41:18 +02004497 raid5_set_bi_hw_segments(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004498 conf->retry_read_aligned = raid_bio;
4499 return handled;
4500 }
4501
Dan Williams36d1c642009-07-14 11:48:22 -07004502 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004503 release_stripe(sh);
4504 handled++;
4505 }
4506 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004507 remaining = raid5_dec_bi_phys_segments(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004508 spin_unlock_irq(&conf->device_lock);
Neil Brown0e13fe232008-06-28 08:31:20 +10004509 if (remaining == 0)
4510 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004511 if (atomic_dec_and_test(&conf->active_aligned_reads))
4512 wake_up(&conf->wait_for_stripe);
4513 return handled;
4514}
4515
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004516
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517/*
4518 * This is our raid5 kernel thread.
4519 *
4520 * We scan the hash table for stripes which can be handled now.
4521 * During the scan, completed stripes are saved for us by the interrupt
4522 * handler, so that they will not have to wait for our next wakeup.
4523 */
NeilBrownfd01b882011-10-11 16:47:53 +11004524static void raid5d(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525{
4526 struct stripe_head *sh;
NeilBrownd1688a62011-10-11 16:49:52 +11004527 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004529 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530
Dan Williams45b42332007-07-09 11:56:43 -07004531 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532
4533 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004535 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 handled = 0;
4537 spin_lock_irq(&conf->device_lock);
4538 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004539 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540
NeilBrown7c13edc2011-04-18 18:25:43 +10004541 if (atomic_read(&mddev->plug_cnt) == 0 &&
4542 !list_empty(&conf->bitmap_list)) {
4543 /* Now is a good time to flush some bitmap updates */
4544 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004545 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004546 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004547 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004548 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004549 activate_bit_delay(conf);
4550 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004551 if (atomic_read(&mddev->plug_cnt) == 0)
4552 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004553
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004554 while ((bio = remove_bio_from_retry(conf))) {
4555 int ok;
4556 spin_unlock_irq(&conf->device_lock);
4557 ok = retry_aligned_read(conf, bio);
4558 spin_lock_irq(&conf->device_lock);
4559 if (!ok)
4560 break;
4561 handled++;
4562 }
4563
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004564 sh = __get_priority_stripe(conf);
4565
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004566 if (!sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 spin_unlock_irq(&conf->device_lock);
4569
4570 handled++;
Dan Williams417b8d42009-10-16 16:25:22 +11004571 handle_stripe(sh);
4572 release_stripe(sh);
4573 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574
NeilBrownde393cd2011-07-28 11:31:48 +10004575 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
4576 md_check_recovery(mddev);
4577
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 spin_lock_irq(&conf->device_lock);
4579 }
Dan Williams45b42332007-07-09 11:56:43 -07004580 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581
4582 spin_unlock_irq(&conf->device_lock);
4583
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004584 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004585 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586
Dan Williams45b42332007-07-09 11:56:43 -07004587 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588}
4589
NeilBrown3f294f42005-11-08 21:39:25 -08004590static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004591raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004592{
NeilBrownd1688a62011-10-11 16:49:52 +11004593 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004594 if (conf)
4595 return sprintf(page, "%d\n", conf->max_nr_stripes);
4596 else
4597 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004598}
4599
NeilBrownc41d4ac2010-06-01 19:37:24 +10004600int
NeilBrownfd01b882011-10-11 16:47:53 +11004601raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004602{
NeilBrownd1688a62011-10-11 16:49:52 +11004603 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004604 int err;
4605
4606 if (size <= 16 || size > 32768)
4607 return -EINVAL;
4608 while (size < conf->max_nr_stripes) {
4609 if (drop_one_stripe(conf))
4610 conf->max_nr_stripes--;
4611 else
4612 break;
4613 }
4614 err = md_allow_write(mddev);
4615 if (err)
4616 return err;
4617 while (size > conf->max_nr_stripes) {
4618 if (grow_one_stripe(conf))
4619 conf->max_nr_stripes++;
4620 else break;
4621 }
4622 return 0;
4623}
4624EXPORT_SYMBOL(raid5_set_cache_size);
4625
NeilBrown3f294f42005-11-08 21:39:25 -08004626static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004627raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004628{
NeilBrownd1688a62011-10-11 16:49:52 +11004629 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004630 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004631 int err;
4632
NeilBrown3f294f42005-11-08 21:39:25 -08004633 if (len >= PAGE_SIZE)
4634 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004635 if (!conf)
4636 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004637
Dan Williams4ef197d82008-04-28 02:15:54 -07004638 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004639 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004640 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004641 if (err)
4642 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004643 return len;
4644}
NeilBrown007583c2005-11-08 21:39:30 -08004645
NeilBrown96de1e62005-11-08 21:39:39 -08004646static struct md_sysfs_entry
4647raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4648 raid5_show_stripe_cache_size,
4649 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004650
4651static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004652raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004653{
NeilBrownd1688a62011-10-11 16:49:52 +11004654 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004655 if (conf)
4656 return sprintf(page, "%d\n", conf->bypass_threshold);
4657 else
4658 return 0;
4659}
4660
4661static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004662raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004663{
NeilBrownd1688a62011-10-11 16:49:52 +11004664 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004665 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004666 if (len >= PAGE_SIZE)
4667 return -EINVAL;
4668 if (!conf)
4669 return -ENODEV;
4670
Dan Williams4ef197d82008-04-28 02:15:54 -07004671 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004672 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004673 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004674 return -EINVAL;
4675 conf->bypass_threshold = new;
4676 return len;
4677}
4678
4679static struct md_sysfs_entry
4680raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4681 S_IRUGO | S_IWUSR,
4682 raid5_show_preread_threshold,
4683 raid5_store_preread_threshold);
4684
4685static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004686stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004687{
NeilBrownd1688a62011-10-11 16:49:52 +11004688 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004689 if (conf)
4690 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4691 else
4692 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004693}
4694
NeilBrown96de1e62005-11-08 21:39:39 -08004695static struct md_sysfs_entry
4696raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004697
NeilBrown007583c2005-11-08 21:39:30 -08004698static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004699 &raid5_stripecache_size.attr,
4700 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004701 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004702 NULL,
4703};
NeilBrown007583c2005-11-08 21:39:30 -08004704static struct attribute_group raid5_attrs_group = {
4705 .name = NULL,
4706 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004707};
4708
Dan Williams80c3a6c2009-03-17 18:10:40 -07004709static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11004710raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07004711{
NeilBrownd1688a62011-10-11 16:49:52 +11004712 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004713
4714 if (!sectors)
4715 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004716 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11004717 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11004718 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004719
Andre Noll9d8f0362009-06-18 08:45:01 +10004720 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004721 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004722 return sectors * (raid_disks - conf->max_degraded);
4723}
4724
NeilBrownd1688a62011-10-11 16:49:52 +11004725static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004726{
4727 struct raid5_percpu *percpu;
4728 unsigned long cpu;
4729
4730 if (!conf->percpu)
4731 return;
4732
4733 get_online_cpus();
4734 for_each_possible_cpu(cpu) {
4735 percpu = per_cpu_ptr(conf->percpu, cpu);
4736 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004737 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004738 }
4739#ifdef CONFIG_HOTPLUG_CPU
4740 unregister_cpu_notifier(&conf->cpu_notify);
4741#endif
4742 put_online_cpus();
4743
4744 free_percpu(conf->percpu);
4745}
4746
NeilBrownd1688a62011-10-11 16:49:52 +11004747static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10004748{
4749 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004750 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10004751 kfree(conf->disks);
4752 kfree(conf->stripe_hashtbl);
4753 kfree(conf);
4754}
4755
Dan Williams36d1c642009-07-14 11:48:22 -07004756#ifdef CONFIG_HOTPLUG_CPU
4757static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4758 void *hcpu)
4759{
NeilBrownd1688a62011-10-11 16:49:52 +11004760 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07004761 long cpu = (long)hcpu;
4762 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4763
4764 switch (action) {
4765 case CPU_UP_PREPARE:
4766 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07004767 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07004768 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004769 if (!percpu->scribble)
4770 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4771
4772 if (!percpu->scribble ||
4773 (conf->level == 6 && !percpu->spare_page)) {
4774 safe_put_page(percpu->spare_page);
4775 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004776 pr_err("%s: failed memory allocation for cpu%ld\n",
4777 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07004778 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07004779 }
4780 break;
4781 case CPU_DEAD:
4782 case CPU_DEAD_FROZEN:
4783 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004784 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004785 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004786 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07004787 break;
4788 default:
4789 break;
4790 }
4791 return NOTIFY_OK;
4792}
4793#endif
4794
NeilBrownd1688a62011-10-11 16:49:52 +11004795static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004796{
4797 unsigned long cpu;
4798 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09004799 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004800 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004801 int err;
4802
Dan Williams36d1c642009-07-14 11:48:22 -07004803 allcpus = alloc_percpu(struct raid5_percpu);
4804 if (!allcpus)
4805 return -ENOMEM;
4806 conf->percpu = allcpus;
4807
4808 get_online_cpus();
4809 err = 0;
4810 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07004811 if (conf->level == 6) {
4812 spare_page = alloc_page(GFP_KERNEL);
4813 if (!spare_page) {
4814 err = -ENOMEM;
4815 break;
4816 }
4817 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
4818 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11004819 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004820 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07004821 err = -ENOMEM;
4822 break;
4823 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07004824 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004825 }
4826#ifdef CONFIG_HOTPLUG_CPU
4827 conf->cpu_notify.notifier_call = raid456_cpu_notify;
4828 conf->cpu_notify.priority = 0;
4829 if (err == 0)
4830 err = register_cpu_notifier(&conf->cpu_notify);
4831#endif
4832 put_online_cpus();
4833
4834 return err;
4835}
4836
NeilBrownd1688a62011-10-11 16:49:52 +11004837static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838{
NeilBrownd1688a62011-10-11 16:49:52 +11004839 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004840 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11004841 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 struct disk_info *disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843
NeilBrown91adb562009-03-31 14:39:39 +11004844 if (mddev->new_level != 5
4845 && mddev->new_level != 4
4846 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10004847 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004848 mdname(mddev), mddev->new_level);
4849 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 }
NeilBrown91adb562009-03-31 14:39:39 +11004851 if ((mddev->new_level == 5
4852 && !algorithm_valid_raid5(mddev->new_layout)) ||
4853 (mddev->new_level == 6
4854 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004855 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11004856 mdname(mddev), mddev->new_layout);
4857 return ERR_PTR(-EIO);
4858 }
4859 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10004860 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004861 mdname(mddev), mddev->raid_disks);
4862 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11004863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864
Andre Noll664e7c42009-06-18 08:45:27 +10004865 if (!mddev->new_chunk_sectors ||
4866 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
4867 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10004868 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
4869 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11004870 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11004871 }
4872
NeilBrownd1688a62011-10-11 16:49:52 +11004873 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11004874 if (conf == NULL)
4875 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11004876 spin_lock_init(&conf->device_lock);
4877 init_waitqueue_head(&conf->wait_for_stripe);
4878 init_waitqueue_head(&conf->wait_for_overlap);
4879 INIT_LIST_HEAD(&conf->handle_list);
4880 INIT_LIST_HEAD(&conf->hold_list);
4881 INIT_LIST_HEAD(&conf->delayed_list);
4882 INIT_LIST_HEAD(&conf->bitmap_list);
4883 INIT_LIST_HEAD(&conf->inactive_list);
4884 atomic_set(&conf->active_stripes, 0);
4885 atomic_set(&conf->preread_active_stripes, 0);
4886 atomic_set(&conf->active_aligned_reads, 0);
4887 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11004888 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11004889
4890 conf->raid_disks = mddev->raid_disks;
4891 if (mddev->reshape_position == MaxSector)
4892 conf->previous_raid_disks = mddev->raid_disks;
4893 else
4894 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004895 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
4896 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11004897
NeilBrown5e5e3e72009-10-16 16:35:30 +11004898 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11004899 GFP_KERNEL);
4900 if (!conf->disks)
4901 goto abort;
4902
4903 conf->mddev = mddev;
4904
4905 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
4906 goto abort;
4907
Dan Williams36d1c642009-07-14 11:48:22 -07004908 conf->level = mddev->new_level;
4909 if (raid5_alloc_percpu(conf) != 0)
4910 goto abort;
4911
NeilBrown0c55e022010-05-03 14:09:02 +10004912 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11004913
NeilBrowndafb20f2012-03-19 12:46:39 +11004914 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11004915 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004916 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11004917 || raid_disk < 0)
4918 continue;
4919 disk = conf->disks + raid_disk;
4920
NeilBrown17045f52011-12-23 10:17:53 +11004921 if (test_bit(Replacement, &rdev->flags)) {
4922 if (disk->replacement)
4923 goto abort;
4924 disk->replacement = rdev;
4925 } else {
4926 if (disk->rdev)
4927 goto abort;
4928 disk->rdev = rdev;
4929 }
NeilBrown91adb562009-03-31 14:39:39 +11004930
4931 if (test_bit(In_sync, &rdev->flags)) {
4932 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10004933 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
4934 " disk %d\n",
4935 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05004936 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11004937 /* Cannot rely on bitmap to complete recovery */
4938 conf->fullsync = 1;
4939 }
4940
Andre Noll09c9e5f2009-06-18 08:45:55 +10004941 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11004942 conf->level = mddev->new_level;
4943 if (conf->level == 6)
4944 conf->max_degraded = 2;
4945 else
4946 conf->max_degraded = 1;
4947 conf->algorithm = mddev->new_layout;
4948 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11004949 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11004950 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10004951 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11004952 conf->prev_algo = mddev->layout;
4953 }
NeilBrown91adb562009-03-31 14:39:39 +11004954
4955 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11004956 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11004957 if (grow_stripes(conf, conf->max_nr_stripes)) {
4958 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004959 "md/raid:%s: couldn't allocate %dkB for buffers\n",
4960 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004961 goto abort;
4962 } else
NeilBrown0c55e022010-05-03 14:09:02 +10004963 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
4964 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004965
NeilBrown0da3c612009-09-23 18:09:45 +10004966 conf->thread = md_register_thread(raid5d, mddev, NULL);
NeilBrown91adb562009-03-31 14:39:39 +11004967 if (!conf->thread) {
4968 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004969 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11004970 mdname(mddev));
4971 goto abort;
4972 }
4973
4974 return conf;
4975
4976 abort:
4977 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10004978 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11004979 return ERR_PTR(-EIO);
4980 } else
4981 return ERR_PTR(-ENOMEM);
4982}
4983
NeilBrownc148ffd2009-11-13 17:47:00 +11004984
4985static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
4986{
4987 switch (algo) {
4988 case ALGORITHM_PARITY_0:
4989 if (raid_disk < max_degraded)
4990 return 1;
4991 break;
4992 case ALGORITHM_PARITY_N:
4993 if (raid_disk >= raid_disks - max_degraded)
4994 return 1;
4995 break;
4996 case ALGORITHM_PARITY_0_6:
4997 if (raid_disk == 0 ||
4998 raid_disk == raid_disks - 1)
4999 return 1;
5000 break;
5001 case ALGORITHM_LEFT_ASYMMETRIC_6:
5002 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5003 case ALGORITHM_LEFT_SYMMETRIC_6:
5004 case ALGORITHM_RIGHT_SYMMETRIC_6:
5005 if (raid_disk == raid_disks - 1)
5006 return 1;
5007 }
5008 return 0;
5009}
5010
NeilBrownfd01b882011-10-11 16:47:53 +11005011static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11005012{
NeilBrownd1688a62011-10-11 16:49:52 +11005013 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10005014 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11005015 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11005016 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11005017 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11005018 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10005019 long long min_offset_diff = 0;
5020 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11005021
Andre Noll8c6ac862009-06-18 08:48:06 +10005022 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10005023 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10005024 " -- starting background reconstruction\n",
5025 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10005026
5027 rdev_for_each(rdev, mddev) {
5028 long long diff;
5029 if (rdev->raid_disk < 0)
5030 continue;
5031 diff = (rdev->new_data_offset - rdev->data_offset);
5032 if (first) {
5033 min_offset_diff = diff;
5034 first = 0;
5035 } else if (mddev->reshape_backwards &&
5036 diff < min_offset_diff)
5037 min_offset_diff = diff;
5038 else if (!mddev->reshape_backwards &&
5039 diff > min_offset_diff)
5040 min_offset_diff = diff;
5041 }
5042
NeilBrownf6705572006-03-27 01:18:11 -08005043 if (mddev->reshape_position != MaxSector) {
5044 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10005045 * Difficulties arise if the stripe we would write to
5046 * next is at or after the stripe we would read from next.
5047 * For a reshape that changes the number of devices, this
5048 * is only possible for a very short time, and mdadm makes
5049 * sure that time appears to have past before assembling
5050 * the array. So we fail if that time hasn't passed.
5051 * For a reshape that keeps the number of devices the same
5052 * mdadm must be monitoring the reshape can keeping the
5053 * critical areas read-only and backed up. It will start
5054 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08005055 */
5056 sector_t here_new, here_old;
5057 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11005058 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08005059
NeilBrown88ce4932009-03-31 15:24:23 +11005060 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10005061 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08005062 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08005063 mdname(mddev));
5064 return -EINVAL;
5065 }
NeilBrownf6705572006-03-27 01:18:11 -08005066 old_disks = mddev->raid_disks - mddev->delta_disks;
5067 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08005068 * further up in new geometry must map after here in old
5069 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08005070 */
5071 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10005072 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005073 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005074 printk(KERN_ERR "md/raid:%s: reshape_position not "
5075 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005076 return -EINVAL;
5077 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005078 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005079 /* here_new is the stripe we will write to */
5080 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10005081 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005082 (old_disks-max_degraded));
5083 /* here_old is the first stripe that we might need to read
5084 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10005085 if (mddev->delta_disks == 0) {
NeilBrownb5254dd2012-05-21 09:27:01 +10005086 if ((here_new * mddev->new_chunk_sectors !=
5087 here_old * mddev->chunk_sectors)) {
5088 printk(KERN_ERR "md/raid:%s: reshape position is"
5089 " confused - aborting\n", mdname(mddev));
5090 return -EINVAL;
5091 }
NeilBrown67ac6012009-08-13 10:06:24 +10005092 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10005093 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10005094 * and taking constant backups.
5095 * mdadm always starts a situation like this in
5096 * readonly mode so it can take control before
5097 * allowing any writes. So just check for that.
5098 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005099 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
5100 abs(min_offset_diff) >= mddev->new_chunk_sectors)
5101 /* not really in-place - so OK */;
5102 else if (mddev->ro == 0) {
5103 printk(KERN_ERR "md/raid:%s: in-place reshape "
5104 "must be started in read-only mode "
5105 "- aborting\n",
NeilBrown0c55e022010-05-03 14:09:02 +10005106 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005107 return -EINVAL;
5108 }
NeilBrown2c810cd2012-05-21 09:27:00 +10005109 } else if (mddev->reshape_backwards
NeilBrownb5254dd2012-05-21 09:27:01 +10005110 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
NeilBrown67ac6012009-08-13 10:06:24 +10005111 here_old * mddev->chunk_sectors)
5112 : (here_new * mddev->new_chunk_sectors >=
NeilBrownb5254dd2012-05-21 09:27:01 +10005113 here_old * mddev->chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08005114 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005115 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5116 "auto-recovery - aborting.\n",
5117 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005118 return -EINVAL;
5119 }
NeilBrown0c55e022010-05-03 14:09:02 +10005120 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5121 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005122 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005123 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005124 BUG_ON(mddev->level != mddev->new_level);
5125 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005126 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005127 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005128 }
5129
NeilBrown245f46c2009-03-31 14:39:39 +11005130 if (mddev->private == NULL)
5131 conf = setup_conf(mddev);
5132 else
5133 conf = mddev->private;
5134
NeilBrown91adb562009-03-31 14:39:39 +11005135 if (IS_ERR(conf))
5136 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005137
NeilBrownb5254dd2012-05-21 09:27:01 +10005138 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11005139 mddev->thread = conf->thread;
5140 conf->thread = NULL;
5141 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142
NeilBrown17045f52011-12-23 10:17:53 +11005143 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5144 i++) {
5145 rdev = conf->disks[i].rdev;
5146 if (!rdev && conf->disks[i].replacement) {
5147 /* The replacement is all we have yet */
5148 rdev = conf->disks[i].replacement;
5149 conf->disks[i].replacement = NULL;
5150 clear_bit(Replacement, &rdev->flags);
5151 conf->disks[i].rdev = rdev;
5152 }
5153 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11005154 continue;
NeilBrown17045f52011-12-23 10:17:53 +11005155 if (conf->disks[i].replacement &&
5156 conf->reshape_progress != MaxSector) {
5157 /* replacements and reshape simply do not mix. */
5158 printk(KERN_ERR "md: cannot handle concurrent "
5159 "replacement and reshape.\n");
5160 goto abort;
5161 }
NeilBrown2f115882010-06-17 17:41:03 +10005162 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005163 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005164 continue;
5165 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005166 /* This disc is not fully in-sync. However if it
5167 * just stored parity (beyond the recovery_offset),
5168 * when we don't need to be concerned about the
5169 * array being dirty.
5170 * When reshape goes 'backwards', we never have
5171 * partially completed devices, so we only need
5172 * to worry about reshape going forwards.
5173 */
5174 /* Hack because v0.91 doesn't store recovery_offset properly. */
5175 if (mddev->major_version == 0 &&
5176 mddev->minor_version > 90)
5177 rdev->recovery_offset = reshape_offset;
5178
NeilBrownc148ffd2009-11-13 17:47:00 +11005179 if (rdev->recovery_offset < reshape_offset) {
5180 /* We need to check old and new layout */
5181 if (!only_parity(rdev->raid_disk,
5182 conf->algorithm,
5183 conf->raid_disks,
5184 conf->max_degraded))
5185 continue;
5186 }
5187 if (!only_parity(rdev->raid_disk,
5188 conf->prev_algo,
5189 conf->previous_raid_disks,
5190 conf->max_degraded))
5191 continue;
5192 dirty_parity_disks++;
5193 }
NeilBrown91adb562009-03-31 14:39:39 +11005194
NeilBrown17045f52011-12-23 10:17:53 +11005195 /*
5196 * 0 for a fully functional array, 1 or 2 for a degraded array.
5197 */
NeilBrown908f4fb2011-12-23 10:17:50 +11005198 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199
NeilBrown674806d2010-06-16 17:17:53 +10005200 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005201 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07005203 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 goto abort;
5205 }
5206
NeilBrown91adb562009-03-31 14:39:39 +11005207 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10005208 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11005209 mddev->resync_max_sectors = mddev->dev_sectors;
5210
NeilBrownc148ffd2009-11-13 17:47:00 +11005211 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005213 if (mddev->ok_start_degraded)
5214 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10005215 "md/raid:%s: starting dirty degraded array"
5216 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005217 mdname(mddev));
5218 else {
5219 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005220 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005221 mdname(mddev));
5222 goto abort;
5223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224 }
5225
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10005227 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5228 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11005229 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5230 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 else
NeilBrown0c55e022010-05-03 14:09:02 +10005232 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5233 " out of %d devices, algorithm %d\n",
5234 mdname(mddev), conf->level,
5235 mddev->raid_disks - mddev->degraded,
5236 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237
5238 print_raid5_conf(conf);
5239
NeilBrownfef9c612009-03-31 15:16:46 +11005240 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11005241 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005242 atomic_set(&conf->reshape_stripes, 0);
5243 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5244 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5245 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5246 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5247 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005248 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005249 }
5250
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251
5252 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005253 if (mddev->to_remove == &raid5_attrs_group)
5254 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005255 else if (mddev->kobj.sd &&
5256 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005257 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10005258 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08005259 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10005260 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5261
5262 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005263 int chunk_size;
NeilBrown4a5add42010-06-01 19:37:28 +10005264 /* read-ahead size must cover two whole stripes, which
5265 * is 2 * (datadisks) * chunksize where 'n' is the
5266 * number of raid devices
5267 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5269 int stripe = data_disks *
5270 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5271 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5272 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10005273
5274 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005275
5276 mddev->queue->backing_dev_info.congested_data = mddev;
5277 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10005278
5279 chunk_size = mddev->chunk_sectors << 9;
5280 blk_queue_io_min(mddev->queue, chunk_size);
5281 blk_queue_io_opt(mddev->queue, chunk_size *
5282 (conf->raid_disks - conf->max_degraded));
5283
NeilBrown05616be2012-05-21 09:27:00 +10005284 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005285 disk_stack_limits(mddev->gendisk, rdev->bdev,
5286 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10005287 disk_stack_limits(mddev->gendisk, rdev->bdev,
5288 rdev->new_data_offset << 9);
5289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290 }
5291
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 return 0;
5293abort:
NeilBrown01f96c02011-09-21 15:30:20 +10005294 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11005295 print_raid5_conf(conf);
5296 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005298 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 return -EIO;
5300}
5301
NeilBrownfd01b882011-10-11 16:47:53 +11005302static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303{
NeilBrownd1688a62011-10-11 16:49:52 +11005304 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305
NeilBrown01f96c02011-09-21 15:30:20 +10005306 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005307 if (mddev->queue)
5308 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10005309 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005310 mddev->private = NULL;
5311 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 return 0;
5313}
5314
NeilBrownfd01b882011-10-11 16:47:53 +11005315static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316{
NeilBrownd1688a62011-10-11 16:49:52 +11005317 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 int i;
5319
Andre Noll9d8f0362009-06-18 08:45:01 +10005320 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5321 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005322 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 for (i = 0; i < conf->raid_disks; i++)
5324 seq_printf (seq, "%s",
5325 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005326 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328}
5329
NeilBrownd1688a62011-10-11 16:49:52 +11005330static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331{
5332 int i;
5333 struct disk_info *tmp;
5334
NeilBrown0c55e022010-05-03 14:09:02 +10005335 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 if (!conf) {
5337 printk("(conf==NULL)\n");
5338 return;
5339 }
NeilBrown0c55e022010-05-03 14:09:02 +10005340 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5341 conf->raid_disks,
5342 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343
5344 for (i = 0; i < conf->raid_disks; i++) {
5345 char b[BDEVNAME_SIZE];
5346 tmp = conf->disks + i;
5347 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005348 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5349 i, !test_bit(Faulty, &tmp->rdev->flags),
5350 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 }
5352}
5353
NeilBrownfd01b882011-10-11 16:47:53 +11005354static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355{
5356 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005357 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005359 int count = 0;
5360 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361
5362 for (i = 0; i < conf->raid_disks; i++) {
5363 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11005364 if (tmp->replacement
5365 && tmp->replacement->recovery_offset == MaxSector
5366 && !test_bit(Faulty, &tmp->replacement->flags)
5367 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
5368 /* Replacement has just become active. */
5369 if (!tmp->rdev
5370 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
5371 count++;
5372 if (tmp->rdev) {
5373 /* Replaced device not technically faulty,
5374 * but we need to be sure it gets removed
5375 * and never re-added.
5376 */
5377 set_bit(Faulty, &tmp->rdev->flags);
5378 sysfs_notify_dirent_safe(
5379 tmp->rdev->sysfs_state);
5380 }
5381 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
5382 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005383 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005384 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005385 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005386 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005387 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 }
5389 }
NeilBrown6b965622010-08-18 11:56:59 +10005390 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005391 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005392 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005394 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395}
5396
NeilBrownb8321b62011-12-23 10:17:51 +11005397static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398{
NeilBrownd1688a62011-10-11 16:49:52 +11005399 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11005401 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11005402 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 struct disk_info *p = conf->disks + number;
5404
5405 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11005406 if (rdev == p->rdev)
5407 rdevp = &p->rdev;
5408 else if (rdev == p->replacement)
5409 rdevp = &p->replacement;
5410 else
5411 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11005412
NeilBrown657e3e42011-12-23 10:17:52 +11005413 if (number >= conf->raid_disks &&
5414 conf->reshape_progress == MaxSector)
5415 clear_bit(In_sync, &rdev->flags);
5416
5417 if (test_bit(In_sync, &rdev->flags) ||
5418 atomic_read(&rdev->nr_pending)) {
5419 err = -EBUSY;
5420 goto abort;
5421 }
5422 /* Only remove non-faulty devices if recovery
5423 * isn't possible.
5424 */
5425 if (!test_bit(Faulty, &rdev->flags) &&
5426 mddev->recovery_disabled != conf->recovery_disabled &&
5427 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11005428 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11005429 number < conf->raid_disks) {
5430 err = -EBUSY;
5431 goto abort;
5432 }
5433 *rdevp = NULL;
5434 synchronize_rcu();
5435 if (atomic_read(&rdev->nr_pending)) {
5436 /* lost the race, try later */
5437 err = -EBUSY;
5438 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11005439 } else if (p->replacement) {
5440 /* We must have just cleared 'rdev' */
5441 p->rdev = p->replacement;
5442 clear_bit(Replacement, &p->replacement->flags);
5443 smp_mb(); /* Make sure other CPUs may see both as identical
5444 * but will never see neither - if they are careful
5445 */
5446 p->replacement = NULL;
5447 clear_bit(WantReplacement, &rdev->flags);
5448 } else
5449 /* We might have just removed the Replacement as faulty-
5450 * clear the bit just in case
5451 */
5452 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453abort:
5454
5455 print_raid5_conf(conf);
5456 return err;
5457}
5458
NeilBrownfd01b882011-10-11 16:47:53 +11005459static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460{
NeilBrownd1688a62011-10-11 16:49:52 +11005461 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005462 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 int disk;
5464 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005465 int first = 0;
5466 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467
NeilBrown7f0da592011-07-28 11:39:22 +10005468 if (mddev->recovery_disabled == conf->recovery_disabled)
5469 return -EBUSY;
5470
NeilBrowndc10c642012-03-19 12:46:37 +11005471 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005473 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474
Neil Brown6c2fce22008-06-28 08:31:31 +10005475 if (rdev->raid_disk >= 0)
5476 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477
5478 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005479 * find the disk ... but prefer rdev->saved_raid_disk
5480 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005482 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005483 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005484 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10005485 first = rdev->saved_raid_disk;
5486
5487 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11005488 p = conf->disks + disk;
5489 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005490 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005492 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005493 if (rdev->saved_raid_disk != disk)
5494 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005495 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10005496 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005498 }
5499 for (disk = first; disk <= last; disk++) {
5500 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11005501 if (test_bit(WantReplacement, &p->rdev->flags) &&
5502 p->replacement == NULL) {
5503 clear_bit(In_sync, &rdev->flags);
5504 set_bit(Replacement, &rdev->flags);
5505 rdev->raid_disk = disk;
5506 err = 0;
5507 conf->fullsync = 1;
5508 rcu_assign_pointer(p->replacement, rdev);
5509 break;
5510 }
5511 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005512out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005514 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515}
5516
NeilBrownfd01b882011-10-11 16:47:53 +11005517static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518{
5519 /* no resync is happening, and there is enough space
5520 * on all devices, so we can resize.
5521 * We need to make sure resync covers any new space.
5522 * If the array is shrinking we should possibly wait until
5523 * any io in the removed space completes, but it hardly seems
5524 * worth it.
5525 */
NeilBrowna4a61252012-05-22 13:55:27 +10005526 sector_t newsize;
Andre Noll9d8f0362009-06-18 08:45:01 +10005527 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10005528 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
5529 if (mddev->external_size &&
5530 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11005531 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10005532 if (mddev->bitmap) {
5533 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
5534 if (ret)
5535 return ret;
5536 }
5537 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10005538 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005539 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005540 if (sectors > mddev->dev_sectors &&
5541 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005542 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5544 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005545 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005546 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 return 0;
5548}
5549
NeilBrownfd01b882011-10-11 16:47:53 +11005550static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005551{
5552 /* Can only proceed if there are plenty of stripe_heads.
5553 * We need a minimum of one full stripe,, and for sensible progress
5554 * it is best to have about 4 times that.
5555 * If we require 4 times, then the default 256 4K stripe_heads will
5556 * allow for chunk sizes up to 256K, which is probably OK.
5557 * If the chunk size is greater, user-space should request more
5558 * stripe_heads first.
5559 */
NeilBrownd1688a62011-10-11 16:49:52 +11005560 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005561 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5562 > conf->max_nr_stripes ||
5563 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5564 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005565 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5566 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005567 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5568 / STRIPE_SIZE)*4);
5569 return 0;
5570 }
5571 return 1;
5572}
5573
NeilBrownfd01b882011-10-11 16:47:53 +11005574static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005575{
NeilBrownd1688a62011-10-11 16:49:52 +11005576 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005577
NeilBrown88ce4932009-03-31 15:24:23 +11005578 if (mddev->delta_disks == 0 &&
5579 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005580 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005581 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10005582 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005583 return -EINVAL;
5584 if (mddev->delta_disks < 0) {
5585 /* We might be able to shrink, but the devices must
5586 * be made bigger first.
5587 * For raid6, 4 is the minimum size.
5588 * Otherwise 2 is the minimum
5589 */
5590 int min = 2;
5591 if (mddev->level == 6)
5592 min = 4;
5593 if (mddev->raid_disks + mddev->delta_disks < min)
5594 return -EINVAL;
5595 }
NeilBrown29269552006-03-27 01:18:10 -08005596
NeilBrown01ee22b2009-06-18 08:47:20 +10005597 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005598 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005599
NeilBrownec32a2b2009-03-31 15:17:38 +11005600 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
NeilBrown63c70c42006-03-27 01:18:13 -08005601}
5602
NeilBrownfd01b882011-10-11 16:47:53 +11005603static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005604{
NeilBrownd1688a62011-10-11 16:49:52 +11005605 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005606 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005607 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005608 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005609
NeilBrownf4168852007-02-28 20:11:53 -08005610 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005611 return -EBUSY;
5612
NeilBrown01ee22b2009-06-18 08:47:20 +10005613 if (!check_stripe_cache(mddev))
5614 return -ENOSPC;
5615
NeilBrown30b67642012-05-22 13:55:28 +10005616 if (has_failed(conf))
5617 return -EINVAL;
5618
NeilBrownc6563a82012-05-21 09:27:00 +10005619 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11005620 if (!test_bit(In_sync, &rdev->flags)
5621 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005622 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10005623 }
NeilBrown63c70c42006-03-27 01:18:13 -08005624
NeilBrownf4168852007-02-28 20:11:53 -08005625 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005626 /* Not enough devices even to make a degraded array
5627 * of that size
5628 */
5629 return -EINVAL;
5630
NeilBrownec32a2b2009-03-31 15:17:38 +11005631 /* Refuse to reduce size of the array. Any reductions in
5632 * array size must be through explicit setting of array_size
5633 * attribute.
5634 */
5635 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5636 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10005637 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11005638 "before number of disks\n", mdname(mddev));
5639 return -EINVAL;
5640 }
5641
NeilBrownf6705572006-03-27 01:18:11 -08005642 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005643 spin_lock_irq(&conf->device_lock);
5644 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005645 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005646 conf->prev_chunk_sectors = conf->chunk_sectors;
5647 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005648 conf->prev_algo = conf->algorithm;
5649 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10005650 conf->generation++;
5651 /* Code that selects data_offset needs to see the generation update
5652 * if reshape_progress has been set - so a memory barrier needed.
5653 */
5654 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10005655 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11005656 conf->reshape_progress = raid5_size(mddev, 0, 0);
5657 else
5658 conf->reshape_progress = 0;
5659 conf->reshape_safe = conf->reshape_progress;
NeilBrown29269552006-03-27 01:18:10 -08005660 spin_unlock_irq(&conf->device_lock);
5661
5662 /* Add some new drives, as many as will fit.
5663 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10005664 * Don't add devices if we are reducing the number of
5665 * devices in the array. This is because it is not possible
5666 * to correctly record the "partially reconstructed" state of
5667 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08005668 */
NeilBrown87a8dec2011-01-31 11:57:43 +11005669 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11005670 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11005671 if (rdev->raid_disk < 0 &&
5672 !test_bit(Faulty, &rdev->flags)) {
5673 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11005674 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11005675 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11005676 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11005677 else
NeilBrown87a8dec2011-01-31 11:57:43 +11005678 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10005679
5680 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11005681 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11005682 }
NeilBrown87a8dec2011-01-31 11:57:43 +11005683 } else if (rdev->raid_disk >= conf->previous_raid_disks
5684 && !test_bit(Faulty, &rdev->flags)) {
5685 /* This is a spare that was manually added */
5686 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11005687 }
NeilBrown29269552006-03-27 01:18:10 -08005688
NeilBrown87a8dec2011-01-31 11:57:43 +11005689 /* When a reshape changes the number of devices,
5690 * ->degraded is measured against the larger of the
5691 * pre and post number of devices.
5692 */
NeilBrownec32a2b2009-03-31 15:17:38 +11005693 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005694 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11005695 spin_unlock_irqrestore(&conf->device_lock, flags);
5696 }
NeilBrown63c70c42006-03-27 01:18:13 -08005697 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10005698 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07005699 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005700
NeilBrown29269552006-03-27 01:18:10 -08005701 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5702 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5703 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5704 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5705 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005706 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08005707 if (!mddev->sync_thread) {
5708 mddev->recovery = 0;
5709 spin_lock_irq(&conf->device_lock);
5710 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10005711 rdev_for_each(rdev, mddev)
5712 rdev->new_data_offset = rdev->data_offset;
5713 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11005714 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11005715 mddev->reshape_position = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005716 spin_unlock_irq(&conf->device_lock);
5717 return -EAGAIN;
5718 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005719 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005720 md_wakeup_thread(mddev->sync_thread);
5721 md_new_event(mddev);
5722 return 0;
5723}
NeilBrown29269552006-03-27 01:18:10 -08005724
NeilBrownec32a2b2009-03-31 15:17:38 +11005725/* This is called from the reshape thread and should make any
5726 * changes needed in 'conf'
5727 */
NeilBrownd1688a62011-10-11 16:49:52 +11005728static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08005729{
NeilBrown29269552006-03-27 01:18:10 -08005730
NeilBrownf6705572006-03-27 01:18:11 -08005731 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10005732 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07005733
NeilBrownf6705572006-03-27 01:18:11 -08005734 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11005735 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10005736 rdev_for_each(rdev, conf->mddev)
5737 rdev->data_offset = rdev->new_data_offset;
5738 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11005739 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08005740 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005741 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07005742
5743 /* read-ahead size must cover two whole stripes, which is
5744 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5745 */
NeilBrown4a5add42010-06-01 19:37:28 +10005746 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11005747 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005748 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11005749 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07005750 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5751 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5752 }
NeilBrown29269552006-03-27 01:18:10 -08005753 }
NeilBrown29269552006-03-27 01:18:10 -08005754}
5755
NeilBrownec32a2b2009-03-31 15:17:38 +11005756/* This is called from the raid5d thread with mddev_lock held.
5757 * It makes config changes to the device.
5758 */
NeilBrownfd01b882011-10-11 16:47:53 +11005759static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11005760{
NeilBrownd1688a62011-10-11 16:49:52 +11005761 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11005762
5763 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5764
NeilBrownec32a2b2009-03-31 15:17:38 +11005765 if (mddev->delta_disks > 0) {
5766 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5767 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005768 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11005769 } else {
5770 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11005771 spin_lock_irq(&conf->device_lock);
5772 mddev->degraded = calc_degraded(conf);
5773 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11005774 for (d = conf->raid_disks ;
5775 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10005776 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11005777 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10005778 if (rdev)
5779 clear_bit(In_sync, &rdev->flags);
5780 rdev = conf->disks[d].replacement;
5781 if (rdev)
5782 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10005783 }
NeilBrowncea9c222009-03-31 15:15:05 +11005784 }
NeilBrown88ce4932009-03-31 15:24:23 +11005785 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005786 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11005787 mddev->reshape_position = MaxSector;
5788 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005789 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11005790 }
5791}
5792
NeilBrownfd01b882011-10-11 16:47:53 +11005793static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07005794{
NeilBrownd1688a62011-10-11 16:49:52 +11005795 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07005796
5797 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08005798 case 2: /* resume for a suspend */
5799 wake_up(&conf->wait_for_overlap);
5800 break;
5801
NeilBrown72626682005-09-09 16:23:54 -07005802 case 1: /* stop all writes */
5803 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005804 /* '2' tells resync/reshape to pause so that all
5805 * active stripes can drain
5806 */
5807 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07005808 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005809 atomic_read(&conf->active_stripes) == 0 &&
5810 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07005811 conf->device_lock, /* nothing */);
NeilBrown64bd6602009-08-03 10:59:58 +10005812 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07005813 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005814 /* allow reshape to continue */
5815 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005816 break;
5817
5818 case 0: /* re-enable writes */
5819 spin_lock_irq(&conf->device_lock);
5820 conf->quiesce = 0;
5821 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08005822 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005823 spin_unlock_irq(&conf->device_lock);
5824 break;
5825 }
NeilBrown72626682005-09-09 16:23:54 -07005826}
NeilBrownb15c2e52006-01-06 00:20:16 -08005827
NeilBrownd562b0c2009-03-31 14:39:39 +11005828
NeilBrownfd01b882011-10-11 16:47:53 +11005829static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11005830{
NeilBrowne373ab12011-10-11 16:48:59 +11005831 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07005832 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11005833
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005834 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11005835 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10005836 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
5837 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005838 return ERR_PTR(-EINVAL);
5839 }
5840
NeilBrowne373ab12011-10-11 16:48:59 +11005841 sectors = raid0_conf->strip_zone[0].zone_end;
5842 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10005843 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005844 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11005845 mddev->new_layout = ALGORITHM_PARITY_N;
5846 mddev->new_chunk_sectors = mddev->chunk_sectors;
5847 mddev->raid_disks += 1;
5848 mddev->delta_disks = 1;
5849 /* make sure it will be not marked as dirty */
5850 mddev->recovery_cp = MaxSector;
5851
5852 return setup_conf(mddev);
5853}
5854
5855
NeilBrownfd01b882011-10-11 16:47:53 +11005856static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005857{
5858 int chunksect;
5859
5860 if (mddev->raid_disks != 2 ||
5861 mddev->degraded > 1)
5862 return ERR_PTR(-EINVAL);
5863
5864 /* Should check if there are write-behind devices? */
5865
5866 chunksect = 64*2; /* 64K by default */
5867
5868 /* The array must be an exact multiple of chunksize */
5869 while (chunksect && (mddev->array_sectors & (chunksect-1)))
5870 chunksect >>= 1;
5871
5872 if ((chunksect<<9) < STRIPE_SIZE)
5873 /* array size does not allow a suitable chunk size */
5874 return ERR_PTR(-EINVAL);
5875
5876 mddev->new_level = 5;
5877 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10005878 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11005879
5880 return setup_conf(mddev);
5881}
5882
NeilBrownfd01b882011-10-11 16:47:53 +11005883static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11005884{
5885 int new_layout;
5886
5887 switch (mddev->layout) {
5888 case ALGORITHM_LEFT_ASYMMETRIC_6:
5889 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
5890 break;
5891 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5892 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
5893 break;
5894 case ALGORITHM_LEFT_SYMMETRIC_6:
5895 new_layout = ALGORITHM_LEFT_SYMMETRIC;
5896 break;
5897 case ALGORITHM_RIGHT_SYMMETRIC_6:
5898 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
5899 break;
5900 case ALGORITHM_PARITY_0_6:
5901 new_layout = ALGORITHM_PARITY_0;
5902 break;
5903 case ALGORITHM_PARITY_N:
5904 new_layout = ALGORITHM_PARITY_N;
5905 break;
5906 default:
5907 return ERR_PTR(-EINVAL);
5908 }
5909 mddev->new_level = 5;
5910 mddev->new_layout = new_layout;
5911 mddev->delta_disks = -1;
5912 mddev->raid_disks -= 1;
5913 return setup_conf(mddev);
5914}
5915
NeilBrownd562b0c2009-03-31 14:39:39 +11005916
NeilBrownfd01b882011-10-11 16:47:53 +11005917static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11005918{
NeilBrown88ce4932009-03-31 15:24:23 +11005919 /* For a 2-drive array, the layout and chunk size can be changed
5920 * immediately as not restriping is needed.
5921 * For larger arrays we record the new value - after validation
5922 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11005923 */
NeilBrownd1688a62011-10-11 16:49:52 +11005924 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10005925 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11005926
NeilBrown597a7112009-06-18 08:47:42 +10005927 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11005928 return -EINVAL;
5929 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005930 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11005931 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005932 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11005933 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005934 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11005935 /* not factor of array size */
5936 return -EINVAL;
5937 }
5938
5939 /* They look valid */
5940
NeilBrown88ce4932009-03-31 15:24:23 +11005941 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10005942 /* can make the change immediately */
5943 if (mddev->new_layout >= 0) {
5944 conf->algorithm = mddev->new_layout;
5945 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11005946 }
5947 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10005948 conf->chunk_sectors = new_chunk ;
5949 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11005950 }
5951 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5952 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11005953 }
NeilBrown50ac1682009-06-18 08:47:55 +10005954 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11005955}
5956
NeilBrownfd01b882011-10-11 16:47:53 +11005957static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11005958{
NeilBrown597a7112009-06-18 08:47:42 +10005959 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10005960
NeilBrown597a7112009-06-18 08:47:42 +10005961 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11005962 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005963 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005964 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11005965 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005966 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11005967 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005968 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11005969 /* not factor of array size */
5970 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005971 }
NeilBrown88ce4932009-03-31 15:24:23 +11005972
5973 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10005974 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11005975}
5976
NeilBrownfd01b882011-10-11 16:47:53 +11005977static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005978{
5979 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005980 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005981 * raid1 - if there are two drives. We need to know the chunk size
5982 * raid4 - trivial - just use a raid4 layout.
5983 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005984 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005985 if (mddev->level == 0)
5986 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11005987 if (mddev->level == 1)
5988 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11005989 if (mddev->level == 4) {
5990 mddev->new_layout = ALGORITHM_PARITY_N;
5991 mddev->new_level = 5;
5992 return setup_conf(mddev);
5993 }
NeilBrownfc9739c2009-03-31 14:57:20 +11005994 if (mddev->level == 6)
5995 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11005996
5997 return ERR_PTR(-EINVAL);
5998}
5999
NeilBrownfd01b882011-10-11 16:47:53 +11006000static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11006001{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006002 /* raid4 can take over:
6003 * raid0 - if there is only one strip zone
6004 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11006005 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006006 if (mddev->level == 0)
6007 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11006008 if (mddev->level == 5 &&
6009 mddev->layout == ALGORITHM_PARITY_N) {
6010 mddev->new_layout = 0;
6011 mddev->new_level = 4;
6012 return setup_conf(mddev);
6013 }
6014 return ERR_PTR(-EINVAL);
6015}
NeilBrownd562b0c2009-03-31 14:39:39 +11006016
NeilBrown84fc4b52011-10-11 16:49:58 +11006017static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11006018
NeilBrownfd01b882011-10-11 16:47:53 +11006019static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11006020{
6021 /* Currently can only take over a raid5. We map the
6022 * personality to an equivalent raid6 personality
6023 * with the Q block at the end.
6024 */
6025 int new_layout;
6026
6027 if (mddev->pers != &raid5_personality)
6028 return ERR_PTR(-EINVAL);
6029 if (mddev->degraded > 1)
6030 return ERR_PTR(-EINVAL);
6031 if (mddev->raid_disks > 253)
6032 return ERR_PTR(-EINVAL);
6033 if (mddev->raid_disks < 3)
6034 return ERR_PTR(-EINVAL);
6035
6036 switch (mddev->layout) {
6037 case ALGORITHM_LEFT_ASYMMETRIC:
6038 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
6039 break;
6040 case ALGORITHM_RIGHT_ASYMMETRIC:
6041 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
6042 break;
6043 case ALGORITHM_LEFT_SYMMETRIC:
6044 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
6045 break;
6046 case ALGORITHM_RIGHT_SYMMETRIC:
6047 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
6048 break;
6049 case ALGORITHM_PARITY_0:
6050 new_layout = ALGORITHM_PARITY_0_6;
6051 break;
6052 case ALGORITHM_PARITY_N:
6053 new_layout = ALGORITHM_PARITY_N;
6054 break;
6055 default:
6056 return ERR_PTR(-EINVAL);
6057 }
6058 mddev->new_level = 6;
6059 mddev->new_layout = new_layout;
6060 mddev->delta_disks = 1;
6061 mddev->raid_disks += 1;
6062 return setup_conf(mddev);
6063}
6064
6065
NeilBrown84fc4b52011-10-11 16:49:58 +11006066static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07006067{
6068 .name = "raid6",
6069 .level = 6,
6070 .owner = THIS_MODULE,
6071 .make_request = make_request,
6072 .run = run,
6073 .stop = stop,
6074 .status = status,
6075 .error_handler = error,
6076 .hot_add_disk = raid5_add_disk,
6077 .hot_remove_disk= raid5_remove_disk,
6078 .spare_active = raid5_spare_active,
6079 .sync_request = sync_request,
6080 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006081 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10006082 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08006083 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006084 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07006085 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11006086 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07006087};
NeilBrown84fc4b52011-10-11 16:49:58 +11006088static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089{
6090 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08006091 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092 .owner = THIS_MODULE,
6093 .make_request = make_request,
6094 .run = run,
6095 .stop = stop,
6096 .status = status,
6097 .error_handler = error,
6098 .hot_add_disk = raid5_add_disk,
6099 .hot_remove_disk= raid5_remove_disk,
6100 .spare_active = raid5_spare_active,
6101 .sync_request = sync_request,
6102 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006103 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08006104 .check_reshape = raid5_check_reshape,
6105 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006106 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07006107 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11006108 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109};
6110
NeilBrown84fc4b52011-10-11 16:49:58 +11006111static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112{
NeilBrown2604b702006-01-06 00:20:36 -08006113 .name = "raid4",
6114 .level = 4,
6115 .owner = THIS_MODULE,
6116 .make_request = make_request,
6117 .run = run,
6118 .stop = stop,
6119 .status = status,
6120 .error_handler = error,
6121 .hot_add_disk = raid5_add_disk,
6122 .hot_remove_disk= raid5_remove_disk,
6123 .spare_active = raid5_spare_active,
6124 .sync_request = sync_request,
6125 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006126 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08006127 .check_reshape = raid5_check_reshape,
6128 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006129 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08006130 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11006131 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08006132};
6133
6134static int __init raid5_init(void)
6135{
NeilBrown16a53ec2006-06-26 00:27:38 -07006136 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006137 register_md_personality(&raid5_personality);
6138 register_md_personality(&raid4_personality);
6139 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140}
6141
NeilBrown2604b702006-01-06 00:20:36 -08006142static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143{
NeilBrown16a53ec2006-06-26 00:27:38 -07006144 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006145 unregister_md_personality(&raid5_personality);
6146 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147}
6148
6149module_init(raid5_init);
6150module_exit(raid5_exit);
6151MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11006152MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006153MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08006154MODULE_ALIAS("md-raid5");
6155MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08006156MODULE_ALIAS("md-level-5");
6157MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07006158MODULE_ALIAS("md-personality-8"); /* RAID6 */
6159MODULE_ALIAS("md-raid6");
6160MODULE_ALIAS("md-level-6");
6161
6162/* This used to be two separate modules, they were: */
6163MODULE_ALIAS("raid5");
6164MODULE_ALIAS("raid6");