blob: 23e501518c48797b3314b25481a42276266e1ae8 [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)) {
Shaohua Li5bbbd742012-07-03 15:57:19 +1000199 if (test_bit(STRIPE_DELAYED, &sh->state) &&
200 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 list_add_tail(&sh->lru, &conf->delayed_list);
NeilBrown482c0832011-04-18 18:25:42 +1000202 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
203 sh->bm_seq - conf->seq_write > 0)
NeilBrown72626682005-09-09 16:23:54 -0700204 list_add_tail(&sh->lru, &conf->bitmap_list);
NeilBrown482c0832011-04-18 18:25:42 +1000205 else {
Shaohua Li5bbbd742012-07-03 15:57:19 +1000206 clear_bit(STRIPE_DELAYED, &sh->state);
NeilBrown72626682005-09-09 16:23:54 -0700207 clear_bit(STRIPE_BIT_DELAY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 list_add_tail(&sh->lru, &conf->handle_list);
NeilBrown72626682005-09-09 16:23:54 -0700209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 md_wakeup_thread(conf->mddev->thread);
211 } else {
Dan Williams600aa102008-06-28 08:32:05 +1000212 BUG_ON(stripe_operations_active(sh));
majianpeng41fe75f2012-03-13 11:21:25 +1100213 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
214 if (atomic_dec_return(&conf->preread_active_stripes)
215 < IO_THRESHOLD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 md_wakeup_thread(conf->mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 atomic_dec(&conf->active_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800218 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
219 list_add_tail(&sh->lru, &conf->inactive_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 wake_up(&conf->wait_for_stripe);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -0800221 if (conf->retry_read_aligned)
222 md_wakeup_thread(conf->mddev->thread);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
225 }
226}
NeilBrownd0dabf72009-03-31 14:39:38 +1100227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228static void release_stripe(struct stripe_head *sh)
229{
NeilBrownd1688a62011-10-11 16:49:52 +1100230 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 unsigned long flags;
NeilBrown16a53ec2006-06-26 00:27:38 -0700232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 spin_lock_irqsave(&conf->device_lock, flags);
234 __release_stripe(conf, sh);
235 spin_unlock_irqrestore(&conf->device_lock, flags);
236}
237
NeilBrownfccddba2006-01-06 00:20:33 -0800238static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Dan Williams45b42332007-07-09 11:56:43 -0700240 pr_debug("remove_hash(), stripe %llu\n",
241 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
NeilBrownfccddba2006-01-06 00:20:33 -0800243 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
NeilBrownd1688a62011-10-11 16:49:52 +1100246static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
NeilBrownfccddba2006-01-06 00:20:33 -0800248 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Dan Williams45b42332007-07-09 11:56:43 -0700250 pr_debug("insert_hash(), stripe %llu\n",
251 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
NeilBrownfccddba2006-01-06 00:20:33 -0800253 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
256
257/* find an idle stripe, make sure it is unhashed, and return it. */
NeilBrownd1688a62011-10-11 16:49:52 +1100258static struct stripe_head *get_free_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 struct stripe_head *sh = NULL;
261 struct list_head *first;
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (list_empty(&conf->inactive_list))
264 goto out;
265 first = conf->inactive_list.next;
266 sh = list_entry(first, struct stripe_head, lru);
267 list_del_init(first);
268 remove_hash(sh);
269 atomic_inc(&conf->active_stripes);
270out:
271 return sh;
272}
273
NeilBrowne4e11e32010-06-16 16:45:16 +1000274static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 struct page *p;
277 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000278 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
NeilBrowne4e11e32010-06-16 16:45:16 +1000280 for (i = 0; i < num ; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 p = sh->dev[i].page;
282 if (!p)
283 continue;
284 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800285 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
287}
288
NeilBrowne4e11e32010-06-16 16:45:16 +1000289static int grow_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000292 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
NeilBrowne4e11e32010-06-16 16:45:16 +1000294 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 struct page *page;
296
297 if (!(page = alloc_page(GFP_KERNEL))) {
298 return 1;
299 }
300 sh->dev[i].page = page;
301 }
302 return 0;
303}
304
NeilBrown784052e2009-03-31 15:19:07 +1100305static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrownd1688a62011-10-11 16:49:52 +1100306static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100307 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
NeilBrownb5663ba2009-03-31 14:39:38 +1100309static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
NeilBrownd1688a62011-10-11 16:49:52 +1100311 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800312 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200314 BUG_ON(atomic_read(&sh->count) != 0);
315 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000316 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700317
Dan Williams45b42332007-07-09 11:56:43 -0700318 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 (unsigned long long)sh->sector);
320
321 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700322
NeilBrown86b42c72009-03-31 15:19:03 +1100323 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100324 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100326 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 sh->state = 0;
328
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800329
330 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 struct r5dev *dev = &sh->dev[i];
332
Dan Williamsd84e0f12007-01-02 13:52:30 -0700333 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700335 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700337 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000339 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
341 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100342 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344 insert_hash(conf, sh);
345}
346
NeilBrownd1688a62011-10-11 16:49:52 +1100347static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100348 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -0800351 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Dan Williams45b42332007-07-09 11:56:43 -0700353 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
NeilBrownfccddba2006-01-06 00:20:33 -0800354 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100355 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700357 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return NULL;
359}
360
NeilBrown674806d2010-06-16 17:17:53 +1000361/*
362 * Need to check if array has failed when deciding whether to:
363 * - start an array
364 * - remove non-faulty devices
365 * - add a spare
366 * - allow a reshape
367 * This determination is simple when no reshape is happening.
368 * However if there is a reshape, we need to carefully check
369 * both the before and after sections.
370 * This is because some failed devices may only affect one
371 * of the two sections, and some non-in_sync devices may
372 * be insync in the section most affected by failed devices.
373 */
NeilBrown908f4fb2011-12-23 10:17:50 +1100374static int calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000375{
NeilBrown908f4fb2011-12-23 10:17:50 +1100376 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000377 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000378
379 rcu_read_lock();
380 degraded = 0;
381 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100382 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown674806d2010-06-16 17:17:53 +1000383 if (!rdev || test_bit(Faulty, &rdev->flags))
384 degraded++;
385 else if (test_bit(In_sync, &rdev->flags))
386 ;
387 else
388 /* not in-sync or faulty.
389 * If the reshape increases the number of devices,
390 * this is being recovered by the reshape, so
391 * this 'previous' section is not in_sync.
392 * If the number of devices is being reduced however,
393 * the device can only be part of the array if
394 * we are reverting a reshape, so this section will
395 * be in-sync.
396 */
397 if (conf->raid_disks >= conf->previous_raid_disks)
398 degraded++;
399 }
400 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100401 if (conf->raid_disks == conf->previous_raid_disks)
402 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000403 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100404 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000405 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100406 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown674806d2010-06-16 17:17:53 +1000407 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100408 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000409 else if (test_bit(In_sync, &rdev->flags))
410 ;
411 else
412 /* not in-sync or faulty.
413 * If reshape increases the number of devices, this
414 * section has already been recovered, else it
415 * almost certainly hasn't.
416 */
417 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100418 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000419 }
420 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100421 if (degraded2 > degraded)
422 return degraded2;
423 return degraded;
424}
425
426static int has_failed(struct r5conf *conf)
427{
428 int degraded;
429
430 if (conf->mddev->reshape_position == MaxSector)
431 return conf->mddev->degraded > conf->max_degraded;
432
433 degraded = calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000434 if (degraded > conf->max_degraded)
435 return 1;
436 return 0;
437}
438
NeilBrownb5663ba2009-03-31 14:39:38 +1100439static struct stripe_head *
NeilBrownd1688a62011-10-11 16:49:52 +1100440get_active_stripe(struct r5conf *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000441 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
443 struct stripe_head *sh;
444
Dan Williams45b42332007-07-09 11:56:43 -0700445 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 spin_lock_irq(&conf->device_lock);
448
449 do {
NeilBrown72626682005-09-09 16:23:54 -0700450 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000451 conf->quiesce == 0 || noquiesce,
NeilBrown72626682005-09-09 16:23:54 -0700452 conf->device_lock, /* nothing */);
NeilBrown86b42c72009-03-31 15:19:03 +1100453 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (!sh) {
455 if (!conf->inactive_blocked)
456 sh = get_free_stripe(conf);
457 if (noblock && sh == NULL)
458 break;
459 if (!sh) {
460 conf->inactive_blocked = 1;
461 wait_event_lock_irq(conf->wait_for_stripe,
462 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800463 (atomic_read(&conf->active_stripes)
464 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 || !conf->inactive_blocked),
466 conf->device_lock,
NeilBrown7c13edc2011-04-18 18:25:43 +1000467 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 conf->inactive_blocked = 0;
469 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100470 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 } else {
472 if (atomic_read(&sh->count)) {
NeilBrownab69ae12009-03-31 15:26:47 +1100473 BUG_ON(!list_empty(&sh->lru)
474 && !test_bit(STRIPE_EXPANDING, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 } else {
476 if (!test_bit(STRIPE_HANDLE, &sh->state))
477 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700478 if (list_empty(&sh->lru) &&
479 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700480 BUG();
481 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
483 }
484 } while (sh == NULL);
485
486 if (sh)
487 atomic_inc(&sh->count);
488
489 spin_unlock_irq(&conf->device_lock);
490 return sh;
491}
492
NeilBrown6712ecf2007-09-27 12:47:43 +0200493static void
494raid5_end_read_request(struct bio *bi, int error);
495static void
496raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700497
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000498static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700499{
NeilBrownd1688a62011-10-11 16:49:52 +1100500 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700501 int i, disks = sh->disks;
502
503 might_sleep();
504
505 for (i = disks; i--; ) {
506 int rw;
NeilBrown9a3e1102011-12-23 10:17:53 +1100507 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100508 struct bio *bi, *rbi;
509 struct md_rdev *rdev, *rrdev = NULL;
Tejun Heoe9c74692010-09-03 11:56:18 +0200510 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
511 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
512 rw = WRITE_FUA;
513 else
514 rw = WRITE;
515 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700516 rw = READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100517 else if (test_and_clear_bit(R5_WantReplace,
518 &sh->dev[i].flags)) {
519 rw = WRITE;
520 replace_only = 1;
521 } else
Dan Williams91c00922007-01-02 13:52:30 -0700522 continue;
523
524 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100525 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700526
527 bi->bi_rw = rw;
NeilBrown977df362011-12-23 10:17:53 +1100528 rbi->bi_rw = rw;
529 if (rw & WRITE) {
Dan Williams91c00922007-01-02 13:52:30 -0700530 bi->bi_end_io = raid5_end_write_request;
NeilBrown977df362011-12-23 10:17:53 +1100531 rbi->bi_end_io = raid5_end_write_request;
532 } else
Dan Williams91c00922007-01-02 13:52:30 -0700533 bi->bi_end_io = raid5_end_read_request;
534
535 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100536 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100537 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
538 rdev = rcu_dereference(conf->disks[i].rdev);
539 if (!rdev) {
540 rdev = rrdev;
541 rrdev = NULL;
542 }
NeilBrown9a3e1102011-12-23 10:17:53 +1100543 if (rw & WRITE) {
544 if (replace_only)
545 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100546 if (rdev == rrdev)
547 /* We raced and saw duplicates */
548 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100549 } else {
NeilBrowndd054fc2011-12-23 10:17:53 +1100550 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100551 rdev = rrdev;
552 rrdev = NULL;
553 }
NeilBrown977df362011-12-23 10:17:53 +1100554
Dan Williams91c00922007-01-02 13:52:30 -0700555 if (rdev && test_bit(Faulty, &rdev->flags))
556 rdev = NULL;
557 if (rdev)
558 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100559 if (rrdev && test_bit(Faulty, &rrdev->flags))
560 rrdev = NULL;
561 if (rrdev)
562 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700563 rcu_read_unlock();
564
NeilBrown73e92e52011-07-28 11:39:22 +1000565 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100566 * need to check for writes. We never accept write errors
567 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000568 */
569 while ((rw & WRITE) && rdev &&
570 test_bit(WriteErrorSeen, &rdev->flags)) {
571 sector_t first_bad;
572 int bad_sectors;
573 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
574 &first_bad, &bad_sectors);
575 if (!bad)
576 break;
577
578 if (bad < 0) {
579 set_bit(BlockedBadBlocks, &rdev->flags);
580 if (!conf->mddev->external &&
581 conf->mddev->flags) {
582 /* It is very unlikely, but we might
583 * still need to write out the
584 * bad block log - better give it
585 * a chance*/
586 md_check_recovery(conf->mddev);
587 }
majianpeng8d936982012-07-03 12:11:54 +1000588 /*
589 * Because md_wait_for_blocked_rdev
590 * will dec nr_pending, we must
591 * increment it first.
592 */
593 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +1000594 md_wait_for_blocked_rdev(rdev, conf->mddev);
595 } else {
596 /* Acknowledged bad block - skip the write */
597 rdev_dec_pending(rdev, conf->mddev);
598 rdev = NULL;
599 }
600 }
601
Dan Williams91c00922007-01-02 13:52:30 -0700602 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100603 if (s->syncing || s->expanding || s->expanded
604 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700605 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
606
Dan Williams2b7497f2008-06-28 08:31:52 +1000607 set_bit(STRIPE_IO_STARTED, &sh->state);
608
Dan Williams91c00922007-01-02 13:52:30 -0700609 bi->bi_bdev = rdev->bdev;
610 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700611 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700612 bi->bi_rw, i);
613 atomic_inc(&sh->count);
614 bi->bi_sector = sh->sector + rdev->data_offset;
615 bi->bi_flags = 1 << BIO_UPTODATE;
Dan Williams91c00922007-01-02 13:52:30 -0700616 bi->bi_idx = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700617 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
618 bi->bi_io_vec[0].bv_offset = 0;
619 bi->bi_size = STRIPE_SIZE;
620 bi->bi_next = NULL;
NeilBrown977df362011-12-23 10:17:53 +1100621 if (rrdev)
622 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Dan Williams91c00922007-01-02 13:52:30 -0700623 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100624 }
625 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100626 if (s->syncing || s->expanding || s->expanded
627 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +1100628 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
629
630 set_bit(STRIPE_IO_STARTED, &sh->state);
631
632 rbi->bi_bdev = rrdev->bdev;
633 pr_debug("%s: for %llu schedule op %ld on "
634 "replacement disc %d\n",
635 __func__, (unsigned long long)sh->sector,
636 rbi->bi_rw, i);
637 atomic_inc(&sh->count);
638 rbi->bi_sector = sh->sector + rrdev->data_offset;
639 rbi->bi_flags = 1 << BIO_UPTODATE;
640 rbi->bi_idx = 0;
641 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
642 rbi->bi_io_vec[0].bv_offset = 0;
643 rbi->bi_size = STRIPE_SIZE;
644 rbi->bi_next = NULL;
645 generic_make_request(rbi);
646 }
647 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000648 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700649 set_bit(STRIPE_DEGRADED, &sh->state);
650 pr_debug("skip op %ld on disc %d for sector %llu\n",
651 bi->bi_rw, i, (unsigned long long)sh->sector);
652 clear_bit(R5_LOCKED, &sh->dev[i].flags);
653 set_bit(STRIPE_HANDLE, &sh->state);
654 }
655 }
656}
657
658static struct dma_async_tx_descriptor *
659async_copy_data(int frombio, struct bio *bio, struct page *page,
660 sector_t sector, struct dma_async_tx_descriptor *tx)
661{
662 struct bio_vec *bvl;
663 struct page *bio_page;
664 int i;
665 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700666 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700667 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700668
669 if (bio->bi_sector >= sector)
670 page_offset = (signed)(bio->bi_sector - sector) * 512;
671 else
672 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700673
Dan Williams0403e382009-09-08 17:42:50 -0700674 if (frombio)
675 flags |= ASYNC_TX_FENCE;
676 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
677
Dan Williams91c00922007-01-02 13:52:30 -0700678 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000679 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700680 int clen;
681 int b_offset = 0;
682
683 if (page_offset < 0) {
684 b_offset = -page_offset;
685 page_offset += b_offset;
686 len -= b_offset;
687 }
688
689 if (len > 0 && page_offset + len > STRIPE_SIZE)
690 clen = STRIPE_SIZE - page_offset;
691 else
692 clen = len;
693
694 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000695 b_offset += bvl->bv_offset;
696 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700697 if (frombio)
698 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700699 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700700 else
701 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700702 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700703 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700704 /* chain the operations */
705 submit.depend_tx = tx;
706
Dan Williams91c00922007-01-02 13:52:30 -0700707 if (clen < len) /* hit end of page */
708 break;
709 page_offset += len;
710 }
711
712 return tx;
713}
714
715static void ops_complete_biofill(void *stripe_head_ref)
716{
717 struct stripe_head *sh = stripe_head_ref;
718 struct bio *return_bi = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +1100719 struct r5conf *conf = sh->raid_conf;
Dan Williamse4d84902007-09-24 10:06:13 -0700720 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700721
Harvey Harrisone46b2722008-04-28 02:15:50 -0700722 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700723 (unsigned long long)sh->sector);
724
725 /* clear completed biofills */
Dan Williams83de75c2008-06-28 08:31:58 +1000726 spin_lock_irq(&conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700727 for (i = sh->disks; i--; ) {
728 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700729
730 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700731 /* and check if we need to reply to a read request,
732 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000733 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700734 */
735 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700736 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700737
Dan Williams91c00922007-01-02 13:52:30 -0700738 BUG_ON(!dev->read);
739 rbi = dev->read;
740 dev->read = NULL;
741 while (rbi && rbi->bi_sector <
742 dev->sector + STRIPE_SECTORS) {
743 rbi2 = r5_next_bio(rbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +0200744 if (!raid5_dec_bi_phys_segments(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700745 rbi->bi_next = return_bi;
746 return_bi = rbi;
747 }
Dan Williams91c00922007-01-02 13:52:30 -0700748 rbi = rbi2;
749 }
750 }
751 }
Dan Williams83de75c2008-06-28 08:31:58 +1000752 spin_unlock_irq(&conf->device_lock);
753 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700754
755 return_io(return_bi);
756
Dan Williamse4d84902007-09-24 10:06:13 -0700757 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700758 release_stripe(sh);
759}
760
761static void ops_run_biofill(struct stripe_head *sh)
762{
763 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +1100764 struct r5conf *conf = sh->raid_conf;
Dan Williamsa08abd82009-06-03 11:43:59 -0700765 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700766 int i;
767
Harvey Harrisone46b2722008-04-28 02:15:50 -0700768 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700769 (unsigned long long)sh->sector);
770
771 for (i = sh->disks; i--; ) {
772 struct r5dev *dev = &sh->dev[i];
773 if (test_bit(R5_Wantfill, &dev->flags)) {
774 struct bio *rbi;
775 spin_lock_irq(&conf->device_lock);
776 dev->read = rbi = dev->toread;
777 dev->toread = NULL;
778 spin_unlock_irq(&conf->device_lock);
779 while (rbi && rbi->bi_sector <
780 dev->sector + STRIPE_SECTORS) {
781 tx = async_copy_data(0, rbi, dev->page,
782 dev->sector, tx);
783 rbi = r5_next_bio(rbi, dev->sector);
784 }
785 }
786 }
787
788 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700789 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
790 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700791}
792
Dan Williams4e7d2c02009-08-29 19:13:11 -0700793static void mark_target_uptodate(struct stripe_head *sh, int target)
794{
795 struct r5dev *tgt;
796
797 if (target < 0)
798 return;
799
800 tgt = &sh->dev[target];
801 set_bit(R5_UPTODATE, &tgt->flags);
802 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
803 clear_bit(R5_Wantcompute, &tgt->flags);
804}
805
Dan Williamsac6b53b2009-07-14 13:40:19 -0700806static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700807{
808 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700809
Harvey Harrisone46b2722008-04-28 02:15:50 -0700810 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700811 (unsigned long long)sh->sector);
812
Dan Williamsac6b53b2009-07-14 13:40:19 -0700813 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700814 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700815 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700816
Dan Williamsecc65c92008-06-28 08:31:57 +1000817 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
818 if (sh->check_state == check_state_compute_run)
819 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700820 set_bit(STRIPE_HANDLE, &sh->state);
821 release_stripe(sh);
822}
823
Dan Williamsd6f38f32009-07-14 11:50:52 -0700824/* return a pointer to the address conversion region of the scribble buffer */
825static addr_conv_t *to_addr_conv(struct stripe_head *sh,
826 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700827{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700828 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
829}
830
831static struct dma_async_tx_descriptor *
832ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
833{
Dan Williams91c00922007-01-02 13:52:30 -0700834 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700835 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700836 int target = sh->ops.target;
837 struct r5dev *tgt = &sh->dev[target];
838 struct page *xor_dest = tgt->page;
839 int count = 0;
840 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700841 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700842 int i;
843
844 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700845 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700846 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
847
848 for (i = disks; i--; )
849 if (i != target)
850 xor_srcs[count++] = sh->dev[i].page;
851
852 atomic_inc(&sh->count);
853
Dan Williams0403e382009-09-08 17:42:50 -0700854 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700855 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700856 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700857 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700858 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700859 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700860
Dan Williams91c00922007-01-02 13:52:30 -0700861 return tx;
862}
863
Dan Williamsac6b53b2009-07-14 13:40:19 -0700864/* set_syndrome_sources - populate source buffers for gen_syndrome
865 * @srcs - (struct page *) array of size sh->disks
866 * @sh - stripe_head to parse
867 *
868 * Populates srcs in proper layout order for the stripe and returns the
869 * 'count' of sources to be used in a call to async_gen_syndrome. The P
870 * destination buffer is recorded in srcs[count] and the Q destination
871 * is recorded in srcs[count+1]].
872 */
873static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
874{
875 int disks = sh->disks;
876 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
877 int d0_idx = raid6_d0(sh);
878 int count;
879 int i;
880
881 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100882 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700883
884 count = 0;
885 i = d0_idx;
886 do {
887 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
888
889 srcs[slot] = sh->dev[i].page;
890 i = raid6_next_disk(i, disks);
891 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700892
NeilBrowne4424fe2009-10-16 16:27:34 +1100893 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700894}
895
896static struct dma_async_tx_descriptor *
897ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
898{
899 int disks = sh->disks;
900 struct page **blocks = percpu->scribble;
901 int target;
902 int qd_idx = sh->qd_idx;
903 struct dma_async_tx_descriptor *tx;
904 struct async_submit_ctl submit;
905 struct r5dev *tgt;
906 struct page *dest;
907 int i;
908 int count;
909
910 if (sh->ops.target < 0)
911 target = sh->ops.target2;
912 else if (sh->ops.target2 < 0)
913 target = sh->ops.target;
914 else
915 /* we should only have one valid target */
916 BUG();
917 BUG_ON(target < 0);
918 pr_debug("%s: stripe %llu block: %d\n",
919 __func__, (unsigned long long)sh->sector, target);
920
921 tgt = &sh->dev[target];
922 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
923 dest = tgt->page;
924
925 atomic_inc(&sh->count);
926
927 if (target == qd_idx) {
928 count = set_syndrome_sources(blocks, sh);
929 blocks[count] = NULL; /* regenerating p is not necessary */
930 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700931 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
932 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700933 to_addr_conv(sh, percpu));
934 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
935 } else {
936 /* Compute any data- or p-drive using XOR */
937 count = 0;
938 for (i = disks; i-- ; ) {
939 if (i == target || i == qd_idx)
940 continue;
941 blocks[count++] = sh->dev[i].page;
942 }
943
Dan Williams0403e382009-09-08 17:42:50 -0700944 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
945 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700946 to_addr_conv(sh, percpu));
947 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
948 }
949
950 return tx;
951}
952
953static struct dma_async_tx_descriptor *
954ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
955{
956 int i, count, disks = sh->disks;
957 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
958 int d0_idx = raid6_d0(sh);
959 int faila = -1, failb = -1;
960 int target = sh->ops.target;
961 int target2 = sh->ops.target2;
962 struct r5dev *tgt = &sh->dev[target];
963 struct r5dev *tgt2 = &sh->dev[target2];
964 struct dma_async_tx_descriptor *tx;
965 struct page **blocks = percpu->scribble;
966 struct async_submit_ctl submit;
967
968 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
969 __func__, (unsigned long long)sh->sector, target, target2);
970 BUG_ON(target < 0 || target2 < 0);
971 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
972 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
973
Dan Williams6c910a72009-09-16 12:24:54 -0700974 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -0700975 * slot number conversion for 'faila' and 'failb'
976 */
977 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100978 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700979 count = 0;
980 i = d0_idx;
981 do {
982 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
983
984 blocks[slot] = sh->dev[i].page;
985
986 if (i == target)
987 faila = slot;
988 if (i == target2)
989 failb = slot;
990 i = raid6_next_disk(i, disks);
991 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700992
993 BUG_ON(faila == failb);
994 if (failb < faila)
995 swap(faila, failb);
996 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
997 __func__, (unsigned long long)sh->sector, faila, failb);
998
999 atomic_inc(&sh->count);
1000
1001 if (failb == syndrome_disks+1) {
1002 /* Q disk is one of the missing disks */
1003 if (faila == syndrome_disks) {
1004 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001005 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1006 ops_complete_compute, sh,
1007 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +11001008 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001009 STRIPE_SIZE, &submit);
1010 } else {
1011 struct page *dest;
1012 int data_target;
1013 int qd_idx = sh->qd_idx;
1014
1015 /* Missing D+Q: recompute D from P, then recompute Q */
1016 if (target == qd_idx)
1017 data_target = target2;
1018 else
1019 data_target = target;
1020
1021 count = 0;
1022 for (i = disks; i-- ; ) {
1023 if (i == data_target || i == qd_idx)
1024 continue;
1025 blocks[count++] = sh->dev[i].page;
1026 }
1027 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001028 init_async_submit(&submit,
1029 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1030 NULL, NULL, NULL,
1031 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001032 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1033 &submit);
1034
1035 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001036 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1037 ops_complete_compute, sh,
1038 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001039 return async_gen_syndrome(blocks, 0, count+2,
1040 STRIPE_SIZE, &submit);
1041 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001042 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001043 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1044 ops_complete_compute, sh,
1045 to_addr_conv(sh, percpu));
1046 if (failb == syndrome_disks) {
1047 /* We're missing D+P. */
1048 return async_raid6_datap_recov(syndrome_disks+2,
1049 STRIPE_SIZE, faila,
1050 blocks, &submit);
1051 } else {
1052 /* We're missing D+D. */
1053 return async_raid6_2data_recov(syndrome_disks+2,
1054 STRIPE_SIZE, faila, failb,
1055 blocks, &submit);
1056 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001057 }
1058}
1059
1060
Dan Williams91c00922007-01-02 13:52:30 -07001061static void ops_complete_prexor(void *stripe_head_ref)
1062{
1063 struct stripe_head *sh = stripe_head_ref;
1064
Harvey Harrisone46b2722008-04-28 02:15:50 -07001065 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001066 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001067}
1068
1069static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001070ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1071 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001072{
Dan Williams91c00922007-01-02 13:52:30 -07001073 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001074 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001075 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001076 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001077
1078 /* existing parity data subtracted */
1079 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1080
Harvey Harrisone46b2722008-04-28 02:15:50 -07001081 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001082 (unsigned long long)sh->sector);
1083
1084 for (i = disks; i--; ) {
1085 struct r5dev *dev = &sh->dev[i];
1086 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001087 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001088 xor_srcs[count++] = dev->page;
1089 }
1090
Dan Williams0403e382009-09-08 17:42:50 -07001091 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001092 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001093 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001094
1095 return tx;
1096}
1097
1098static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001099ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001100{
1101 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001102 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001103
Harvey Harrisone46b2722008-04-28 02:15:50 -07001104 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001105 (unsigned long long)sh->sector);
1106
1107 for (i = disks; i--; ) {
1108 struct r5dev *dev = &sh->dev[i];
1109 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001110
Dan Williamsd8ee0722008-06-28 08:32:06 +10001111 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001112 struct bio *wbi;
1113
NeilBrowncbe47ec2011-07-26 11:20:35 +10001114 spin_lock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001115 chosen = dev->towrite;
1116 dev->towrite = NULL;
1117 BUG_ON(dev->written);
1118 wbi = dev->written = chosen;
NeilBrowncbe47ec2011-07-26 11:20:35 +10001119 spin_unlock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001120
1121 while (wbi && wbi->bi_sector <
1122 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001123 if (wbi->bi_rw & REQ_FUA)
1124 set_bit(R5_WantFUA, &dev->flags);
Dan Williams91c00922007-01-02 13:52:30 -07001125 tx = async_copy_data(1, wbi, dev->page,
1126 dev->sector, tx);
1127 wbi = r5_next_bio(wbi, dev->sector);
1128 }
1129 }
1130 }
1131
1132 return tx;
1133}
1134
Dan Williamsac6b53b2009-07-14 13:40:19 -07001135static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001136{
1137 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001138 int disks = sh->disks;
1139 int pd_idx = sh->pd_idx;
1140 int qd_idx = sh->qd_idx;
1141 int i;
Tejun Heoe9c74692010-09-03 11:56:18 +02001142 bool fua = false;
Dan Williams91c00922007-01-02 13:52:30 -07001143
Harvey Harrisone46b2722008-04-28 02:15:50 -07001144 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001145 (unsigned long long)sh->sector);
1146
Tejun Heoe9c74692010-09-03 11:56:18 +02001147 for (i = disks; i--; )
1148 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
1149
Dan Williams91c00922007-01-02 13:52:30 -07001150 for (i = disks; i--; ) {
1151 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001152
Tejun Heoe9c74692010-09-03 11:56:18 +02001153 if (dev->written || i == pd_idx || i == qd_idx) {
Dan Williams91c00922007-01-02 13:52:30 -07001154 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001155 if (fua)
1156 set_bit(R5_WantFUA, &dev->flags);
1157 }
Dan Williams91c00922007-01-02 13:52:30 -07001158 }
1159
Dan Williamsd8ee0722008-06-28 08:32:06 +10001160 if (sh->reconstruct_state == reconstruct_state_drain_run)
1161 sh->reconstruct_state = reconstruct_state_drain_result;
1162 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1163 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1164 else {
1165 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1166 sh->reconstruct_state = reconstruct_state_result;
1167 }
Dan Williams91c00922007-01-02 13:52:30 -07001168
1169 set_bit(STRIPE_HANDLE, &sh->state);
1170 release_stripe(sh);
1171}
1172
1173static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001174ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1175 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001176{
Dan Williams91c00922007-01-02 13:52:30 -07001177 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001178 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001179 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001180 int count = 0, pd_idx = sh->pd_idx, i;
1181 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001182 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001183 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001184
Harvey Harrisone46b2722008-04-28 02:15:50 -07001185 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001186 (unsigned long long)sh->sector);
1187
1188 /* check if prexor is active which means only process blocks
1189 * that are part of a read-modify-write (written)
1190 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001191 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1192 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001193 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1194 for (i = disks; i--; ) {
1195 struct r5dev *dev = &sh->dev[i];
1196 if (dev->written)
1197 xor_srcs[count++] = dev->page;
1198 }
1199 } else {
1200 xor_dest = sh->dev[pd_idx].page;
1201 for (i = disks; i--; ) {
1202 struct r5dev *dev = &sh->dev[i];
1203 if (i != pd_idx)
1204 xor_srcs[count++] = dev->page;
1205 }
1206 }
1207
Dan Williams91c00922007-01-02 13:52:30 -07001208 /* 1/ if we prexor'd then the dest is reused as a source
1209 * 2/ if we did not prexor then we are redoing the parity
1210 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1211 * for the synchronous xor case
1212 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001213 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001214 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1215
1216 atomic_inc(&sh->count);
1217
Dan Williamsac6b53b2009-07-14 13:40:19 -07001218 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001219 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001220 if (unlikely(count == 1))
1221 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1222 else
1223 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001224}
1225
Dan Williamsac6b53b2009-07-14 13:40:19 -07001226static void
1227ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1228 struct dma_async_tx_descriptor *tx)
1229{
1230 struct async_submit_ctl submit;
1231 struct page **blocks = percpu->scribble;
1232 int count;
1233
1234 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1235
1236 count = set_syndrome_sources(blocks, sh);
1237
1238 atomic_inc(&sh->count);
1239
1240 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1241 sh, to_addr_conv(sh, percpu));
1242 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001243}
1244
1245static void ops_complete_check(void *stripe_head_ref)
1246{
1247 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001248
Harvey Harrisone46b2722008-04-28 02:15:50 -07001249 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001250 (unsigned long long)sh->sector);
1251
Dan Williamsecc65c92008-06-28 08:31:57 +10001252 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001253 set_bit(STRIPE_HANDLE, &sh->state);
1254 release_stripe(sh);
1255}
1256
Dan Williamsac6b53b2009-07-14 13:40:19 -07001257static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001258{
Dan Williams91c00922007-01-02 13:52:30 -07001259 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001260 int pd_idx = sh->pd_idx;
1261 int qd_idx = sh->qd_idx;
1262 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001263 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001264 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001265 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001266 int count;
1267 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001268
Harvey Harrisone46b2722008-04-28 02:15:50 -07001269 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001270 (unsigned long long)sh->sector);
1271
Dan Williamsac6b53b2009-07-14 13:40:19 -07001272 count = 0;
1273 xor_dest = sh->dev[pd_idx].page;
1274 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001275 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001276 if (i == pd_idx || i == qd_idx)
1277 continue;
1278 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001279 }
1280
Dan Williamsd6f38f32009-07-14 11:50:52 -07001281 init_async_submit(&submit, 0, NULL, NULL, NULL,
1282 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001283 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001284 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001285
Dan Williams91c00922007-01-02 13:52:30 -07001286 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001287 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1288 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001289}
1290
Dan Williamsac6b53b2009-07-14 13:40:19 -07001291static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1292{
1293 struct page **srcs = percpu->scribble;
1294 struct async_submit_ctl submit;
1295 int count;
1296
1297 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1298 (unsigned long long)sh->sector, checkp);
1299
1300 count = set_syndrome_sources(srcs, sh);
1301 if (!checkp)
1302 srcs[count] = NULL;
1303
1304 atomic_inc(&sh->count);
1305 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1306 sh, to_addr_conv(sh, percpu));
1307 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1308 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1309}
1310
Dan Williams417b8d42009-10-16 16:25:22 +11001311static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001312{
1313 int overlap_clear = 0, i, disks = sh->disks;
1314 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001315 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001316 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001317 struct raid5_percpu *percpu;
1318 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001319
Dan Williamsd6f38f32009-07-14 11:50:52 -07001320 cpu = get_cpu();
1321 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001322 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001323 ops_run_biofill(sh);
1324 overlap_clear++;
1325 }
1326
Dan Williams7b3a8712008-06-28 08:32:09 +10001327 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001328 if (level < 6)
1329 tx = ops_run_compute5(sh, percpu);
1330 else {
1331 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1332 tx = ops_run_compute6_1(sh, percpu);
1333 else
1334 tx = ops_run_compute6_2(sh, percpu);
1335 }
1336 /* terminate the chain if reconstruct is not set to be run */
1337 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001338 async_tx_ack(tx);
1339 }
Dan Williams91c00922007-01-02 13:52:30 -07001340
Dan Williams600aa102008-06-28 08:32:05 +10001341 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001342 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001343
Dan Williams600aa102008-06-28 08:32:05 +10001344 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001345 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001346 overlap_clear++;
1347 }
1348
Dan Williamsac6b53b2009-07-14 13:40:19 -07001349 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1350 if (level < 6)
1351 ops_run_reconstruct5(sh, percpu, tx);
1352 else
1353 ops_run_reconstruct6(sh, percpu, tx);
1354 }
Dan Williams91c00922007-01-02 13:52:30 -07001355
Dan Williamsac6b53b2009-07-14 13:40:19 -07001356 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1357 if (sh->check_state == check_state_run)
1358 ops_run_check_p(sh, percpu);
1359 else if (sh->check_state == check_state_run_q)
1360 ops_run_check_pq(sh, percpu, 0);
1361 else if (sh->check_state == check_state_run_pq)
1362 ops_run_check_pq(sh, percpu, 1);
1363 else
1364 BUG();
1365 }
Dan Williams91c00922007-01-02 13:52:30 -07001366
Dan Williams91c00922007-01-02 13:52:30 -07001367 if (overlap_clear)
1368 for (i = disks; i--; ) {
1369 struct r5dev *dev = &sh->dev[i];
1370 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1371 wake_up(&sh->raid_conf->wait_for_overlap);
1372 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001373 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001374}
1375
Dan Williams417b8d42009-10-16 16:25:22 +11001376#ifdef CONFIG_MULTICORE_RAID456
1377static void async_run_ops(void *param, async_cookie_t cookie)
1378{
1379 struct stripe_head *sh = param;
1380 unsigned long ops_request = sh->ops.request;
1381
1382 clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
1383 wake_up(&sh->ops.wait_for_ops);
1384
1385 __raid_run_ops(sh, ops_request);
1386 release_stripe(sh);
1387}
1388
1389static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1390{
1391 /* since handle_stripe can be called outside of raid5d context
1392 * we need to ensure sh->ops.request is de-staged before another
1393 * request arrives
1394 */
1395 wait_event(sh->ops.wait_for_ops,
1396 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
1397 sh->ops.request = ops_request;
1398
1399 atomic_inc(&sh->count);
1400 async_schedule(async_run_ops, sh);
1401}
1402#else
1403#define raid_run_ops __raid_run_ops
1404#endif
1405
NeilBrownd1688a62011-10-11 16:49:52 +11001406static int grow_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
1408 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001409 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001410 if (!sh)
1411 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001412
NeilBrown3f294f42005-11-08 21:39:25 -08001413 sh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001414 #ifdef CONFIG_MULTICORE_RAID456
1415 init_waitqueue_head(&sh->ops.wait_for_ops);
1416 #endif
NeilBrown3f294f42005-11-08 21:39:25 -08001417
NeilBrowne4e11e32010-06-16 16:45:16 +10001418 if (grow_buffers(sh)) {
1419 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001420 kmem_cache_free(conf->slab_cache, sh);
1421 return 0;
1422 }
1423 /* we just created an active stripe so... */
1424 atomic_set(&sh->count, 1);
1425 atomic_inc(&conf->active_stripes);
1426 INIT_LIST_HEAD(&sh->lru);
1427 release_stripe(sh);
1428 return 1;
1429}
1430
NeilBrownd1688a62011-10-11 16:49:52 +11001431static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001432{
Christoph Lametere18b8902006-12-06 20:33:20 -08001433 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001434 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
NeilBrownf4be6b42010-06-01 19:37:25 +10001436 if (conf->mddev->gendisk)
1437 sprintf(conf->cache_name[0],
1438 "raid%d-%s", conf->level, mdname(conf->mddev));
1439 else
1440 sprintf(conf->cache_name[0],
1441 "raid%d-%p", conf->level, conf->mddev);
1442 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1443
NeilBrownad01c9e2006-03-27 01:18:07 -08001444 conf->active_name = 0;
1445 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001447 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 if (!sc)
1449 return 1;
1450 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001451 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001452 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001453 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 return 0;
1456}
NeilBrown29269552006-03-27 01:18:10 -08001457
Dan Williamsd6f38f32009-07-14 11:50:52 -07001458/**
1459 * scribble_len - return the required size of the scribble region
1460 * @num - total number of disks in the array
1461 *
1462 * The size must be enough to contain:
1463 * 1/ a struct page pointer for each device in the array +2
1464 * 2/ room to convert each entry in (1) to its corresponding dma
1465 * (dma_map_page()) or page (page_address()) address.
1466 *
1467 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1468 * calculate over all devices (not just the data blocks), using zeros in place
1469 * of the P and Q blocks.
1470 */
1471static size_t scribble_len(int num)
1472{
1473 size_t len;
1474
1475 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1476
1477 return len;
1478}
1479
NeilBrownd1688a62011-10-11 16:49:52 +11001480static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001481{
1482 /* Make all the stripes able to hold 'newsize' devices.
1483 * New slots in each stripe get 'page' set to a new page.
1484 *
1485 * This happens in stages:
1486 * 1/ create a new kmem_cache and allocate the required number of
1487 * stripe_heads.
1488 * 2/ gather all the old stripe_heads and tranfer the pages across
1489 * to the new stripe_heads. This will have the side effect of
1490 * freezing the array as once all stripe_heads have been collected,
1491 * no IO will be possible. Old stripe heads are freed once their
1492 * pages have been transferred over, and the old kmem_cache is
1493 * freed when all stripes are done.
1494 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1495 * we simple return a failre status - no need to clean anything up.
1496 * 4/ allocate new pages for the new slots in the new stripe_heads.
1497 * If this fails, we don't bother trying the shrink the
1498 * stripe_heads down again, we just leave them as they are.
1499 * As each stripe_head is processed the new one is released into
1500 * active service.
1501 *
1502 * Once step2 is started, we cannot afford to wait for a write,
1503 * so we use GFP_NOIO allocations.
1504 */
1505 struct stripe_head *osh, *nsh;
1506 LIST_HEAD(newstripes);
1507 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001508 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001509 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001510 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001511 int i;
1512
1513 if (newsize <= conf->pool_size)
1514 return 0; /* never bother to shrink */
1515
Dan Williamsb5470dc2008-06-27 21:44:04 -07001516 err = md_allow_write(conf->mddev);
1517 if (err)
1518 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001519
NeilBrownad01c9e2006-03-27 01:18:07 -08001520 /* Step 1 */
1521 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1522 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001523 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001524 if (!sc)
1525 return -ENOMEM;
1526
1527 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001528 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001529 if (!nsh)
1530 break;
1531
NeilBrownad01c9e2006-03-27 01:18:07 -08001532 nsh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001533 #ifdef CONFIG_MULTICORE_RAID456
1534 init_waitqueue_head(&nsh->ops.wait_for_ops);
1535 #endif
NeilBrownad01c9e2006-03-27 01:18:07 -08001536
1537 list_add(&nsh->lru, &newstripes);
1538 }
1539 if (i) {
1540 /* didn't get enough, give up */
1541 while (!list_empty(&newstripes)) {
1542 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1543 list_del(&nsh->lru);
1544 kmem_cache_free(sc, nsh);
1545 }
1546 kmem_cache_destroy(sc);
1547 return -ENOMEM;
1548 }
1549 /* Step 2 - Must use GFP_NOIO now.
1550 * OK, we have enough stripes, start collecting inactive
1551 * stripes and copying them over
1552 */
1553 list_for_each_entry(nsh, &newstripes, lru) {
1554 spin_lock_irq(&conf->device_lock);
1555 wait_event_lock_irq(conf->wait_for_stripe,
1556 !list_empty(&conf->inactive_list),
1557 conf->device_lock,
NeilBrown482c0832011-04-18 18:25:42 +10001558 );
NeilBrownad01c9e2006-03-27 01:18:07 -08001559 osh = get_free_stripe(conf);
1560 spin_unlock_irq(&conf->device_lock);
1561 atomic_set(&nsh->count, 1);
1562 for(i=0; i<conf->pool_size; i++)
1563 nsh->dev[i].page = osh->dev[i].page;
1564 for( ; i<newsize; i++)
1565 nsh->dev[i].page = NULL;
1566 kmem_cache_free(conf->slab_cache, osh);
1567 }
1568 kmem_cache_destroy(conf->slab_cache);
1569
1570 /* Step 3.
1571 * At this point, we are holding all the stripes so the array
1572 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001573 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001574 */
1575 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1576 if (ndisks) {
1577 for (i=0; i<conf->raid_disks; i++)
1578 ndisks[i] = conf->disks[i];
1579 kfree(conf->disks);
1580 conf->disks = ndisks;
1581 } else
1582 err = -ENOMEM;
1583
Dan Williamsd6f38f32009-07-14 11:50:52 -07001584 get_online_cpus();
1585 conf->scribble_len = scribble_len(newsize);
1586 for_each_present_cpu(cpu) {
1587 struct raid5_percpu *percpu;
1588 void *scribble;
1589
1590 percpu = per_cpu_ptr(conf->percpu, cpu);
1591 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1592
1593 if (scribble) {
1594 kfree(percpu->scribble);
1595 percpu->scribble = scribble;
1596 } else {
1597 err = -ENOMEM;
1598 break;
1599 }
1600 }
1601 put_online_cpus();
1602
NeilBrownad01c9e2006-03-27 01:18:07 -08001603 /* Step 4, return new stripes to service */
1604 while(!list_empty(&newstripes)) {
1605 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1606 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001607
NeilBrownad01c9e2006-03-27 01:18:07 -08001608 for (i=conf->raid_disks; i < newsize; i++)
1609 if (nsh->dev[i].page == NULL) {
1610 struct page *p = alloc_page(GFP_NOIO);
1611 nsh->dev[i].page = p;
1612 if (!p)
1613 err = -ENOMEM;
1614 }
1615 release_stripe(nsh);
1616 }
1617 /* critical section pass, GFP_NOIO no longer needed */
1618
1619 conf->slab_cache = sc;
1620 conf->active_name = 1-conf->active_name;
1621 conf->pool_size = newsize;
1622 return err;
1623}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
NeilBrownd1688a62011-10-11 16:49:52 +11001625static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626{
1627 struct stripe_head *sh;
1628
NeilBrown3f294f42005-11-08 21:39:25 -08001629 spin_lock_irq(&conf->device_lock);
1630 sh = get_free_stripe(conf);
1631 spin_unlock_irq(&conf->device_lock);
1632 if (!sh)
1633 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001634 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001635 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001636 kmem_cache_free(conf->slab_cache, sh);
1637 atomic_dec(&conf->active_stripes);
1638 return 1;
1639}
1640
NeilBrownd1688a62011-10-11 16:49:52 +11001641static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001642{
1643 while (drop_one_stripe(conf))
1644 ;
1645
NeilBrown29fc7e32006-02-03 03:03:41 -08001646 if (conf->slab_cache)
1647 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 conf->slab_cache = NULL;
1649}
1650
NeilBrown6712ecf2007-09-27 12:47:43 +02001651static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652{
NeilBrown99c0fb52009-03-31 14:39:38 +11001653 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001654 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001655 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001657 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11001658 struct md_rdev *rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 for (i=0 ; i<disks; i++)
1662 if (bi == &sh->dev[i].req)
1663 break;
1664
Dan Williams45b42332007-07-09 11:56:43 -07001665 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1666 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 uptodate);
1668 if (i == disks) {
1669 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001670 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 }
NeilBrown14a75d32011-12-23 10:17:52 +11001672 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11001673 /* If replacement finished while this request was outstanding,
1674 * 'replacement' might be NULL already.
1675 * In that case it moved down to 'rdev'.
1676 * rdev is not removed until all requests are finished.
1677 */
NeilBrown14a75d32011-12-23 10:17:52 +11001678 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001679 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11001680 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001684 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11001685 /* Note that this cannot happen on a
1686 * replacement device. We just fail those on
1687 * any error
1688 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001689 printk_ratelimited(
1690 KERN_INFO
1691 "md/raid:%s: read error corrected"
1692 " (%lu sectors at %llu on %s)\n",
1693 mdname(conf->mddev), STRIPE_SECTORS,
1694 (unsigned long long)(sh->sector
1695 + rdev->data_offset),
1696 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001697 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001698 clear_bit(R5_ReadError, &sh->dev[i].flags);
1699 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1700 }
NeilBrown14a75d32011-12-23 10:17:52 +11001701 if (atomic_read(&rdev->read_errors))
1702 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11001704 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001705 int retry = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001708 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11001709 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1710 printk_ratelimited(
1711 KERN_WARNING
1712 "md/raid:%s: read error on replacement device "
1713 "(sector %llu on %s).\n",
1714 mdname(conf->mddev),
1715 (unsigned long long)(sh->sector
1716 + rdev->data_offset),
1717 bdn);
1718 else if (conf->mddev->degraded >= conf->max_degraded)
Christian Dietrich8bda4702011-07-27 11:00:36 +10001719 printk_ratelimited(
1720 KERN_WARNING
1721 "md/raid:%s: read error not correctable "
1722 "(sector %llu on %s).\n",
1723 mdname(conf->mddev),
1724 (unsigned long long)(sh->sector
1725 + rdev->data_offset),
1726 bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001727 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
NeilBrown4e5314b2005-11-08 21:39:22 -08001728 /* Oh, no!!! */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001729 printk_ratelimited(
1730 KERN_WARNING
1731 "md/raid:%s: read error NOT corrected!! "
1732 "(sector %llu on %s).\n",
1733 mdname(conf->mddev),
1734 (unsigned long long)(sh->sector
1735 + rdev->data_offset),
1736 bdn);
NeilBrownd6950432006-07-10 04:44:20 -07001737 else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001738 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001739 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001740 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001741 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001742 else
1743 retry = 1;
1744 if (retry)
1745 set_bit(R5_ReadError, &sh->dev[i].flags);
1746 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001747 clear_bit(R5_ReadError, &sh->dev[i].flags);
1748 clear_bit(R5_ReWrite, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001749 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 }
NeilBrown14a75d32011-12-23 10:17:52 +11001752 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1754 set_bit(STRIPE_HANDLE, &sh->state);
1755 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756}
1757
NeilBrownd710e132008-10-13 11:55:12 +11001758static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
NeilBrown99c0fb52009-03-31 14:39:38 +11001760 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001761 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001762 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11001763 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001765 sector_t first_bad;
1766 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11001767 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
NeilBrown977df362011-12-23 10:17:53 +11001769 for (i = 0 ; i < disks; i++) {
1770 if (bi == &sh->dev[i].req) {
1771 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 break;
NeilBrown977df362011-12-23 10:17:53 +11001773 }
1774 if (bi == &sh->dev[i].rreq) {
1775 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001776 if (rdev)
1777 replacement = 1;
1778 else
1779 /* rdev was removed and 'replacement'
1780 * replaced it. rdev is not removed
1781 * until all requests are finished.
1782 */
1783 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11001784 break;
1785 }
1786 }
Dan Williams45b42332007-07-09 11:56:43 -07001787 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1789 uptodate);
1790 if (i == disks) {
1791 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001792 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 }
1794
NeilBrown977df362011-12-23 10:17:53 +11001795 if (replacement) {
1796 if (!uptodate)
1797 md_error(conf->mddev, rdev);
1798 else if (is_badblock(rdev, sh->sector,
1799 STRIPE_SECTORS,
1800 &first_bad, &bad_sectors))
1801 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1802 } else {
1803 if (!uptodate) {
1804 set_bit(WriteErrorSeen, &rdev->flags);
1805 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11001806 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1807 set_bit(MD_RECOVERY_NEEDED,
1808 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11001809 } else if (is_badblock(rdev, sh->sector,
1810 STRIPE_SECTORS,
1811 &first_bad, &bad_sectors))
1812 set_bit(R5_MadeGood, &sh->dev[i].flags);
1813 }
1814 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
NeilBrown977df362011-12-23 10:17:53 +11001816 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1817 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001819 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820}
1821
NeilBrown784052e2009-03-31 15:19:07 +11001822static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
NeilBrown784052e2009-03-31 15:19:07 +11001824static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825{
1826 struct r5dev *dev = &sh->dev[i];
1827
1828 bio_init(&dev->req);
1829 dev->req.bi_io_vec = &dev->vec;
1830 dev->req.bi_vcnt++;
1831 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11001833 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
NeilBrown977df362011-12-23 10:17:53 +11001835 bio_init(&dev->rreq);
1836 dev->rreq.bi_io_vec = &dev->rvec;
1837 dev->rreq.bi_vcnt++;
1838 dev->rreq.bi_max_vecs++;
1839 dev->rreq.bi_private = sh;
1840 dev->rvec.bv_page = dev->page;
1841
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001843 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844}
1845
NeilBrownfd01b882011-10-11 16:47:53 +11001846static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847{
1848 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11001849 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11001850 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10001851 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
NeilBrown908f4fb2011-12-23 10:17:50 +11001853 spin_lock_irqsave(&conf->device_lock, flags);
1854 clear_bit(In_sync, &rdev->flags);
1855 mddev->degraded = calc_degraded(conf);
1856 spin_unlock_irqrestore(&conf->device_lock, flags);
1857 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1858
NeilBrownde393cd2011-07-28 11:31:48 +10001859 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001860 set_bit(Faulty, &rdev->flags);
1861 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1862 printk(KERN_ALERT
1863 "md/raid:%s: Disk failure on %s, disabling device.\n"
1864 "md/raid:%s: Operation continuing on %d devices.\n",
1865 mdname(mddev),
1866 bdevname(rdev->bdev, b),
1867 mdname(mddev),
1868 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001869}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
1871/*
1872 * Input: a 'big' sector number,
1873 * Output: index of the data and parity disk, and the sector # in them.
1874 */
NeilBrownd1688a62011-10-11 16:49:52 +11001875static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001876 int previous, int *dd_idx,
1877 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001879 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001880 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001882 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001883 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001885 int algorithm = previous ? conf->prev_algo
1886 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001887 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1888 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001889 int raid_disks = previous ? conf->previous_raid_disks
1890 : conf->raid_disks;
1891 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
1893 /* First compute the information on this sector */
1894
1895 /*
1896 * Compute the chunk number and the sector offset inside the chunk
1897 */
1898 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1899 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
1901 /*
1902 * Compute the stripe number
1903 */
NeilBrown35f2a592010-04-20 14:13:34 +10001904 stripe = chunk_number;
1905 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10001906 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 /*
1908 * Select the parity disk based on the user selected algorithm.
1909 */
NeilBrown84789552011-07-27 11:00:36 +10001910 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07001911 switch(conf->level) {
1912 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001913 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001914 break;
1915 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001916 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001918 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001919 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 (*dd_idx)++;
1921 break;
1922 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001923 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001924 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 (*dd_idx)++;
1926 break;
1927 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001928 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001929 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 break;
1931 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001932 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001933 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001935 case ALGORITHM_PARITY_0:
1936 pd_idx = 0;
1937 (*dd_idx)++;
1938 break;
1939 case ALGORITHM_PARITY_N:
1940 pd_idx = data_disks;
1941 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11001943 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001944 }
1945 break;
1946 case 6:
1947
NeilBrowne183eae2009-03-31 15:20:22 +11001948 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001949 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001950 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001951 qd_idx = pd_idx + 1;
1952 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001953 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001954 qd_idx = 0;
1955 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001956 (*dd_idx) += 2; /* D D P Q D */
1957 break;
1958 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001959 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001960 qd_idx = pd_idx + 1;
1961 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001962 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001963 qd_idx = 0;
1964 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001965 (*dd_idx) += 2; /* D D P Q D */
1966 break;
1967 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001968 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001969 qd_idx = (pd_idx + 1) % raid_disks;
1970 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001971 break;
1972 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001973 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001974 qd_idx = (pd_idx + 1) % raid_disks;
1975 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001976 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001977
1978 case ALGORITHM_PARITY_0:
1979 pd_idx = 0;
1980 qd_idx = 1;
1981 (*dd_idx) += 2;
1982 break;
1983 case ALGORITHM_PARITY_N:
1984 pd_idx = data_disks;
1985 qd_idx = data_disks + 1;
1986 break;
1987
1988 case ALGORITHM_ROTATING_ZERO_RESTART:
1989 /* Exactly the same as RIGHT_ASYMMETRIC, but or
1990 * of blocks for computing Q is different.
1991 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001992 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11001993 qd_idx = pd_idx + 1;
1994 if (pd_idx == raid_disks-1) {
1995 (*dd_idx)++; /* Q D D D P */
1996 qd_idx = 0;
1997 } else if (*dd_idx >= pd_idx)
1998 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001999 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002000 break;
2001
2002 case ALGORITHM_ROTATING_N_RESTART:
2003 /* Same a left_asymmetric, by first stripe is
2004 * D D D P Q rather than
2005 * Q D D D P
2006 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002007 stripe2 += 1;
2008 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002009 qd_idx = pd_idx + 1;
2010 if (pd_idx == raid_disks-1) {
2011 (*dd_idx)++; /* Q D D D P */
2012 qd_idx = 0;
2013 } else if (*dd_idx >= pd_idx)
2014 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002015 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002016 break;
2017
2018 case ALGORITHM_ROTATING_N_CONTINUE:
2019 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002020 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002021 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2022 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002023 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002024 break;
2025
2026 case ALGORITHM_LEFT_ASYMMETRIC_6:
2027 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002028 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002029 if (*dd_idx >= pd_idx)
2030 (*dd_idx)++;
2031 qd_idx = raid_disks - 1;
2032 break;
2033
2034 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002035 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002036 if (*dd_idx >= pd_idx)
2037 (*dd_idx)++;
2038 qd_idx = raid_disks - 1;
2039 break;
2040
2041 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002042 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002043 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2044 qd_idx = raid_disks - 1;
2045 break;
2046
2047 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002048 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002049 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2050 qd_idx = raid_disks - 1;
2051 break;
2052
2053 case ALGORITHM_PARITY_0_6:
2054 pd_idx = 0;
2055 (*dd_idx)++;
2056 qd_idx = raid_disks - 1;
2057 break;
2058
NeilBrown16a53ec2006-06-26 00:27:38 -07002059 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002060 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002061 }
2062 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 }
2064
NeilBrown911d4ee2009-03-31 14:39:38 +11002065 if (sh) {
2066 sh->pd_idx = pd_idx;
2067 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002068 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 /*
2071 * Finally, compute the new sector number
2072 */
2073 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2074 return new_sector;
2075}
2076
2077
NeilBrown784052e2009-03-31 15:19:07 +11002078static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079{
NeilBrownd1688a62011-10-11 16:49:52 +11002080 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002081 int raid_disks = sh->disks;
2082 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002084 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2085 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002086 int algorithm = previous ? conf->prev_algo
2087 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 sector_t stripe;
2089 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002090 sector_t chunk_number;
2091 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002093 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
NeilBrown16a53ec2006-06-26 00:27:38 -07002095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2097 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
NeilBrown16a53ec2006-06-26 00:27:38 -07002099 if (i == sh->pd_idx)
2100 return 0;
2101 switch(conf->level) {
2102 case 4: break;
2103 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002104 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 case ALGORITHM_LEFT_ASYMMETRIC:
2106 case ALGORITHM_RIGHT_ASYMMETRIC:
2107 if (i > sh->pd_idx)
2108 i--;
2109 break;
2110 case ALGORITHM_LEFT_SYMMETRIC:
2111 case ALGORITHM_RIGHT_SYMMETRIC:
2112 if (i < sh->pd_idx)
2113 i += raid_disks;
2114 i -= (sh->pd_idx + 1);
2115 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002116 case ALGORITHM_PARITY_0:
2117 i -= 1;
2118 break;
2119 case ALGORITHM_PARITY_N:
2120 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002122 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002123 }
2124 break;
2125 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002126 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002127 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002128 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002129 case ALGORITHM_LEFT_ASYMMETRIC:
2130 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002131 case ALGORITHM_ROTATING_ZERO_RESTART:
2132 case ALGORITHM_ROTATING_N_RESTART:
2133 if (sh->pd_idx == raid_disks-1)
2134 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002135 else if (i > sh->pd_idx)
2136 i -= 2; /* D D P Q D */
2137 break;
2138 case ALGORITHM_LEFT_SYMMETRIC:
2139 case ALGORITHM_RIGHT_SYMMETRIC:
2140 if (sh->pd_idx == raid_disks-1)
2141 i--; /* Q D D D P */
2142 else {
2143 /* D D P Q D */
2144 if (i < sh->pd_idx)
2145 i += raid_disks;
2146 i -= (sh->pd_idx + 2);
2147 }
2148 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002149 case ALGORITHM_PARITY_0:
2150 i -= 2;
2151 break;
2152 case ALGORITHM_PARITY_N:
2153 break;
2154 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002155 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002156 if (sh->pd_idx == 0)
2157 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002158 else {
2159 /* D D Q P D */
2160 if (i < sh->pd_idx)
2161 i += raid_disks;
2162 i -= (sh->pd_idx + 1);
2163 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002164 break;
2165 case ALGORITHM_LEFT_ASYMMETRIC_6:
2166 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2167 if (i > sh->pd_idx)
2168 i--;
2169 break;
2170 case ALGORITHM_LEFT_SYMMETRIC_6:
2171 case ALGORITHM_RIGHT_SYMMETRIC_6:
2172 if (i < sh->pd_idx)
2173 i += data_disks + 1;
2174 i -= (sh->pd_idx + 1);
2175 break;
2176 case ALGORITHM_PARITY_0_6:
2177 i -= 1;
2178 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002179 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002180 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002181 }
2182 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 }
2184
2185 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002186 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
NeilBrown112bf892009-03-31 14:39:38 +11002188 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002189 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002190 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2191 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002192 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2193 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 return 0;
2195 }
2196 return r_sector;
2197}
2198
2199
Dan Williams600aa102008-06-28 08:32:05 +10002200static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002201schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002202 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002203{
2204 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002205 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002206 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002207
Dan Williamse33129d2007-01-02 13:52:30 -07002208 if (rcw) {
2209 /* if we are not expanding this is a proper write request, and
2210 * there will be bios with new data to be drained into the
2211 * stripe cache
2212 */
2213 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10002214 sh->reconstruct_state = reconstruct_state_drain_run;
2215 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2216 } else
2217 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07002218
Dan Williamsac6b53b2009-07-14 13:40:19 -07002219 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002220
2221 for (i = disks; i--; ) {
2222 struct r5dev *dev = &sh->dev[i];
2223
2224 if (dev->towrite) {
2225 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002226 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002227 if (!expand)
2228 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002229 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002230 }
2231 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002232 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002233 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002234 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002235 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002236 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002237 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2238 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2239
Dan Williamsd8ee0722008-06-28 08:32:06 +10002240 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002241 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2242 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002243 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002244
2245 for (i = disks; i--; ) {
2246 struct r5dev *dev = &sh->dev[i];
2247 if (i == pd_idx)
2248 continue;
2249
Dan Williamse33129d2007-01-02 13:52:30 -07002250 if (dev->towrite &&
2251 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002252 test_bit(R5_Wantcompute, &dev->flags))) {
2253 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002254 set_bit(R5_LOCKED, &dev->flags);
2255 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002256 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002257 }
2258 }
2259 }
2260
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002261 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002262 * are in flight
2263 */
2264 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2265 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002266 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002267
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002268 if (level == 6) {
2269 int qd_idx = sh->qd_idx;
2270 struct r5dev *dev = &sh->dev[qd_idx];
2271
2272 set_bit(R5_LOCKED, &dev->flags);
2273 clear_bit(R5_UPTODATE, &dev->flags);
2274 s->locked++;
2275 }
2276
Dan Williams600aa102008-06-28 08:32:05 +10002277 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002278 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002279 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002280}
NeilBrown16a53ec2006-06-26 00:27:38 -07002281
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282/*
2283 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002284 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 * The bi_next chain must be in order.
2286 */
2287static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2288{
2289 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002290 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002291 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
NeilBrowncbe47ec2011-07-26 11:20:35 +10002293 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 (unsigned long long)bi->bi_sector,
2295 (unsigned long long)sh->sector);
2296
2297
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07002299 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -07002301 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
2302 firstwrite = 1;
2303 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 bip = &sh->dev[dd_idx].toread;
2305 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2306 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2307 goto overlap;
2308 bip = & (*bip)->bi_next;
2309 }
2310 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2311 goto overlap;
2312
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002313 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 if (*bip)
2315 bi->bi_next = *bip;
2316 *bip = bi;
Jens Axboe960e7392008-08-15 10:41:18 +02002317 bi->bi_phys_segments++;
NeilBrown72626682005-09-09 16:23:54 -07002318
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 if (forwrite) {
2320 /* check if page is covered */
2321 sector_t sector = sh->dev[dd_idx].sector;
2322 for (bi=sh->dev[dd_idx].towrite;
2323 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2324 bi && bi->bi_sector <= sector;
2325 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2326 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2327 sector = bi->bi_sector + (bi->bi_size>>9);
2328 }
2329 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2330 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2331 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002332 spin_unlock_irq(&conf->device_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002333
2334 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2335 (unsigned long long)(*bip)->bi_sector,
2336 (unsigned long long)sh->sector, dd_idx);
2337
2338 if (conf->mddev->bitmap && firstwrite) {
2339 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2340 STRIPE_SECTORS, 0);
2341 sh->bm_seq = conf->seq_flush+1;
2342 set_bit(STRIPE_BIT_DELAY, &sh->state);
2343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 return 1;
2345
2346 overlap:
2347 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
2348 spin_unlock_irq(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 return 0;
2350}
2351
NeilBrownd1688a62011-10-11 16:49:52 +11002352static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002353
NeilBrownd1688a62011-10-11 16:49:52 +11002354static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002355 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002356{
NeilBrown784052e2009-03-31 15:19:07 +11002357 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002358 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002359 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002360 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002361 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002362
NeilBrown112bf892009-03-31 14:39:38 +11002363 raid5_compute_sector(conf,
2364 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002365 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002366 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002367 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002368}
2369
Dan Williamsa4456852007-07-09 11:56:43 -07002370static void
NeilBrownd1688a62011-10-11 16:49:52 +11002371handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002372 struct stripe_head_state *s, int disks,
2373 struct bio **return_bi)
2374{
2375 int i;
2376 for (i = disks; i--; ) {
2377 struct bio *bi;
2378 int bitmap_end = 0;
2379
2380 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002381 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002382 rcu_read_lock();
2383 rdev = rcu_dereference(conf->disks[i].rdev);
2384 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002385 atomic_inc(&rdev->nr_pending);
2386 else
2387 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002388 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002389 if (rdev) {
2390 if (!rdev_set_badblocks(
2391 rdev,
2392 sh->sector,
2393 STRIPE_SECTORS, 0))
2394 md_error(conf->mddev, rdev);
2395 rdev_dec_pending(rdev, conf->mddev);
2396 }
Dan Williamsa4456852007-07-09 11:56:43 -07002397 }
2398 spin_lock_irq(&conf->device_lock);
2399 /* fail all writes first */
2400 bi = sh->dev[i].towrite;
2401 sh->dev[i].towrite = NULL;
2402 if (bi) {
2403 s->to_write--;
2404 bitmap_end = 1;
2405 }
2406
2407 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2408 wake_up(&conf->wait_for_overlap);
2409
2410 while (bi && bi->bi_sector <
2411 sh->dev[i].sector + STRIPE_SECTORS) {
2412 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2413 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002414 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002415 md_write_end(conf->mddev);
2416 bi->bi_next = *return_bi;
2417 *return_bi = bi;
2418 }
2419 bi = nextbi;
2420 }
2421 /* and fail all 'written' */
2422 bi = sh->dev[i].written;
2423 sh->dev[i].written = NULL;
2424 if (bi) bitmap_end = 1;
2425 while (bi && bi->bi_sector <
2426 sh->dev[i].sector + STRIPE_SECTORS) {
2427 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2428 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002429 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002430 md_write_end(conf->mddev);
2431 bi->bi_next = *return_bi;
2432 *return_bi = bi;
2433 }
2434 bi = bi2;
2435 }
2436
Dan Williamsb5e98d62007-01-02 13:52:31 -07002437 /* fail any reads if this device is non-operational and
2438 * the data has not reached the cache yet.
2439 */
2440 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2441 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2442 test_bit(R5_ReadError, &sh->dev[i].flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002443 bi = sh->dev[i].toread;
2444 sh->dev[i].toread = NULL;
2445 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2446 wake_up(&conf->wait_for_overlap);
2447 if (bi) s->to_read--;
2448 while (bi && bi->bi_sector <
2449 sh->dev[i].sector + STRIPE_SECTORS) {
2450 struct bio *nextbi =
2451 r5_next_bio(bi, sh->dev[i].sector);
2452 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002453 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002454 bi->bi_next = *return_bi;
2455 *return_bi = bi;
2456 }
2457 bi = nextbi;
2458 }
2459 }
2460 spin_unlock_irq(&conf->device_lock);
2461 if (bitmap_end)
2462 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2463 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002464 /* If we were in the middle of a write the parity block might
2465 * still be locked - so just clear all R5_LOCKED flags
2466 */
2467 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002468 }
2469
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002470 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2471 if (atomic_dec_and_test(&conf->pending_full_writes))
2472 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002473}
2474
NeilBrown7f0da592011-07-28 11:39:22 +10002475static void
NeilBrownd1688a62011-10-11 16:49:52 +11002476handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002477 struct stripe_head_state *s)
2478{
2479 int abort = 0;
2480 int i;
2481
NeilBrown7f0da592011-07-28 11:39:22 +10002482 clear_bit(STRIPE_SYNCING, &sh->state);
2483 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002484 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002485 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10002486 * Don't even need to abort as that is handled elsewhere
2487 * if needed, and not always wanted e.g. if there is a known
2488 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11002489 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002490 * non-sync devices, or abort the recovery
2491 */
NeilBrown18b98372012-04-01 23:48:38 +10002492 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2493 /* During recovery devices cannot be removed, so
2494 * locking and refcounting of rdevs is not needed
2495 */
2496 for (i = 0; i < conf->raid_disks; i++) {
2497 struct md_rdev *rdev = conf->disks[i].rdev;
2498 if (rdev
2499 && !test_bit(Faulty, &rdev->flags)
2500 && !test_bit(In_sync, &rdev->flags)
2501 && !rdev_set_badblocks(rdev, sh->sector,
2502 STRIPE_SECTORS, 0))
2503 abort = 1;
2504 rdev = conf->disks[i].replacement;
2505 if (rdev
2506 && !test_bit(Faulty, &rdev->flags)
2507 && !test_bit(In_sync, &rdev->flags)
2508 && !rdev_set_badblocks(rdev, sh->sector,
2509 STRIPE_SECTORS, 0))
2510 abort = 1;
2511 }
2512 if (abort)
2513 conf->recovery_disabled =
2514 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10002515 }
NeilBrown18b98372012-04-01 23:48:38 +10002516 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10002517}
2518
NeilBrown9a3e1102011-12-23 10:17:53 +11002519static int want_replace(struct stripe_head *sh, int disk_idx)
2520{
2521 struct md_rdev *rdev;
2522 int rv = 0;
2523 /* Doing recovery so rcu locking not required */
2524 rdev = sh->raid_conf->disks[disk_idx].replacement;
2525 if (rdev
2526 && !test_bit(Faulty, &rdev->flags)
2527 && !test_bit(In_sync, &rdev->flags)
2528 && (rdev->recovery_offset <= sh->sector
2529 || rdev->mddev->recovery_cp <= sh->sector))
2530 rv = 1;
2531
2532 return rv;
2533}
2534
NeilBrown93b3dbc2011-07-27 11:00:36 +10002535/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002536 * to be read or computed to satisfy a request.
2537 *
2538 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002539 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002540 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002541static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2542 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002543{
2544 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002545 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2546 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002547
Dan Williamsf38e1212007-01-02 13:52:30 -07002548 /* is the data in this block needed, and can we get it? */
2549 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002550 !test_bit(R5_UPTODATE, &dev->flags) &&
2551 (dev->toread ||
2552 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2553 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002554 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002555 (s->failed >= 1 && fdev[0]->toread) ||
2556 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002557 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2558 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2559 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002560 /* we would like to get this block, possibly by computing it,
2561 * otherwise read it if the backing disk is insync
2562 */
2563 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2564 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2565 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002566 (s->failed && (disk_idx == s->failed_num[0] ||
2567 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002568 /* have disk failed, and we're requested to fetch it;
2569 * do compute it
2570 */
2571 pr_debug("Computing stripe %llu block %d\n",
2572 (unsigned long long)sh->sector, disk_idx);
2573 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2574 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2575 set_bit(R5_Wantcompute, &dev->flags);
2576 sh->ops.target = disk_idx;
2577 sh->ops.target2 = -1; /* no 2nd target */
2578 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002579 /* Careful: from this point on 'uptodate' is in the eye
2580 * of raid_run_ops which services 'compute' operations
2581 * before writes. R5_Wantcompute flags a block that will
2582 * be R5_UPTODATE by the time it is needed for a
2583 * subsequent operation.
2584 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002585 s->uptodate++;
2586 return 1;
2587 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2588 /* Computing 2-failure is *very* expensive; only
2589 * do it if failed >= 2
2590 */
2591 int other;
2592 for (other = disks; other--; ) {
2593 if (other == disk_idx)
2594 continue;
2595 if (!test_bit(R5_UPTODATE,
2596 &sh->dev[other].flags))
2597 break;
2598 }
2599 BUG_ON(other < 0);
2600 pr_debug("Computing stripe %llu blocks %d,%d\n",
2601 (unsigned long long)sh->sector,
2602 disk_idx, other);
2603 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2604 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2605 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2606 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2607 sh->ops.target = disk_idx;
2608 sh->ops.target2 = other;
2609 s->uptodate += 2;
2610 s->req_compute = 1;
2611 return 1;
2612 } else if (test_bit(R5_Insync, &dev->flags)) {
2613 set_bit(R5_LOCKED, &dev->flags);
2614 set_bit(R5_Wantread, &dev->flags);
2615 s->locked++;
2616 pr_debug("Reading block %d (sync=%d)\n",
2617 disk_idx, s->syncing);
2618 }
2619 }
2620
2621 return 0;
2622}
2623
2624/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002625 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002626 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002627static void handle_stripe_fill(struct stripe_head *sh,
2628 struct stripe_head_state *s,
2629 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002630{
2631 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002632
2633 /* look for blocks to read/compute, skip this if a compute
2634 * is already in flight, or if the stripe contents are in the
2635 * midst of changing due to a write
2636 */
2637 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2638 !sh->reconstruct_state)
2639 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002640 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002641 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002642 set_bit(STRIPE_HANDLE, &sh->state);
2643}
2644
2645
Dan Williams1fe797e2008-06-28 09:16:30 +10002646/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002647 * any written block on an uptodate or failed drive can be returned.
2648 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2649 * never LOCKED, so we don't need to test 'failed' directly.
2650 */
NeilBrownd1688a62011-10-11 16:49:52 +11002651static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002652 struct stripe_head *sh, int disks, struct bio **return_bi)
2653{
2654 int i;
2655 struct r5dev *dev;
2656
2657 for (i = disks; i--; )
2658 if (sh->dev[i].written) {
2659 dev = &sh->dev[i];
2660 if (!test_bit(R5_LOCKED, &dev->flags) &&
2661 test_bit(R5_UPTODATE, &dev->flags)) {
2662 /* We can return any write requests */
2663 struct bio *wbi, *wbi2;
2664 int bitmap_end = 0;
Dan Williams45b42332007-07-09 11:56:43 -07002665 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002666 spin_lock_irq(&conf->device_lock);
2667 wbi = dev->written;
2668 dev->written = NULL;
2669 while (wbi && wbi->bi_sector <
2670 dev->sector + STRIPE_SECTORS) {
2671 wbi2 = r5_next_bio(wbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +02002672 if (!raid5_dec_bi_phys_segments(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002673 md_write_end(conf->mddev);
2674 wbi->bi_next = *return_bi;
2675 *return_bi = wbi;
2676 }
2677 wbi = wbi2;
2678 }
2679 if (dev->towrite == NULL)
2680 bitmap_end = 1;
2681 spin_unlock_irq(&conf->device_lock);
2682 if (bitmap_end)
2683 bitmap_endwrite(conf->mddev->bitmap,
2684 sh->sector,
2685 STRIPE_SECTORS,
2686 !test_bit(STRIPE_DEGRADED, &sh->state),
2687 0);
2688 }
2689 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002690
2691 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2692 if (atomic_dec_and_test(&conf->pending_full_writes))
2693 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002694}
2695
NeilBrownd1688a62011-10-11 16:49:52 +11002696static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002697 struct stripe_head *sh,
2698 struct stripe_head_state *s,
2699 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002700{
2701 int rmw = 0, rcw = 0, i;
NeilBrownc8ac1802011-07-27 11:00:36 +10002702 if (conf->max_degraded == 2) {
2703 /* RAID6 requires 'rcw' in current implementation
2704 * Calculate the real rcw later - for now fake it
2705 * look like rcw is cheaper
2706 */
2707 rcw = 1; rmw = 2;
2708 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002709 /* would I have to read this buffer for read_modify_write */
2710 struct r5dev *dev = &sh->dev[i];
2711 if ((dev->towrite || i == sh->pd_idx) &&
2712 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002713 !(test_bit(R5_UPTODATE, &dev->flags) ||
2714 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002715 if (test_bit(R5_Insync, &dev->flags))
2716 rmw++;
2717 else
2718 rmw += 2*disks; /* cannot read it */
2719 }
2720 /* Would I have to read this buffer for reconstruct_write */
2721 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2722 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002723 !(test_bit(R5_UPTODATE, &dev->flags) ||
2724 test_bit(R5_Wantcompute, &dev->flags))) {
2725 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002726 else
2727 rcw += 2*disks;
2728 }
2729 }
Dan Williams45b42332007-07-09 11:56:43 -07002730 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002731 (unsigned long long)sh->sector, rmw, rcw);
2732 set_bit(STRIPE_HANDLE, &sh->state);
2733 if (rmw < rcw && rmw > 0)
2734 /* prefer read-modify-write, but need to get some data */
2735 for (i = disks; i--; ) {
2736 struct r5dev *dev = &sh->dev[i];
2737 if ((dev->towrite || i == sh->pd_idx) &&
2738 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002739 !(test_bit(R5_UPTODATE, &dev->flags) ||
2740 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002741 test_bit(R5_Insync, &dev->flags)) {
2742 if (
2743 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002744 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002745 "%d for r-m-w\n", i);
2746 set_bit(R5_LOCKED, &dev->flags);
2747 set_bit(R5_Wantread, &dev->flags);
2748 s->locked++;
2749 } else {
2750 set_bit(STRIPE_DELAYED, &sh->state);
2751 set_bit(STRIPE_HANDLE, &sh->state);
2752 }
2753 }
2754 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002755 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002756 /* want reconstruct write, but need to get some data */
NeilBrownc8ac1802011-07-27 11:00:36 +10002757 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002758 for (i = disks; i--; ) {
2759 struct r5dev *dev = &sh->dev[i];
2760 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002761 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002762 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002763 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002764 test_bit(R5_Wantcompute, &dev->flags))) {
2765 rcw++;
2766 if (!test_bit(R5_Insync, &dev->flags))
2767 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002768 if (
2769 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002770 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002771 "%d for Reconstruct\n", i);
2772 set_bit(R5_LOCKED, &dev->flags);
2773 set_bit(R5_Wantread, &dev->flags);
2774 s->locked++;
2775 } else {
2776 set_bit(STRIPE_DELAYED, &sh->state);
2777 set_bit(STRIPE_HANDLE, &sh->state);
2778 }
2779 }
2780 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002781 }
Dan Williamsa4456852007-07-09 11:56:43 -07002782 /* now if nothing is locked, and if we have enough data,
2783 * we can start a write request
2784 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002785 /* since handle_stripe can be called at any time we need to handle the
2786 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002787 * subsequent call wants to start a write request. raid_run_ops only
2788 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002789 * simultaneously. If this is not the case then new writes need to be
2790 * held off until the compute completes.
2791 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002792 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2793 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2794 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002795 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002796}
2797
NeilBrownd1688a62011-10-11 16:49:52 +11002798static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002799 struct stripe_head_state *s, int disks)
2800{
Dan Williamsecc65c92008-06-28 08:31:57 +10002801 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002802
Dan Williamsbd2ab672008-04-10 21:29:27 -07002803 set_bit(STRIPE_HANDLE, &sh->state);
2804
Dan Williamsecc65c92008-06-28 08:31:57 +10002805 switch (sh->check_state) {
2806 case check_state_idle:
2807 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002808 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002809 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002810 sh->check_state = check_state_run;
2811 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002812 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002813 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002814 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002815 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002816 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002817 /* fall through */
2818 case check_state_compute_result:
2819 sh->check_state = check_state_idle;
2820 if (!dev)
2821 dev = &sh->dev[sh->pd_idx];
2822
2823 /* check that a write has not made the stripe insync */
2824 if (test_bit(STRIPE_INSYNC, &sh->state))
2825 break;
2826
2827 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002828 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2829 BUG_ON(s->uptodate != disks);
2830
2831 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002832 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002833 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002834
Dan Williamsa4456852007-07-09 11:56:43 -07002835 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002836 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002837 break;
2838 case check_state_run:
2839 break; /* we will be called again upon completion */
2840 case check_state_check_result:
2841 sh->check_state = check_state_idle;
2842
2843 /* if a failure occurred during the check operation, leave
2844 * STRIPE_INSYNC not set and let the stripe be handled again
2845 */
2846 if (s->failed)
2847 break;
2848
2849 /* handle a successful check operation, if parity is correct
2850 * we are done. Otherwise update the mismatch count and repair
2851 * parity if !MD_RECOVERY_CHECK
2852 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002853 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002854 /* parity is correct (on disc,
2855 * not in buffer any more)
2856 */
2857 set_bit(STRIPE_INSYNC, &sh->state);
2858 else {
2859 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2860 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2861 /* don't try to repair!! */
2862 set_bit(STRIPE_INSYNC, &sh->state);
2863 else {
2864 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002865 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002866 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2867 set_bit(R5_Wantcompute,
2868 &sh->dev[sh->pd_idx].flags);
2869 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002870 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10002871 s->uptodate++;
2872 }
2873 }
2874 break;
2875 case check_state_compute_run:
2876 break;
2877 default:
2878 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2879 __func__, sh->check_state,
2880 (unsigned long long) sh->sector);
2881 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002882 }
2883}
2884
2885
NeilBrownd1688a62011-10-11 16:49:52 +11002886static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07002887 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10002888 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002889{
Dan Williamsa4456852007-07-09 11:56:43 -07002890 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11002891 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07002892 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07002893
2894 set_bit(STRIPE_HANDLE, &sh->state);
2895
2896 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002897
Dan Williamsa4456852007-07-09 11:56:43 -07002898 /* Want to check and possibly repair P and Q.
2899 * However there could be one 'failed' device, in which
2900 * case we can only check one of them, possibly using the
2901 * other to generate missing data
2902 */
2903
Dan Williamsd82dfee2009-07-14 13:40:57 -07002904 switch (sh->check_state) {
2905 case check_state_idle:
2906 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10002907 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002908 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07002909 * makes sense to check P (If anything else were failed,
2910 * we would have used P to recreate it).
2911 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002912 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07002913 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002914 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002915 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07002916 * anything, so it makes sense to check it
2917 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002918 if (sh->check_state == check_state_run)
2919 sh->check_state = check_state_run_pq;
2920 else
2921 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07002922 }
Dan Williams36d1c642009-07-14 11:48:22 -07002923
Dan Williamsd82dfee2009-07-14 13:40:57 -07002924 /* discard potentially stale zero_sum_result */
2925 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07002926
Dan Williamsd82dfee2009-07-14 13:40:57 -07002927 if (sh->check_state == check_state_run) {
2928 /* async_xor_zero_sum destroys the contents of P */
2929 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
2930 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07002931 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002932 if (sh->check_state >= check_state_run &&
2933 sh->check_state <= check_state_run_pq) {
2934 /* async_syndrome_zero_sum preserves P and Q, so
2935 * no need to mark them !uptodate here
2936 */
2937 set_bit(STRIPE_OP_CHECK, &s->ops_request);
2938 break;
2939 }
Dan Williams36d1c642009-07-14 11:48:22 -07002940
Dan Williamsd82dfee2009-07-14 13:40:57 -07002941 /* we have 2-disk failure */
2942 BUG_ON(s->failed != 2);
2943 /* fall through */
2944 case check_state_compute_result:
2945 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07002946
Dan Williamsd82dfee2009-07-14 13:40:57 -07002947 /* check that a write has not made the stripe insync */
2948 if (test_bit(STRIPE_INSYNC, &sh->state))
2949 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002950
2951 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07002952 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07002953 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002954 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07002955 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10002956 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07002957 s->locked++;
2958 set_bit(R5_LOCKED, &dev->flags);
2959 set_bit(R5_Wantwrite, &dev->flags);
2960 }
2961 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10002962 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07002963 s->locked++;
2964 set_bit(R5_LOCKED, &dev->flags);
2965 set_bit(R5_Wantwrite, &dev->flags);
2966 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002967 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002968 dev = &sh->dev[pd_idx];
2969 s->locked++;
2970 set_bit(R5_LOCKED, &dev->flags);
2971 set_bit(R5_Wantwrite, &dev->flags);
2972 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002973 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002974 dev = &sh->dev[qd_idx];
2975 s->locked++;
2976 set_bit(R5_LOCKED, &dev->flags);
2977 set_bit(R5_Wantwrite, &dev->flags);
2978 }
2979 clear_bit(STRIPE_DEGRADED, &sh->state);
2980
2981 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002982 break;
2983 case check_state_run:
2984 case check_state_run_q:
2985 case check_state_run_pq:
2986 break; /* we will be called again upon completion */
2987 case check_state_check_result:
2988 sh->check_state = check_state_idle;
2989
2990 /* handle a successful check operation, if parity is correct
2991 * we are done. Otherwise update the mismatch count and repair
2992 * parity if !MD_RECOVERY_CHECK
2993 */
2994 if (sh->ops.zero_sum_result == 0) {
2995 /* both parities are correct */
2996 if (!s->failed)
2997 set_bit(STRIPE_INSYNC, &sh->state);
2998 else {
2999 /* in contrast to the raid5 case we can validate
3000 * parity, but still have a failure to write
3001 * back
3002 */
3003 sh->check_state = check_state_compute_result;
3004 /* Returning at this point means that we may go
3005 * off and bring p and/or q uptodate again so
3006 * we make sure to check zero_sum_result again
3007 * to verify if p or q need writeback
3008 */
3009 }
3010 } else {
3011 conf->mddev->resync_mismatches += STRIPE_SECTORS;
3012 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3013 /* don't try to repair!! */
3014 set_bit(STRIPE_INSYNC, &sh->state);
3015 else {
3016 int *target = &sh->ops.target;
3017
3018 sh->ops.target = -1;
3019 sh->ops.target2 = -1;
3020 sh->check_state = check_state_compute_run;
3021 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3022 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3023 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3024 set_bit(R5_Wantcompute,
3025 &sh->dev[pd_idx].flags);
3026 *target = pd_idx;
3027 target = &sh->ops.target2;
3028 s->uptodate++;
3029 }
3030 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3031 set_bit(R5_Wantcompute,
3032 &sh->dev[qd_idx].flags);
3033 *target = qd_idx;
3034 s->uptodate++;
3035 }
3036 }
3037 }
3038 break;
3039 case check_state_compute_run:
3040 break;
3041 default:
3042 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3043 __func__, sh->check_state,
3044 (unsigned long long) sh->sector);
3045 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003046 }
3047}
3048
NeilBrownd1688a62011-10-11 16:49:52 +11003049static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003050{
3051 int i;
3052
3053 /* We have read all the blocks in this stripe and now we need to
3054 * copy some of them into a target stripe for expand.
3055 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003056 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003057 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3058 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003059 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003060 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003061 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003062 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003063
NeilBrown784052e2009-03-31 15:19:07 +11003064 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003065 sector_t s = raid5_compute_sector(conf, bn, 0,
3066 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003067 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003068 if (sh2 == NULL)
3069 /* so far only the early blocks of this stripe
3070 * have been requested. When later blocks
3071 * get requested, we will try again
3072 */
3073 continue;
3074 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3075 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3076 /* must have already done this block */
3077 release_stripe(sh2);
3078 continue;
3079 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003080
3081 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003082 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003083 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003084 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003085 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003086
Dan Williamsa4456852007-07-09 11:56:43 -07003087 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3088 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3089 for (j = 0; j < conf->raid_disks; j++)
3090 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003091 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003092 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3093 break;
3094 if (j == conf->raid_disks) {
3095 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3096 set_bit(STRIPE_HANDLE, &sh2->state);
3097 }
3098 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003099
Dan Williamsa4456852007-07-09 11:56:43 -07003100 }
NeilBrowna2e08552007-09-11 15:23:36 -07003101 /* done submitting copies, wait for them to complete */
3102 if (tx) {
3103 async_tx_ack(tx);
3104 dma_wait_for_async_tx(tx);
3105 }
Dan Williamsa4456852007-07-09 11:56:43 -07003106}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
3108/*
3109 * handle_stripe - do things to a stripe.
3110 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003111 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3112 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003114 * return some read requests which now have data
3115 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 * schedule a read on some buffers
3117 * schedule a write of some buffers
3118 * return confirmation of parity correctness
3119 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 */
Dan Williamsa4456852007-07-09 11:56:43 -07003121
NeilBrownacfe7262011-07-27 11:00:36 +10003122static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003123{
NeilBrownd1688a62011-10-11 16:49:52 +11003124 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003125 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003126 struct r5dev *dev;
3127 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003128 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003129
NeilBrownacfe7262011-07-27 11:00:36 +10003130 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003131
NeilBrownacfe7262011-07-27 11:00:36 +10003132 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3133 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3134 s->failed_num[0] = -1;
3135 s->failed_num[1] = -1;
3136
3137 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003138 rcu_read_lock();
NeilBrownc4c16632011-07-26 11:34:20 +10003139 spin_lock_irq(&conf->device_lock);
NeilBrown16a53ec2006-06-26 00:27:38 -07003140 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003141 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003142 sector_t first_bad;
3143 int bad_sectors;
3144 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003145
NeilBrown16a53ec2006-06-26 00:27:38 -07003146 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003147
Dan Williams45b42332007-07-09 11:56:43 -07003148 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003149 i, dev->flags,
3150 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003151 /* maybe we can reply to a read
3152 *
3153 * new wantfill requests are only permitted while
3154 * ops_complete_biofill is guaranteed to be inactive
3155 */
3156 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3157 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3158 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003159
3160 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003161 if (test_bit(R5_LOCKED, &dev->flags))
3162 s->locked++;
3163 if (test_bit(R5_UPTODATE, &dev->flags))
3164 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003165 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003166 s->compute++;
3167 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003168 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003169
NeilBrownacfe7262011-07-27 11:00:36 +10003170 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003171 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003172 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003173 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003174 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003175 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003176 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003177 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003178 }
Dan Williamsa4456852007-07-09 11:56:43 -07003179 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003180 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003181 /* Prefer to use the replacement for reads, but only
3182 * if it is recovered enough and has no bad blocks.
3183 */
3184 rdev = rcu_dereference(conf->disks[i].replacement);
3185 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3186 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3187 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3188 &first_bad, &bad_sectors))
3189 set_bit(R5_ReadRepl, &dev->flags);
3190 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003191 if (rdev)
3192 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003193 rdev = rcu_dereference(conf->disks[i].rdev);
3194 clear_bit(R5_ReadRepl, &dev->flags);
3195 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003196 if (rdev && test_bit(Faulty, &rdev->flags))
3197 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003198 if (rdev) {
3199 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3200 &first_bad, &bad_sectors);
3201 if (s->blocked_rdev == NULL
3202 && (test_bit(Blocked, &rdev->flags)
3203 || is_bad < 0)) {
3204 if (is_bad < 0)
3205 set_bit(BlockedBadBlocks,
3206 &rdev->flags);
3207 s->blocked_rdev = rdev;
3208 atomic_inc(&rdev->nr_pending);
3209 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003210 }
NeilBrown415e72d2010-06-17 17:25:21 +10003211 clear_bit(R5_Insync, &dev->flags);
3212 if (!rdev)
3213 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003214 else if (is_bad) {
3215 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10003216 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3217 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003218 /* treat as in-sync, but with a read error
3219 * which we can now try to correct
3220 */
3221 set_bit(R5_Insync, &dev->flags);
3222 set_bit(R5_ReadError, &dev->flags);
3223 }
3224 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003225 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003226 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003227 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003228 set_bit(R5_Insync, &dev->flags);
3229 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3230 test_bit(R5_Expanded, &dev->flags))
3231 /* If we've reshaped into here, we assume it is Insync.
3232 * We will shortly update recovery_offset to make
3233 * it official.
3234 */
3235 set_bit(R5_Insync, &dev->flags);
3236
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003237 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003238 /* This flag does not apply to '.replacement'
3239 * only to .rdev, so make sure to check that*/
3240 struct md_rdev *rdev2 = rcu_dereference(
3241 conf->disks[i].rdev);
3242 if (rdev2 == rdev)
3243 clear_bit(R5_Insync, &dev->flags);
3244 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003245 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003246 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003247 } else
3248 clear_bit(R5_WriteError, &dev->flags);
3249 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003250 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003251 /* This flag does not apply to '.replacement'
3252 * only to .rdev, so make sure to check that*/
3253 struct md_rdev *rdev2 = rcu_dereference(
3254 conf->disks[i].rdev);
3255 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003256 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003257 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003258 } else
3259 clear_bit(R5_MadeGood, &dev->flags);
3260 }
NeilBrown977df362011-12-23 10:17:53 +11003261 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3262 struct md_rdev *rdev2 = rcu_dereference(
3263 conf->disks[i].replacement);
3264 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3265 s->handle_bad_blocks = 1;
3266 atomic_inc(&rdev2->nr_pending);
3267 } else
3268 clear_bit(R5_MadeGoodRepl, &dev->flags);
3269 }
NeilBrown415e72d2010-06-17 17:25:21 +10003270 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003271 /* The ReadError flag will just be confusing now */
3272 clear_bit(R5_ReadError, &dev->flags);
3273 clear_bit(R5_ReWrite, &dev->flags);
3274 }
NeilBrown415e72d2010-06-17 17:25:21 +10003275 if (test_bit(R5_ReadError, &dev->flags))
3276 clear_bit(R5_Insync, &dev->flags);
3277 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003278 if (s->failed < 2)
3279 s->failed_num[s->failed] = i;
3280 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003281 if (rdev && !test_bit(Faulty, &rdev->flags))
3282 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003283 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003284 }
NeilBrownc4c16632011-07-26 11:34:20 +10003285 spin_unlock_irq(&conf->device_lock);
NeilBrown9a3e1102011-12-23 10:17:53 +11003286 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3287 /* If there is a failed device being replaced,
3288 * we must be recovering.
3289 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10003290 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11003291 * else we can only be replacing
3292 * sync and recovery both need to read all devices, and so
3293 * use the same flag.
3294 */
3295 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10003296 sh->sector >= conf->mddev->recovery_cp ||
3297 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11003298 s->syncing = 1;
3299 else
3300 s->replacing = 1;
3301 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003302 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003303}
NeilBrownf4168852007-02-28 20:11:53 -08003304
NeilBrowncc940152011-07-26 11:35:35 +10003305static void handle_stripe(struct stripe_head *sh)
3306{
3307 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003308 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003309 int i;
NeilBrown84789552011-07-27 11:00:36 +10003310 int prexor;
3311 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003312 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003313
3314 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003315 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003316 /* already being handled, ensure it gets handled
3317 * again when current action finishes */
3318 set_bit(STRIPE_HANDLE, &sh->state);
3319 return;
3320 }
3321
3322 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3323 set_bit(STRIPE_SYNCING, &sh->state);
3324 clear_bit(STRIPE_INSYNC, &sh->state);
3325 }
3326 clear_bit(STRIPE_DELAYED, &sh->state);
3327
3328 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3329 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3330 (unsigned long long)sh->sector, sh->state,
3331 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3332 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003333
NeilBrownacfe7262011-07-27 11:00:36 +10003334 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003335
NeilBrownbc2607f2011-07-28 11:39:22 +10003336 if (s.handle_bad_blocks) {
3337 set_bit(STRIPE_HANDLE, &sh->state);
3338 goto finish;
3339 }
3340
NeilBrown474af965fe2011-07-27 11:00:36 +10003341 if (unlikely(s.blocked_rdev)) {
3342 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003343 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003344 set_bit(STRIPE_HANDLE, &sh->state);
3345 goto finish;
3346 }
3347 /* There is nothing for the blocked_rdev to block */
3348 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3349 s.blocked_rdev = NULL;
3350 }
3351
3352 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3353 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3354 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3355 }
3356
3357 pr_debug("locked=%d uptodate=%d to_read=%d"
3358 " to_write=%d failed=%d failed_num=%d,%d\n",
3359 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3360 s.failed_num[0], s.failed_num[1]);
3361 /* check if the array has lost more than max_degraded devices and,
3362 * if so, some requests might need to be failed.
3363 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003364 if (s.failed > conf->max_degraded) {
3365 sh->check_state = 0;
3366 sh->reconstruct_state = 0;
3367 if (s.to_read+s.to_write+s.written)
3368 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003369 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003370 handle_failed_sync(conf, sh, &s);
3371 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003372
3373 /*
3374 * might be able to return some write requests if the parity blocks
3375 * are safe, or on a failed drive
3376 */
3377 pdev = &sh->dev[sh->pd_idx];
3378 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3379 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3380 qdev = &sh->dev[sh->qd_idx];
3381 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3382 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3383 || conf->level < 6;
3384
3385 if (s.written &&
3386 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3387 && !test_bit(R5_LOCKED, &pdev->flags)
3388 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3389 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3390 && !test_bit(R5_LOCKED, &qdev->flags)
3391 && test_bit(R5_UPTODATE, &qdev->flags)))))
3392 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3393
3394 /* Now we might consider reading some blocks, either to check/generate
3395 * parity, or to satisfy requests
3396 * or to load a block that is being partially written.
3397 */
3398 if (s.to_read || s.non_overwrite
3399 || (conf->level == 6 && s.to_write && s.failed)
NeilBrown9a3e1102011-12-23 10:17:53 +11003400 || (s.syncing && (s.uptodate + s.compute < disks))
3401 || s.replacing
3402 || s.expanding)
NeilBrown474af965fe2011-07-27 11:00:36 +10003403 handle_stripe_fill(sh, &s, disks);
3404
NeilBrown84789552011-07-27 11:00:36 +10003405 /* Now we check to see if any write operations have recently
3406 * completed
3407 */
3408 prexor = 0;
3409 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3410 prexor = 1;
3411 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3412 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3413 sh->reconstruct_state = reconstruct_state_idle;
3414
3415 /* All the 'written' buffers and the parity block are ready to
3416 * be written back to disk
3417 */
3418 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3419 BUG_ON(sh->qd_idx >= 0 &&
3420 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags));
3421 for (i = disks; i--; ) {
3422 struct r5dev *dev = &sh->dev[i];
3423 if (test_bit(R5_LOCKED, &dev->flags) &&
3424 (i == sh->pd_idx || i == sh->qd_idx ||
3425 dev->written)) {
3426 pr_debug("Writing block %d\n", i);
3427 set_bit(R5_Wantwrite, &dev->flags);
3428 if (prexor)
3429 continue;
3430 if (!test_bit(R5_Insync, &dev->flags) ||
3431 ((i == sh->pd_idx || i == sh->qd_idx) &&
3432 s.failed == 0))
3433 set_bit(STRIPE_INSYNC, &sh->state);
3434 }
3435 }
3436 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3437 s.dec_preread_active = 1;
3438 }
3439
3440 /* Now to consider new write requests and what else, if anything
3441 * should be read. We do not handle new writes when:
3442 * 1/ A 'write' operation (copy+xor) is already in flight.
3443 * 2/ A 'check' operation is in flight, as it may clobber the parity
3444 * block.
3445 */
3446 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3447 handle_stripe_dirtying(conf, sh, &s, disks);
3448
3449 /* maybe we need to check and possibly fix the parity for this stripe
3450 * Any reads will already have been scheduled, so we just see if enough
3451 * data is available. The parity check is held off while parity
3452 * dependent operations are in flight.
3453 */
3454 if (sh->check_state ||
3455 (s.syncing && s.locked == 0 &&
3456 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3457 !test_bit(STRIPE_INSYNC, &sh->state))) {
3458 if (conf->level == 6)
3459 handle_parity_checks6(conf, sh, &s, disks);
3460 else
3461 handle_parity_checks5(conf, sh, &s, disks);
3462 }
NeilBrownc5a31002011-07-27 11:00:36 +10003463
NeilBrown9a3e1102011-12-23 10:17:53 +11003464 if (s.replacing && s.locked == 0
3465 && !test_bit(STRIPE_INSYNC, &sh->state)) {
3466 /* Write out to replacement devices where possible */
3467 for (i = 0; i < conf->raid_disks; i++)
3468 if (test_bit(R5_UPTODATE, &sh->dev[i].flags) &&
3469 test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3470 set_bit(R5_WantReplace, &sh->dev[i].flags);
3471 set_bit(R5_LOCKED, &sh->dev[i].flags);
3472 s.locked++;
3473 }
3474 set_bit(STRIPE_INSYNC, &sh->state);
3475 }
3476 if ((s.syncing || s.replacing) && s.locked == 0 &&
3477 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003478 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3479 clear_bit(STRIPE_SYNCING, &sh->state);
3480 }
3481
3482 /* If the failed drives are just a ReadError, then we might need
3483 * to progress the repair/check process
3484 */
3485 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3486 for (i = 0; i < s.failed; i++) {
3487 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3488 if (test_bit(R5_ReadError, &dev->flags)
3489 && !test_bit(R5_LOCKED, &dev->flags)
3490 && test_bit(R5_UPTODATE, &dev->flags)
3491 ) {
3492 if (!test_bit(R5_ReWrite, &dev->flags)) {
3493 set_bit(R5_Wantwrite, &dev->flags);
3494 set_bit(R5_ReWrite, &dev->flags);
3495 set_bit(R5_LOCKED, &dev->flags);
3496 s.locked++;
3497 } else {
3498 /* let's read it back */
3499 set_bit(R5_Wantread, &dev->flags);
3500 set_bit(R5_LOCKED, &dev->flags);
3501 s.locked++;
3502 }
3503 }
3504 }
3505
3506
NeilBrown3687c062011-07-27 11:00:36 +10003507 /* Finish reconstruct operations initiated by the expansion process */
3508 if (sh->reconstruct_state == reconstruct_state_result) {
3509 struct stripe_head *sh_src
3510 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3511 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3512 /* sh cannot be written until sh_src has been read.
3513 * so arrange for sh to be delayed a little
3514 */
3515 set_bit(STRIPE_DELAYED, &sh->state);
3516 set_bit(STRIPE_HANDLE, &sh->state);
3517 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3518 &sh_src->state))
3519 atomic_inc(&conf->preread_active_stripes);
3520 release_stripe(sh_src);
3521 goto finish;
3522 }
3523 if (sh_src)
3524 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003525
NeilBrown3687c062011-07-27 11:00:36 +10003526 sh->reconstruct_state = reconstruct_state_idle;
3527 clear_bit(STRIPE_EXPANDING, &sh->state);
3528 for (i = conf->raid_disks; i--; ) {
3529 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3530 set_bit(R5_LOCKED, &sh->dev[i].flags);
3531 s.locked++;
3532 }
3533 }
3534
3535 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3536 !sh->reconstruct_state) {
3537 /* Need to write out all blocks after computing parity */
3538 sh->disks = conf->raid_disks;
3539 stripe_set_idx(sh->sector, conf, 0, sh);
3540 schedule_reconstruction(sh, &s, 1, 1);
3541 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3542 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3543 atomic_dec(&conf->reshape_stripes);
3544 wake_up(&conf->wait_for_overlap);
3545 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3546 }
3547
3548 if (s.expanding && s.locked == 0 &&
3549 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3550 handle_stripe_expansion(conf, sh);
3551
3552finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003553 /* wait for this device to become unblocked */
NeilBrown43220aa2011-08-31 12:49:14 +10003554 if (conf->mddev->external && unlikely(s.blocked_rdev))
NeilBrownc5709ef2011-07-26 11:35:20 +10003555 md_wait_for_blocked_rdev(s.blocked_rdev, conf->mddev);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003556
NeilBrownbc2607f2011-07-28 11:39:22 +10003557 if (s.handle_bad_blocks)
3558 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003559 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003560 struct r5dev *dev = &sh->dev[i];
3561 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3562 /* We own a safe reference to the rdev */
3563 rdev = conf->disks[i].rdev;
3564 if (!rdev_set_badblocks(rdev, sh->sector,
3565 STRIPE_SECTORS, 0))
3566 md_error(conf->mddev, rdev);
3567 rdev_dec_pending(rdev, conf->mddev);
3568 }
NeilBrownb84db562011-07-28 11:39:23 +10003569 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3570 rdev = conf->disks[i].rdev;
3571 rdev_clear_badblocks(rdev, sh->sector,
3572 STRIPE_SECTORS);
3573 rdev_dec_pending(rdev, conf->mddev);
3574 }
NeilBrown977df362011-12-23 10:17:53 +11003575 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3576 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11003577 if (!rdev)
3578 /* rdev have been moved down */
3579 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11003580 rdev_clear_badblocks(rdev, sh->sector,
3581 STRIPE_SECTORS);
3582 rdev_dec_pending(rdev, conf->mddev);
3583 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003584 }
3585
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003586 if (s.ops_request)
3587 raid_run_ops(sh, s.ops_request);
3588
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003589 ops_run_io(sh, &s);
3590
NeilBrownc5709ef2011-07-26 11:35:20 +10003591 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003592 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003593 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003594 * have actually been submitted.
3595 */
3596 atomic_dec(&conf->preread_active_stripes);
3597 if (atomic_read(&conf->preread_active_stripes) <
3598 IO_THRESHOLD)
3599 md_wakeup_thread(conf->mddev->thread);
3600 }
3601
NeilBrownc5709ef2011-07-26 11:35:20 +10003602 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003603
Dan Williams257a4b42011-11-08 16:22:06 +11003604 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003605}
3606
NeilBrownd1688a62011-10-11 16:49:52 +11003607static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608{
3609 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3610 while (!list_empty(&conf->delayed_list)) {
3611 struct list_head *l = conf->delayed_list.next;
3612 struct stripe_head *sh;
3613 sh = list_entry(l, struct stripe_head, lru);
3614 list_del_init(l);
3615 clear_bit(STRIPE_DELAYED, &sh->state);
3616 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3617 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003618 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 }
NeilBrown482c0832011-04-18 18:25:42 +10003620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621}
3622
NeilBrownd1688a62011-10-11 16:49:52 +11003623static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003624{
3625 /* device_lock is held */
3626 struct list_head head;
3627 list_add(&head, &conf->bitmap_list);
3628 list_del_init(&conf->bitmap_list);
3629 while (!list_empty(&head)) {
3630 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3631 list_del_init(&sh->lru);
3632 atomic_inc(&sh->count);
3633 __release_stripe(conf, sh);
3634 }
3635}
3636
NeilBrownfd01b882011-10-11 16:47:53 +11003637int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003638{
NeilBrownd1688a62011-10-11 16:49:52 +11003639 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003640
3641 /* No difference between reads and writes. Just check
3642 * how busy the stripe_cache is
3643 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003644
NeilBrownf022b2f2006-10-03 01:15:56 -07003645 if (conf->inactive_blocked)
3646 return 1;
3647 if (conf->quiesce)
3648 return 1;
3649 if (list_empty_careful(&conf->inactive_list))
3650 return 1;
3651
3652 return 0;
3653}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003654EXPORT_SYMBOL_GPL(md_raid5_congested);
3655
3656static int raid5_congested(void *data, int bits)
3657{
NeilBrownfd01b882011-10-11 16:47:53 +11003658 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003659
3660 return mddev_congested(mddev, bits) ||
3661 md_raid5_congested(mddev, bits);
3662}
NeilBrownf022b2f2006-10-03 01:15:56 -07003663
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003664/* We want read requests to align with chunks where possible,
3665 * but write requests don't need to.
3666 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003667static int raid5_mergeable_bvec(struct request_queue *q,
3668 struct bvec_merge_data *bvm,
3669 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003670{
NeilBrownfd01b882011-10-11 16:47:53 +11003671 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003672 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003673 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003674 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003675 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003676
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003677 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003678 return biovec->bv_len; /* always allow writes to be mergeable */
3679
Andre Noll664e7c42009-06-18 08:45:27 +10003680 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3681 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003682 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3683 if (max < 0) max = 0;
3684 if (max <= biovec->bv_len && bio_sectors == 0)
3685 return biovec->bv_len;
3686 else
3687 return max;
3688}
3689
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003690
NeilBrownfd01b882011-10-11 16:47:53 +11003691static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003692{
3693 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003694 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003695 unsigned int bio_sectors = bio->bi_size >> 9;
3696
Andre Noll664e7c42009-06-18 08:45:27 +10003697 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3698 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003699 return chunk_sectors >=
3700 ((sector & (chunk_sectors - 1)) + bio_sectors);
3701}
3702
3703/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003704 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3705 * later sampled by raid5d.
3706 */
NeilBrownd1688a62011-10-11 16:49:52 +11003707static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003708{
3709 unsigned long flags;
3710
3711 spin_lock_irqsave(&conf->device_lock, flags);
3712
3713 bi->bi_next = conf->retry_read_aligned_list;
3714 conf->retry_read_aligned_list = bi;
3715
3716 spin_unlock_irqrestore(&conf->device_lock, flags);
3717 md_wakeup_thread(conf->mddev->thread);
3718}
3719
3720
NeilBrownd1688a62011-10-11 16:49:52 +11003721static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003722{
3723 struct bio *bi;
3724
3725 bi = conf->retry_read_aligned;
3726 if (bi) {
3727 conf->retry_read_aligned = NULL;
3728 return bi;
3729 }
3730 bi = conf->retry_read_aligned_list;
3731 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003732 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003733 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003734 /*
3735 * this sets the active strip count to 1 and the processed
3736 * strip count to zero (upper 8 bits)
3737 */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003738 bi->bi_phys_segments = 1; /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003739 }
3740
3741 return bi;
3742}
3743
3744
3745/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003746 * The "raid5_align_endio" should check if the read succeeded and if it
3747 * did, call bio_endio on the original bio (having bio_put the new bio
3748 * first).
3749 * If the read failed..
3750 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003751static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003752{
3753 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003754 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003755 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003756 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003757 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003758
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003759 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003760
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003761 rdev = (void*)raid_bi->bi_next;
3762 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003763 mddev = rdev->mddev;
3764 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003765
3766 rdev_dec_pending(rdev, conf->mddev);
3767
3768 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003769 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003770 if (atomic_dec_and_test(&conf->active_aligned_reads))
3771 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003772 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003773 }
3774
3775
Dan Williams45b42332007-07-09 11:56:43 -07003776 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003777
3778 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003779}
3780
Neil Brown387bb172007-02-08 14:20:29 -08003781static int bio_fits_rdev(struct bio *bi)
3782{
Jens Axboe165125e2007-07-24 09:28:11 +02003783 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003784
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003785 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003786 return 0;
3787 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003788 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003789 return 0;
3790
3791 if (q->merge_bvec_fn)
3792 /* it's too hard to apply the merge_bvec_fn at this stage,
3793 * just just give up
3794 */
3795 return 0;
3796
3797 return 1;
3798}
3799
3800
NeilBrownfd01b882011-10-11 16:47:53 +11003801static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003802{
NeilBrownd1688a62011-10-11 16:49:52 +11003803 struct r5conf *conf = mddev->private;
NeilBrown8553fe72009-12-14 12:49:47 +11003804 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003805 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11003806 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11003807 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003808
3809 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003810 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003811 return 0;
3812 }
3813 /*
NeilBrowna167f662010-10-26 18:31:13 +11003814 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003815 */
NeilBrowna167f662010-10-26 18:31:13 +11003816 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003817 if (!align_bi)
3818 return 0;
3819 /*
3820 * set bi_end_io to a new function, and set bi_private to the
3821 * original bio.
3822 */
3823 align_bi->bi_end_io = raid5_align_endio;
3824 align_bi->bi_private = raid_bio;
3825 /*
3826 * compute position
3827 */
NeilBrown112bf892009-03-31 14:39:38 +11003828 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3829 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003830 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003831
NeilBrown671488c2011-12-23 10:17:52 +11003832 end_sector = align_bi->bi_sector + (align_bi->bi_size >> 9);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003833 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11003834 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
3835 if (!rdev || test_bit(Faulty, &rdev->flags) ||
3836 rdev->recovery_offset < end_sector) {
3837 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3838 if (rdev &&
3839 (test_bit(Faulty, &rdev->flags) ||
3840 !(test_bit(In_sync, &rdev->flags) ||
3841 rdev->recovery_offset >= end_sector)))
3842 rdev = NULL;
3843 }
3844 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10003845 sector_t first_bad;
3846 int bad_sectors;
3847
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003848 atomic_inc(&rdev->nr_pending);
3849 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003850 raid_bio->bi_next = (void*)rdev;
3851 align_bi->bi_bdev = rdev->bdev;
3852 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
3853 align_bi->bi_sector += rdev->data_offset;
3854
NeilBrown31c176e2011-07-28 11:39:22 +10003855 if (!bio_fits_rdev(align_bi) ||
3856 is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
3857 &first_bad, &bad_sectors)) {
3858 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08003859 bio_put(align_bi);
3860 rdev_dec_pending(rdev, mddev);
3861 return 0;
3862 }
3863
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003864 spin_lock_irq(&conf->device_lock);
3865 wait_event_lock_irq(conf->wait_for_stripe,
3866 conf->quiesce == 0,
3867 conf->device_lock, /* nothing */);
3868 atomic_inc(&conf->active_aligned_reads);
3869 spin_unlock_irq(&conf->device_lock);
3870
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003871 generic_make_request(align_bi);
3872 return 1;
3873 } else {
3874 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003875 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003876 return 0;
3877 }
3878}
3879
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003880/* __get_priority_stripe - get the next stripe to process
3881 *
3882 * Full stripe writes are allowed to pass preread active stripes up until
3883 * the bypass_threshold is exceeded. In general the bypass_count
3884 * increments when the handle_list is handled before the hold_list; however, it
3885 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3886 * stripe with in flight i/o. The bypass_count will be reset when the
3887 * head of the hold_list has changed, i.e. the head was promoted to the
3888 * handle_list.
3889 */
NeilBrownd1688a62011-10-11 16:49:52 +11003890static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003891{
3892 struct stripe_head *sh;
3893
3894 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3895 __func__,
3896 list_empty(&conf->handle_list) ? "empty" : "busy",
3897 list_empty(&conf->hold_list) ? "empty" : "busy",
3898 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3899
3900 if (!list_empty(&conf->handle_list)) {
3901 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3902
3903 if (list_empty(&conf->hold_list))
3904 conf->bypass_count = 0;
3905 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3906 if (conf->hold_list.next == conf->last_hold)
3907 conf->bypass_count++;
3908 else {
3909 conf->last_hold = conf->hold_list.next;
3910 conf->bypass_count -= conf->bypass_threshold;
3911 if (conf->bypass_count < 0)
3912 conf->bypass_count = 0;
3913 }
3914 }
3915 } else if (!list_empty(&conf->hold_list) &&
3916 ((conf->bypass_threshold &&
3917 conf->bypass_count > conf->bypass_threshold) ||
3918 atomic_read(&conf->pending_full_writes) == 0)) {
3919 sh = list_entry(conf->hold_list.next,
3920 typeof(*sh), lru);
3921 conf->bypass_count -= conf->bypass_threshold;
3922 if (conf->bypass_count < 0)
3923 conf->bypass_count = 0;
3924 } else
3925 return NULL;
3926
3927 list_del_init(&sh->lru);
3928 atomic_inc(&sh->count);
3929 BUG_ON(atomic_read(&sh->count) != 1);
3930 return sh;
3931}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003932
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07003933static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934{
NeilBrownd1688a62011-10-11 16:49:52 +11003935 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11003936 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 sector_t new_sector;
3938 sector_t logical_sector, last_sector;
3939 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01003940 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11003941 int remaining;
NeilBrown7c13edc2011-04-18 18:25:43 +10003942 int plugged;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943
Tejun Heoe9c74692010-09-03 11:56:18 +02003944 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
3945 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02003946 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07003947 }
3948
NeilBrown3d310eb2005-06-21 17:17:26 -07003949 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07003950
NeilBrown802ba062006-12-13 00:34:13 -08003951 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003952 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11003953 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02003954 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003955
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
3957 last_sector = bi->bi_sector + (bi->bi_size>>9);
3958 bi->bi_next = NULL;
3959 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07003960
NeilBrown7c13edc2011-04-18 18:25:43 +10003961 plugged = mddev_check_plugged(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
3963 DEFINE_WAIT(w);
NeilBrown16a53ec2006-06-26 00:27:38 -07003964 int disks, data_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003965 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08003966
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003967 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11003968 previous = 0;
NeilBrownb0f9ec02009-03-31 15:27:18 +11003969 disks = conf->raid_disks;
NeilBrownb578d552006-03-27 01:18:12 -08003970 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11003971 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11003972 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08003973 * 64bit on a 32bit platform, and so it might be
3974 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02003975 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08003976 * the lock is dropped, so once we get a reference
3977 * to the stripe that we think it is, we will have
3978 * to check again.
3979 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003980 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11003981 if (mddev->delta_disks < 0
3982 ? logical_sector < conf->reshape_progress
3983 : logical_sector >= conf->reshape_progress) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003984 disks = conf->previous_raid_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003985 previous = 1;
3986 } else {
NeilBrownfef9c612009-03-31 15:16:46 +11003987 if (mddev->delta_disks < 0
3988 ? logical_sector < conf->reshape_safe
3989 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08003990 spin_unlock_irq(&conf->device_lock);
3991 schedule();
3992 goto retry;
3993 }
3994 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003995 spin_unlock_irq(&conf->device_lock);
3996 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003997 data_disks = disks - conf->max_degraded;
3998
NeilBrown112bf892009-03-31 14:39:38 +11003999 new_sector = raid5_compute_sector(conf, logical_sector,
4000 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11004001 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10004002 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 (unsigned long long)new_sector,
4004 (unsigned long long)logical_sector);
4005
NeilBrownb5663ba2009-03-31 14:39:38 +11004006 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10004007 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004009 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004010 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08004011 * stripe, so we must do the range check again.
4012 * Expansion could still move past after this
4013 * test, but as we are holding a reference to
4014 * 'sh', we know that if that happens,
4015 * STRIPE_EXPANDING will get set and the expansion
4016 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004017 */
4018 int must_retry = 0;
4019 spin_lock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004020 if (mddev->delta_disks < 0
4021 ? logical_sector >= conf->reshape_progress
4022 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004023 /* mismatch, need to try again */
4024 must_retry = 1;
4025 spin_unlock_irq(&conf->device_lock);
4026 if (must_retry) {
4027 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004028 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004029 goto retry;
4030 }
4031 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004032
Namhyung Kimffd96e32011-07-18 17:38:51 +10004033 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004034 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004035 logical_sector < mddev->suspend_hi) {
4036 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004037 /* As the suspend_* range is controlled by
4038 * userspace, we want an interruptible
4039 * wait.
4040 */
4041 flush_signals(current);
4042 prepare_to_wait(&conf->wait_for_overlap,
4043 &w, TASK_INTERRUPTIBLE);
4044 if (logical_sector >= mddev->suspend_lo &&
4045 logical_sector < mddev->suspend_hi)
4046 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004047 goto retry;
4048 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004049
4050 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004051 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004052 /* Stripe is busy expanding or
4053 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 * and wait a while
4055 */
NeilBrown482c0832011-04-18 18:25:42 +10004056 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 release_stripe(sh);
4058 schedule();
4059 goto retry;
4060 }
4061 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004062 set_bit(STRIPE_HANDLE, &sh->state);
4063 clear_bit(STRIPE_DELAYED, &sh->state);
Tejun Heoe9c74692010-09-03 11:56:18 +02004064 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004065 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4066 atomic_inc(&conf->preread_active_stripes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 } else {
4069 /* cannot get stripe for read-ahead, just give-up */
4070 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4071 finish_wait(&conf->wait_for_overlap, &w);
4072 break;
4073 }
4074
4075 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004076 if (!plugged)
4077 md_wakeup_thread(mddev->thread);
4078
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004080 remaining = raid5_dec_bi_phys_segments(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004081 spin_unlock_irq(&conf->device_lock);
4082 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083
NeilBrown16a53ec2006-06-26 00:27:38 -07004084 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004086
Neil Brown0e13fe232008-06-28 08:31:20 +10004087 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089}
4090
NeilBrownfd01b882011-10-11 16:47:53 +11004091static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004092
NeilBrownfd01b882011-10-11 16:47:53 +11004093static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094{
NeilBrown52c03292006-06-26 00:27:43 -07004095 /* reshaping is quite different to recovery/resync so it is
4096 * handled quite separately ... here.
4097 *
4098 * On each call to sync_request, we gather one chunk worth of
4099 * destination stripes and flag them as expanding.
4100 * Then we find all the source stripes and request reads.
4101 * As the reads complete, handle_stripe will copy the data
4102 * into the destination stripe and release that stripe.
4103 */
NeilBrownd1688a62011-10-11 16:49:52 +11004104 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004106 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004107 int raid_disks = conf->previous_raid_disks;
4108 int data_disks = raid_disks - conf->max_degraded;
4109 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004110 int i;
4111 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004112 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004113 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004114 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004115 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004116
NeilBrownfef9c612009-03-31 15:16:46 +11004117 if (sector_nr == 0) {
4118 /* If restarting in the middle, skip the initial sectors */
4119 if (mddev->delta_disks < 0 &&
4120 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4121 sector_nr = raid5_size(mddev, 0, 0)
4122 - conf->reshape_progress;
NeilBrowna6397552009-08-13 10:13:00 +10004123 } else if (mddev->delta_disks >= 0 &&
NeilBrownfef9c612009-03-31 15:16:46 +11004124 conf->reshape_progress > 0)
4125 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004126 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004127 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004128 mddev->curr_resync_completed = sector_nr;
4129 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004130 *skipped = 1;
4131 return sector_nr;
4132 }
NeilBrown52c03292006-06-26 00:27:43 -07004133 }
4134
NeilBrown7a661382009-03-31 15:21:40 +11004135 /* We need to process a full chunk at a time.
4136 * If old and new chunk sizes differ, we need to process the
4137 * largest of these
4138 */
Andre Noll664e7c42009-06-18 08:45:27 +10004139 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4140 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004141 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004142 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004143
NeilBrown52c03292006-06-26 00:27:43 -07004144 /* we update the metadata when there is more than 3Meg
4145 * in the block range (that is rather arbitrary, should
4146 * probably be time based) or when the data about to be
4147 * copied would over-write the source of the data at
4148 * the front of the range.
NeilBrownfef9c612009-03-31 15:16:46 +11004149 * i.e. one new_stripe along from reshape_progress new_maps
4150 * to after where reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004151 */
NeilBrownfef9c612009-03-31 15:16:46 +11004152 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004153 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004154 readpos = conf->reshape_progress;
4155 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004156 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004157 sector_div(safepos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004158 if (mddev->delta_disks < 0) {
NeilBrowned37d832009-05-27 21:39:05 +10004159 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004160 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004161 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004162 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004163 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004164 readpos -= min_t(sector_t, reshape_sectors, readpos);
4165 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004166 }
NeilBrown52c03292006-06-26 00:27:43 -07004167
NeilBrownc8f517c2009-03-31 15:28:40 +11004168 /* 'writepos' is the most advanced device address we might write.
4169 * 'readpos' is the least advanced device address we might read.
4170 * 'safepos' is the least address recorded in the metadata as having
4171 * been reshaped.
4172 * If 'readpos' is behind 'writepos', then there is no way that we can
4173 * ensure safety in the face of a crash - that must be done by userspace
4174 * making a backup of the data. So in that case there is no particular
4175 * rush to update metadata.
4176 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4177 * update the metadata to advance 'safepos' to match 'readpos' so that
4178 * we can be safe in the event of a crash.
4179 * So we insist on updating metadata if safepos is behind writepos and
4180 * readpos is beyond writepos.
4181 * In any case, update the metadata every 10 seconds.
4182 * Maybe that number should be configurable, but I'm not sure it is
4183 * worth it.... maybe it could be a multiple of safemode_delay???
4184 */
NeilBrownfef9c612009-03-31 15:16:46 +11004185 if ((mddev->delta_disks < 0
NeilBrownc8f517c2009-03-31 15:28:40 +11004186 ? (safepos > writepos && readpos < writepos)
4187 : (safepos < writepos && readpos > writepos)) ||
4188 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004189 /* Cannot proceed until we've updated the superblock... */
4190 wait_event(conf->wait_for_overlap,
4191 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004192 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004193 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004194 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07004195 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004196 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07004197 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004198 kthread_should_stop());
4199 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004200 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004201 spin_unlock_irq(&conf->device_lock);
4202 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004203 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004204 }
4205
NeilBrownec32a2b2009-03-31 15:17:38 +11004206 if (mddev->delta_disks < 0) {
4207 BUG_ON(conf->reshape_progress == 0);
4208 stripe_addr = writepos;
4209 BUG_ON((mddev->dev_sectors &
NeilBrown7a661382009-03-31 15:21:40 +11004210 ~((sector_t)reshape_sectors - 1))
4211 - reshape_sectors - stripe_addr
NeilBrownec32a2b2009-03-31 15:17:38 +11004212 != sector_nr);
4213 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004214 BUG_ON(writepos != sector_nr + reshape_sectors);
NeilBrownec32a2b2009-03-31 15:17:38 +11004215 stripe_addr = sector_nr;
4216 }
NeilBrownab69ae12009-03-31 15:26:47 +11004217 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004218 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004219 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004220 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004221 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004222 set_bit(STRIPE_EXPANDING, &sh->state);
4223 atomic_inc(&conf->reshape_stripes);
4224 /* If any of this stripe is beyond the end of the old
4225 * array, then we need to zero those blocks
4226 */
4227 for (j=sh->disks; j--;) {
4228 sector_t s;
4229 if (j == sh->pd_idx)
4230 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004231 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004232 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004233 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004234 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004235 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004236 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004237 continue;
4238 }
4239 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4240 set_bit(R5_Expanded, &sh->dev[j].flags);
4241 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4242 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004243 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004244 set_bit(STRIPE_EXPAND_READY, &sh->state);
4245 set_bit(STRIPE_HANDLE, &sh->state);
4246 }
NeilBrownab69ae12009-03-31 15:26:47 +11004247 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004248 }
4249 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004250 if (mddev->delta_disks < 0)
NeilBrown7a661382009-03-31 15:21:40 +11004251 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004252 else
NeilBrown7a661382009-03-31 15:21:40 +11004253 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004254 spin_unlock_irq(&conf->device_lock);
4255 /* Ok, those stripe are ready. We can start scheduling
4256 * reads on the source stripes.
4257 * The source stripes are determined by mapping the first and last
4258 * block on the destination stripes.
4259 */
NeilBrown52c03292006-06-26 00:27:43 -07004260 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004261 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004262 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004263 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004264 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004265 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004266 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004267 if (last_sector >= mddev->dev_sectors)
4268 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004269 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004270 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004271 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4272 set_bit(STRIPE_HANDLE, &sh->state);
4273 release_stripe(sh);
4274 first_sector += STRIPE_SECTORS;
4275 }
NeilBrownab69ae12009-03-31 15:26:47 +11004276 /* Now that the sources are clearly marked, we can release
4277 * the destination stripes
4278 */
4279 while (!list_empty(&stripes)) {
4280 sh = list_entry(stripes.next, struct stripe_head, lru);
4281 list_del_init(&sh->lru);
4282 release_stripe(sh);
4283 }
NeilBrownc6207272008-02-06 01:39:52 -08004284 /* If this takes us to the resync_max point where we have to pause,
4285 * then we need to write out the superblock.
4286 */
NeilBrown7a661382009-03-31 15:21:40 +11004287 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004288 if ((sector_nr - mddev->curr_resync_completed) * 2
4289 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004290 /* Cannot proceed until we've updated the superblock... */
4291 wait_event(conf->wait_for_overlap,
4292 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004293 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004294 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004295 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004296 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4297 md_wakeup_thread(mddev->thread);
4298 wait_event(mddev->sb_wait,
4299 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4300 || kthread_should_stop());
4301 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004302 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004303 spin_unlock_irq(&conf->device_lock);
4304 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004305 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004306 }
NeilBrown7a661382009-03-31 15:21:40 +11004307 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004308}
4309
4310/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004311static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004312{
NeilBrownd1688a62011-10-11 16:49:52 +11004313 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004314 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004315 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004316 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004317 int still_degraded = 0;
4318 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319
NeilBrown72626682005-09-09 16:23:54 -07004320 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004322
NeilBrown29269552006-03-27 01:18:10 -08004323 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4324 end_reshape(conf);
4325 return 0;
4326 }
NeilBrown72626682005-09-09 16:23:54 -07004327
4328 if (mddev->curr_resync < max_sector) /* aborted */
4329 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4330 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004331 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004332 conf->fullsync = 0;
4333 bitmap_close_sync(mddev->bitmap);
4334
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 return 0;
4336 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004337
NeilBrown64bd6602009-08-03 10:59:58 +10004338 /* Allow raid5_quiesce to complete */
4339 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4340
NeilBrown52c03292006-06-26 00:27:43 -07004341 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4342 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004343
NeilBrownc6207272008-02-06 01:39:52 -08004344 /* No need to check resync_max as we never do more than one
4345 * stripe, and as resync_max will always be on a chunk boundary,
4346 * if the check in md_do_sync didn't fire, there is no chance
4347 * of overstepping resync_max here
4348 */
4349
NeilBrown16a53ec2006-06-26 00:27:38 -07004350 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 * to resync, then assert that we are finished, because there is
4352 * nothing we can do.
4353 */
NeilBrown3285edf2006-06-26 00:27:55 -07004354 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004355 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004356 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004357 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 return rv;
4359 }
NeilBrown72626682005-09-09 16:23:54 -07004360 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004361 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004362 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4363 /* we can skip this block, and probably more */
4364 sync_blocks /= STRIPE_SECTORS;
4365 *skipped = 1;
4366 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368
NeilBrownb47490c2008-02-06 01:39:50 -08004369 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4370
NeilBrowna8c906c2009-06-09 14:39:59 +10004371 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004373 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004375 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004377 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004379 /* Need to check if array will still be degraded after recovery/resync
4380 * We don't need to check the 'failed' flag as when that gets set,
4381 * recovery aborts.
4382 */
NeilBrownf001a702009-06-09 14:30:31 +10004383 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004384 if (conf->disks[i].rdev == NULL)
4385 still_degraded = 1;
4386
4387 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4388
NeilBrown83206d62011-07-26 11:19:49 +10004389 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390
NeilBrown14425772009-10-16 15:55:25 +11004391 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 release_stripe(sh);
4393
4394 return STRIPE_SECTORS;
4395}
4396
NeilBrownd1688a62011-10-11 16:49:52 +11004397static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004398{
4399 /* We may not be able to submit a whole bio at once as there
4400 * may not be enough stripe_heads available.
4401 * We cannot pre-allocate enough stripe_heads as we may need
4402 * more than exist in the cache (if we allow ever large chunks).
4403 * So we do one stripe head at a time and record in
4404 * ->bi_hw_segments how many have been done.
4405 *
4406 * We *know* that this entire raid_bio is in one chunk, so
4407 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4408 */
4409 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004410 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004411 sector_t sector, logical_sector, last_sector;
4412 int scnt = 0;
4413 int remaining;
4414 int handled = 0;
4415
4416 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004417 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004418 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004419 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4420
4421 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004422 logical_sector += STRIPE_SECTORS,
4423 sector += STRIPE_SECTORS,
4424 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004425
Jens Axboe960e7392008-08-15 10:41:18 +02004426 if (scnt < raid5_bi_hw_segments(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004427 /* already done this stripe */
4428 continue;
4429
NeilBrowna8c906c2009-06-09 14:39:59 +10004430 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004431
4432 if (!sh) {
4433 /* failed to get a stripe - must wait */
Jens Axboe960e7392008-08-15 10:41:18 +02004434 raid5_set_bi_hw_segments(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004435 conf->retry_read_aligned = raid_bio;
4436 return handled;
4437 }
4438
Neil Brown387bb172007-02-08 14:20:29 -08004439 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4440 release_stripe(sh);
Jens Axboe960e7392008-08-15 10:41:18 +02004441 raid5_set_bi_hw_segments(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004442 conf->retry_read_aligned = raid_bio;
4443 return handled;
4444 }
4445
Dan Williams36d1c642009-07-14 11:48:22 -07004446 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004447 release_stripe(sh);
4448 handled++;
4449 }
4450 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004451 remaining = raid5_dec_bi_phys_segments(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004452 spin_unlock_irq(&conf->device_lock);
Neil Brown0e13fe232008-06-28 08:31:20 +10004453 if (remaining == 0)
4454 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004455 if (atomic_dec_and_test(&conf->active_aligned_reads))
4456 wake_up(&conf->wait_for_stripe);
4457 return handled;
4458}
4459
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004460
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461/*
4462 * This is our raid5 kernel thread.
4463 *
4464 * We scan the hash table for stripes which can be handled now.
4465 * During the scan, completed stripes are saved for us by the interrupt
4466 * handler, so that they will not have to wait for our next wakeup.
4467 */
NeilBrownfd01b882011-10-11 16:47:53 +11004468static void raid5d(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469{
4470 struct stripe_head *sh;
NeilBrownd1688a62011-10-11 16:49:52 +11004471 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004473 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474
Dan Williams45b42332007-07-09 11:56:43 -07004475 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476
4477 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004479 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 handled = 0;
4481 spin_lock_irq(&conf->device_lock);
4482 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004483 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484
NeilBrown7c13edc2011-04-18 18:25:43 +10004485 if (atomic_read(&mddev->plug_cnt) == 0 &&
4486 !list_empty(&conf->bitmap_list)) {
4487 /* Now is a good time to flush some bitmap updates */
4488 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004489 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004490 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004491 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004492 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004493 activate_bit_delay(conf);
4494 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004495 if (atomic_read(&mddev->plug_cnt) == 0)
4496 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004497
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004498 while ((bio = remove_bio_from_retry(conf))) {
4499 int ok;
4500 spin_unlock_irq(&conf->device_lock);
4501 ok = retry_aligned_read(conf, bio);
4502 spin_lock_irq(&conf->device_lock);
4503 if (!ok)
4504 break;
4505 handled++;
4506 }
4507
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004508 sh = __get_priority_stripe(conf);
4509
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004510 if (!sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 spin_unlock_irq(&conf->device_lock);
4513
4514 handled++;
Dan Williams417b8d42009-10-16 16:25:22 +11004515 handle_stripe(sh);
4516 release_stripe(sh);
4517 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518
NeilBrownde393cd2011-07-28 11:31:48 +10004519 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
4520 md_check_recovery(mddev);
4521
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 spin_lock_irq(&conf->device_lock);
4523 }
Dan Williams45b42332007-07-09 11:56:43 -07004524 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525
4526 spin_unlock_irq(&conf->device_lock);
4527
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004528 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004529 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530
Dan Williams45b42332007-07-09 11:56:43 -07004531 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532}
4533
NeilBrown3f294f42005-11-08 21:39:25 -08004534static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004535raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004536{
NeilBrownd1688a62011-10-11 16:49:52 +11004537 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004538 if (conf)
4539 return sprintf(page, "%d\n", conf->max_nr_stripes);
4540 else
4541 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004542}
4543
NeilBrownc41d4ac2010-06-01 19:37:24 +10004544int
NeilBrownfd01b882011-10-11 16:47:53 +11004545raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004546{
NeilBrownd1688a62011-10-11 16:49:52 +11004547 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004548 int err;
4549
4550 if (size <= 16 || size > 32768)
4551 return -EINVAL;
4552 while (size < conf->max_nr_stripes) {
4553 if (drop_one_stripe(conf))
4554 conf->max_nr_stripes--;
4555 else
4556 break;
4557 }
4558 err = md_allow_write(mddev);
4559 if (err)
4560 return err;
4561 while (size > conf->max_nr_stripes) {
4562 if (grow_one_stripe(conf))
4563 conf->max_nr_stripes++;
4564 else break;
4565 }
4566 return 0;
4567}
4568EXPORT_SYMBOL(raid5_set_cache_size);
4569
NeilBrown3f294f42005-11-08 21:39:25 -08004570static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004571raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004572{
NeilBrownd1688a62011-10-11 16:49:52 +11004573 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004574 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004575 int err;
4576
NeilBrown3f294f42005-11-08 21:39:25 -08004577 if (len >= PAGE_SIZE)
4578 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004579 if (!conf)
4580 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004581
Dan Williams4ef197d82008-04-28 02:15:54 -07004582 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004583 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004584 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004585 if (err)
4586 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004587 return len;
4588}
NeilBrown007583c2005-11-08 21:39:30 -08004589
NeilBrown96de1e62005-11-08 21:39:39 -08004590static struct md_sysfs_entry
4591raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4592 raid5_show_stripe_cache_size,
4593 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004594
4595static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004596raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004597{
NeilBrownd1688a62011-10-11 16:49:52 +11004598 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004599 if (conf)
4600 return sprintf(page, "%d\n", conf->bypass_threshold);
4601 else
4602 return 0;
4603}
4604
4605static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004606raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004607{
NeilBrownd1688a62011-10-11 16:49:52 +11004608 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004609 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004610 if (len >= PAGE_SIZE)
4611 return -EINVAL;
4612 if (!conf)
4613 return -ENODEV;
4614
Dan Williams4ef197d82008-04-28 02:15:54 -07004615 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004616 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004617 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004618 return -EINVAL;
4619 conf->bypass_threshold = new;
4620 return len;
4621}
4622
4623static struct md_sysfs_entry
4624raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4625 S_IRUGO | S_IWUSR,
4626 raid5_show_preread_threshold,
4627 raid5_store_preread_threshold);
4628
4629static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004630stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004631{
NeilBrownd1688a62011-10-11 16:49:52 +11004632 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004633 if (conf)
4634 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4635 else
4636 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004637}
4638
NeilBrown96de1e62005-11-08 21:39:39 -08004639static struct md_sysfs_entry
4640raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004641
NeilBrown007583c2005-11-08 21:39:30 -08004642static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004643 &raid5_stripecache_size.attr,
4644 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004645 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004646 NULL,
4647};
NeilBrown007583c2005-11-08 21:39:30 -08004648static struct attribute_group raid5_attrs_group = {
4649 .name = NULL,
4650 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004651};
4652
Dan Williams80c3a6c2009-03-17 18:10:40 -07004653static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11004654raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07004655{
NeilBrownd1688a62011-10-11 16:49:52 +11004656 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004657
4658 if (!sectors)
4659 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004660 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11004661 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11004662 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004663
Andre Noll9d8f0362009-06-18 08:45:01 +10004664 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004665 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004666 return sectors * (raid_disks - conf->max_degraded);
4667}
4668
NeilBrownd1688a62011-10-11 16:49:52 +11004669static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004670{
4671 struct raid5_percpu *percpu;
4672 unsigned long cpu;
4673
4674 if (!conf->percpu)
4675 return;
4676
4677 get_online_cpus();
4678 for_each_possible_cpu(cpu) {
4679 percpu = per_cpu_ptr(conf->percpu, cpu);
4680 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004681 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004682 }
4683#ifdef CONFIG_HOTPLUG_CPU
4684 unregister_cpu_notifier(&conf->cpu_notify);
4685#endif
4686 put_online_cpus();
4687
4688 free_percpu(conf->percpu);
4689}
4690
NeilBrownd1688a62011-10-11 16:49:52 +11004691static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10004692{
4693 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004694 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10004695 kfree(conf->disks);
4696 kfree(conf->stripe_hashtbl);
4697 kfree(conf);
4698}
4699
Dan Williams36d1c642009-07-14 11:48:22 -07004700#ifdef CONFIG_HOTPLUG_CPU
4701static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4702 void *hcpu)
4703{
NeilBrownd1688a62011-10-11 16:49:52 +11004704 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07004705 long cpu = (long)hcpu;
4706 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4707
4708 switch (action) {
4709 case CPU_UP_PREPARE:
4710 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07004711 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07004712 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004713 if (!percpu->scribble)
4714 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4715
4716 if (!percpu->scribble ||
4717 (conf->level == 6 && !percpu->spare_page)) {
4718 safe_put_page(percpu->spare_page);
4719 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004720 pr_err("%s: failed memory allocation for cpu%ld\n",
4721 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07004722 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07004723 }
4724 break;
4725 case CPU_DEAD:
4726 case CPU_DEAD_FROZEN:
4727 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004728 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004729 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004730 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07004731 break;
4732 default:
4733 break;
4734 }
4735 return NOTIFY_OK;
4736}
4737#endif
4738
NeilBrownd1688a62011-10-11 16:49:52 +11004739static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004740{
4741 unsigned long cpu;
4742 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09004743 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004744 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004745 int err;
4746
Dan Williams36d1c642009-07-14 11:48:22 -07004747 allcpus = alloc_percpu(struct raid5_percpu);
4748 if (!allcpus)
4749 return -ENOMEM;
4750 conf->percpu = allcpus;
4751
4752 get_online_cpus();
4753 err = 0;
4754 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07004755 if (conf->level == 6) {
4756 spare_page = alloc_page(GFP_KERNEL);
4757 if (!spare_page) {
4758 err = -ENOMEM;
4759 break;
4760 }
4761 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
4762 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11004763 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004764 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07004765 err = -ENOMEM;
4766 break;
4767 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07004768 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004769 }
4770#ifdef CONFIG_HOTPLUG_CPU
4771 conf->cpu_notify.notifier_call = raid456_cpu_notify;
4772 conf->cpu_notify.priority = 0;
4773 if (err == 0)
4774 err = register_cpu_notifier(&conf->cpu_notify);
4775#endif
4776 put_online_cpus();
4777
4778 return err;
4779}
4780
NeilBrownd1688a62011-10-11 16:49:52 +11004781static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782{
NeilBrownd1688a62011-10-11 16:49:52 +11004783 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004784 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11004785 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 struct disk_info *disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787
NeilBrown91adb562009-03-31 14:39:39 +11004788 if (mddev->new_level != 5
4789 && mddev->new_level != 4
4790 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10004791 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004792 mdname(mddev), mddev->new_level);
4793 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 }
NeilBrown91adb562009-03-31 14:39:39 +11004795 if ((mddev->new_level == 5
4796 && !algorithm_valid_raid5(mddev->new_layout)) ||
4797 (mddev->new_level == 6
4798 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004799 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11004800 mdname(mddev), mddev->new_layout);
4801 return ERR_PTR(-EIO);
4802 }
4803 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10004804 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004805 mdname(mddev), mddev->raid_disks);
4806 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11004807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808
Andre Noll664e7c42009-06-18 08:45:27 +10004809 if (!mddev->new_chunk_sectors ||
4810 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
4811 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10004812 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
4813 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11004814 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11004815 }
4816
NeilBrownd1688a62011-10-11 16:49:52 +11004817 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11004818 if (conf == NULL)
4819 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11004820 spin_lock_init(&conf->device_lock);
4821 init_waitqueue_head(&conf->wait_for_stripe);
4822 init_waitqueue_head(&conf->wait_for_overlap);
4823 INIT_LIST_HEAD(&conf->handle_list);
4824 INIT_LIST_HEAD(&conf->hold_list);
4825 INIT_LIST_HEAD(&conf->delayed_list);
4826 INIT_LIST_HEAD(&conf->bitmap_list);
4827 INIT_LIST_HEAD(&conf->inactive_list);
4828 atomic_set(&conf->active_stripes, 0);
4829 atomic_set(&conf->preread_active_stripes, 0);
4830 atomic_set(&conf->active_aligned_reads, 0);
4831 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11004832 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11004833
4834 conf->raid_disks = mddev->raid_disks;
4835 if (mddev->reshape_position == MaxSector)
4836 conf->previous_raid_disks = mddev->raid_disks;
4837 else
4838 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004839 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
4840 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11004841
NeilBrown5e5e3e72009-10-16 16:35:30 +11004842 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11004843 GFP_KERNEL);
4844 if (!conf->disks)
4845 goto abort;
4846
4847 conf->mddev = mddev;
4848
4849 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
4850 goto abort;
4851
Dan Williams36d1c642009-07-14 11:48:22 -07004852 conf->level = mddev->new_level;
4853 if (raid5_alloc_percpu(conf) != 0)
4854 goto abort;
4855
NeilBrown0c55e022010-05-03 14:09:02 +10004856 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11004857
NeilBrowndafb20f2012-03-19 12:46:39 +11004858 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11004859 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004860 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11004861 || raid_disk < 0)
4862 continue;
4863 disk = conf->disks + raid_disk;
4864
NeilBrown17045f52011-12-23 10:17:53 +11004865 if (test_bit(Replacement, &rdev->flags)) {
4866 if (disk->replacement)
4867 goto abort;
4868 disk->replacement = rdev;
4869 } else {
4870 if (disk->rdev)
4871 goto abort;
4872 disk->rdev = rdev;
4873 }
NeilBrown91adb562009-03-31 14:39:39 +11004874
4875 if (test_bit(In_sync, &rdev->flags)) {
4876 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10004877 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
4878 " disk %d\n",
4879 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05004880 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11004881 /* Cannot rely on bitmap to complete recovery */
4882 conf->fullsync = 1;
4883 }
4884
Andre Noll09c9e5f2009-06-18 08:45:55 +10004885 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11004886 conf->level = mddev->new_level;
4887 if (conf->level == 6)
4888 conf->max_degraded = 2;
4889 else
4890 conf->max_degraded = 1;
4891 conf->algorithm = mddev->new_layout;
4892 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11004893 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11004894 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10004895 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11004896 conf->prev_algo = mddev->layout;
4897 }
NeilBrown91adb562009-03-31 14:39:39 +11004898
4899 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11004900 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11004901 if (grow_stripes(conf, conf->max_nr_stripes)) {
4902 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004903 "md/raid:%s: couldn't allocate %dkB for buffers\n",
4904 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004905 goto abort;
4906 } else
NeilBrown0c55e022010-05-03 14:09:02 +10004907 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
4908 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004909
NeilBrown0da3c612009-09-23 18:09:45 +10004910 conf->thread = md_register_thread(raid5d, mddev, NULL);
NeilBrown91adb562009-03-31 14:39:39 +11004911 if (!conf->thread) {
4912 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004913 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11004914 mdname(mddev));
4915 goto abort;
4916 }
4917
4918 return conf;
4919
4920 abort:
4921 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10004922 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11004923 return ERR_PTR(-EIO);
4924 } else
4925 return ERR_PTR(-ENOMEM);
4926}
4927
NeilBrownc148ffd2009-11-13 17:47:00 +11004928
4929static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
4930{
4931 switch (algo) {
4932 case ALGORITHM_PARITY_0:
4933 if (raid_disk < max_degraded)
4934 return 1;
4935 break;
4936 case ALGORITHM_PARITY_N:
4937 if (raid_disk >= raid_disks - max_degraded)
4938 return 1;
4939 break;
4940 case ALGORITHM_PARITY_0_6:
4941 if (raid_disk == 0 ||
4942 raid_disk == raid_disks - 1)
4943 return 1;
4944 break;
4945 case ALGORITHM_LEFT_ASYMMETRIC_6:
4946 case ALGORITHM_RIGHT_ASYMMETRIC_6:
4947 case ALGORITHM_LEFT_SYMMETRIC_6:
4948 case ALGORITHM_RIGHT_SYMMETRIC_6:
4949 if (raid_disk == raid_disks - 1)
4950 return 1;
4951 }
4952 return 0;
4953}
4954
NeilBrownfd01b882011-10-11 16:47:53 +11004955static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11004956{
NeilBrownd1688a62011-10-11 16:49:52 +11004957 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10004958 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11004959 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11004960 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11004961 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11004962 int i;
NeilBrown91adb562009-03-31 14:39:39 +11004963
Andre Noll8c6ac862009-06-18 08:48:06 +10004964 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10004965 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10004966 " -- starting background reconstruction\n",
4967 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004968 if (mddev->reshape_position != MaxSector) {
4969 /* Check that we can continue the reshape.
4970 * Currently only disks can change, it must
4971 * increase, and we must be past the point where
4972 * a stripe over-writes itself
4973 */
4974 sector_t here_new, here_old;
4975 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11004976 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08004977
NeilBrown88ce4932009-03-31 15:24:23 +11004978 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10004979 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08004980 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08004981 mdname(mddev));
4982 return -EINVAL;
4983 }
NeilBrownf6705572006-03-27 01:18:11 -08004984 old_disks = mddev->raid_disks - mddev->delta_disks;
4985 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08004986 * further up in new geometry must map after here in old
4987 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08004988 */
4989 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10004990 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08004991 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004992 printk(KERN_ERR "md/raid:%s: reshape_position not "
4993 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004994 return -EINVAL;
4995 }
NeilBrownc148ffd2009-11-13 17:47:00 +11004996 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08004997 /* here_new is the stripe we will write to */
4998 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10004999 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005000 (old_disks-max_degraded));
5001 /* here_old is the first stripe that we might need to read
5002 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10005003 if (mddev->delta_disks == 0) {
5004 /* We cannot be sure it is safe to start an in-place
5005 * reshape. It is only safe if user-space if monitoring
5006 * and taking constant backups.
5007 * mdadm always starts a situation like this in
5008 * readonly mode so it can take control before
5009 * allowing any writes. So just check for that.
5010 */
5011 if ((here_new * mddev->new_chunk_sectors !=
5012 here_old * mddev->chunk_sectors) ||
5013 mddev->ro == 0) {
NeilBrown0c55e022010-05-03 14:09:02 +10005014 printk(KERN_ERR "md/raid:%s: in-place reshape must be started"
5015 " in read-only mode - aborting\n",
5016 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005017 return -EINVAL;
5018 }
5019 } else if (mddev->delta_disks < 0
5020 ? (here_new * mddev->new_chunk_sectors <=
5021 here_old * mddev->chunk_sectors)
5022 : (here_new * mddev->new_chunk_sectors >=
5023 here_old * mddev->chunk_sectors)) {
NeilBrownf6705572006-03-27 01:18:11 -08005024 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005025 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5026 "auto-recovery - aborting.\n",
5027 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005028 return -EINVAL;
5029 }
NeilBrown0c55e022010-05-03 14:09:02 +10005030 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5031 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005032 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005033 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005034 BUG_ON(mddev->level != mddev->new_level);
5035 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005036 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005037 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005038 }
5039
NeilBrown245f46c2009-03-31 14:39:39 +11005040 if (mddev->private == NULL)
5041 conf = setup_conf(mddev);
5042 else
5043 conf = mddev->private;
5044
NeilBrown91adb562009-03-31 14:39:39 +11005045 if (IS_ERR(conf))
5046 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005047
NeilBrown91adb562009-03-31 14:39:39 +11005048 mddev->thread = conf->thread;
5049 conf->thread = NULL;
5050 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051
NeilBrown17045f52011-12-23 10:17:53 +11005052 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5053 i++) {
5054 rdev = conf->disks[i].rdev;
5055 if (!rdev && conf->disks[i].replacement) {
5056 /* The replacement is all we have yet */
5057 rdev = conf->disks[i].replacement;
5058 conf->disks[i].replacement = NULL;
5059 clear_bit(Replacement, &rdev->flags);
5060 conf->disks[i].rdev = rdev;
5061 }
5062 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11005063 continue;
NeilBrown17045f52011-12-23 10:17:53 +11005064 if (conf->disks[i].replacement &&
5065 conf->reshape_progress != MaxSector) {
5066 /* replacements and reshape simply do not mix. */
5067 printk(KERN_ERR "md: cannot handle concurrent "
5068 "replacement and reshape.\n");
5069 goto abort;
5070 }
NeilBrown2f115882010-06-17 17:41:03 +10005071 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005072 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005073 continue;
5074 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005075 /* This disc is not fully in-sync. However if it
5076 * just stored parity (beyond the recovery_offset),
5077 * when we don't need to be concerned about the
5078 * array being dirty.
5079 * When reshape goes 'backwards', we never have
5080 * partially completed devices, so we only need
5081 * to worry about reshape going forwards.
5082 */
5083 /* Hack because v0.91 doesn't store recovery_offset properly. */
5084 if (mddev->major_version == 0 &&
5085 mddev->minor_version > 90)
5086 rdev->recovery_offset = reshape_offset;
5087
NeilBrownc148ffd2009-11-13 17:47:00 +11005088 if (rdev->recovery_offset < reshape_offset) {
5089 /* We need to check old and new layout */
5090 if (!only_parity(rdev->raid_disk,
5091 conf->algorithm,
5092 conf->raid_disks,
5093 conf->max_degraded))
5094 continue;
5095 }
5096 if (!only_parity(rdev->raid_disk,
5097 conf->prev_algo,
5098 conf->previous_raid_disks,
5099 conf->max_degraded))
5100 continue;
5101 dirty_parity_disks++;
5102 }
NeilBrown91adb562009-03-31 14:39:39 +11005103
NeilBrown17045f52011-12-23 10:17:53 +11005104 /*
5105 * 0 for a fully functional array, 1 or 2 for a degraded array.
5106 */
NeilBrown908f4fb2011-12-23 10:17:50 +11005107 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108
NeilBrown674806d2010-06-16 17:17:53 +10005109 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005110 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07005112 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 goto abort;
5114 }
5115
NeilBrown91adb562009-03-31 14:39:39 +11005116 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10005117 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11005118 mddev->resync_max_sectors = mddev->dev_sectors;
5119
NeilBrownc148ffd2009-11-13 17:47:00 +11005120 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8e2006-01-06 00:20:15 -08005122 if (mddev->ok_start_degraded)
5123 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10005124 "md/raid:%s: starting dirty degraded array"
5125 " - data corruption possible.\n",
NeilBrown6ff8d8e2006-01-06 00:20:15 -08005126 mdname(mddev));
5127 else {
5128 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005129 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8e2006-01-06 00:20:15 -08005130 mdname(mddev));
5131 goto abort;
5132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 }
5134
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10005136 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5137 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11005138 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5139 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 else
NeilBrown0c55e022010-05-03 14:09:02 +10005141 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5142 " out of %d devices, algorithm %d\n",
5143 mdname(mddev), conf->level,
5144 mddev->raid_disks - mddev->degraded,
5145 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146
5147 print_raid5_conf(conf);
5148
NeilBrownfef9c612009-03-31 15:16:46 +11005149 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11005150 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005151 atomic_set(&conf->reshape_stripes, 0);
5152 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5153 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5154 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5155 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5156 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005157 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005158 }
5159
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160
5161 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005162 if (mddev->to_remove == &raid5_attrs_group)
5163 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005164 else if (mddev->kobj.sd &&
5165 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005166 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10005167 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08005168 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10005169 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5170
5171 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005172 int chunk_size;
NeilBrown4a5add42010-06-01 19:37:28 +10005173 /* read-ahead size must cover two whole stripes, which
5174 * is 2 * (datadisks) * chunksize where 'n' is the
5175 * number of raid devices
5176 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5178 int stripe = data_disks *
5179 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5180 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5181 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10005182
5183 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005184
5185 mddev->queue->backing_dev_info.congested_data = mddev;
5186 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10005187
5188 chunk_size = mddev->chunk_sectors << 9;
5189 blk_queue_io_min(mddev->queue, chunk_size);
5190 blk_queue_io_opt(mddev->queue, chunk_size *
5191 (conf->raid_disks - conf->max_degraded));
5192
NeilBrowndafb20f2012-03-19 12:46:39 +11005193 rdev_for_each(rdev, mddev)
NeilBrown9f7c2222010-07-26 12:04:13 +10005194 disk_stack_limits(mddev->gendisk, rdev->bdev,
5195 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 }
5197
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 return 0;
5199abort:
NeilBrown01f96c02011-09-21 15:30:20 +10005200 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11005201 print_raid5_conf(conf);
5202 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005204 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 return -EIO;
5206}
5207
NeilBrownfd01b882011-10-11 16:47:53 +11005208static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209{
NeilBrownd1688a62011-10-11 16:49:52 +11005210 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211
NeilBrown01f96c02011-09-21 15:30:20 +10005212 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005213 if (mddev->queue)
5214 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10005215 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005216 mddev->private = NULL;
5217 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 return 0;
5219}
5220
NeilBrownfd01b882011-10-11 16:47:53 +11005221static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222{
NeilBrownd1688a62011-10-11 16:49:52 +11005223 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224 int i;
5225
Andre Noll9d8f0362009-06-18 08:45:01 +10005226 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5227 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005228 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 for (i = 0; i < conf->raid_disks; i++)
5230 seq_printf (seq, "%s",
5231 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005232 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234}
5235
NeilBrownd1688a62011-10-11 16:49:52 +11005236static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237{
5238 int i;
5239 struct disk_info *tmp;
5240
NeilBrown0c55e022010-05-03 14:09:02 +10005241 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 if (!conf) {
5243 printk("(conf==NULL)\n");
5244 return;
5245 }
NeilBrown0c55e022010-05-03 14:09:02 +10005246 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5247 conf->raid_disks,
5248 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249
5250 for (i = 0; i < conf->raid_disks; i++) {
5251 char b[BDEVNAME_SIZE];
5252 tmp = conf->disks + i;
5253 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005254 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5255 i, !test_bit(Faulty, &tmp->rdev->flags),
5256 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257 }
5258}
5259
NeilBrownfd01b882011-10-11 16:47:53 +11005260static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261{
5262 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005263 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005265 int count = 0;
5266 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267
5268 for (i = 0; i < conf->raid_disks; i++) {
5269 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11005270 if (tmp->replacement
5271 && tmp->replacement->recovery_offset == MaxSector
5272 && !test_bit(Faulty, &tmp->replacement->flags)
5273 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
5274 /* Replacement has just become active. */
5275 if (!tmp->rdev
5276 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
5277 count++;
5278 if (tmp->rdev) {
5279 /* Replaced device not technically faulty,
5280 * but we need to be sure it gets removed
5281 * and never re-added.
5282 */
5283 set_bit(Faulty, &tmp->rdev->flags);
5284 sysfs_notify_dirent_safe(
5285 tmp->rdev->sysfs_state);
5286 }
5287 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
5288 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005289 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005290 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005291 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005292 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005293 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 }
5295 }
NeilBrown6b965622010-08-18 11:56:59 +10005296 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005297 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005298 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005300 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301}
5302
NeilBrownb8321b62011-12-23 10:17:51 +11005303static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304{
NeilBrownd1688a62011-10-11 16:49:52 +11005305 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11005307 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11005308 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 struct disk_info *p = conf->disks + number;
5310
5311 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11005312 if (rdev == p->rdev)
5313 rdevp = &p->rdev;
5314 else if (rdev == p->replacement)
5315 rdevp = &p->replacement;
5316 else
5317 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11005318
NeilBrown657e3e42011-12-23 10:17:52 +11005319 if (number >= conf->raid_disks &&
5320 conf->reshape_progress == MaxSector)
5321 clear_bit(In_sync, &rdev->flags);
5322
5323 if (test_bit(In_sync, &rdev->flags) ||
5324 atomic_read(&rdev->nr_pending)) {
5325 err = -EBUSY;
5326 goto abort;
5327 }
5328 /* Only remove non-faulty devices if recovery
5329 * isn't possible.
5330 */
5331 if (!test_bit(Faulty, &rdev->flags) &&
5332 mddev->recovery_disabled != conf->recovery_disabled &&
5333 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11005334 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11005335 number < conf->raid_disks) {
5336 err = -EBUSY;
5337 goto abort;
5338 }
5339 *rdevp = NULL;
5340 synchronize_rcu();
5341 if (atomic_read(&rdev->nr_pending)) {
5342 /* lost the race, try later */
5343 err = -EBUSY;
5344 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11005345 } else if (p->replacement) {
5346 /* We must have just cleared 'rdev' */
5347 p->rdev = p->replacement;
5348 clear_bit(Replacement, &p->replacement->flags);
5349 smp_mb(); /* Make sure other CPUs may see both as identical
5350 * but will never see neither - if they are careful
5351 */
5352 p->replacement = NULL;
5353 clear_bit(WantReplacement, &rdev->flags);
5354 } else
5355 /* We might have just removed the Replacement as faulty-
5356 * clear the bit just in case
5357 */
5358 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359abort:
5360
5361 print_raid5_conf(conf);
5362 return err;
5363}
5364
NeilBrownfd01b882011-10-11 16:47:53 +11005365static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366{
NeilBrownd1688a62011-10-11 16:49:52 +11005367 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005368 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 int disk;
5370 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005371 int first = 0;
5372 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373
NeilBrown7f0da592011-07-28 11:39:22 +10005374 if (mddev->recovery_disabled == conf->recovery_disabled)
5375 return -EBUSY;
5376
NeilBrowndc10c642012-03-19 12:46:37 +11005377 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005379 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380
Neil Brown6c2fce22008-06-28 08:31:31 +10005381 if (rdev->raid_disk >= 0)
5382 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383
5384 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005385 * find the disk ... but prefer rdev->saved_raid_disk
5386 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005388 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005389 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005390 conf->disks[rdev->saved_raid_disk].rdev == NULL)
5391 disk = rdev->saved_raid_disk;
5392 else
Neil Brown6c2fce22008-06-28 08:31:31 +10005393 disk = first;
NeilBrown7bfec5f2011-12-23 10:17:53 +11005394 for ( ; disk <= last ; disk++) {
5395 p = conf->disks + disk;
5396 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005397 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005399 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005400 if (rdev->saved_raid_disk != disk)
5401 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005402 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 break;
5404 }
NeilBrown7bfec5f2011-12-23 10:17:53 +11005405 if (test_bit(WantReplacement, &p->rdev->flags) &&
5406 p->replacement == NULL) {
5407 clear_bit(In_sync, &rdev->flags);
5408 set_bit(Replacement, &rdev->flags);
5409 rdev->raid_disk = disk;
5410 err = 0;
5411 conf->fullsync = 1;
5412 rcu_assign_pointer(p->replacement, rdev);
5413 break;
5414 }
5415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005417 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418}
5419
NeilBrownfd01b882011-10-11 16:47:53 +11005420static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421{
5422 /* no resync is happening, and there is enough space
5423 * on all devices, so we can resize.
5424 * We need to make sure resync covers any new space.
5425 * If the array is shrinking we should possibly wait until
5426 * any io in the removed space completes, but it hardly seems
5427 * worth it.
5428 */
Andre Noll9d8f0362009-06-18 08:45:01 +10005429 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Dan Williams1f403622009-03-31 14:59:03 +11005430 md_set_array_sectors(mddev, raid5_size(mddev, sectors,
5431 mddev->raid_disks));
Dan Williamsb522adc2009-03-31 15:00:31 +11005432 if (mddev->array_sectors >
5433 raid5_size(mddev, sectors, mddev->raid_disks))
5434 return -EINVAL;
Andre Nollf233ea52008-07-21 17:05:22 +10005435 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005436 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005437 if (sectors > mddev->dev_sectors &&
5438 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005439 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5441 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005442 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005443 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 return 0;
5445}
5446
NeilBrownfd01b882011-10-11 16:47:53 +11005447static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005448{
5449 /* Can only proceed if there are plenty of stripe_heads.
5450 * We need a minimum of one full stripe,, and for sensible progress
5451 * it is best to have about 4 times that.
5452 * If we require 4 times, then the default 256 4K stripe_heads will
5453 * allow for chunk sizes up to 256K, which is probably OK.
5454 * If the chunk size is greater, user-space should request more
5455 * stripe_heads first.
5456 */
NeilBrownd1688a62011-10-11 16:49:52 +11005457 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005458 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5459 > conf->max_nr_stripes ||
5460 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5461 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005462 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5463 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005464 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5465 / STRIPE_SIZE)*4);
5466 return 0;
5467 }
5468 return 1;
5469}
5470
NeilBrownfd01b882011-10-11 16:47:53 +11005471static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005472{
NeilBrownd1688a62011-10-11 16:49:52 +11005473 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005474
NeilBrown88ce4932009-03-31 15:24:23 +11005475 if (mddev->delta_disks == 0 &&
5476 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005477 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005478 return 0; /* nothing to do */
NeilBrowndba034e2008-08-05 15:54:13 +10005479 if (mddev->bitmap)
5480 /* Cannot grow a bitmap yet */
5481 return -EBUSY;
NeilBrown674806d2010-06-16 17:17:53 +10005482 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005483 return -EINVAL;
5484 if (mddev->delta_disks < 0) {
5485 /* We might be able to shrink, but the devices must
5486 * be made bigger first.
5487 * For raid6, 4 is the minimum size.
5488 * Otherwise 2 is the minimum
5489 */
5490 int min = 2;
5491 if (mddev->level == 6)
5492 min = 4;
5493 if (mddev->raid_disks + mddev->delta_disks < min)
5494 return -EINVAL;
5495 }
NeilBrown29269552006-03-27 01:18:10 -08005496
NeilBrown01ee22b2009-06-18 08:47:20 +10005497 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005498 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005499
NeilBrownec32a2b2009-03-31 15:17:38 +11005500 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
NeilBrown63c70c42006-03-27 01:18:13 -08005501}
5502
NeilBrownfd01b882011-10-11 16:47:53 +11005503static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005504{
NeilBrownd1688a62011-10-11 16:49:52 +11005505 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005506 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005507 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005508 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005509
NeilBrownf4168852007-02-28 20:11:53 -08005510 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005511 return -EBUSY;
5512
NeilBrown01ee22b2009-06-18 08:47:20 +10005513 if (!check_stripe_cache(mddev))
5514 return -ENOSPC;
5515
NeilBrowndafb20f2012-03-19 12:46:39 +11005516 rdev_for_each(rdev, mddev)
NeilBrown469518a2011-01-31 11:57:43 +11005517 if (!test_bit(In_sync, &rdev->flags)
5518 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005519 spares++;
NeilBrown63c70c42006-03-27 01:18:13 -08005520
NeilBrownf4168852007-02-28 20:11:53 -08005521 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005522 /* Not enough devices even to make a degraded array
5523 * of that size
5524 */
5525 return -EINVAL;
5526
NeilBrownec32a2b2009-03-31 15:17:38 +11005527 /* Refuse to reduce size of the array. Any reductions in
5528 * array size must be through explicit setting of array_size
5529 * attribute.
5530 */
5531 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5532 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10005533 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11005534 "before number of disks\n", mdname(mddev));
5535 return -EINVAL;
5536 }
5537
NeilBrownf6705572006-03-27 01:18:11 -08005538 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005539 spin_lock_irq(&conf->device_lock);
5540 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005541 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005542 conf->prev_chunk_sectors = conf->chunk_sectors;
5543 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005544 conf->prev_algo = conf->algorithm;
5545 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11005546 if (mddev->delta_disks < 0)
5547 conf->reshape_progress = raid5_size(mddev, 0, 0);
5548 else
5549 conf->reshape_progress = 0;
5550 conf->reshape_safe = conf->reshape_progress;
NeilBrown86b42c72009-03-31 15:19:03 +11005551 conf->generation++;
NeilBrown29269552006-03-27 01:18:10 -08005552 spin_unlock_irq(&conf->device_lock);
5553
5554 /* Add some new drives, as many as will fit.
5555 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10005556 * Don't add devices if we are reducing the number of
5557 * devices in the array. This is because it is not possible
5558 * to correctly record the "partially reconstructed" state of
5559 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08005560 */
NeilBrown87a8dec2011-01-31 11:57:43 +11005561 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11005562 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11005563 if (rdev->raid_disk < 0 &&
5564 !test_bit(Faulty, &rdev->flags)) {
5565 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11005566 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11005567 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11005568 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11005569 else
NeilBrown87a8dec2011-01-31 11:57:43 +11005570 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10005571
5572 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11005573 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11005574 }
NeilBrown87a8dec2011-01-31 11:57:43 +11005575 } else if (rdev->raid_disk >= conf->previous_raid_disks
5576 && !test_bit(Faulty, &rdev->flags)) {
5577 /* This is a spare that was manually added */
5578 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11005579 }
NeilBrown29269552006-03-27 01:18:10 -08005580
NeilBrown87a8dec2011-01-31 11:57:43 +11005581 /* When a reshape changes the number of devices,
5582 * ->degraded is measured against the larger of the
5583 * pre and post number of devices.
5584 */
NeilBrownec32a2b2009-03-31 15:17:38 +11005585 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005586 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11005587 spin_unlock_irqrestore(&conf->device_lock, flags);
5588 }
NeilBrown63c70c42006-03-27 01:18:13 -08005589 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10005590 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07005591 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005592
NeilBrown29269552006-03-27 01:18:10 -08005593 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5594 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5595 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5596 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5597 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005598 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08005599 if (!mddev->sync_thread) {
5600 mddev->recovery = 0;
5601 spin_lock_irq(&conf->device_lock);
5602 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005603 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11005604 mddev->reshape_position = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005605 spin_unlock_irq(&conf->device_lock);
5606 return -EAGAIN;
5607 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005608 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005609 md_wakeup_thread(mddev->sync_thread);
5610 md_new_event(mddev);
5611 return 0;
5612}
NeilBrown29269552006-03-27 01:18:10 -08005613
NeilBrownec32a2b2009-03-31 15:17:38 +11005614/* This is called from the reshape thread and should make any
5615 * changes needed in 'conf'
5616 */
NeilBrownd1688a62011-10-11 16:49:52 +11005617static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08005618{
NeilBrown29269552006-03-27 01:18:10 -08005619
NeilBrownf6705572006-03-27 01:18:11 -08005620 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
Dan Williams80c3a6c2009-03-17 18:10:40 -07005621
NeilBrownf6705572006-03-27 01:18:11 -08005622 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11005623 conf->previous_raid_disks = conf->raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005624 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08005625 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005626 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07005627
5628 /* read-ahead size must cover two whole stripes, which is
5629 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5630 */
NeilBrown4a5add42010-06-01 19:37:28 +10005631 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11005632 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005633 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11005634 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07005635 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5636 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5637 }
NeilBrown29269552006-03-27 01:18:10 -08005638 }
NeilBrown29269552006-03-27 01:18:10 -08005639}
5640
NeilBrownec32a2b2009-03-31 15:17:38 +11005641/* This is called from the raid5d thread with mddev_lock held.
5642 * It makes config changes to the device.
5643 */
NeilBrownfd01b882011-10-11 16:47:53 +11005644static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11005645{
NeilBrownd1688a62011-10-11 16:49:52 +11005646 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11005647
5648 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5649
NeilBrownec32a2b2009-03-31 15:17:38 +11005650 if (mddev->delta_disks > 0) {
5651 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5652 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005653 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11005654 } else {
5655 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11005656 spin_lock_irq(&conf->device_lock);
5657 mddev->degraded = calc_degraded(conf);
5658 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11005659 for (d = conf->raid_disks ;
5660 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10005661 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11005662 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownb8321b62011-12-23 10:17:51 +11005663 if (rdev &&
5664 raid5_remove_disk(mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10005665 sysfs_unlink_rdev(mddev, rdev);
NeilBrown1a67dde2009-08-13 10:41:49 +10005666 rdev->raid_disk = -1;
5667 }
5668 }
NeilBrowncea9c222009-03-31 15:15:05 +11005669 }
NeilBrown88ce4932009-03-31 15:24:23 +11005670 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005671 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11005672 mddev->reshape_position = MaxSector;
5673 mddev->delta_disks = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11005674 }
5675}
5676
NeilBrownfd01b882011-10-11 16:47:53 +11005677static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07005678{
NeilBrownd1688a62011-10-11 16:49:52 +11005679 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07005680
5681 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08005682 case 2: /* resume for a suspend */
5683 wake_up(&conf->wait_for_overlap);
5684 break;
5685
NeilBrown72626682005-09-09 16:23:54 -07005686 case 1: /* stop all writes */
5687 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005688 /* '2' tells resync/reshape to pause so that all
5689 * active stripes can drain
5690 */
5691 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07005692 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005693 atomic_read(&conf->active_stripes) == 0 &&
5694 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07005695 conf->device_lock, /* nothing */);
NeilBrown64bd6602009-08-03 10:59:58 +10005696 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07005697 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005698 /* allow reshape to continue */
5699 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005700 break;
5701
5702 case 0: /* re-enable writes */
5703 spin_lock_irq(&conf->device_lock);
5704 conf->quiesce = 0;
5705 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08005706 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005707 spin_unlock_irq(&conf->device_lock);
5708 break;
5709 }
NeilBrown72626682005-09-09 16:23:54 -07005710}
NeilBrownb15c2e52006-01-06 00:20:16 -08005711
NeilBrownd562b0c2009-03-31 14:39:39 +11005712
NeilBrownfd01b882011-10-11 16:47:53 +11005713static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11005714{
NeilBrowne373ab12011-10-11 16:48:59 +11005715 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07005716 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11005717
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005718 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11005719 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10005720 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
5721 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005722 return ERR_PTR(-EINVAL);
5723 }
5724
NeilBrowne373ab12011-10-11 16:48:59 +11005725 sectors = raid0_conf->strip_zone[0].zone_end;
5726 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10005727 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005728 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11005729 mddev->new_layout = ALGORITHM_PARITY_N;
5730 mddev->new_chunk_sectors = mddev->chunk_sectors;
5731 mddev->raid_disks += 1;
5732 mddev->delta_disks = 1;
5733 /* make sure it will be not marked as dirty */
5734 mddev->recovery_cp = MaxSector;
5735
5736 return setup_conf(mddev);
5737}
5738
5739
NeilBrownfd01b882011-10-11 16:47:53 +11005740static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005741{
5742 int chunksect;
5743
5744 if (mddev->raid_disks != 2 ||
5745 mddev->degraded > 1)
5746 return ERR_PTR(-EINVAL);
5747
5748 /* Should check if there are write-behind devices? */
5749
5750 chunksect = 64*2; /* 64K by default */
5751
5752 /* The array must be an exact multiple of chunksize */
5753 while (chunksect && (mddev->array_sectors & (chunksect-1)))
5754 chunksect >>= 1;
5755
5756 if ((chunksect<<9) < STRIPE_SIZE)
5757 /* array size does not allow a suitable chunk size */
5758 return ERR_PTR(-EINVAL);
5759
5760 mddev->new_level = 5;
5761 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10005762 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11005763
5764 return setup_conf(mddev);
5765}
5766
NeilBrownfd01b882011-10-11 16:47:53 +11005767static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11005768{
5769 int new_layout;
5770
5771 switch (mddev->layout) {
5772 case ALGORITHM_LEFT_ASYMMETRIC_6:
5773 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
5774 break;
5775 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5776 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
5777 break;
5778 case ALGORITHM_LEFT_SYMMETRIC_6:
5779 new_layout = ALGORITHM_LEFT_SYMMETRIC;
5780 break;
5781 case ALGORITHM_RIGHT_SYMMETRIC_6:
5782 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
5783 break;
5784 case ALGORITHM_PARITY_0_6:
5785 new_layout = ALGORITHM_PARITY_0;
5786 break;
5787 case ALGORITHM_PARITY_N:
5788 new_layout = ALGORITHM_PARITY_N;
5789 break;
5790 default:
5791 return ERR_PTR(-EINVAL);
5792 }
5793 mddev->new_level = 5;
5794 mddev->new_layout = new_layout;
5795 mddev->delta_disks = -1;
5796 mddev->raid_disks -= 1;
5797 return setup_conf(mddev);
5798}
5799
NeilBrownd562b0c2009-03-31 14:39:39 +11005800
NeilBrownfd01b882011-10-11 16:47:53 +11005801static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11005802{
NeilBrown88ce4932009-03-31 15:24:23 +11005803 /* For a 2-drive array, the layout and chunk size can be changed
5804 * immediately as not restriping is needed.
5805 * For larger arrays we record the new value - after validation
5806 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11005807 */
NeilBrownd1688a62011-10-11 16:49:52 +11005808 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10005809 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11005810
NeilBrown597a7112009-06-18 08:47:42 +10005811 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11005812 return -EINVAL;
5813 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005814 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11005815 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005816 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11005817 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005818 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11005819 /* not factor of array size */
5820 return -EINVAL;
5821 }
5822
5823 /* They look valid */
5824
NeilBrown88ce4932009-03-31 15:24:23 +11005825 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10005826 /* can make the change immediately */
5827 if (mddev->new_layout >= 0) {
5828 conf->algorithm = mddev->new_layout;
5829 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11005830 }
5831 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10005832 conf->chunk_sectors = new_chunk ;
5833 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11005834 }
5835 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5836 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11005837 }
NeilBrown50ac1682009-06-18 08:47:55 +10005838 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11005839}
5840
NeilBrownfd01b882011-10-11 16:47:53 +11005841static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11005842{
NeilBrown597a7112009-06-18 08:47:42 +10005843 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10005844
NeilBrown597a7112009-06-18 08:47:42 +10005845 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11005846 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005847 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005848 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11005849 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005850 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11005851 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005852 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11005853 /* not factor of array size */
5854 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005855 }
NeilBrown88ce4932009-03-31 15:24:23 +11005856
5857 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10005858 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11005859}
5860
NeilBrownfd01b882011-10-11 16:47:53 +11005861static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005862{
5863 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005864 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005865 * raid1 - if there are two drives. We need to know the chunk size
5866 * raid4 - trivial - just use a raid4 layout.
5867 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005868 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005869 if (mddev->level == 0)
5870 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11005871 if (mddev->level == 1)
5872 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11005873 if (mddev->level == 4) {
5874 mddev->new_layout = ALGORITHM_PARITY_N;
5875 mddev->new_level = 5;
5876 return setup_conf(mddev);
5877 }
NeilBrownfc9739c2009-03-31 14:57:20 +11005878 if (mddev->level == 6)
5879 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11005880
5881 return ERR_PTR(-EINVAL);
5882}
5883
NeilBrownfd01b882011-10-11 16:47:53 +11005884static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11005885{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005886 /* raid4 can take over:
5887 * raid0 - if there is only one strip zone
5888 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11005889 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005890 if (mddev->level == 0)
5891 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11005892 if (mddev->level == 5 &&
5893 mddev->layout == ALGORITHM_PARITY_N) {
5894 mddev->new_layout = 0;
5895 mddev->new_level = 4;
5896 return setup_conf(mddev);
5897 }
5898 return ERR_PTR(-EINVAL);
5899}
NeilBrownd562b0c2009-03-31 14:39:39 +11005900
NeilBrown84fc4b52011-10-11 16:49:58 +11005901static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11005902
NeilBrownfd01b882011-10-11 16:47:53 +11005903static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11005904{
5905 /* Currently can only take over a raid5. We map the
5906 * personality to an equivalent raid6 personality
5907 * with the Q block at the end.
5908 */
5909 int new_layout;
5910
5911 if (mddev->pers != &raid5_personality)
5912 return ERR_PTR(-EINVAL);
5913 if (mddev->degraded > 1)
5914 return ERR_PTR(-EINVAL);
5915 if (mddev->raid_disks > 253)
5916 return ERR_PTR(-EINVAL);
5917 if (mddev->raid_disks < 3)
5918 return ERR_PTR(-EINVAL);
5919
5920 switch (mddev->layout) {
5921 case ALGORITHM_LEFT_ASYMMETRIC:
5922 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
5923 break;
5924 case ALGORITHM_RIGHT_ASYMMETRIC:
5925 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
5926 break;
5927 case ALGORITHM_LEFT_SYMMETRIC:
5928 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
5929 break;
5930 case ALGORITHM_RIGHT_SYMMETRIC:
5931 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
5932 break;
5933 case ALGORITHM_PARITY_0:
5934 new_layout = ALGORITHM_PARITY_0_6;
5935 break;
5936 case ALGORITHM_PARITY_N:
5937 new_layout = ALGORITHM_PARITY_N;
5938 break;
5939 default:
5940 return ERR_PTR(-EINVAL);
5941 }
5942 mddev->new_level = 6;
5943 mddev->new_layout = new_layout;
5944 mddev->delta_disks = 1;
5945 mddev->raid_disks += 1;
5946 return setup_conf(mddev);
5947}
5948
5949
NeilBrown84fc4b52011-10-11 16:49:58 +11005950static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07005951{
5952 .name = "raid6",
5953 .level = 6,
5954 .owner = THIS_MODULE,
5955 .make_request = make_request,
5956 .run = run,
5957 .stop = stop,
5958 .status = status,
5959 .error_handler = error,
5960 .hot_add_disk = raid5_add_disk,
5961 .hot_remove_disk= raid5_remove_disk,
5962 .spare_active = raid5_spare_active,
5963 .sync_request = sync_request,
5964 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005965 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10005966 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08005967 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005968 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07005969 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11005970 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07005971};
NeilBrown84fc4b52011-10-11 16:49:58 +11005972static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973{
5974 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08005975 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976 .owner = THIS_MODULE,
5977 .make_request = make_request,
5978 .run = run,
5979 .stop = stop,
5980 .status = status,
5981 .error_handler = error,
5982 .hot_add_disk = raid5_add_disk,
5983 .hot_remove_disk= raid5_remove_disk,
5984 .spare_active = raid5_spare_active,
5985 .sync_request = sync_request,
5986 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005987 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08005988 .check_reshape = raid5_check_reshape,
5989 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005990 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07005991 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11005992 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993};
5994
NeilBrown84fc4b52011-10-11 16:49:58 +11005995static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996{
NeilBrown2604b702006-01-06 00:20:36 -08005997 .name = "raid4",
5998 .level = 4,
5999 .owner = THIS_MODULE,
6000 .make_request = make_request,
6001 .run = run,
6002 .stop = stop,
6003 .status = status,
6004 .error_handler = error,
6005 .hot_add_disk = raid5_add_disk,
6006 .hot_remove_disk= raid5_remove_disk,
6007 .spare_active = raid5_spare_active,
6008 .sync_request = sync_request,
6009 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006010 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08006011 .check_reshape = raid5_check_reshape,
6012 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006013 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08006014 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11006015 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08006016};
6017
6018static int __init raid5_init(void)
6019{
NeilBrown16a53ec2006-06-26 00:27:38 -07006020 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006021 register_md_personality(&raid5_personality);
6022 register_md_personality(&raid4_personality);
6023 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024}
6025
NeilBrown2604b702006-01-06 00:20:36 -08006026static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027{
NeilBrown16a53ec2006-06-26 00:27:38 -07006028 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006029 unregister_md_personality(&raid5_personality);
6030 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031}
6032
6033module_init(raid5_init);
6034module_exit(raid5_exit);
6035MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11006036MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08006038MODULE_ALIAS("md-raid5");
6039MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08006040MODULE_ALIAS("md-level-5");
6041MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07006042MODULE_ALIAS("md-personality-8"); /* RAID6 */
6043MODULE_ALIAS("md-raid6");
6044MODULE_ALIAS("md-level-6");
6045
6046/* This used to be two separate modules, they were: */
6047MODULE_ALIAS("raid5");
6048MODULE_ALIAS("raid6");